blob: c9ae3404b1bb11572e34255cb3eae86ca1dd8131 [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 const struct sctp_association *asoc,
Xin Long172a1592017-08-11 10:23:57 +0800157 struct sctp_chunk *chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700158
Xin Long172a1592017-08-11 10:23:57 +0800159static enum sctp_disposition __sctp_sf_do_9_1_abort(
160 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000161 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800162 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +0800163 const union sctp_subtype type,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800164 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +0800165 struct sctp_cmd_seq *commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -0800166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Small helper function that checks if the chunk length
168 * is of the appropriate length. The 'required_length' argument
169 * is set to be the size of a specific chunk we are testing.
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200170 * Return Values: true = Valid length
171 * false = Invalid length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 *
173 */
Xin Long172a1592017-08-11 10:23:57 +0800174static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
175 __u16 required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
178
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200179 /* Previously already marked? */
180 if (unlikely(chunk->pdiscard))
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200181 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (unlikely(chunk_length < required_length))
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200183 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Marcelo Ricardo Leitner509e7a312016-12-28 09:26:35 -0200185 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/**********************************************************
189 * These are the state functions for handling chunk events.
190 **********************************************************/
191
192/*
193 * Process the final SHUTDOWN COMPLETE.
194 *
195 * Section: 4 (C) (diagram), 9.2
196 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
197 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
198 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
199 * should stop the T2-shutdown timer and remove all knowledge of the
200 * association (and thus the association enters the CLOSED state).
201 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700202 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * C) Rules for packet carrying SHUTDOWN COMPLETE:
204 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700205 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
206 * if the Verification Tag field of the packet matches its own tag and
207 * the T bit is not set
208 * OR
209 * it is set to its peer's tag and the T bit is set in the Chunk
210 * Flags.
211 * Otherwise, the receiver MUST silently discard the packet
212 * and take no further action. An endpoint MUST ignore the
213 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 *
215 * Inputs
216 * (endpoint, asoc, chunk)
217 *
218 * Outputs
219 * (asoc, reply_msg, msg_up, timers, counters)
220 *
221 * The return value is the disposition of the chunk.
222 */
Xin Long172a1592017-08-11 10:23:57 +0800223enum sctp_disposition sctp_sf_do_4_C(struct net *net,
224 const struct sctp_endpoint *ep,
225 const struct sctp_association *asoc,
226 const union sctp_subtype type,
227 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct sctp_chunk *chunk = arg;
230 struct sctp_ulpevent *ev;
231
Vlad Yasevichece25df2007-09-07 16:30:54 -0400232 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000233 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 /* RFC 2960 6.10 Bundling
236 *
237 * An endpoint MUST NOT bundle INIT, INIT ACK or
238 * SHUTDOWN COMPLETE with any other chunks.
239 */
240 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000241 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Vlad Yasevichece25df2007-09-07 16:30:54 -0400243 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +0800244 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000245 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400246 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 /* RFC 2960 10.2 SCTP-to-ULP
249 *
250 * H) SHUTDOWN COMPLETE notification
251 *
252 * When SCTP completes the shutdown procedures (section 9.2) this
253 * notification is passed to the upper layer.
254 */
255 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700256 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700257 if (ev)
258 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900259 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
262 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
263 * not the chunk should be discarded. If the endpoint is in
264 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
265 * T2-shutdown timer and remove all knowledge of the
266 * association (and thus the association enters the CLOSED
267 * state).
268 */
269 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
270 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
271
272 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
273 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
274
275 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
276 SCTP_STATE(SCTP_STATE_CLOSED));
277
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000278 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
279 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
282
283 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
286/*
287 * Respond to a normal INIT chunk.
288 * We are the side that is being asked for an association.
289 *
290 * Section: 5.1 Normal Establishment of an Association, B
291 * B) "Z" shall respond immediately with an INIT ACK chunk. The
292 * destination IP address of the INIT ACK MUST be set to the source
293 * IP address of the INIT to which this INIT ACK is responding. In
294 * the response, besides filling in other parameters, "Z" must set the
295 * Verification Tag field to Tag_A, and also provide its own
296 * Verification Tag (Tag_Z) in the Initiate Tag field.
297 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900298 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *
300 * Inputs
301 * (endpoint, asoc, chunk)
302 *
303 * Outputs
304 * (asoc, reply_msg, msg_up, timers, counters)
305 *
306 * The return value is the disposition of the chunk.
307 */
Xin Long172a1592017-08-11 10:23:57 +0800308enum sctp_disposition sctp_sf_do_5_1B_init(struct net *net,
309 const struct sctp_endpoint *ep,
310 const struct sctp_association *asoc,
311 const union sctp_subtype type,
312 void *arg,
313 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Xin Long62e6b7e2017-07-23 09:34:28 +0800315 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
316 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 struct sctp_association *new_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int len;
320
Richard Haines2277c7c2018-02-13 20:56:24 +0000321 /* Update socket peer label if first association. */
322 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
323 chunk->skb))
324 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* 6.10 Bundling
327 * An endpoint MUST NOT bundle INIT, INIT ACK or
328 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900329 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * IG Section 2.11.2
331 * Furthermore, we require that the receiver of an INIT chunk MUST
332 * enforce these rules by silently discarding an arriving packet
333 * with an INIT chunk that is bundled with other chunks.
334 */
335 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* If the packet is an OOTB packet which is temporarily on the
339 * control endpoint, respond with an ABORT.
340 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000341 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000342 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000343 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900347 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
349 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000350 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* Make sure that the INIT chunk has a valid length.
353 * Normally, this would cause an ABORT with a Protocol Violation
354 * error, but since we don't have an association, we'll
355 * just discard the packet.
356 */
Xin Long01a992b2017-06-30 11:52:22 +0800357 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Vlad Yasevichbec96402009-07-30 18:08:28 -0400360 /* If the INIT is coming toward a closing socket, we'll send back
361 * and ABORT. Essentially, this catches the race of INIT being
362 * backloged to the socket at the same time as the user isses close().
363 * Since the socket and all its associations are going away, we
364 * can treat this OOTB
365 */
366 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000367 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Verify the INIT chunk before processing it. */
370 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200371 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800372 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 &err_chunk)) {
374 /* This chunk contains fatal error. It is to be discarded.
375 * Send an ABORT, with causes if there is any.
376 */
377 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000378 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800380 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800382 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 sctp_chunk_free(err_chunk);
385
386 if (packet) {
387 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
388 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000389 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return SCTP_DISPOSITION_CONSUME;
391 } else {
392 return SCTP_DISPOSITION_NOMEM;
393 }
394 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000395 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 commands);
397 }
398 }
399
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900400 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800401 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +0800404 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
407 if (!new_asoc)
408 goto nomem;
409
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000410 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
411 sctp_scope(sctp_source(chunk)),
412 GFP_ATOMIC) < 0)
413 goto nomem_init;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000416 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +0800417 (struct sctp_init_chunk *)chunk->chunk_hdr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 GFP_ATOMIC))
419 goto nomem_init;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
422
423 /* If there are errors need to be reported for unknown parameters,
424 * make sure to reserve enough room in the INIT ACK for them.
425 */
426 len = 0;
427 if (err_chunk)
428 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800429 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
432 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700433 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* If there are errors need to be reported for unknown parameters,
436 * include them in the outgoing INIT ACK as "Unrecognized parameter"
437 * parameter.
438 */
439 if (err_chunk) {
440 /* Get the "Unrecognized parameter" parameter(s) out of the
441 * ERROR chunk generated by sctp_verify_init(). Since the
442 * error cause code for "unknown parameter" and the
443 * "Unrecognized parameter" type is the same, we can
444 * construct the parameters in INIT ACK by copying the
445 * ERROR causes over.
446 */
Xin Long62e6b7e2017-07-23 09:34:28 +0800447 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800449 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 /* Replace the cause code with the "Unrecognized parameter"
451 * parameter type.
452 */
453 sctp_addto_chunk(repl, len, unk_param);
454 sctp_chunk_free(err_chunk);
455 }
456
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700457 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
460
461 /*
462 * Note: After sending out INIT ACK with the State Cookie parameter,
463 * "Z" MUST NOT allocate any resources, nor keep any states for the
464 * new association. Otherwise, "Z" will be vulnerable to resource
465 * attacks.
466 */
467 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
468
469 return SCTP_DISPOSITION_DELETE_TCB;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471nomem_init:
472 sctp_association_free(new_asoc);
473nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700474 if (err_chunk)
475 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return SCTP_DISPOSITION_NOMEM;
477}
478
479/*
480 * Respond to a normal INIT ACK chunk.
481 * We are the side that is initiating the association.
482 *
483 * Section: 5.1 Normal Establishment of an Association, C
484 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
485 * timer and leave COOKIE-WAIT state. "A" shall then send the State
486 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
487 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
488 *
489 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
490 * DATA chunks, but it MUST be the first chunk in the packet and
491 * until the COOKIE ACK is returned the sender MUST NOT send any
492 * other packets to the peer.
493 *
494 * Verification Tag: 3.3.3
495 * If the value of the Initiate Tag in a received INIT ACK chunk is
496 * found to be 0, the receiver MUST treat it as an error and close the
497 * association by transmitting an ABORT.
498 *
499 * Inputs
500 * (endpoint, asoc, chunk)
501 *
502 * Outputs
503 * (asoc, reply_msg, msg_up, timers, counters)
504 *
505 * The return value is the disposition of the chunk.
506 */
Xin Long172a1592017-08-11 10:23:57 +0800507enum sctp_disposition sctp_sf_do_5_1C_ack(struct net *net,
508 const struct sctp_endpoint *ep,
509 const struct sctp_association *asoc,
510 const union sctp_subtype type,
511 void *arg,
512 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Xin Long01a992b2017-06-30 11:52:22 +0800514 struct sctp_init_chunk *initchunk;
Xin Long172a1592017-08-11 10:23:57 +0800515 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sctp_chunk *err_chunk;
517 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000520 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* 6.10 Bundling
523 * An endpoint MUST NOT bundle INIT, INIT ACK or
524 * SHUTDOWN COMPLETE with any other chunks.
525 */
526 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000527 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Vlad Yasevichece25df2007-09-07 16:30:54 -0400529 /* Make sure that the INIT-ACK chunk has a valid length */
Xin Longcb1844c2017-07-23 09:34:26 +0800530 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_initack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000531 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400532 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +0800534 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Verify the INIT chunk before processing it. */
537 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200538 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +0800539 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 &err_chunk)) {
541
Xin Long2a493212017-08-03 15:42:13 +0800542 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800545 * Send an ABORT, with causes. If there are no causes,
546 * then there wasn't enough memory. Just terminate
547 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000550 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +0800552 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800554 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 sctp_chunk_free(err_chunk);
557
558 if (packet) {
559 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
560 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000561 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700562 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700565
566 /* SCTP-AUTH, Section 6.3:
567 * It should be noted that if the receiver wants to tear
568 * down an association in an authenticated way only, the
569 * handling of malformed packets should not result in
570 * tearing down the association.
571 *
572 * This means that if we only want to abort associations
573 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300574 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700575 * was malformed.
576 */
577 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000578 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700579
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000580 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
581 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700582 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /* Tag the variable length parameters. Note that we never
586 * convert the parameters in an INIT chunk.
587 */
Xin Long4ae70c02017-06-30 11:52:21 +0800588 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Xin Long01a992b2017-06-30 11:52:22 +0800590 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
593 SCTP_PEER_INIT(initchunk));
594
Frank Filz3f7a87d2005-06-20 13:14:57 -0700595 /* Reset init error count upon receipt of INIT-ACK. */
596 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* 5.1 C) "A" shall stop the T1-init timer and leave
599 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
600 * timer, and enter the COOKIE-ECHOED state.
601 */
602 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
603 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
606 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
607 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
608
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700609 /* SCTP-AUTH: genereate the assocition shared keys so that
610 * we can potentially signe the COOKIE-ECHO.
611 */
612 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* 5.1 C) "A" shall then send the State Cookie received in the
615 * INIT ACK chunk in a COOKIE ECHO chunk, ...
616 */
617 /* If there is any errors to report, send the ERROR chunk generated
618 * for unknown parameters as well.
619 */
620 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
621 SCTP_CHUNK(err_chunk));
622
623 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Xin Long59d8d442018-05-05 14:59:47 +0800626static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
627 const struct sctp_association *asoc)
628{
629 struct sctp_chunk auth;
630
631 if (!chunk->auth_chunk)
632 return true;
633
634 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
635 * is supposed to be authenticated and we have to do delayed
636 * authentication. We've just recreated the association using
637 * the information in the cookie and now it's much easier to
638 * do the authentication.
639 */
640
641 /* Make sure that we and the peer are AUTH capable */
642 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
643 return false;
644
645 /* set-up our fake chunk so that we can process it */
646 auth.skb = chunk->auth_chunk;
647 auth.asoc = chunk->asoc;
648 auth.sctp_hdr = chunk->sctp_hdr;
649 auth.chunk_hdr = (struct sctp_chunkhdr *)
650 skb_push(chunk->auth_chunk,
651 sizeof(struct sctp_chunkhdr));
652 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
653 auth.transport = chunk->transport;
654
655 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * Respond to a normal COOKIE ECHO chunk.
660 * We are the side that is being asked for an association.
661 *
662 * Section: 5.1 Normal Establishment of an Association, D
663 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
664 * with a COOKIE ACK chunk after building a TCB and moving to
665 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
666 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
667 * chunk MUST be the first chunk in the packet.
668 *
669 * IMPLEMENTATION NOTE: An implementation may choose to send the
670 * Communication Up notification to the SCTP user upon reception
671 * of a valid COOKIE ECHO chunk.
672 *
673 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
674 * D) Rules for packet carrying a COOKIE ECHO
675 *
676 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
677 * Initial Tag received in the INIT ACK.
678 *
679 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
680 *
681 * Inputs
682 * (endpoint, asoc, chunk)
683 *
684 * Outputs
685 * (asoc, reply_msg, msg_up, timers, counters)
686 *
687 * The return value is the disposition of the chunk.
688 */
Xin Long172a1592017-08-11 10:23:57 +0800689enum sctp_disposition sctp_sf_do_5_1D_ce(struct net *net,
690 const struct sctp_endpoint *ep,
691 const struct sctp_association *asoc,
692 const union sctp_subtype type,
693 void *arg,
694 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Xin Long30f6ebf2018-03-14 19:05:34 +0800696 struct sctp_ulpevent *ev, *ai_ev = NULL, *auth_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct sctp_association *new_asoc;
Xin Long01a992b2017-06-30 11:52:22 +0800698 struct sctp_init_chunk *peer_init;
Xin Long172a1592017-08-11 10:23:57 +0800699 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct sctp_chunk *err_chk_p;
Xin Long172a1592017-08-11 10:23:57 +0800701 struct sctp_chunk *repl;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900702 struct sock *sk;
Xin Long172a1592017-08-11 10:23:57 +0800703 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* If the packet is an OOTB packet which is temporarily on the
706 * control endpoint, respond with an ABORT.
707 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000708 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000709 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000710 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* Make sure that the COOKIE_ECHO chunk has a valid length.
714 * In this case, we check that we have enough for at least a
715 * chunk header. More detailed verification is done
716 * in sctp_unpack_cookie().
717 */
Xin Long922dbc52017-06-30 11:52:13 +0800718 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000719 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Vlad Yasevich609ee462007-08-31 03:10:59 +0900721 /* If the endpoint is not listening or if the number of associations
722 * on the TCP-style socket exceed the max backlog, respond with an
723 * ABORT.
724 */
725 sk = ep->base.sk;
726 if (!sctp_sstate(sk, LISTENING) ||
727 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000728 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* "Decode" the chunk. We have no optional parameters so we
731 * are in good shape.
732 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900733 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700735 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +0800736 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700737 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
740 * "Z" will reply with a COOKIE ACK chunk after building a TCB
741 * and moving to the ESTABLISHED state.
742 */
743 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
744 &err_chk_p);
745
746 /* FIXME:
747 * If the re-build failed, what is the proper error path
748 * from here?
749 *
750 * [We should abort the association. --piggy]
751 */
752 if (!new_asoc) {
753 /* FIXME: Several errors are possible. A bad cookie should
754 * be silently discarded, but think about logging it too.
755 */
756 switch (error) {
757 case -SCTP_IERROR_NOMEM:
758 goto nomem;
759
760 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000761 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000763 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 case -SCTP_IERROR_BAD_SIG:
766 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000767 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700772 /* Delay state machine commands until later.
773 *
774 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 * the sctp_unpack_cookie() already.
776 */
777 /* This is a brand-new association, so these are not yet side
778 * effects--it is safe to run them here.
779 */
780 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
781
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000782 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800783 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 peer_init, GFP_ATOMIC))
785 goto nomem_init;
786
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700787 /* SCTP-AUTH: Now that we've populate required fields in
788 * sctp_process_init, set up the assocaition shared keys as
789 * necessary so that we can potentially authenticate the ACK
790 */
791 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
792 if (error)
793 goto nomem_init;
794
Xin Long59d8d442018-05-05 14:59:47 +0800795 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
796 sctp_association_free(new_asoc);
797 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 repl = sctp_make_cookie_ack(new_asoc, chunk);
801 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700802 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* RFC 2960 5.1 Normal Establishment of an Association
805 *
806 * D) IMPLEMENTATION NOTE: An implementation may choose to
807 * send the Communication Up notification to the SCTP user
808 * upon reception of a valid COOKIE ECHO chunk.
809 */
810 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
811 new_asoc->c.sinit_num_ostreams,
812 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700813 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (!ev)
815 goto nomem_ev;
816
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900817 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800818 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800820 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800822 if (new_asoc->peer.adaptation_ind) {
823 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700825 if (!ai_ev)
826 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Xin Long30f6ebf2018-03-14 19:05:34 +0800829 if (!new_asoc->peer.auth_capable) {
830 auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
831 SCTP_AUTH_NO_AUTH,
832 GFP_ATOMIC);
833 if (!auth_ev)
834 goto nomem_authev;
835 }
836
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700837 /* Add all the state machine commands now since we've created
838 * everything. This way we don't introduce memory corruptions
839 * during side-effect processing and correclty count established
840 * associations.
841 */
842 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
843 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
844 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000845 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
846 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700847 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
848
Neil Horman9f70f462013-12-10 06:48:15 -0500849 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700850 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
851 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
852
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700853 /* This will send the COOKIE ACK */
854 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
855
856 /* Queue the ASSOC_CHANGE event */
857 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
858
859 /* Send up the Adaptation Layer Indication event */
860 if (ai_ev)
861 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
862 SCTP_ULPEVENT(ai_ev));
863
Xin Long30f6ebf2018-03-14 19:05:34 +0800864 if (auth_ev)
865 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
866 SCTP_ULPEVENT(auth_ev));
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return SCTP_DISPOSITION_CONSUME;
869
Xin Long30f6ebf2018-03-14 19:05:34 +0800870nomem_authev:
871 sctp_ulpevent_free(ai_ev);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700872nomem_aiev:
873 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874nomem_ev:
875 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876nomem_init:
877 sctp_association_free(new_asoc);
878nomem:
879 return SCTP_DISPOSITION_NOMEM;
880}
881
882/*
883 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300884 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 *
886 * RFC 2960 5.1 Normal Establishment of an Association
887 *
888 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
889 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
890 * timer. It may also notify its ULP about the successful
891 * establishment of the association with a Communication Up
892 * notification (see Section 10).
893 *
894 * Verification Tag:
895 * Inputs
896 * (endpoint, asoc, chunk)
897 *
898 * Outputs
899 * (asoc, reply_msg, msg_up, timers, counters)
900 *
901 * The return value is the disposition of the chunk.
902 */
Xin Long172a1592017-08-11 10:23:57 +0800903enum sctp_disposition sctp_sf_do_5_1E_ca(struct net *net,
904 const struct sctp_endpoint *ep,
905 const struct sctp_association *asoc,
906 const union sctp_subtype type,
907 void *arg,
908 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 struct sctp_chunk *chunk = arg;
911 struct sctp_ulpevent *ev;
912
913 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000914 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /* Verify that the chunk length for the COOKIE-ACK is OK.
917 * If we don't do this, any bundled chunks may be junked.
918 */
Xin Long922dbc52017-06-30 11:52:13 +0800919 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000920 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 commands);
922
923 /* Reset init error count upon receipt of COOKIE-ACK,
924 * to avoid problems with the managemement of this
925 * counter in stale cookie situations when a transition back
926 * from the COOKIE-ECHOED state to the COOKIE-WAIT
927 * state is performed.
928 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700929 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Richard Haines2277c7c2018-02-13 20:56:24 +0000931 /* Set peer label for connection. */
932 security_inet_conn_established(ep->base.sk, chunk->skb);
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 /* RFC 2960 5.1 Normal Establishment of an Association
935 *
936 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
937 * from the COOKIE-ECHOED state to the ESTABLISHED state,
938 * stopping the T1-cookie timer.
939 */
940 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
941 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
942 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
943 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000944 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
945 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500947 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
949 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* It may also notify its ULP about the successful
952 * establishment of the association with a Communication Up
953 * notification (see Section 10).
954 */
955 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
956 0, asoc->c.sinit_num_ostreams,
957 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700958 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (!ev)
961 goto nomem;
962
963 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
964
965 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800966 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800968 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800970 if (asoc->peer.adaptation_ind) {
971 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (!ev)
973 goto nomem;
974
975 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
976 SCTP_ULPEVENT(ev));
977 }
978
Xin Long30f6ebf2018-03-14 19:05:34 +0800979 if (!asoc->peer.auth_capable) {
980 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
981 GFP_ATOMIC);
982 if (!ev)
983 goto nomem;
984 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
985 SCTP_ULPEVENT(ev));
986 }
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return SCTP_DISPOSITION_CONSUME;
989nomem:
990 return SCTP_DISPOSITION_NOMEM;
991}
992
993/* Generate and sendout a heartbeat packet. */
Xin Long172a1592017-08-11 10:23:57 +0800994static enum sctp_disposition sctp_sf_heartbeat(
995 const struct sctp_endpoint *ep,
996 const struct sctp_association *asoc,
997 const union sctp_subtype type,
998 void *arg,
999 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
1001 struct sctp_transport *transport = (struct sctp_transport *) arg;
1002 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
1004 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +00001005 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!reply)
1007 return SCTP_DISPOSITION_NOMEM;
1008
1009 /* Set rto_pending indicating that an RTT measurement
1010 * is started with this heartbeat chunk.
1011 */
1012 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1013 SCTP_TRANSPORT(transport));
1014
1015 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1016 return SCTP_DISPOSITION_CONSUME;
1017}
1018
1019/* Generate a HEARTBEAT packet on the given transport. */
Xin Long172a1592017-08-11 10:23:57 +08001020enum sctp_disposition sctp_sf_sendbeat_8_3(struct net *net,
1021 const struct sctp_endpoint *ep,
1022 const struct sctp_association *asoc,
1023 const union sctp_subtype type,
1024 void *arg,
1025 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
1027 struct sctp_transport *transport = (struct sctp_transport *) arg;
1028
Vlad Yasevichb9f84782009-08-26 09:36:25 -04001029 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001030 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1031 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1033 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08001034 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001035 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1036 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return SCTP_DISPOSITION_DELETE_TCB;
1038 }
1039
1040 /* Section 3.3.5.
1041 * The Sender-specific Heartbeat Info field should normally include
1042 * information about the sender's current time when this HEARTBEAT
1043 * chunk is sent and the destination transport address to which this
1044 * HEARTBEAT is sent (see Section 8.3).
1045 */
1046
Frank Filz52ccb8e2005-12-22 11:36:46 -08001047 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 if (SCTP_DISPOSITION_NOMEM ==
1049 sctp_sf_heartbeat(ep, asoc, type, arg,
1050 commands))
1051 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Set transport error counter and association error counter
1054 * when sending heartbeat.
1055 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001056 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 SCTP_TRANSPORT(transport));
1058 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001059 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1060 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1062 SCTP_TRANSPORT(transport));
1063
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001064 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Xin Long7b9438d2017-01-18 00:44:43 +08001067/* resend asoc strreset_chunk. */
Xin Long172a1592017-08-11 10:23:57 +08001068enum sctp_disposition sctp_sf_send_reconf(struct net *net,
1069 const struct sctp_endpoint *ep,
1070 const struct sctp_association *asoc,
1071 const union sctp_subtype type,
1072 void *arg,
1073 struct sctp_cmd_seq *commands)
Xin Long7b9438d2017-01-18 00:44:43 +08001074{
1075 struct sctp_transport *transport = arg;
1076
1077 if (asoc->overall_error_count >= asoc->max_retrans) {
1078 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1079 SCTP_ERROR(ETIMEDOUT));
1080 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1081 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1082 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1083 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1084 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1085 return SCTP_DISPOSITION_DELETE_TCB;
1086 }
1087
1088 sctp_chunk_hold(asoc->strreset_chunk);
1089 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1090 SCTP_CHUNK(asoc->strreset_chunk));
1091 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1092
1093 return SCTP_DISPOSITION_CONSUME;
1094}
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096/*
1097 * Process an heartbeat request.
1098 *
1099 * Section: 8.3 Path Heartbeat
1100 * The receiver of the HEARTBEAT should immediately respond with a
1101 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1102 * from the received HEARTBEAT chunk.
1103 *
1104 * Verification Tag: 8.5 Verification Tag [Normal verification]
1105 * When receiving an SCTP packet, the endpoint MUST ensure that the
1106 * value in the Verification Tag field of the received SCTP packet
1107 * matches its own Tag. If the received Verification Tag value does not
1108 * match the receiver's own tag value, the receiver shall silently
1109 * discard the packet and shall not process it any further except for
1110 * those cases listed in Section 8.5.1 below.
1111 *
1112 * Inputs
1113 * (endpoint, asoc, chunk)
1114 *
1115 * Outputs
1116 * (asoc, reply_msg, msg_up, timers, counters)
1117 *
1118 * The return value is the disposition of the chunk.
1119 */
Xin Long172a1592017-08-11 10:23:57 +08001120enum sctp_disposition sctp_sf_beat_8_3(struct net *net,
1121 const struct sctp_endpoint *ep,
1122 const struct sctp_association *asoc,
1123 const union sctp_subtype type,
1124 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Xin Long3c918702017-06-30 11:52:16 +08001126 struct sctp_paramhdr *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 struct sctp_chunk *chunk = arg;
1128 struct sctp_chunk *reply;
1129 size_t paylen = 0;
1130
1131 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001132 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* Make sure that the HEARTBEAT chunk has a valid length. */
Xin Long38c00f72017-07-23 09:34:35 +08001135 if (!sctp_chunk_length_valid(chunk,
1136 sizeof(struct sctp_heartbeat_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001137 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 commands);
1139
1140 /* 8.3 The receiver of the HEARTBEAT should immediately
1141 * respond with a HEARTBEAT ACK that contains the Heartbeat
1142 * Information field copied from the received HEARTBEAT chunk.
1143 */
Xin Long4d2dcdf2017-07-23 09:34:34 +08001144 chunk->subh.hb_hdr = (struct sctp_heartbeathdr *)chunk->skb->data;
Xin Long3c918702017-06-30 11:52:16 +08001145 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
Xin Long922dbc52017-06-30 11:52:13 +08001146 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
Thomas Graf06a31e22012-11-30 02:16:27 +00001147
1148 if (ntohs(param_hdr->length) > paylen)
1149 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1150 param_hdr, commands);
1151
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001152 if (!pskb_pull(chunk->skb, paylen))
1153 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Thomas Graf06a31e22012-11-30 02:16:27 +00001155 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (!reply)
1157 goto nomem;
1158
1159 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1160 return SCTP_DISPOSITION_CONSUME;
1161
1162nomem:
1163 return SCTP_DISPOSITION_NOMEM;
1164}
1165
1166/*
1167 * Process the returning HEARTBEAT ACK.
1168 *
1169 * Section: 8.3 Path Heartbeat
1170 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1171 * should clear the error counter of the destination transport
1172 * address to which the HEARTBEAT was sent, and mark the destination
1173 * transport address as active if it is not so marked. The endpoint may
1174 * optionally report to the upper layer when an inactive destination
1175 * address is marked as active due to the reception of the latest
1176 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1177 * clear the association overall error count as well (as defined
1178 * in section 8.1).
1179 *
1180 * The receiver of the HEARTBEAT ACK should also perform an RTT
1181 * measurement for that destination transport address using the time
1182 * value carried in the HEARTBEAT ACK chunk.
1183 *
1184 * Verification Tag: 8.5 Verification Tag [Normal verification]
1185 *
1186 * Inputs
1187 * (endpoint, asoc, chunk)
1188 *
1189 * Outputs
1190 * (asoc, reply_msg, msg_up, timers, counters)
1191 *
1192 * The return value is the disposition of the chunk.
1193 */
Xin Long172a1592017-08-11 10:23:57 +08001194enum sctp_disposition sctp_sf_backbeat_8_3(struct net *net,
1195 const struct sctp_endpoint *ep,
1196 const struct sctp_association *asoc,
1197 const union sctp_subtype type,
1198 void *arg,
1199 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Xin Longedf903f2017-08-11 10:23:46 +08001201 struct sctp_sender_hb_info *hbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct sctp_transport *link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 unsigned long max_interval;
Xin Longedf903f2017-08-11 10:23:46 +08001205 union sctp_addr from_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001208 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08001211 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
Xin Longedf903f2017-08-11 10:23:46 +08001212 sizeof(*hbinfo)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001213 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 commands);
1215
Xin Longedf903f2017-08-11 10:23:46 +08001216 hbinfo = (struct sctp_sender_hb_info *)chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001217 /* Make sure that the length of the parameter is what we expect */
Xin Longedf903f2017-08-11 10:23:46 +08001218 if (ntohs(hbinfo->param_hdr.length) != sizeof(*hbinfo))
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001219 return SCTP_DISPOSITION_DISCARD;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001222 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001225 if (unlikely(!link)) {
1226 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001227 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1228 __func__,
1229 asoc,
1230 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001231 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001232 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1233 __func__,
1234 asoc,
1235 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return SCTP_DISPOSITION_DISCARD;
1238 }
1239
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001240 /* Validate the 64-bit random nonce. */
1241 if (hbinfo->hb_nonce != link->hb_nonce)
1242 return SCTP_DISPOSITION_DISCARD;
1243
Frank Filz52ccb8e2005-12-22 11:36:46 -08001244 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* Check if the timestamp looks valid. */
1247 if (time_after(hbinfo->sent_at, jiffies) ||
1248 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001249 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1250 "for transport:%p\n", __func__, link);
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return SCTP_DISPOSITION_DISCARD;
1253 }
1254
1255 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1256 * the HEARTBEAT should clear the error counter of the
1257 * destination transport address to which the HEARTBEAT was
1258 * sent and mark the destination transport address as active if
1259 * it is not so marked.
1260 */
1261 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1262
1263 return SCTP_DISPOSITION_CONSUME;
1264}
1265
1266/* Helper function to send out an abort for the restart
1267 * condition.
1268 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001269static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct sctp_chunk *init,
Xin Longa85bbeb2017-08-11 10:23:52 +08001271 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Xin Long172a1592017-08-11 10:23:57 +08001273 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 union sctp_addr_param *addrparm;
1275 struct sctp_errhdr *errhdr;
Xin Longd8238d92017-08-03 15:42:11 +08001276 char buffer[sizeof(*errhdr) + sizeof(*addrparm)];
Xin Long172a1592017-08-11 10:23:57 +08001277 struct sctp_endpoint *ep;
1278 struct sctp_packet *pkt;
1279 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /* Build the error on the stack. We are way to malloc crazy
1282 * throughout the code today.
1283 */
1284 errhdr = (struct sctp_errhdr *)buffer;
1285 addrparm = (union sctp_addr_param *)errhdr->variable;
1286
1287 /* Copy into a parm format. */
1288 len = af->to_addr_param(ssa, addrparm);
Xin Longd8238d92017-08-03 15:42:11 +08001289 len += sizeof(*errhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 errhdr->cause = SCTP_ERROR_RESTART;
1292 errhdr->length = htons(len);
1293
1294 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001295 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* Association is NULL since this may be a restart attack and we
1298 * want to send back the attacker's vtag.
1299 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001300 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 if (!pkt)
1303 goto out;
1304 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1305
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001306 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* Discard the rest of the inbound packet. */
1309 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1310
1311out:
1312 /* Even if there is no memory, treat as a failure so
1313 * the packet will get dropped.
1314 */
1315 return 0;
1316}
1317
Joe Perches123031c2010-09-08 11:04:21 +00001318static bool list_has_sctp_addr(const struct list_head *list,
1319 union sctp_addr *ipaddr)
1320{
1321 struct sctp_transport *addr;
1322
1323 list_for_each_entry(addr, list, transports) {
1324 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1325 return true;
1326 }
1327
1328 return false;
1329}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330/* A restart is occurring, check to make sure no new addresses
1331 * are being added as we may be under a takeover attack.
1332 */
1333static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1334 const struct sctp_association *asoc,
1335 struct sctp_chunk *init,
Xin Longa85bbeb2017-08-11 10:23:52 +08001336 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001338 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001339 struct sctp_transport *new_addr;
1340 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Joe Perches123031c2010-09-08 11:04:21 +00001342 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 * ...
1344 * Before responding the endpoint MUST check to see if the
1345 * unexpected INIT adds new addresses to the association. If new
1346 * addresses are added to the association, the endpoint MUST respond
1347 * with an ABORT..
1348 */
1349
1350 /* Search through all current addresses and make sure
1351 * we aren't adding any new ones.
1352 */
Robert P. J. Day9dbc15f02008-04-12 18:54:24 -07001353 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001354 transports) {
1355 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1356 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001357 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001358 commands);
1359 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
Joe Perches123031c2010-09-08 11:04:21 +00001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001365 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
1368/* Populate the verification/tie tags based on overlapping INIT
1369 * scenario.
1370 *
1371 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1372 */
1373static void sctp_tietags_populate(struct sctp_association *new_asoc,
1374 const struct sctp_association *asoc)
1375{
1376 switch (asoc->state) {
1377
1378 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1379
1380 case SCTP_STATE_COOKIE_WAIT:
1381 new_asoc->c.my_vtag = asoc->c.my_vtag;
1382 new_asoc->c.my_ttag = asoc->c.my_vtag;
1383 new_asoc->c.peer_ttag = 0;
1384 break;
1385
1386 case SCTP_STATE_COOKIE_ECHOED:
1387 new_asoc->c.my_vtag = asoc->c.my_vtag;
1388 new_asoc->c.my_ttag = asoc->c.my_vtag;
1389 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1390 break;
1391
1392 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1393 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1394 */
1395 default:
1396 new_asoc->c.my_ttag = asoc->c.my_vtag;
1397 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1398 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 /* Other parameters for the endpoint SHOULD be copied from the
1402 * existing parameters of the association (e.g. number of
1403 * outbound streams) into the INIT ACK and cookie.
1404 */
1405 new_asoc->rwnd = asoc->rwnd;
1406 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1407 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1408 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1409}
1410
1411/*
1412 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1413 * handling action.
1414 *
1415 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1416 *
1417 * Returns value representing action to be taken. These action values
1418 * correspond to Action/Description values in RFC 2960, Table 2.
1419 */
1420static char sctp_tietags_compare(struct sctp_association *new_asoc,
1421 const struct sctp_association *asoc)
1422{
1423 /* In this case, the peer may have restarted. */
1424 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1425 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1426 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1427 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1428 return 'A';
1429
1430 /* Collision case B. */
1431 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1432 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1433 (0 == asoc->c.peer_vtag))) {
1434 return 'B';
1435 }
1436
1437 /* Collision case D. */
1438 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1439 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1440 return 'D';
1441
1442 /* Collision case C. */
1443 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1444 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1445 (0 == new_asoc->c.my_ttag) &&
1446 (0 == new_asoc->c.peer_ttag))
1447 return 'C';
1448
1449 /* No match to any of the special cases; discard this packet. */
1450 return 'E';
1451}
1452
1453/* Common helper routine for both duplicate and simulataneous INIT
1454 * chunk handling.
1455 */
Xin Long172a1592017-08-11 10:23:57 +08001456static enum sctp_disposition sctp_sf_do_unexpected_init(
1457 struct net *net,
1458 const struct sctp_endpoint *ep,
1459 const struct sctp_association *asoc,
1460 const union sctp_subtype type,
1461 void *arg,
1462 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Xin Long62e6b7e2017-07-23 09:34:28 +08001464 struct sctp_chunk *chunk = arg, *repl, *err_chunk;
1465 struct sctp_unrecognized_param *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct sctp_association *new_asoc;
Xin Long172a1592017-08-11 10:23:57 +08001467 enum sctp_disposition retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 int len;
1470
Richard Haines2277c7c2018-02-13 20:56:24 +00001471 /* Update socket peer label if first association. */
1472 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
1473 chunk->skb))
1474 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /* 6.10 Bundling
1477 * An endpoint MUST NOT bundle INIT, INIT ACK or
1478 * SHUTDOWN COMPLETE with any other chunks.
1479 *
1480 * IG Section 2.11.2
1481 * Furthermore, we require that the receiver of an INIT chunk MUST
1482 * enforce these rules by silently discarding an arriving packet
1483 * with an INIT chunk that is bundled with other chunks.
1484 */
1485 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001486 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001489 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 */
1491 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001492 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 /* Make sure that the INIT chunk has a valid length.
1495 * In this case, we generate a protocol violation since we have
1496 * an association established.
1497 */
Xin Long01a992b2017-06-30 11:52:22 +08001498 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001499 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 commands);
1501 /* Grab the INIT header. */
Xin Long4ae70c02017-06-30 11:52:21 +08001502 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /* Tag the variable length parameters. */
Xin Long4ae70c02017-06-30 11:52:21 +08001505 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 /* Verify the INIT chunk before processing it. */
1508 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001509 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Xin Long01a992b2017-06-30 11:52:22 +08001510 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 &err_chunk)) {
1512 /* This chunk contains fatal error. It is to be discarded.
1513 * Send an ABORT, with causes if there is any.
1514 */
1515 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001516 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 (__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001518 sizeof(struct sctp_chunkhdr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001520 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 if (packet) {
1523 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1524 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001525 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 retval = SCTP_DISPOSITION_CONSUME;
1527 } else {
1528 retval = SCTP_DISPOSITION_NOMEM;
1529 }
1530 goto cleanup;
1531 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001532 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 commands);
1534 }
1535 }
1536
1537 /*
1538 * Other parameters for the endpoint SHOULD be copied from the
1539 * existing parameters of the association (e.g. number of
1540 * outbound streams) into the INIT ACK and cookie.
1541 * FIXME: We are copying parameters from the endpoint not the
1542 * association.
1543 */
1544 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1545 if (!new_asoc)
1546 goto nomem;
1547
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001548 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1549 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1550 goto nomem;
1551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* In the outbound INIT ACK the endpoint MUST copy its current
1553 * Verification Tag and Peers Verification tag into a reserved
1554 * place (local tie-tag and per tie-tag) within the state cookie.
1555 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001556 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Xin Long01a992b2017-06-30 11:52:22 +08001557 (struct sctp_init_chunk *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001558 GFP_ATOMIC))
1559 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /* Make sure no new addresses are being added during the
1562 * restart. Do not do this check for COOKIE-WAIT state,
1563 * since there are no peer addresses to check against.
1564 * Upon return an ABORT will have been sent if needed.
1565 */
1566 if (!sctp_state(asoc, COOKIE_WAIT)) {
1567 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1568 commands)) {
1569 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001570 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 }
1573
1574 sctp_tietags_populate(new_asoc, asoc);
1575
1576 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1577
1578 /* If there are errors need to be reported for unknown parameters,
1579 * make sure to reserve enough room in the INIT ACK for them.
1580 */
1581 len = 0;
1582 if (err_chunk) {
1583 len = ntohs(err_chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08001584 sizeof(struct sctp_chunkhdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1588 if (!repl)
1589 goto nomem;
1590
1591 /* If there are errors need to be reported for unknown parameters,
1592 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1593 * parameter.
1594 */
1595 if (err_chunk) {
1596 /* Get the "Unrecognized parameter" parameter(s) out of the
1597 * ERROR chunk generated by sctp_verify_init(). Since the
1598 * error cause code for "unknown parameter" and the
1599 * "Unrecognized parameter" type is the same, we can
1600 * construct the parameters in INIT ACK by copying the
1601 * ERROR causes over.
1602 */
Xin Long62e6b7e2017-07-23 09:34:28 +08001603 unk_param = (struct sctp_unrecognized_param *)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 ((__u8 *)(err_chunk->chunk_hdr) +
Xin Long922dbc52017-06-30 11:52:13 +08001605 sizeof(struct sctp_chunkhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* Replace the cause code with the "Unrecognized parameter"
1607 * parameter type.
1608 */
1609 sctp_addto_chunk(repl, len, unk_param);
1610 }
1611
1612 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1613 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1614
1615 /*
1616 * Note: After sending out INIT ACK with the State Cookie parameter,
1617 * "Z" MUST NOT allocate any resources for this new association.
1618 * Otherwise, "Z" will be vulnerable to resource attacks.
1619 */
1620 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1621 retval = SCTP_DISPOSITION_CONSUME;
1622
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001623 return retval;
1624
1625nomem:
1626 retval = SCTP_DISPOSITION_NOMEM;
1627nomem_retval:
1628 if (new_asoc)
1629 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630cleanup:
1631 if (err_chunk)
1632 sctp_chunk_free(err_chunk);
1633 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001637 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * This means we started an INIT and then we got an INIT request from
1639 * our peer.
1640 *
1641 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1642 * This usually indicates an initialization collision, i.e., each
1643 * endpoint is attempting, at about the same time, to establish an
1644 * association with the other endpoint.
1645 *
1646 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1647 * endpoint MUST respond with an INIT ACK using the same parameters it
1648 * sent in its original INIT chunk (including its Verification Tag,
1649 * unchanged). These original parameters are combined with those from the
1650 * newly received INIT chunk. The endpoint shall also generate a State
1651 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1652 * INIT to calculate the State Cookie.
1653 *
1654 * After that, the endpoint MUST NOT change its state, the T1-init
1655 * timer shall be left running and the corresponding TCB MUST NOT be
1656 * destroyed. The normal procedures for handling State Cookies when
1657 * a TCB exists will resolve the duplicate INITs to a single association.
1658 *
1659 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1660 * its Tie-Tags with the Tag information of itself and its peer (see
1661 * section 5.2.2 for a description of the Tie-Tags).
1662 *
1663 * Verification Tag: Not explicit, but an INIT can not have a valid
1664 * verification tag, so we skip the check.
1665 *
1666 * Inputs
1667 * (endpoint, asoc, chunk)
1668 *
1669 * Outputs
1670 * (asoc, reply_msg, msg_up, timers, counters)
1671 *
1672 * The return value is the disposition of the chunk.
1673 */
Xin Long172a1592017-08-11 10:23:57 +08001674enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1675 struct net *net,
1676 const struct sctp_endpoint *ep,
1677 const struct sctp_association *asoc,
1678 const union sctp_subtype type,
1679 void *arg,
1680 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
1682 /* Call helper to do the real work for both simulataneous and
1683 * duplicate INIT chunk handling.
1684 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001685 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688/*
1689 * Handle duplicated INIT messages. These are usually delayed
1690 * restransmissions.
1691 *
1692 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1693 * COOKIE-ECHOED and COOKIE-WAIT
1694 *
1695 * Unless otherwise stated, upon reception of an unexpected INIT for
1696 * this association, the endpoint shall generate an INIT ACK with a
1697 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1698 * current Verification Tag and peer's Verification Tag into a reserved
1699 * place within the state cookie. We shall refer to these locations as
1700 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1701 * containing this INIT ACK MUST carry a Verification Tag value equal to
1702 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1703 * MUST contain a new Initiation Tag (randomly generated see Section
1704 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1705 * existing parameters of the association (e.g. number of outbound
1706 * streams) into the INIT ACK and cookie.
1707 *
1708 * After sending out the INIT ACK, the endpoint shall take no further
1709 * actions, i.e., the existing association, including its current state,
1710 * and the corresponding TCB MUST NOT be changed.
1711 *
1712 * Note: Only when a TCB exists and the association is not in a COOKIE-
1713 * WAIT state are the Tie-Tags populated. For a normal association INIT
1714 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1715 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1716 * State Cookie are populated as specified in section 5.2.1.
1717 *
1718 * Verification Tag: Not specified, but an INIT has no way of knowing
1719 * what the verification tag could be, so we ignore it.
1720 *
1721 * Inputs
1722 * (endpoint, asoc, chunk)
1723 *
1724 * Outputs
1725 * (asoc, reply_msg, msg_up, timers, counters)
1726 *
1727 * The return value is the disposition of the chunk.
1728 */
Xin Long172a1592017-08-11 10:23:57 +08001729enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1730 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001731 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08001733 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08001735 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
1737 /* Call helper to do the real work for both simulataneous and
1738 * duplicate INIT chunk handling.
1739 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001740 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
1743
Vlad Yasevich610ab732007-01-15 19:18:30 -08001744/*
1745 * Unexpected INIT-ACK handler.
1746 *
1747 * Section 5.2.3
1748 * If an INIT ACK received by an endpoint in any state other than the
1749 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1750 * An unexpected INIT ACK usually indicates the processing of an old or
1751 * duplicated INIT chunk.
1752*/
Xin Long172a1592017-08-11 10:23:57 +08001753enum sctp_disposition sctp_sf_do_5_2_3_initack(
1754 struct net *net,
1755 const struct sctp_endpoint *ep,
1756 const struct sctp_association *asoc,
1757 const union sctp_subtype type,
1758 void *arg,
1759 struct sctp_cmd_seq *commands)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001760{
1761 /* Per the above section, we'll discard the chunk if we have an
1762 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1763 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001764 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001765 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001766 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001767 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1771 *
1772 * Section 5.2.4
1773 * A) In this case, the peer may have restarted.
1774 */
Xin Long172a1592017-08-11 10:23:57 +08001775static enum sctp_disposition sctp_sf_do_dupcook_a(
1776 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001777 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 const struct sctp_association *asoc,
1779 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001780 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 struct sctp_association *new_asoc)
1782{
Xin Long01a992b2017-06-30 11:52:22 +08001783 struct sctp_init_chunk *peer_init;
Xin Long172a1592017-08-11 10:23:57 +08001784 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct sctp_ulpevent *ev;
1786 struct sctp_chunk *repl;
1787 struct sctp_chunk *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 /* new_asoc is a brand-new association, so these are not yet
1790 * side effects--it is safe to run them here.
1791 */
1792 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1793
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001794 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 GFP_ATOMIC))
1796 goto nomem;
1797
Xin Long4842a082018-05-02 13:37:44 +08001798 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1799 goto nomem;
1800
Xin Long59d8d442018-05-05 14:59:47 +08001801 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1802 return SCTP_DISPOSITION_DISCARD;
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 /* Make sure no new addresses are being added during the
1805 * restart. Though this is a pretty complicated attack
1806 * since you'd have to get inside the cookie.
1807 */
Xin Long59d8d442018-05-05 14:59:47 +08001808 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1812 * the peer has restarted (Action A), it MUST NOT setup a new
1813 * association but instead resend the SHUTDOWN ACK and send an ERROR
1814 * chunk with a "Cookie Received while Shutting Down" error cause to
1815 * its peer.
1816 */
1817 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001818 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1820 chunk, commands);
1821 if (SCTP_DISPOSITION_NOMEM == disposition)
1822 goto nomem;
1823
1824 err = sctp_make_op_error(asoc, chunk,
1825 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001826 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (err)
1828 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1829 SCTP_CHUNK(err));
1830
1831 return SCTP_DISPOSITION_CONSUME;
1832 }
1833
Wei Yongjuna000c012011-05-29 23:23:36 +00001834 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1835 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001837 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1838 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1839 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1841
Wei Yongjuna000c012011-05-29 23:23:36 +00001842 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1843 * and ASCONF-ACK cache.
1844 */
1845 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1846 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1847 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 repl = sctp_make_cookie_ack(new_asoc, chunk);
1850 if (!repl)
1851 goto nomem;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 /* Report association restart to upper layer. */
1854 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1855 new_asoc->c.sinit_num_ostreams,
1856 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001857 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (!ev)
1859 goto nomem_ev;
1860
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001861 /* Update the content of current association. */
1862 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001864 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1865 (sctp_sstate(asoc->base.sk, CLOSING) ||
1866 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1867 /* if were currently in SHUTDOWN_PENDING, but the socket
1868 * has been closed by user, don't transition to ESTABLISHED.
1869 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1870 */
1871 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1872 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1873 SCTP_ST_CHUNK(0), NULL,
1874 commands);
1875 } else {
1876 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1877 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1878 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 return SCTP_DISPOSITION_CONSUME;
1881
1882nomem_ev:
1883 sctp_chunk_free(repl);
1884nomem:
1885 return SCTP_DISPOSITION_NOMEM;
1886}
1887
1888/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1889 *
1890 * Section 5.2.4
1891 * B) In this case, both sides may be attempting to start an association
1892 * at about the same time but the peer endpoint started its INIT
1893 * after responding to the local endpoint's INIT
1894 */
1895/* This case represents an initialization collision. */
Xin Long172a1592017-08-11 10:23:57 +08001896static enum sctp_disposition sctp_sf_do_dupcook_b(
1897 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001898 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 const struct sctp_association *asoc,
1900 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001901 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct sctp_association *new_asoc)
1903{
Xin Long01a992b2017-06-30 11:52:22 +08001904 struct sctp_init_chunk *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct sctp_chunk *repl;
1906
1907 /* new_asoc is a brand-new association, so these are not yet
1908 * side effects--it is safe to run them here.
1909 */
1910 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001911 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 GFP_ATOMIC))
1913 goto nomem;
1914
Xin Long4842a082018-05-02 13:37:44 +08001915 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1916 goto nomem;
1917
Xin Long59d8d442018-05-05 14:59:47 +08001918 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1919 return SCTP_DISPOSITION_DISCARD;
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* Update the content of current association. */
1922 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1923 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1924 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001925 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1927
1928 repl = sctp_make_cookie_ack(new_asoc, chunk);
1929 if (!repl)
1930 goto nomem;
1931
1932 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* RFC 2960 5.1 Normal Establishment of an Association
1935 *
1936 * D) IMPLEMENTATION NOTE: An implementation may choose to
1937 * send the Communication Up notification to the SCTP user
1938 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001939 *
1940 * Sadly, this needs to be implemented as a side-effect, because
1941 * we are not guaranteed to have set the association id of the real
1942 * association and so these notifications need to be delayed until
1943 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Vlad Yasevich07d93962007-05-04 13:55:27 -07001946 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001949 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001951 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001952 *
1953 * This also needs to be done as a side effect for the same reason as
1954 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001956 if (asoc->peer.adaptation_ind)
1957 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Xin Long30f6ebf2018-03-14 19:05:34 +08001959 if (!asoc->peer.auth_capable)
1960 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return SCTP_DISPOSITION_CONSUME;
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964nomem:
1965 return SCTP_DISPOSITION_NOMEM;
1966}
1967
1968/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1969 *
1970 * Section 5.2.4
1971 * C) In this case, the local endpoint's cookie has arrived late.
1972 * Before it arrived, the local endpoint sent an INIT and received an
1973 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1974 * but a new tag of its own.
1975 */
1976/* This case represents an initialization collision. */
Xin Long172a1592017-08-11 10:23:57 +08001977static enum sctp_disposition sctp_sf_do_dupcook_c(
1978 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001979 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 const struct sctp_association *asoc,
1981 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08001982 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct sctp_association *new_asoc)
1984{
1985 /* The cookie should be silently discarded.
1986 * The endpoint SHOULD NOT change states and should leave
1987 * any timers running.
1988 */
1989 return SCTP_DISPOSITION_DISCARD;
1990}
1991
1992/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1993 *
1994 * Section 5.2.4
1995 *
1996 * D) When both local and remote tags match the endpoint should always
1997 * enter the ESTABLISHED state, if it has not already done so.
1998 */
1999/* This case represents an initialization collision. */
Xin Long172a1592017-08-11 10:23:57 +08002000static enum sctp_disposition sctp_sf_do_dupcook_d(
2001 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002002 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 const struct sctp_association *asoc,
2004 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08002005 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct sctp_association *new_asoc)
2007{
Xin Long30f6ebf2018-03-14 19:05:34 +08002008 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct sctp_chunk *repl;
2010
2011 /* Clarification from Implementor's Guide:
2012 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002013 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
2014 * It should stop any cookie timer that may be running and send
2015 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
2017
Xin Long59d8d442018-05-05 14:59:47 +08002018 if (!sctp_auth_chunk_verify(net, chunk, asoc))
2019 return SCTP_DISPOSITION_DISCARD;
2020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /* Don't accidentally move back into established state. */
2022 if (asoc->state < SCTP_STATE_ESTABLISHED) {
2023 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2024 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2025 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2026 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002027 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2029 SCTP_NULL());
2030
2031 /* RFC 2960 5.1 Normal Establishment of an Association
2032 *
2033 * D) IMPLEMENTATION NOTE: An implementation may choose
2034 * to send the Communication Up notification to the
2035 * SCTP user upon reception of a valid COOKIE
2036 * ECHO chunk.
2037 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002038 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002040 asoc->c.sinit_num_ostreams,
2041 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09002042 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (!ev)
2044 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002047 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002049 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08002051 if (asoc->peer.adaptation_ind) {
2052 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002054 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 goto nomem;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
Xin Long30f6ebf2018-03-14 19:05:34 +08002058
2059 if (!asoc->peer.auth_capable) {
2060 auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2061 SCTP_AUTH_NO_AUTH,
2062 GFP_ATOMIC);
2063 if (!auth_ev)
2064 goto nomem;
2065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Xin Long46e16d42018-05-02 13:39:46 +08002068 repl = sctp_make_cookie_ack(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (!repl)
2070 goto nomem;
2071
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07002072 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2073
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002074 if (ev)
2075 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2076 SCTP_ULPEVENT(ev));
2077 if (ai_ev)
2078 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2079 SCTP_ULPEVENT(ai_ev));
Xin Long30f6ebf2018-03-14 19:05:34 +08002080 if (auth_ev)
2081 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2082 SCTP_ULPEVENT(auth_ev));
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return SCTP_DISPOSITION_CONSUME;
2085
2086nomem:
Xin Long30f6ebf2018-03-14 19:05:34 +08002087 if (auth_ev)
2088 sctp_ulpevent_free(auth_ev);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002089 if (ai_ev)
2090 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 if (ev)
2092 sctp_ulpevent_free(ev);
2093 return SCTP_DISPOSITION_NOMEM;
2094}
2095
2096/*
2097 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2098 * chunk was retransmitted and then delayed in the network.
2099 *
2100 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2101 *
2102 * Verification Tag: None. Do cookie validation.
2103 *
2104 * Inputs
2105 * (endpoint, asoc, chunk)
2106 *
2107 * Outputs
2108 * (asoc, reply_msg, msg_up, timers, counters)
2109 *
2110 * The return value is the disposition of the chunk.
2111 */
Xin Long172a1592017-08-11 10:23:57 +08002112enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2113 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002114 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002116 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002118 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 struct sctp_association *new_asoc;
Xin Long172a1592017-08-11 10:23:57 +08002121 struct sctp_chunk *chunk = arg;
2122 enum sctp_disposition retval;
2123 struct sctp_chunk *err_chk_p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 int error = 0;
2125 char action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 /* Make sure that the chunk has a valid length from the protocol
2128 * perspective. In this case check to make sure we have at least
2129 * enough for the chunk header. Cookie length verification is
2130 * done later.
2131 */
Xin Long922dbc52017-06-30 11:52:13 +08002132 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002133 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 commands);
2135
2136 /* "Decode" the chunk. We have no optional parameters so we
2137 * are in good shape.
2138 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002139 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002140 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
Xin Long922dbc52017-06-30 11:52:13 +08002141 sizeof(struct sctp_chunkhdr)))
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002142 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2145 * of a duplicate COOKIE ECHO match the Verification Tags of the
2146 * current association, consider the State Cookie valid even if
2147 * the lifespan is exceeded.
2148 */
2149 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2150 &err_chk_p);
2151
2152 /* FIXME:
2153 * If the re-build failed, what is the proper error path
2154 * from here?
2155 *
2156 * [We should abort the association. --piggy]
2157 */
2158 if (!new_asoc) {
2159 /* FIXME: Several errors are possible. A bad cookie should
2160 * be silently discarded, but think about logging it too.
2161 */
2162 switch (error) {
2163 case -SCTP_IERROR_NOMEM:
2164 goto nomem;
2165
2166 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002167 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002169 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 case -SCTP_IERROR_BAD_SIG:
2171 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002172 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175
Richard Haines2277c7c2018-02-13 20:56:24 +00002176 /* Update socket peer label if first association. */
2177 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
2178 chunk->skb))
2179 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2180
Xin Long7e062972017-05-23 13:28:55 +08002181 /* Set temp so that it won't be added into hashtable */
2182 new_asoc->temp = 1;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* Compare the tie_tag in cookie with the verification tag of
2185 * current association.
2186 */
2187 action = sctp_tietags_compare(new_asoc, asoc);
2188
2189 switch (action) {
2190 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002191 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 new_asoc);
2193 break;
2194
2195 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002196 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 new_asoc);
2198 break;
2199
2200 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002201 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 new_asoc);
2203 break;
2204
2205 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002206 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 new_asoc);
2208 break;
2209
2210 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002211 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002216 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2218
Max Matveevd5ccd492011-08-29 21:02:24 +00002219 /* Restore association pointer to provide SCTP command interpeter
2220 * with a valid context in case it needs to manipulate
2221 * the queues */
2222 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2223 SCTP_ASOC((struct sctp_association *)asoc));
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return retval;
2226
2227nomem:
2228 return SCTP_DISPOSITION_NOMEM;
2229}
2230
2231/*
2232 * Process an ABORT. (SHUTDOWN-PENDING state)
2233 *
2234 * See sctp_sf_do_9_1_abort().
2235 */
Xin Long172a1592017-08-11 10:23:57 +08002236enum sctp_disposition sctp_sf_shutdown_pending_abort(
2237 struct net *net,
2238 const struct sctp_endpoint *ep,
2239 const struct sctp_association *asoc,
2240 const union sctp_subtype type,
2241 void *arg,
2242 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 struct sctp_chunk *chunk = arg;
2245
2246 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002247 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 /* Make sure that the ABORT chunk has a valid length.
2250 * Since this is an ABORT chunk, we have to discard it
2251 * because of the following text:
2252 * RFC 2960, Section 3.3.7
2253 * If an endpoint receives an ABORT with a format error or for an
2254 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002255 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 * as we do not know its true length. So, to be safe, discard the
2257 * packet.
2258 */
Xin Long441ae652017-07-23 09:34:36 +08002259 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002260 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Vlad Yasevich75205f42007-12-20 14:12:59 -08002262 /* ADD-IP: Special case for ABORT chunks
2263 * F4) One special consideration is that ABORT Chunks arriving
2264 * destined to the IP address being deleted MUST be
2265 * ignored (see Section 5.3.1 for further details).
2266 */
2267 if (SCTP_ADDR_DEL ==
2268 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002269 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002270
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002271 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272}
2273
2274/*
2275 * Process an ABORT. (SHUTDOWN-SENT state)
2276 *
2277 * See sctp_sf_do_9_1_abort().
2278 */
Xin Long172a1592017-08-11 10:23:57 +08002279enum sctp_disposition sctp_sf_shutdown_sent_abort(
2280 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002281 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002283 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002285 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 struct sctp_chunk *chunk = arg;
2288
2289 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002290 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 /* Make sure that the ABORT chunk has a valid length.
2293 * Since this is an ABORT chunk, we have to discard it
2294 * because of the following text:
2295 * RFC 2960, Section 3.3.7
2296 * If an endpoint receives an ABORT with a format error or for an
2297 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002298 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 * as we do not know its true length. So, to be safe, discard the
2300 * packet.
2301 */
Xin Long441ae652017-07-23 09:34:36 +08002302 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002303 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Vlad Yasevich75205f42007-12-20 14:12:59 -08002305 /* ADD-IP: Special case for ABORT chunks
2306 * F4) One special consideration is that ABORT Chunks arriving
2307 * destined to the IP address being deleted MUST be
2308 * ignored (see Section 5.3.1 for further details).
2309 */
2310 if (SCTP_ADDR_DEL ==
2311 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002312 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* Stop the T2-shutdown timer. */
2315 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2316 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2317
2318 /* Stop the T5-shutdown guard timer. */
2319 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2320 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2321
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002322 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
2325/*
2326 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2327 *
2328 * See sctp_sf_do_9_1_abort().
2329 */
Xin Long172a1592017-08-11 10:23:57 +08002330enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2331 struct net *net,
2332 const struct sctp_endpoint *ep,
2333 const struct sctp_association *asoc,
2334 const union sctp_subtype type,
2335 void *arg,
2336 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337{
2338 /* The same T2 timer, so we should be able to use
2339 * common function with the SHUTDOWN-SENT state.
2340 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002341 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344/*
2345 * Handle an Error received in COOKIE_ECHOED state.
2346 *
2347 * Only handle the error type of stale COOKIE Error, the other errors will
2348 * be ignored.
2349 *
2350 * Inputs
2351 * (endpoint, asoc, chunk)
2352 *
2353 * Outputs
2354 * (asoc, reply_msg, msg_up, timers, counters)
2355 *
2356 * The return value is the disposition of the chunk.
2357 */
Xin Long172a1592017-08-11 10:23:57 +08002358enum sctp_disposition sctp_sf_cookie_echoed_err(
2359 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002360 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002362 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002364 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08002367 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002370 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 /* Make sure that the ERROR chunk has a valid length.
2373 * The parameter walking depends on this as well.
2374 */
Xin Long87caeba2017-08-03 15:42:12 +08002375 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002376 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 commands);
2378
2379 /* Process the error here */
2380 /* FUTURE FIXME: When PR-SCTP related and other optional
2381 * parms are emitted, this will have to change to handle multiple
2382 * errors.
2383 */
2384 sctp_walk_errors(err, chunk->chunk_hdr) {
2385 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002386 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 arg, commands);
2388 }
2389
2390 /* It is possible to have malformed error causes, and that
2391 * will cause us to end the walk early. However, since
2392 * we are discarding the packet, there should be no adverse
2393 * affects.
2394 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002395 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396}
2397
2398/*
2399 * Handle a Stale COOKIE Error
2400 *
2401 * Section: 5.2.6 Handle Stale COOKIE Error
2402 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2403 * one of the following three alternatives.
2404 * ...
2405 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2406 * Preservative parameter requesting an extension to the lifetime of
2407 * the State Cookie. When calculating the time extension, an
2408 * implementation SHOULD use the RTT information measured based on the
2409 * previous COOKIE ECHO / ERROR exchange, and should add no more
2410 * than 1 second beyond the measured RTT, due to long State Cookie
2411 * lifetimes making the endpoint more subject to a replay attack.
2412 *
2413 * Verification Tag: Not explicit, but safe to ignore.
2414 *
2415 * Inputs
2416 * (endpoint, asoc, chunk)
2417 *
2418 * Outputs
2419 * (asoc, reply_msg, msg_up, timers, counters)
2420 *
2421 * The return value is the disposition of the chunk.
2422 */
Xin Long172a1592017-08-11 10:23:57 +08002423static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2424 struct net *net,
2425 const struct sctp_endpoint *ep,
2426 const struct sctp_association *asoc,
2427 const union sctp_subtype type,
2428 void *arg,
2429 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002431 int attempts = asoc->init_err_counter + 1;
Xin Long365ddb62017-07-17 11:29:51 +08002432 struct sctp_chunk *chunk = arg, *reply;
2433 struct sctp_cookie_preserve_param bht;
2434 struct sctp_bind_addr *bp;
Xin Longd8238d92017-08-03 15:42:11 +08002435 struct sctp_errhdr *err;
Xin Long365ddb62017-07-17 11:29:51 +08002436 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Vlad Yasevich81845c22006-01-30 15:59:54 -08002438 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002439 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2440 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002442 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return SCTP_DISPOSITION_DELETE_TCB;
2444 }
2445
Xin Longd8238d92017-08-03 15:42:11 +08002446 err = (struct sctp_errhdr *)(chunk->skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
2448 /* When calculating the time extension, an implementation
2449 * SHOULD use the RTT information measured based on the
2450 * previous COOKIE ECHO / ERROR exchange, and should add no
2451 * more than 1 second beyond the measured RTT, due to long
2452 * State Cookie lifetimes making the endpoint more subject to
2453 * a replay attack.
2454 * Measure of Staleness's unit is usec. (1/1000000 sec)
2455 * Suggested Cookie Life-span Increment's unit is msec.
2456 * (1/1000 sec)
2457 * In general, if you use the suggested cookie life, the value
2458 * found in the field of measure of staleness should be doubled
2459 * to give ample time to retransmit the new cookie and thus
2460 * yield a higher probability of success on the reattempt.
2461 */
Xin Longd8238d92017-08-03 15:42:11 +08002462 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 stale = (stale * 2) / 1000;
2464
2465 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2466 bht.param_hdr.length = htons(sizeof(bht));
2467 bht.lifespan_increment = htonl(stale);
2468
2469 /* Build that new INIT chunk. */
2470 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2471 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2472 if (!reply)
2473 goto nomem;
2474
2475 sctp_addto_chunk(reply, sizeof(bht), &bht);
2476
2477 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2478 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2479
2480 /* Stop pending T3-rtx and heartbeat timers */
2481 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2482 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2483
2484 /* Delete non-primary peer ip addresses since we are transitioning
2485 * back to the COOKIE-WAIT state
2486 */
2487 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2488
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002489 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2490 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002492 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 SCTP_TRANSPORT(asoc->peer.primary_path));
2494
2495 /* Cast away the const modifier, as we want to just
2496 * rerun it through as a sideffect.
2497 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002498 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2501 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2502 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2503 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2504 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2505 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2506
2507 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2508
2509 return SCTP_DISPOSITION_CONSUME;
2510
2511nomem:
2512 return SCTP_DISPOSITION_NOMEM;
2513}
2514
2515/*
2516 * Process an ABORT.
2517 *
2518 * Section: 9.1
2519 * After checking the Verification Tag, the receiving endpoint shall
2520 * remove the association from its record, and shall report the
2521 * termination to its upper layer.
2522 *
2523 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2524 * B) Rules for packet carrying ABORT:
2525 *
2526 * - The endpoint shall always fill in the Verification Tag field of the
2527 * outbound packet with the destination endpoint's tag value if it
2528 * is known.
2529 *
2530 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2531 * MUST follow the procedure described in Section 8.4.
2532 *
2533 * - The receiver MUST accept the packet if the Verification Tag
2534 * matches either its own tag, OR the tag of its peer. Otherwise, the
2535 * receiver MUST silently discard the packet and take no further
2536 * action.
2537 *
2538 * Inputs
2539 * (endpoint, asoc, chunk)
2540 *
2541 * Outputs
2542 * (asoc, reply_msg, msg_up, timers, counters)
2543 *
2544 * The return value is the disposition of the chunk.
2545 */
Xin Long172a1592017-08-11 10:23:57 +08002546enum sctp_disposition sctp_sf_do_9_1_abort(
2547 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002548 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002550 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002552 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
2554 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002557 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 /* Make sure that the ABORT chunk has a valid length.
2560 * Since this is an ABORT chunk, we have to discard it
2561 * because of the following text:
2562 * RFC 2960, Section 3.3.7
2563 * If an endpoint receives an ABORT with a format error or for an
2564 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002565 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 * as we do not know its true length. So, to be safe, discard the
2567 * packet.
2568 */
Xin Long441ae652017-07-23 09:34:36 +08002569 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002570 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Vlad Yasevich75205f42007-12-20 14:12:59 -08002572 /* ADD-IP: Special case for ABORT chunks
2573 * F4) One special consideration is that ABORT Chunks arriving
2574 * destined to the IP address being deleted MUST be
2575 * ignored (see Section 5.3.1 for further details).
2576 */
2577 if (SCTP_ADDR_DEL ==
2578 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002579 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002580
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002581 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002582}
2583
Xin Long172a1592017-08-11 10:23:57 +08002584static enum sctp_disposition __sctp_sf_do_9_1_abort(
2585 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002586 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002587 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002588 const union sctp_subtype type,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002589 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002590 struct sctp_cmd_seq *commands)
Vlad Yasevich75205f42007-12-20 14:12:59 -08002591{
Xin Long172a1592017-08-11 10:23:57 +08002592 __be16 error = SCTP_ERROR_NO_ERROR;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002593 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002594 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /* See if we have an error cause code in the chunk. */
2597 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002598 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
Xin Longd8238d92017-08-03 15:42:11 +08002599 struct sctp_errhdr *err;
Shan Wei96ca4682011-04-19 21:26:26 +00002600
Shan Wei96ca4682011-04-19 21:26:26 +00002601 sctp_walk_errors(err, chunk->chunk_hdr);
2602 if ((void *)err != (void *)chunk->chunk_end)
Xin Longd8238d92017-08-03 15:42:11 +08002603 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
2604 commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002605
Xin Longd8238d92017-08-03 15:42:11 +08002606 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002609 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002610 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002611 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002612 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2613 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 return SCTP_DISPOSITION_ABORT;
2616}
2617
2618/*
2619 * Process an ABORT. (COOKIE-WAIT state)
2620 *
2621 * See sctp_sf_do_9_1_abort() above.
2622 */
Xin Long172a1592017-08-11 10:23:57 +08002623enum sctp_disposition sctp_sf_cookie_wait_abort(
2624 struct net *net,
2625 const struct sctp_endpoint *ep,
2626 const struct sctp_association *asoc,
2627 const union sctp_subtype type,
2628 void *arg,
2629 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Xin Long172a1592017-08-11 10:23:57 +08002631 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002633 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002636 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /* Make sure that the ABORT chunk has a valid length.
2639 * Since this is an ABORT chunk, we have to discard it
2640 * because of the following text:
2641 * RFC 2960, Section 3.3.7
2642 * If an endpoint receives an ABORT with a format error or for an
2643 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002644 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 * as we do not know its true length. So, to be safe, discard the
2646 * packet.
2647 */
Xin Long441ae652017-07-23 09:34:36 +08002648 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002649 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /* See if we have an error cause code in the chunk. */
2652 len = ntohs(chunk->chunk_hdr->length);
2653 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
Xin Longd8238d92017-08-03 15:42:11 +08002654 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002656 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002657 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659
2660/*
2661 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2662 */
Xin Long172a1592017-08-11 10:23:57 +08002663enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2664 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002665 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08002667 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08002669 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002671 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002672 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002673 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
2676/*
2677 * Process an ABORT. (COOKIE-ECHOED state)
2678 */
Xin Long172a1592017-08-11 10:23:57 +08002679enum sctp_disposition sctp_sf_cookie_echoed_abort(
2680 struct net *net,
2681 const struct sctp_endpoint *ep,
2682 const struct sctp_association *asoc,
2683 const union sctp_subtype type,
2684 void *arg,
2685 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
2687 /* There is a single T1 timer, so we should be able to use
2688 * common function with the COOKIE-WAIT state.
2689 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002690 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
2693/*
2694 * Stop T1 timer and abort association with "INIT failed".
2695 *
2696 * This is common code called by several sctp_sf_*_abort() functions above.
2697 */
Xin Long172a1592017-08-11 10:23:57 +08002698static enum sctp_disposition sctp_stop_t1_and_abort(
2699 struct net *net,
2700 struct sctp_cmd_seq *commands,
2701 __be16 error, int sk_err,
2702 const struct sctp_association *asoc,
2703 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002705 pr_debug("%s: ABORT received (INIT)\n", __func__);
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2708 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002709 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2711 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002712 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /* CMD_INIT_FAILED will DELETE_TCB. */
2714 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002715 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002716
Frank Filz3f7a87d2005-06-20 13:14:57 -07002717 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720/*
2721 * sctp_sf_do_9_2_shut
2722 *
2723 * Section: 9.2
2724 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2725 * - enter the SHUTDOWN-RECEIVED state,
2726 *
2727 * - stop accepting new data from its SCTP user
2728 *
2729 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2730 * that all its outstanding DATA chunks have been received by the
2731 * SHUTDOWN sender.
2732 *
2733 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2734 * send a SHUTDOWN in response to a ULP request. And should discard
2735 * subsequent SHUTDOWN chunks.
2736 *
2737 * If there are still outstanding DATA chunks left, the SHUTDOWN
2738 * receiver shall continue to follow normal data transmission
2739 * procedures defined in Section 6 until all outstanding DATA chunks
2740 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2741 * new data from its SCTP user.
2742 *
2743 * Verification Tag: 8.5 Verification Tag [Normal verification]
2744 *
2745 * Inputs
2746 * (endpoint, asoc, chunk)
2747 *
2748 * Outputs
2749 * (asoc, reply_msg, msg_up, timers, counters)
2750 *
2751 * The return value is the disposition of the chunk.
2752 */
Xin Long172a1592017-08-11 10:23:57 +08002753enum sctp_disposition sctp_sf_do_9_2_shutdown(
2754 struct net *net,
2755 const struct sctp_endpoint *ep,
2756 const struct sctp_association *asoc,
2757 const union sctp_subtype type,
2758 void *arg,
2759 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Xin Long172a1592017-08-11 10:23:57 +08002761 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 struct sctp_chunk *chunk = arg;
Xin Longe61e4052017-08-03 15:42:09 +08002763 struct sctp_shutdownhdr *sdh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002765 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002768 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002771 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002772 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 commands);
2774
2775 /* Convert the elaborate header. */
Xin Longe61e4052017-08-03 15:42:09 +08002776 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2777 skb_pull(chunk->skb, sizeof(*sdh));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002779 ctsn = ntohl(sdh->cum_tsn_ack);
2780
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002781 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002782 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2783 asoc->ctsn_ack_point);
2784
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002785 return SCTP_DISPOSITION_DISCARD;
2786 }
2787
Wei Yongjundf10eec2008-10-23 01:00:21 -07002788 /* If Cumulative TSN Ack beyond the max tsn currently
2789 * send, terminating the association and respond to the
2790 * sender with an ABORT.
2791 */
2792 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002793 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002795 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2796 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2797 * inform the application that it should cease sending data.
2798 */
2799 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2800 if (!ev) {
2801 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002802 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002803 }
2804 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2807 * - enter the SHUTDOWN-RECEIVED state,
2808 * - stop accepting new data from its SCTP user
2809 *
2810 * [This is implicit in the new state.]
2811 */
2812 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2813 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2814 disposition = SCTP_DISPOSITION_CONSUME;
2815
2816 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002817 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 arg, commands);
2819 }
2820
2821 if (SCTP_DISPOSITION_NOMEM == disposition)
2822 goto out;
2823
2824 /* - verify, by checking the Cumulative TSN Ack field of the
2825 * chunk, that all its outstanding DATA chunks have been
2826 * received by the SHUTDOWN sender.
2827 */
2828 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002829 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831out:
2832 return disposition;
2833}
2834
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002835/*
2836 * sctp_sf_do_9_2_shut_ctsn
2837 *
2838 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2839 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2840 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2841 * MUST be processed.
2842 */
Xin Long172a1592017-08-11 10:23:57 +08002843enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2844 struct net *net,
2845 const struct sctp_endpoint *ep,
2846 const struct sctp_association *asoc,
2847 const union sctp_subtype type,
2848 void *arg,
2849 struct sctp_cmd_seq *commands)
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002850{
2851 struct sctp_chunk *chunk = arg;
Xin Longe61e4052017-08-03 15:42:09 +08002852 struct sctp_shutdownhdr *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002853 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002854
2855 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002856 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002857
2858 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08002859 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002860 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002861 commands);
2862
Xin Longe61e4052017-08-03 15:42:09 +08002863 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002864 ctsn = ntohl(sdh->cum_tsn_ack);
2865
2866 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002867 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2868 asoc->ctsn_ack_point);
2869
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002870 return SCTP_DISPOSITION_DISCARD;
2871 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002872
2873 /* If Cumulative TSN Ack beyond the max tsn currently
2874 * send, terminating the association and respond to the
2875 * sender with an ABORT.
2876 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002877 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002878 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002879
2880 /* verify, by checking the Cumulative TSN Ack field of the
2881 * chunk, that all its outstanding DATA chunks have been
2882 * received by the SHUTDOWN sender.
2883 */
2884 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2885 SCTP_BE32(sdh->cum_tsn_ack));
2886
2887 return SCTP_DISPOSITION_CONSUME;
2888}
2889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890/* RFC 2960 9.2
2891 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2892 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2893 * transport addresses (either in the IP addresses or in the INIT chunk)
2894 * that belong to this association, it should discard the INIT chunk and
2895 * retransmit the SHUTDOWN ACK chunk.
2896 */
Xin Long172a1592017-08-11 10:23:57 +08002897enum sctp_disposition sctp_sf_do_9_2_reshutack(
2898 struct net *net,
2899 const struct sctp_endpoint *ep,
2900 const struct sctp_association *asoc,
2901 const union sctp_subtype type,
2902 void *arg,
2903 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Xin Long172a1592017-08-11 10:23:57 +08002905 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 struct sctp_chunk *reply;
2907
Vlad Yasevichece25df2007-09-07 16:30:54 -04002908 /* Make sure that the chunk has a valid length */
Xin Long922dbc52017-06-30 11:52:13 +08002909 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002910 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002911 commands);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /* Since we are not going to really process this INIT, there
2914 * is no point in verifying chunk boundries. Just generate
2915 * the SHUTDOWN ACK.
2916 */
2917 reply = sctp_make_shutdown_ack(asoc, chunk);
2918 if (NULL == reply)
2919 goto nomem;
2920
2921 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2922 * the T2-SHUTDOWN timer.
2923 */
2924 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2925
2926 /* and restart the T2-shutdown timer. */
2927 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2928 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2929
2930 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2931
2932 return SCTP_DISPOSITION_CONSUME;
2933nomem:
2934 return SCTP_DISPOSITION_NOMEM;
2935}
2936
2937/*
2938 * sctp_sf_do_ecn_cwr
2939 *
2940 * Section: Appendix A: Explicit Congestion Notification
2941 *
2942 * CWR:
2943 *
2944 * RFC 2481 details a specific bit for a sender to send in the header of
2945 * its next outbound TCP segment to indicate to its peer that it has
2946 * reduced its congestion window. This is termed the CWR bit. For
2947 * SCTP the same indication is made by including the CWR chunk.
2948 * This chunk contains one data element, i.e. the TSN number that
2949 * was sent in the ECNE chunk. This element represents the lowest
2950 * TSN number in the datagram that was originally marked with the
2951 * CE bit.
2952 *
2953 * Verification Tag: 8.5 Verification Tag [Normal verification]
2954 * Inputs
2955 * (endpoint, asoc, chunk)
2956 *
2957 * Outputs
2958 * (asoc, reply_msg, msg_up, timers, counters)
2959 *
2960 * The return value is the disposition of the chunk.
2961 */
Xin Long172a1592017-08-11 10:23:57 +08002962enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
2963 const struct sctp_endpoint *ep,
2964 const struct sctp_association *asoc,
2965 const union sctp_subtype type,
2966 void *arg,
2967 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 struct sctp_chunk *chunk = arg;
Xin Long65f77102017-08-03 15:42:16 +08002970 struct sctp_cwrhdr *cwr;
Al Viro34bcca22006-11-20 17:27:15 -08002971 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002974 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Xin Longb515fd22017-08-03 15:42:15 +08002976 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002977 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002979
Xin Long65f77102017-08-03 15:42:16 +08002980 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2981 skb_pull(chunk->skb, sizeof(*cwr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
Al Viro34bcca22006-11-20 17:27:15 -08002983 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002986 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 /* Stop sending ECNE. */
2988 sctp_add_cmd_sf(commands,
2989 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002990 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992 return SCTP_DISPOSITION_CONSUME;
2993}
2994
2995/*
2996 * sctp_sf_do_ecne
2997 *
2998 * Section: Appendix A: Explicit Congestion Notification
2999 *
3000 * ECN-Echo
3001 *
3002 * RFC 2481 details a specific bit for a receiver to send back in its
3003 * TCP acknowledgements to notify the sender of the Congestion
3004 * Experienced (CE) bit having arrived from the network. For SCTP this
3005 * same indication is made by including the ECNE chunk. This chunk
3006 * contains one data element, i.e. the lowest TSN associated with the IP
3007 * datagram marked with the CE bit.....
3008 *
3009 * Verification Tag: 8.5 Verification Tag [Normal verification]
3010 * Inputs
3011 * (endpoint, asoc, chunk)
3012 *
3013 * Outputs
3014 * (asoc, reply_msg, msg_up, timers, counters)
3015 *
3016 * The return value is the disposition of the chunk.
3017 */
Xin Long172a1592017-08-11 10:23:57 +08003018enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3019 const struct sctp_endpoint *ep,
3020 const struct sctp_association *asoc,
3021 const union sctp_subtype type,
3022 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 struct sctp_chunk *chunk = arg;
Xin Long1fb6d832017-08-03 15:42:14 +08003025 struct sctp_ecnehdr *ecne;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
3027 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003028 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Xin Longb515fd22017-08-03 15:42:15 +08003030 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003031 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 commands);
3033
Xin Long1fb6d832017-08-03 15:42:14 +08003034 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3035 skb_pull(chunk->skb, sizeof(*ecne));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 /* If this is a newer ECNE than the last CWR packet we sent out */
3038 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3039 SCTP_U32(ntohl(ecne->lowest_tsn)));
3040
3041 return SCTP_DISPOSITION_CONSUME;
3042}
3043
3044/*
3045 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3046 *
3047 * The SCTP endpoint MUST always acknowledge the reception of each valid
3048 * DATA chunk.
3049 *
3050 * The guidelines on delayed acknowledgement algorithm specified in
3051 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3052 * acknowledgement SHOULD be generated for at least every second packet
3053 * (not every second DATA chunk) received, and SHOULD be generated within
3054 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3055 * situations it may be beneficial for an SCTP transmitter to be more
3056 * conservative than the algorithms detailed in this document allow.
3057 * However, an SCTP transmitter MUST NOT be more aggressive than the
3058 * following algorithms allow.
3059 *
3060 * A SCTP receiver MUST NOT generate more than one SACK for every
3061 * incoming packet, other than to update the offered window as the
3062 * receiving application consumes new data.
3063 *
3064 * Verification Tag: 8.5 Verification Tag [Normal verification]
3065 *
3066 * Inputs
3067 * (endpoint, asoc, chunk)
3068 *
3069 * Outputs
3070 * (asoc, reply_msg, msg_up, timers, counters)
3071 *
3072 * The return value is the disposition of the chunk.
3073 */
Xin Long172a1592017-08-11 10:23:57 +08003074enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3075 const struct sctp_endpoint *ep,
3076 const struct sctp_association *asoc,
3077 const union sctp_subtype type,
3078 void *arg,
3079 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080{
Xin Longc488b772017-08-11 10:23:53 +08003081 union sctp_arg force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 struct sctp_chunk *chunk = arg;
3083 int error;
3084
3085 if (!sctp_vtag_verify(chunk, asoc)) {
3086 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3087 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003088 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
Xin Long9d4ceaf2017-12-08 21:04:03 +08003091 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003092 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 commands);
3094
wangweidongcb3f8372013-12-23 12:16:50 +08003095 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 switch (error) {
3097 case SCTP_IERROR_NO_ERROR:
3098 break;
3099 case SCTP_IERROR_HIGH_TSN:
3100 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003101 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 goto discard_noforce;
3103 case SCTP_IERROR_DUP_TSN:
3104 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003105 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 goto discard_force;
3107 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003108 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003109 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003110 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003111 (u8 *)chunk->subh.data_hdr,
Xin Long9d4ceaf2017-12-08 21:04:03 +08003112 sctp_datahdr_len(&asoc->stream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 default:
3114 BUG();
3115 }
3116
Wei Yongjun6dc76942009-11-23 15:53:53 -05003117 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3118 force = SCTP_FORCE();
3119
Neil Horman9f70f462013-12-10 06:48:15 -05003120 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3122 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3123 }
3124
3125 /* If this is the last chunk in a packet, we need to count it
3126 * toward sack generation. Note that we need to SACK every
3127 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3128 * THEM. We elect to NOT generate SACK's if the chunk fails
3129 * the verification tag test.
3130 *
3131 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3132 *
3133 * The SCTP endpoint MUST always acknowledge the reception of
3134 * each valid DATA chunk.
3135 *
3136 * The guidelines on delayed acknowledgement algorithm
3137 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3138 * Specifically, an acknowledgement SHOULD be generated for at
3139 * least every second packet (not every second DATA chunk)
3140 * received, and SHOULD be generated within 200 ms of the
3141 * arrival of any unacknowledged DATA chunk. In some
3142 * situations it may be beneficial for an SCTP transmitter to
3143 * be more conservative than the algorithms detailed in this
3144 * document allow. However, an SCTP transmitter MUST NOT be
3145 * more aggressive than the following algorithms allow.
3146 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003147 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003148 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return SCTP_DISPOSITION_CONSUME;
3151
3152discard_force:
3153 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3154 *
3155 * When a packet arrives with duplicate DATA chunk(s) and with
3156 * no new DATA chunk(s), the endpoint MUST immediately send a
3157 * SACK with no delay. If a packet arrives with duplicate
3158 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3159 * MAY immediately send a SACK. Normally receipt of duplicate
3160 * DATA chunks will occur when the original SACK chunk was lost
3161 * and the peer's RTO has expired. The duplicate TSN number(s)
3162 * SHOULD be reported in the SACK as duplicate.
3163 */
3164 /* In our case, we split the MAY SACK advice up whether or not
3165 * the last chunk is a duplicate.'
3166 */
3167 if (chunk->end_of_packet)
3168 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3169 return SCTP_DISPOSITION_DISCARD;
3170
3171discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003172 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003173 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177
3178/*
3179 * sctp_sf_eat_data_fast_4_4
3180 *
3181 * Section: 4 (4)
3182 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3183 * DATA chunks without delay.
3184 *
3185 * Verification Tag: 8.5 Verification Tag [Normal verification]
3186 * Inputs
3187 * (endpoint, asoc, chunk)
3188 *
3189 * Outputs
3190 * (asoc, reply_msg, msg_up, timers, counters)
3191 *
3192 * The return value is the disposition of the chunk.
3193 */
Xin Long172a1592017-08-11 10:23:57 +08003194enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3195 struct net *net,
3196 const struct sctp_endpoint *ep,
3197 const struct sctp_association *asoc,
3198 const union sctp_subtype type,
3199 void *arg,
3200 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
3202 struct sctp_chunk *chunk = arg;
3203 int error;
3204
3205 if (!sctp_vtag_verify(chunk, asoc)) {
3206 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3207 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003208 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 }
3210
Xin Long9d4ceaf2017-12-08 21:04:03 +08003211 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003212 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 commands);
3214
wangweidongcb3f8372013-12-23 12:16:50 +08003215 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 switch (error) {
3217 case SCTP_IERROR_NO_ERROR:
3218 case SCTP_IERROR_HIGH_TSN:
3219 case SCTP_IERROR_DUP_TSN:
3220 case SCTP_IERROR_IGNORE_TSN:
3221 case SCTP_IERROR_BAD_STREAM:
3222 break;
3223 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003224 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003225 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003226 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Xin Long3583df12017-06-30 11:52:19 +08003227 (u8 *)chunk->subh.data_hdr,
Xin Long9d4ceaf2017-12-08 21:04:03 +08003228 sctp_datahdr_len(&asoc->stream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 default:
3230 BUG();
3231 }
3232
3233 /* Go a head and force a SACK, since we are shutting down. */
3234
3235 /* Implementor's Guide.
3236 *
3237 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3238 * respond to each received packet containing one or more DATA chunk(s)
3239 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3240 */
3241 if (chunk->end_of_packet) {
3242 /* We must delay the chunk creation since the cumulative
3243 * TSN has not been updated yet.
3244 */
3245 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3246 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3247 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3248 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3249 }
3250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return SCTP_DISPOSITION_CONSUME;
3252}
3253
3254/*
3255 * Section: 6.2 Processing a Received SACK
3256 * D) Any time a SACK arrives, the endpoint performs the following:
3257 *
3258 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3259 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3260 * increasing, a SACK whose Cumulative TSN Ack is less than the
3261 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3262 *
3263 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3264 * of bytes still outstanding after processing the Cumulative TSN Ack
3265 * and the Gap Ack Blocks.
3266 *
3267 * iii) If the SACK is missing a TSN that was previously
3268 * acknowledged via a Gap Ack Block (e.g., the data receiver
3269 * reneged on the data), then mark the corresponding DATA chunk
3270 * as available for retransmit: Mark it as missing for fast
3271 * retransmit as described in Section 7.2.4 and if no retransmit
3272 * timer is running for the destination address to which the DATA
3273 * chunk was originally transmitted, then T3-rtx is started for
3274 * that destination address.
3275 *
3276 * Verification Tag: 8.5 Verification Tag [Normal verification]
3277 *
3278 * Inputs
3279 * (endpoint, asoc, chunk)
3280 *
3281 * Outputs
3282 * (asoc, reply_msg, msg_up, timers, counters)
3283 *
3284 * The return value is the disposition of the chunk.
3285 */
Xin Long172a1592017-08-11 10:23:57 +08003286enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3287 const struct sctp_endpoint *ep,
3288 const struct sctp_association *asoc,
3289 const union sctp_subtype type,
3290 void *arg,
3291 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
3293 struct sctp_chunk *chunk = arg;
Xin Long78731082017-07-23 09:34:32 +08003294 struct sctp_sackhdr *sackh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 __u32 ctsn;
3296
Masami Hiramatsu103d7502017-12-29 11:46:51 +09003297 trace_sctp_probe(ep, asoc, chunk);
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003300 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 /* Make sure that the SACK chunk has a valid length. */
Xin Longd4d6c612017-07-23 09:34:33 +08003303 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003304 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 commands);
3306
3307 /* Pull the SACK chunk from the data buffer */
3308 sackh = sctp_sm_pull_sack(chunk);
3309 /* Was this a bogus SACK? */
3310 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003311 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 chunk->subh.sack_hdr = sackh;
3313 ctsn = ntohl(sackh->cum_tsn_ack);
3314
3315 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3316 * Ack Point, then drop the SACK. Since Cumulative TSN
3317 * Ack is monotonically increasing, a SACK whose
3318 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3319 * Point indicates an out-of-order SACK.
3320 */
3321 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003322 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3323 asoc->ctsn_ack_point);
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return SCTP_DISPOSITION_DISCARD;
3326 }
3327
Wei Yongjunaecedea2007-08-02 16:57:44 +08003328 /* If Cumulative TSN Ack beyond the max tsn currently
3329 * send, terminating the association and respond to the
3330 * sender with an ABORT.
3331 */
3332 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003333 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003336 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 /* Note: We do the rest of the work on the PROCESS_SACK
3339 * sideeffect.
3340 */
3341 return SCTP_DISPOSITION_CONSUME;
3342}
3343
3344/*
3345 * Generate an ABORT in response to a packet.
3346 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003347 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003349 * 8) The receiver should respond to the sender of the OOTB packet with
3350 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3351 * MUST fill in the Verification Tag field of the outbound packet
3352 * with the value found in the Verification Tag field of the OOTB
3353 * packet and set the T-bit in the Chunk Flags to indicate that the
3354 * Verification Tag is reflected. After sending this ABORT, the
3355 * receiver of the OOTB packet shall discard the OOTB packet and take
3356 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 *
3358 * Verification Tag:
3359 *
3360 * The return value is the disposition of the chunk.
3361*/
Xin Long172a1592017-08-11 10:23:57 +08003362static enum sctp_disposition sctp_sf_tabort_8_4_8(
3363 struct net *net,
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003364 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08003366 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08003368 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
3370 struct sctp_packet *packet = NULL;
3371 struct sctp_chunk *chunk = arg;
3372 struct sctp_chunk *abort;
3373
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003374 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003375 if (!packet)
3376 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003378 /* Make an ABORT. The T bit will be set if the asoc
3379 * is NULL.
3380 */
3381 abort = sctp_make_abort(asoc, chunk, 0);
3382 if (!abort) {
3383 sctp_ootb_pkt_free(packet);
3384 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 }
3386
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003387 /* Reflect vtag if T-Bit is set */
3388 if (sctp_test_T_bit(abort))
3389 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3390
3391 /* Set the skb to the belonging sock for accounting. */
3392 abort->skb->sk = ep->base.sk;
3393
3394 sctp_packet_append_chunk(packet, abort);
3395
3396 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3397 SCTP_PACKET(packet));
3398
3399 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3400
3401 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3402 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
3405/*
3406 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3407 * event as ULP notification for each cause included in the chunk.
3408 *
3409 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3410 *
3411 * The return value is the disposition of the chunk.
3412*/
Xin Long172a1592017-08-11 10:23:57 +08003413enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3414 const struct sctp_endpoint *ep,
3415 const struct sctp_association *asoc,
3416 const union sctp_subtype type,
3417 void *arg,
3418 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
3420 struct sctp_chunk *chunk = arg;
Xin Longd8238d92017-08-03 15:42:11 +08003421 struct sctp_errhdr *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003424 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 /* Make sure that the ERROR chunk has a valid length. */
Xin Long87caeba2017-08-03 15:42:12 +08003427 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003428 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003430 sctp_walk_errors(err, chunk->chunk_hdr);
3431 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003432 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003433 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Wei Yongjun3df267872009-03-02 06:46:51 +00003435 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3436 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
3441/*
3442 * Process an inbound SHUTDOWN ACK.
3443 *
3444 * From Section 9.2:
3445 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3446 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3447 * peer, and remove all record of the association.
3448 *
3449 * The return value is the disposition.
3450 */
Xin Long172a1592017-08-11 10:23:57 +08003451enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3452 const struct sctp_endpoint *ep,
3453 const struct sctp_association *asoc,
3454 const union sctp_subtype type,
3455 void *arg,
3456 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457{
3458 struct sctp_chunk *chunk = arg;
3459 struct sctp_chunk *reply;
3460 struct sctp_ulpevent *ev;
3461
3462 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003463 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003466 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003467 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 /* 10.2 H) SHUTDOWN COMPLETE notification
3470 *
3471 * When SCTP completes the shutdown procedures (section 9.2) this
3472 * notification is passed to the upper layer.
3473 */
3474 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003475 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (!ev)
3477 goto nomem;
3478
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003479 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3480 reply = sctp_make_shutdown_complete(asoc, chunk);
3481 if (!reply)
3482 goto nomem_chunk;
3483
3484 /* Do all the commands now (after allocation), so that we
3485 * have consistent state if memory allocation failes
3486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3488
3489 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3490 * stop the T2-shutdown timer,
3491 */
3492 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3493 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3494
3495 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3496 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3499 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003500 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3501 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3503
3504 /* ...and remove all record of the association. */
3505 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3506 return SCTP_DISPOSITION_DELETE_TCB;
3507
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003508nomem_chunk:
3509 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510nomem:
3511 return SCTP_DISPOSITION_NOMEM;
3512}
3513
3514/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003515 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3516 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3518 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3519 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3520 * packet must fill in the Verification Tag field of the outbound
3521 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003522 * set the T-bit in the Chunk Flags to indicate that the Verification
3523 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 *
3525 * 8) The receiver should respond to the sender of the OOTB packet with
3526 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3527 * MUST fill in the Verification Tag field of the outbound packet
3528 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003529 * packet and set the T-bit in the Chunk Flags to indicate that the
3530 * Verification Tag is reflected. After sending this ABORT, the
3531 * receiver of the OOTB packet shall discard the OOTB packet and take
3532 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 */
Xin Long172a1592017-08-11 10:23:57 +08003534enum sctp_disposition sctp_sf_ootb(struct net *net,
3535 const struct sctp_endpoint *ep,
3536 const struct sctp_association *asoc,
3537 const union sctp_subtype type,
3538 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
3540 struct sctp_chunk *chunk = arg;
3541 struct sk_buff *skb = chunk->skb;
Xin Long922dbc52017-06-30 11:52:13 +08003542 struct sctp_chunkhdr *ch;
Xin Longd8238d92017-08-03 15:42:11 +08003543 struct sctp_errhdr *err;
Shan Wei85c5ed42011-04-19 21:30:01 +00003544 int ootb_cookie_ack = 0;
Xin Long172a1592017-08-11 10:23:57 +08003545 int ootb_shut_ack = 0;
3546 __u8 *ch_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003548 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
Xin Long922dbc52017-06-30 11:52:13 +08003550 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003552 /* Report violation if the chunk is less then minimal */
Xin Long922dbc52017-06-30 11:52:13 +08003553 if (ntohs(ch->length) < sizeof(*ch))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003554 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003555 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003557 /* Report violation if chunk len overflows */
3558 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3559 if (ch_end > skb_tail_pointer(skb))
3560 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3561 commands);
3562
Vlad Yasevichece25df2007-09-07 16:30:54 -04003563 /* Now that we know we at least have a chunk header,
3564 * do things that are type appropriate.
3565 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3567 ootb_shut_ack = 1;
3568
3569 /* RFC 2960, Section 3.3.7
3570 * Moreover, under any circumstances, an endpoint that
3571 * receives an ABORT MUST NOT respond to that ABORT by
3572 * sending an ABORT of its own.
3573 */
3574 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003575 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003576
Shan Wei85c5ed42011-04-19 21:30:01 +00003577 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3578 * or a COOKIE ACK the SCTP Packet should be silently
3579 * discarded.
3580 */
3581
3582 if (SCTP_CID_COOKIE_ACK == ch->type)
3583 ootb_cookie_ack = 1;
3584
3585 if (SCTP_CID_ERROR == ch->type) {
3586 sctp_walk_errors(err, ch) {
3587 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3588 ootb_cookie_ack = 1;
3589 break;
3590 }
3591 }
3592 }
3593
Xin Long922dbc52017-06-30 11:52:13 +08003594 ch = (struct sctp_chunkhdr *)ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003595 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003598 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003599 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003600 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003602 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
3605/*
3606 * Handle an "Out of the blue" SHUTDOWN ACK.
3607 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003608 * Section: 8.4 5, sctpimpguide 2.41.
3609 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003611 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3612 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3613 * packet must fill in the Verification Tag field of the outbound
3614 * packet with the Verification Tag received in the SHUTDOWN ACK and
3615 * set the T-bit in the Chunk Flags to indicate that the Verification
3616 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 *
3618 * Inputs
3619 * (endpoint, asoc, type, arg, commands)
3620 *
3621 * Outputs
Xin Long172a1592017-08-11 10:23:57 +08003622 * (enum sctp_disposition)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 *
3624 * The return value is the disposition of the chunk.
3625 */
Xin Long172a1592017-08-11 10:23:57 +08003626static enum sctp_disposition sctp_sf_shut_8_4_5(
3627 struct net *net,
3628 const struct sctp_endpoint *ep,
3629 const struct sctp_association *asoc,
3630 const union sctp_subtype type,
3631 void *arg,
3632 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
3634 struct sctp_packet *packet = NULL;
3635 struct sctp_chunk *chunk = arg;
3636 struct sctp_chunk *shut;
3637
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003638 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003639 if (!packet)
3640 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003642 /* Make an SHUTDOWN_COMPLETE.
3643 * The T bit will be set if the asoc is NULL.
3644 */
3645 shut = sctp_make_shutdown_complete(asoc, chunk);
3646 if (!shut) {
3647 sctp_ootb_pkt_free(packet);
3648 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 }
3650
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003651 /* Reflect vtag if T-Bit is set */
3652 if (sctp_test_T_bit(shut))
3653 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3654
3655 /* Set the skb to the belonging sock for accounting. */
3656 shut->skb->sk = ep->base.sk;
3657
3658 sctp_packet_append_chunk(packet, shut);
3659
3660 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3661 SCTP_PACKET(packet));
3662
3663 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3664
3665 /* If the chunk length is invalid, we don't want to process
3666 * the reset of the packet.
3667 */
Xin Long922dbc52017-06-30 11:52:13 +08003668 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003669 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3670
3671 /* We need to discard the rest of the packet to prevent
3672 * potential bomming attacks from additional bundled chunks.
3673 * This is documented in SCTP Threats ID.
3674 */
3675 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
3678/*
3679 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3680 *
3681 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3682 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3683 * procedures in section 8.4 SHOULD be followed, in other words it
3684 * should be treated as an Out Of The Blue packet.
3685 * [This means that we do NOT check the Verification Tag on these
3686 * chunks. --piggy ]
3687 *
3688 */
Xin Long172a1592017-08-11 10:23:57 +08003689enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3690 const struct sctp_endpoint *ep,
3691 const struct sctp_association *asoc,
3692 const union sctp_subtype type,
3693 void *arg,
3694 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003696 struct sctp_chunk *chunk = arg;
3697
3698 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08003699 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003700 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003701 commands);
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 /* Although we do have an association in this case, it corresponds
3704 * to a restarted association. So the packet is treated as an OOTB
3705 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3706 * called with a NULL association.
3707 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003708 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003709
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003710 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
3713/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Xin Long172a1592017-08-11 10:23:57 +08003714enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3715 const struct sctp_endpoint *ep,
3716 const struct sctp_association *asoc,
3717 const union sctp_subtype type,
3718 void *arg,
3719 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720{
Xin Long172a1592017-08-11 10:23:57 +08003721 struct sctp_paramhdr *err_param = NULL;
3722 struct sctp_chunk *asconf_ack = NULL;
3723 struct sctp_chunk *chunk = arg;
3724 struct sctp_addiphdr *hdr;
3725 __u32 serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726
3727 if (!sctp_vtag_verify(chunk, asoc)) {
3728 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3729 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003730 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 }
3732
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003733 /* ADD-IP: Section 4.1.1
3734 * This chunk MUST be sent in an authenticated way by using
3735 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3736 * is received unauthenticated it MUST be silently discarded as
3737 * described in [I-D.ietf-tsvwg-sctp-auth].
3738 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003739 if (!net->sctp.addip_noauth && !chunk->auth)
Xin Long68d75462017-08-03 15:42:20 +08003740 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3741 commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
Xin Long68d75462017-08-03 15:42:20 +08003744 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003745 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 commands);
3747
Xin Long65205cc2017-08-03 15:42:19 +08003748 hdr = (struct sctp_addiphdr *)chunk->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 serial = ntohl(hdr->serial);
3750
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003751 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003752 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003753 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003754 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003755
Vlad Yasevicha08de642007-12-20 14:11:47 -08003756 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003758 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 */
3760 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003761 /* If this is the first instance of ASCONF in the packet,
3762 * we can clean our old ASCONF-ACKs.
3763 */
3764 if (!chunk->has_asconf)
3765 sctp_assoc_clean_asconf_ack_cache(asoc);
3766
3767 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3768 * expected, process the ASCONF as described below and after
3769 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3770 * the response packet and cache a copy of it (in the event it
3771 * later needs to be retransmitted).
3772 *
3773 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 */
3775 asconf_ack = sctp_process_asconf((struct sctp_association *)
3776 asoc, chunk);
3777 if (!asconf_ack)
3778 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003779 } else if (serial < asoc->peer.addip_serial + 1) {
3780 /* ADDIP 5.2 E2)
3781 * If the value found in the Sequence Number is less than the
3782 * ('Peer- Sequence-Number' + 1), simply skip to the next
3783 * ASCONF, and include in the outbound response packet
3784 * any previously cached ASCONF-ACK response that was
3785 * sent and saved that matches the Sequence Number of the
3786 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3787 * Chunk exists. This will occur when an older ASCONF
3788 * arrives out of order. In such a case, the receiver
3789 * should skip the ASCONF Chunk and not include ASCONF-ACK
3790 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003792 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3793 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003795
3796 /* Reset the transport so that we select the correct one
3797 * this time around. This is to make sure that we don't
3798 * accidentally use a stale transport that's been removed.
3799 */
3800 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003802 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003804 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 return SCTP_DISPOSITION_DISCARD;
3806 }
3807
Vlad Yasevicha08de642007-12-20 14:11:47 -08003808 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3809 * containing the ASCONF-ACK Chunks MUST be the source address of
3810 * the SCTP packet that held the ASCONF Chunks.
3811 *
3812 * To do this properly, we'll set the destination address of the chunk
3813 * and at the transmit time, will try look up the transport to use.
3814 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003815 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003817 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003819 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003820 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003821 ((struct sctp_association *)asoc)->new_transport = NULL;
3822 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return SCTP_DISPOSITION_CONSUME;
3825}
3826
3827/*
3828 * ADDIP Section 4.3 General rules for address manipulation
3829 * When building TLV parameters for the ASCONF Chunk that will add or
3830 * delete IP addresses the D0 to D13 rules should be applied:
3831 */
Xin Long172a1592017-08-11 10:23:57 +08003832enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3833 const struct sctp_endpoint *ep,
3834 const struct sctp_association *asoc,
3835 const union sctp_subtype type,
3836 void *arg,
3837 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
Xin Long172a1592017-08-11 10:23:57 +08003839 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3840 struct sctp_paramhdr *err_param = NULL;
3841 struct sctp_chunk *asconf_ack = arg;
3842 struct sctp_addiphdr *addip_hdr;
3843 __u32 sent_serial, rcvd_serial;
3844 struct sctp_chunk *abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
3846 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3847 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3848 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003849 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003852 /* ADD-IP, Section 4.1.2:
3853 * This chunk MUST be sent in an authenticated way by using
3854 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3855 * is received unauthenticated it MUST be silently discarded as
3856 * described in [I-D.ietf-tsvwg-sctp-auth].
3857 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003858 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Xin Long68d75462017-08-03 15:42:20 +08003859 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3860 commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003861
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 /* Make sure that the ADDIP chunk has a valid length. */
Xin Long68d75462017-08-03 15:42:20 +08003863 if (!sctp_chunk_length_valid(asconf_ack,
3864 sizeof(struct sctp_addip_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003865 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 commands);
3867
Xin Long65205cc2017-08-03 15:42:19 +08003868 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 rcvd_serial = ntohl(addip_hdr->serial);
3870
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003871 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003872 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003873 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003874 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 if (last_asconf) {
Xin Long65205cc2017-08-03 15:42:19 +08003877 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 sent_serial = ntohl(addip_hdr->serial);
3879 } else {
3880 sent_serial = asoc->addip_serial - 1;
3881 }
3882
3883 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3884 * equal to the next serial number to be used but no ASCONF chunk is
3885 * outstanding the endpoint MUST ABORT the association. Note that a
3886 * sequence number is greater than if it is no more than 2^^31-1
3887 * larger than the current sequence number (using serial arithmetic).
3888 */
3889 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3890 !(asoc->addip_last_asconf)) {
3891 abort = sctp_make_abort(asoc, asconf_ack,
Xin Longd8238d92017-08-03 15:42:11 +08003892 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003894 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3896 SCTP_CHUNK(abort));
3897 }
3898 /* We are going to ABORT, so we might as well stop
3899 * processing the rest of the chunks in the packet.
3900 */
3901 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3902 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003903 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003904 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003905 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003907 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003908 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3909 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return SCTP_DISPOSITION_ABORT;
3911 }
3912
3913 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3914 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3915 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3916
3917 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003918 asconf_ack)) {
3919 /* Successfully processed ASCONF_ACK. We can
3920 * release the next asconf if we have one.
3921 */
3922 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3923 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 abort = sctp_make_abort(asoc, asconf_ack,
Xin Longd8238d92017-08-03 15:42:11 +08003928 sizeof(struct sctp_errhdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003930 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3932 SCTP_CHUNK(abort));
3933 }
3934 /* We are going to ABORT, so we might as well stop
3935 * processing the rest of the chunks in the packet.
3936 */
wangweidongcb3f8372013-12-23 12:16:50 +08003937 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003938 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003939 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003941 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003942 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3943 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 return SCTP_DISPOSITION_ABORT;
3945 }
3946
3947 return SCTP_DISPOSITION_DISCARD;
3948}
3949
Xin Long2040d3d2017-02-17 12:45:42 +08003950/* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
Xin Long172a1592017-08-11 10:23:57 +08003951enum sctp_disposition sctp_sf_do_reconf(struct net *net,
3952 const struct sctp_endpoint *ep,
3953 const struct sctp_association *asoc,
3954 const union sctp_subtype type,
3955 void *arg,
3956 struct sctp_cmd_seq *commands)
Xin Long2040d3d2017-02-17 12:45:42 +08003957{
3958 struct sctp_paramhdr *err_param = NULL;
3959 struct sctp_chunk *chunk = arg;
3960 struct sctp_reconf_chunk *hdr;
3961 union sctp_params param;
3962
3963 if (!sctp_vtag_verify(chunk, asoc)) {
3964 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3965 SCTP_NULL());
3966 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3967 }
3968
3969 /* Make sure that the RECONF chunk has a valid length. */
3970 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3971 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3972 commands);
3973
3974 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3975 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3976 (void *)err_param, commands);
3977
3978 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3979 sctp_walk_params(param, hdr, params) {
3980 struct sctp_chunk *reply = NULL;
3981 struct sctp_ulpevent *ev = NULL;
3982
3983 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3984 reply = sctp_process_strreset_outreq(
3985 (struct sctp_association *)asoc, param, &ev);
3986 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3987 reply = sctp_process_strreset_inreq(
3988 (struct sctp_association *)asoc, param, &ev);
Xin Long692787c2017-03-10 12:11:07 +08003989 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3990 reply = sctp_process_strreset_tsnreq(
3991 (struct sctp_association *)asoc, param, &ev);
Xin Long50a41592017-03-10 12:11:09 +08003992 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3993 reply = sctp_process_strreset_addstrm_out(
3994 (struct sctp_association *)asoc, param, &ev);
Xin Longc5c4ebb2017-03-10 12:11:10 +08003995 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3996 reply = sctp_process_strreset_addstrm_in(
3997 (struct sctp_association *)asoc, param, &ev);
Xin Long11ae76e62017-03-10 12:11:11 +08003998 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3999 reply = sctp_process_strreset_resp(
4000 (struct sctp_association *)asoc, param, &ev);
Xin Long2040d3d2017-02-17 12:45:42 +08004001
4002 if (ev)
4003 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4004 SCTP_ULPEVENT(ev));
4005
4006 if (reply)
4007 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4008 SCTP_CHUNK(reply));
4009 }
4010
4011 return SCTP_DISPOSITION_CONSUME;
4012}
4013
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014/*
4015 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4016 *
4017 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4018 * its cumulative TSN point to the value carried in the FORWARD TSN
4019 * chunk, and then MUST further advance its cumulative TSN point locally
4020 * if possible.
4021 * After the above processing, the data receiver MUST stop reporting any
4022 * missing TSNs earlier than or equal to the new cumulative TSN point.
4023 *
4024 * Verification Tag: 8.5 Verification Tag [Normal verification]
4025 *
4026 * The return value is the disposition of the chunk.
4027 */
Xin Long172a1592017-08-11 10:23:57 +08004028enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4029 const struct sctp_endpoint *ep,
4030 const struct sctp_association *asoc,
4031 const union sctp_subtype type,
4032 void *arg,
4033 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Xin Long172a1592017-08-11 10:23:57 +08004036 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 __u16 len;
4038 __u32 tsn;
4039
4040 if (!sctp_vtag_verify(chunk, asoc)) {
4041 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4042 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004043 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 }
4045
Xin Longd15c9ed2017-02-03 17:37:06 +08004046 if (!asoc->peer.prsctp_capable)
4047 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4048
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 /* Make sure that the FORWARD_TSN chunk has valid length. */
Xin Long0fc2ea92017-12-15 00:41:27 +08004050 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004051 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 commands);
4053
4054 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4055 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4056 len = ntohs(chunk->chunk_hdr->length);
4057 len -= sizeof(struct sctp_chunkhdr);
4058 skb_pull(chunk->skb, len);
4059
4060 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004061 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
4063 /* The TSN is too high--silently discard the chunk and count on it
4064 * getting retransmitted later.
4065 */
4066 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4067 goto discard_noforce;
4068
Xin Long0fc2ea92017-12-15 00:41:27 +08004069 if (!asoc->stream.si->validate_ftsn(chunk))
4070 goto discard_noforce;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
Xin Long0fc2ea92017-12-15 00:41:27 +08004073 if (len > sctp_ftsnhdr_len(&asoc->stream))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004074 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05004078 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4080 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4081 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004082
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004084 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 */
4086 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
4088 return SCTP_DISPOSITION_CONSUME;
4089
4090discard_noforce:
4091 return SCTP_DISPOSITION_DISCARD;
4092}
4093
Xin Long172a1592017-08-11 10:23:57 +08004094enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4095 struct net *net,
4096 const struct sctp_endpoint *ep,
4097 const struct sctp_association *asoc,
4098 const union sctp_subtype type,
4099 void *arg,
4100 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Xin Long172a1592017-08-11 10:23:57 +08004103 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 __u16 len;
4105 __u32 tsn;
4106
4107 if (!sctp_vtag_verify(chunk, asoc)) {
4108 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4109 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004110 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 }
4112
Xin Longd15c9ed2017-02-03 17:37:06 +08004113 if (!asoc->peer.prsctp_capable)
4114 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 /* Make sure that the FORWARD_TSN chunk has a valid length. */
Xin Long0fc2ea92017-12-15 00:41:27 +08004117 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004118 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 commands);
4120
4121 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4122 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4123 len = ntohs(chunk->chunk_hdr->length);
4124 len -= sizeof(struct sctp_chunkhdr);
4125 skb_pull(chunk->skb, len);
4126
4127 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004128 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
4130 /* The TSN is too high--silently discard the chunk and count on it
4131 * getting retransmitted later.
4132 */
4133 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4134 goto gen_shutdown;
4135
Xin Long0fc2ea92017-12-15 00:41:27 +08004136 if (!asoc->stream.si->validate_ftsn(chunk))
4137 goto gen_shutdown;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
Xin Long0fc2ea92017-12-15 00:41:27 +08004140 if (len > sctp_ftsnhdr_len(&asoc->stream))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004141 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004143
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 /* Go a head and force a SACK, since we are shutting down. */
4145gen_shutdown:
4146 /* Implementor's Guide.
4147 *
4148 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4149 * respond to each received packet containing one or more DATA chunk(s)
4150 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4151 */
4152 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4153 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4154 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4155 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4156
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004157 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
4160/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004161 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004162 *
4163 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4164 * Identifier field. If this algorithm was not specified by the
4165 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4166 * during association setup, the AUTH chunk and all chunks after it MUST
4167 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4168 * defined in Section 4.1.
4169 *
4170 * If an endpoint with no shared key receives a Shared Key Identifier
4171 * other than 0, it MUST silently discard all authenticated chunks. If
4172 * the endpoint has at least one endpoint pair shared key for the peer,
4173 * it MUST use the key specified by the Shared Key Identifier if a
4174 * key has been configured for that Shared Key Identifier. If no
4175 * endpoint pair shared key has been configured for that Shared Key
4176 * Identifier, all authenticated chunks MUST be silently discarded.
4177 *
4178 * Verification Tag: 8.5 Verification Tag [Normal verification]
4179 *
4180 * The return value is the disposition of the chunk.
4181 */
Xin Long4785c7a2017-08-05 19:59:58 +08004182static enum sctp_ierror sctp_sf_authenticate(
Xin Long172a1592017-08-11 10:23:57 +08004183 const struct sctp_association *asoc,
Xin Long172a1592017-08-11 10:23:57 +08004184 struct sctp_chunk *chunk)
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004185{
Xin Long1b1e0bc2018-03-14 19:05:30 +08004186 struct sctp_shared_key *sh_key = NULL;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004187 struct sctp_authhdr *auth_hdr;
Xin Long172a1592017-08-11 10:23:57 +08004188 __u8 *save_digest, *digest;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004189 struct sctp_hmac *hmac;
4190 unsigned int sig_len;
4191 __u16 key_id;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004192
4193 /* Pull in the auth header, so we can do some more verification */
4194 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4195 chunk->subh.auth_hdr = auth_hdr;
Xin Longbb96dec2017-08-03 15:42:22 +08004196 skb_pull(chunk->skb, sizeof(*auth_hdr));
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004197
Masanari Iida02582e92012-08-22 19:11:26 +09004198 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004199 * chunk.
4200 */
4201 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4202 return SCTP_IERROR_AUTH_BAD_HMAC;
4203
4204 /* Make sure that the provided shared key identifier has been
4205 * configured
4206 */
4207 key_id = ntohs(auth_hdr->shkey_id);
Xin Long1b1e0bc2018-03-14 19:05:30 +08004208 if (key_id != asoc->active_key_id) {
4209 sh_key = sctp_auth_get_shkey(asoc, key_id);
4210 if (!sh_key)
4211 return SCTP_IERROR_AUTH_BAD_KEYID;
4212 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004213
4214 /* Make sure that the length of the signature matches what
4215 * we expect.
4216 */
Xin Longbb96dec2017-08-03 15:42:22 +08004217 sig_len = ntohs(chunk->chunk_hdr->length) -
4218 sizeof(struct sctp_auth_chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004219 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4220 if (sig_len != hmac->hmac_len)
4221 return SCTP_IERROR_PROTO_VIOLATION;
4222
4223 /* Now that we've done validation checks, we can compute and
4224 * verify the hmac. The steps involved are:
4225 * 1. Save the digest from the chunk.
4226 * 2. Zero out the digest in the chunk.
4227 * 3. Compute the new digest
4228 * 4. Compare saved and new digests.
4229 */
4230 digest = auth_hdr->hmac;
4231 skb_pull(chunk->skb, sig_len);
4232
4233 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4234 if (!save_digest)
4235 goto nomem;
4236
4237 memset(digest, 0, sig_len);
4238
4239 sctp_auth_calculate_hmac(asoc, chunk->skb,
Xin Longbb96dec2017-08-03 15:42:22 +08004240 (struct sctp_auth_chunk *)chunk->chunk_hdr,
Xin Long1b1e0bc2018-03-14 19:05:30 +08004241 sh_key, GFP_ATOMIC);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004242
4243 /* Discard the packet if the digests do not match */
4244 if (memcmp(save_digest, digest, sig_len)) {
4245 kfree(save_digest);
4246 return SCTP_IERROR_BAD_SIG;
4247 }
4248
4249 kfree(save_digest);
4250 chunk->auth = 1;
4251
4252 return SCTP_IERROR_NO_ERROR;
4253nomem:
4254 return SCTP_IERROR_NOMEM;
4255}
4256
Xin Long172a1592017-08-11 10:23:57 +08004257enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4258 const struct sctp_endpoint *ep,
4259 const struct sctp_association *asoc,
4260 const union sctp_subtype type,
4261 void *arg, struct sctp_cmd_seq *commands)
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004262{
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004263 struct sctp_chunk *chunk = arg;
Xin Long4785c7a2017-08-05 19:59:58 +08004264 struct sctp_authhdr *auth_hdr;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004265 struct sctp_chunk *err_chunk;
Xin Long4785c7a2017-08-05 19:59:58 +08004266 enum sctp_ierror error;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004267
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004268 /* Make sure that the peer has AUTH capable */
4269 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004270 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004271
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004272 if (!sctp_vtag_verify(chunk, asoc)) {
4273 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4274 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004275 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004276 }
4277
4278 /* Make sure that the AUTH chunk has valid length. */
4279 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004280 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004281 commands);
4282
4283 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Xin Long59d8d442018-05-05 14:59:47 +08004284 error = sctp_sf_authenticate(asoc, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004285 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004286 case SCTP_IERROR_AUTH_BAD_HMAC:
4287 /* Generate the ERROR chunk and discard the rest
4288 * of the packet
4289 */
4290 err_chunk = sctp_make_op_error(asoc, chunk,
4291 SCTP_ERROR_UNSUP_HMAC,
4292 &auth_hdr->hmac_id,
4293 sizeof(__u16), 0);
4294 if (err_chunk) {
4295 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4296 SCTP_CHUNK(err_chunk));
4297 }
4298 /* Fall Through */
4299 case SCTP_IERROR_AUTH_BAD_KEYID:
4300 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004301 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004302
4303 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004304 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004305 commands);
4306
4307 case SCTP_IERROR_NOMEM:
4308 return SCTP_DISPOSITION_NOMEM;
4309
4310 default: /* Prevent gcc warnings */
4311 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004312 }
4313
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004314 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4315 struct sctp_ulpevent *ev;
4316
4317 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
Xin Longec2e5062018-03-14 19:05:33 +08004318 SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004319
4320 if (!ev)
4321 return -ENOMEM;
4322
4323 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4324 SCTP_ULPEVENT(ev));
4325 }
4326
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004327 return SCTP_DISPOSITION_CONSUME;
4328}
4329
4330/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 * Process an unknown chunk.
4332 *
4333 * Section: 3.2. Also, 2.1 in the implementor's guide.
4334 *
4335 * Chunk Types are encoded such that the highest-order two bits specify
4336 * the action that must be taken if the processing endpoint does not
4337 * recognize the Chunk Type.
4338 *
4339 * 00 - Stop processing this SCTP packet and discard it, do not process
4340 * any further chunks within it.
4341 *
4342 * 01 - Stop processing this SCTP packet and discard it, do not process
4343 * any further chunks within it, and report the unrecognized
4344 * chunk in an 'Unrecognized Chunk Type'.
4345 *
4346 * 10 - Skip this chunk and continue processing.
4347 *
4348 * 11 - Skip this chunk and continue processing, but report in an ERROR
4349 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4350 *
4351 * The return value is the disposition of the chunk.
4352 */
Xin Long172a1592017-08-11 10:23:57 +08004353enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4354 const struct sctp_endpoint *ep,
4355 const struct sctp_association *asoc,
4356 const union sctp_subtype type,
4357 void *arg,
4358 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359{
4360 struct sctp_chunk *unk_chunk = arg;
4361 struct sctp_chunk *err_chunk;
Xin Long922dbc52017-06-30 11:52:13 +08004362 struct sctp_chunkhdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Daniel Borkmannbb333812013-06-28 19:49:40 +02004364 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
4366 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004367 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 /* Make sure that the chunk has a valid length.
4370 * Since we don't know the chunk type, we use a general
4371 * chunkhdr structure to make a comparison.
4372 */
Xin Long922dbc52017-06-30 11:52:13 +08004373 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004374 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 commands);
4376
4377 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4378 case SCTP_CID_ACTION_DISCARD:
4379 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004380 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 /* Generate an ERROR chunk as response. */
4383 hdr = unk_chunk->chunk_hdr;
4384 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4385 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004386 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004387 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 if (err_chunk) {
4389 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4390 SCTP_CHUNK(err_chunk));
4391 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004392
4393 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004394 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 case SCTP_CID_ACTION_SKIP:
4397 /* Skip the chunk. */
4398 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 case SCTP_CID_ACTION_SKIP_ERR:
4400 /* Generate an ERROR chunk as response. */
4401 hdr = unk_chunk->chunk_hdr;
4402 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4403 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004404 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004405 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 if (err_chunk) {
4407 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4408 SCTP_CHUNK(err_chunk));
4409 }
4410 /* Skip the chunk. */
4411 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 default:
4413 break;
4414 }
4415
4416 return SCTP_DISPOSITION_DISCARD;
4417}
4418
4419/*
4420 * Discard the chunk.
4421 *
4422 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4423 * [Too numerous to mention...]
4424 * Verification Tag: No verification needed.
4425 * Inputs
4426 * (endpoint, asoc, chunk)
4427 *
4428 * Outputs
4429 * (asoc, reply_msg, msg_up, timers, counters)
4430 *
4431 * The return value is the disposition of the chunk.
4432 */
Xin Long172a1592017-08-11 10:23:57 +08004433enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4434 const struct sctp_endpoint *ep,
4435 const struct sctp_association *asoc,
4436 const union sctp_subtype type,
4437 void *arg,
4438 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004440 struct sctp_chunk *chunk = arg;
4441
4442 /* Make sure that the chunk has a valid length.
4443 * Since we don't know the chunk type, we use a general
4444 * chunkhdr structure to make a comparison.
4445 */
Xin Long922dbc52017-06-30 11:52:13 +08004446 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004447 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004448 commands);
4449
Daniel Borkmannbb333812013-06-28 19:49:40 +02004450 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 return SCTP_DISPOSITION_DISCARD;
4453}
4454
4455/*
4456 * Discard the whole packet.
4457 *
4458 * Section: 8.4 2)
4459 *
4460 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4461 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 *
4463 * Verification Tag: No verification necessary
4464 *
4465 * Inputs
4466 * (endpoint, asoc, chunk)
4467 *
4468 * Outputs
4469 * (asoc, reply_msg, msg_up, timers, counters)
4470 *
4471 * The return value is the disposition of the chunk.
4472 */
Xin Long172a1592017-08-11 10:23:57 +08004473enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4474 const struct sctp_endpoint *ep,
4475 const struct sctp_association *asoc,
4476 const union sctp_subtype type,
4477 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004479 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4481
4482 return SCTP_DISPOSITION_CONSUME;
4483}
4484
4485
4486/*
4487 * The other end is violating protocol.
4488 *
4489 * Section: Not specified
4490 * Verification Tag: Not specified
4491 * Inputs
4492 * (endpoint, asoc, chunk)
4493 *
4494 * Outputs
4495 * (asoc, reply_msg, msg_up, timers, counters)
4496 *
4497 * We simply tag the chunk as a violation. The state machine will log
4498 * the violation and continue.
4499 */
Xin Long172a1592017-08-11 10:23:57 +08004500enum sctp_disposition sctp_sf_violation(struct net *net,
4501 const struct sctp_endpoint *ep,
4502 const struct sctp_association *asoc,
4503 const union sctp_subtype type,
4504 void *arg,
4505 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004507 struct sctp_chunk *chunk = arg;
4508
4509 /* Make sure that the chunk has a valid length. */
Xin Long922dbc52017-06-30 11:52:13 +08004510 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004511 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004512 commands);
4513
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return SCTP_DISPOSITION_VIOLATION;
4515}
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004518 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 */
Xin Long172a1592017-08-11 10:23:57 +08004520static enum sctp_disposition sctp_sf_abort_violation(
4521 struct net *net,
4522 const struct sctp_endpoint *ep,
4523 const struct sctp_association *asoc,
4524 void *arg,
4525 struct sctp_cmd_seq *commands,
4526 const __u8 *payload,
4527 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004529 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 struct sctp_chunk *chunk = arg;
4531 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004533 /* SCTP-AUTH, Section 6.3:
4534 * It should be noted that if the receiver wants to tear
4535 * down an association in an authenticated way only, the
4536 * handling of malformed packets should not result in
4537 * tearing down the association.
4538 *
4539 * This means that if we only want to abort associations
4540 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004541 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004542 * was malformed.
4543 */
4544 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4545 goto discard;
4546
Jesper Juhl9abed242007-11-11 23:57:49 +01004547 /* Make the abort chunk. */
4548 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4549 if (!abort)
4550 goto nomem;
4551
Vlad Yasevichece25df2007-09-07 16:30:54 -04004552 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004553 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4554 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4555 !asoc->peer.i.init_tag) {
Xin Longcb1844c2017-07-23 09:34:26 +08004556 struct sctp_initack_chunk *initack;
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004557
Xin Longcb1844c2017-07-23 09:34:26 +08004558 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4559 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004560 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4561 else {
4562 unsigned int inittag;
4563
4564 inittag = ntohl(initack->init_hdr.init_tag);
4565 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4566 SCTP_U32(inittag));
4567 }
4568 }
4569
Vlad Yasevichece25df2007-09-07 16:30:54 -04004570 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004571 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Vlad Yasevichece25df2007-09-07 16:30:54 -04004573 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4574 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4575 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4576 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4577 SCTP_ERROR(ECONNREFUSED));
4578 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4579 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4580 } else {
4581 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4582 SCTP_ERROR(ECONNABORTED));
4583 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4584 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004585 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004588 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004589
4590 if (!packet)
4591 goto nomem_pkt;
4592
4593 if (sctp_test_T_bit(abort))
4594 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4595
4596 abort->skb->sk = ep->base.sk;
4597
4598 sctp_packet_append_chunk(packet, abort);
4599
4600 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4601 SCTP_PACKET(packet));
4602
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004603 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 }
4605
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004606 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004607
Vlad Yasevich56eb82bb2008-10-09 14:33:01 -07004608discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004609 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 return SCTP_DISPOSITION_ABORT;
4611
Vlad Yasevichece25df2007-09-07 16:30:54 -04004612nomem_pkt:
4613 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614nomem:
4615 return SCTP_DISPOSITION_NOMEM;
4616}
4617
Wei Yongjunaecedea2007-08-02 16:57:44 +08004618/*
4619 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004620 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004621 * given chunk can be. For example, a SACK chunk has invalid length
Xin Longd4d6c612017-07-23 09:34:33 +08004622 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004623 *
4624 * We inform the other end by sending an ABORT with a Protocol Violation
4625 * error code.
4626 *
4627 * Section: Not specified
4628 * Verification Tag: Nothing to do
4629 * Inputs
4630 * (endpoint, asoc, chunk)
4631 *
4632 * Outputs
4633 * (reply_msg, msg_up, counters)
4634 *
4635 * Generate an ABORT chunk and terminate the association.
4636 */
Xin Long172a1592017-08-11 10:23:57 +08004637static enum sctp_disposition sctp_sf_violation_chunklen(
4638 struct net *net,
4639 const struct sctp_endpoint *ep,
4640 const struct sctp_association *asoc,
4641 const union sctp_subtype type,
4642 void *arg,
4643 struct sctp_cmd_seq *commands)
Wei Yongjunaecedea2007-08-02 16:57:44 +08004644{
wangweidongcb3f8372013-12-23 12:16:50 +08004645 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004646
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004647 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Xin Long172a1592017-08-11 10:23:57 +08004648 sizeof(err_str));
Wei Yongjunaecedea2007-08-02 16:57:44 +08004649}
4650
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004651/*
4652 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004653 * If the length is smaller than the minimum length of a given parameter,
4654 * or accumulated length in multi parameters exceeds the end of the chunk,
4655 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004656 */
Xin Long172a1592017-08-11 10:23:57 +08004657static enum sctp_disposition sctp_sf_violation_paramlen(
4658 struct net *net,
4659 const struct sctp_endpoint *ep,
4660 const struct sctp_association *asoc,
4661 const union sctp_subtype type,
4662 void *arg, void *ext,
4663 struct sctp_cmd_seq *commands)
Wei Yongjunba016672008-09-30 05:32:24 -07004664{
Wei Yongjunba016672008-09-30 05:32:24 -07004665 struct sctp_paramhdr *param = ext;
4666 struct sctp_chunk *abort = NULL;
Xin Long172a1592017-08-11 10:23:57 +08004667 struct sctp_chunk *chunk = arg;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004668
Wei Yongjunba016672008-09-30 05:32:24 -07004669 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4670 goto discard;
4671
4672 /* Make the abort chunk. */
4673 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4674 if (!abort)
4675 goto nomem;
4676
4677 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004678 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004679
4680 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4681 SCTP_ERROR(ECONNABORTED));
4682 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4683 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004684 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4685 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004686
4687discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004688 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004689 return SCTP_DISPOSITION_ABORT;
4690nomem:
4691 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004692}
4693
Wei Yongjunaecedea2007-08-02 16:57:44 +08004694/* Handle a protocol violation when the peer trying to advance the
4695 * cumulative tsn ack to a point beyond the max tsn currently sent.
4696 *
4697 * We inform the other end by sending an ABORT with a Protocol Violation
4698 * error code.
4699 */
Xin Long172a1592017-08-11 10:23:57 +08004700static enum sctp_disposition sctp_sf_violation_ctsn(
4701 struct net *net,
4702 const struct sctp_endpoint *ep,
4703 const struct sctp_association *asoc,
4704 const union sctp_subtype type,
4705 void *arg,
4706 struct sctp_cmd_seq *commands)
Wei Yongjunaecedea2007-08-02 16:57:44 +08004707{
wangweidongcb3f8372013-12-23 12:16:50 +08004708 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004709
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004710 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Xin Long172a1592017-08-11 10:23:57 +08004711 sizeof(err_str));
Wei Yongjunaecedea2007-08-02 16:57:44 +08004712}
4713
Vlad Yasevichece25df2007-09-07 16:30:54 -04004714/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004715 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004716 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004717 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004718 * on the path and we may not want to continue this communication.
4719 */
Xin Long172a1592017-08-11 10:23:57 +08004720static enum sctp_disposition sctp_sf_violation_chunk(
4721 struct net *net,
4722 const struct sctp_endpoint *ep,
4723 const struct sctp_association *asoc,
4724 const union sctp_subtype type,
4725 void *arg,
4726 struct sctp_cmd_seq *commands)
Vlad Yasevichece25df2007-09-07 16:30:54 -04004727{
wangweidongcb3f8372013-12-23 12:16:50 +08004728 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004729
4730 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004731 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004732
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004733 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Xin Long172a1592017-08-11 10:23:57 +08004734 sizeof(err_str));
Vlad Yasevichece25df2007-09-07 16:30:54 -04004735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736/***************************************************************************
4737 * These are the state functions for handling primitive (Section 10) events.
4738 ***************************************************************************/
4739/*
4740 * sctp_sf_do_prm_asoc
4741 *
4742 * Section: 10.1 ULP-to-SCTP
4743 * B) Associate
4744 *
4745 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4746 * outbound stream count)
4747 * -> association id [,destination transport addr list] [,outbound stream
4748 * count]
4749 *
4750 * This primitive allows the upper layer to initiate an association to a
4751 * specific peer endpoint.
4752 *
4753 * The peer endpoint shall be specified by one of the transport addresses
4754 * which defines the endpoint (see Section 1.4). If the local SCTP
4755 * instance has not been initialized, the ASSOCIATE is considered an
4756 * error.
4757 * [This is not relevant for the kernel implementation since we do all
4758 * initialization at boot time. It we hadn't initialized we wouldn't
4759 * get anywhere near this code.]
4760 *
4761 * An association id, which is a local handle to the SCTP association,
4762 * will be returned on successful establishment of the association. If
4763 * SCTP is not able to open an SCTP association with the peer endpoint,
4764 * an error is returned.
4765 * [In the kernel implementation, the struct sctp_association needs to
4766 * be created BEFORE causing this primitive to run.]
4767 *
4768 * Other association parameters may be returned, including the
4769 * complete destination transport addresses of the peer as well as the
4770 * outbound stream count of the local endpoint. One of the transport
4771 * address from the returned destination addresses will be selected by
4772 * the local endpoint as default primary path for sending SCTP packets
4773 * to this peer. The returned "destination transport addr list" can
4774 * be used by the ULP to change the default primary path or to force
4775 * sending a packet to a specific transport address. [All of this
4776 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4777 * function.]
4778 *
4779 * Mandatory attributes:
4780 *
4781 * o local SCTP instance name - obtained from the INITIALIZE operation.
4782 * [This is the argument asoc.]
4783 * o destination transport addr - specified as one of the transport
4784 * addresses of the peer endpoint with which the association is to be
4785 * established.
4786 * [This is asoc->peer.active_path.]
4787 * o outbound stream count - the number of outbound streams the ULP
4788 * would like to open towards this peer endpoint.
4789 * [BUG: This is not currently implemented.]
4790 * Optional attributes:
4791 *
4792 * None.
4793 *
4794 * The return value is a disposition.
4795 */
Xin Long172a1592017-08-11 10:23:57 +08004796enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4797 const struct sctp_endpoint *ep,
4798 const struct sctp_association *asoc,
4799 const union sctp_subtype type,
4800 void *arg,
4801 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802{
wangweidong26ac8e52013-12-23 12:16:51 +08004803 struct sctp_association *my_asoc;
Xin Long172a1592017-08-11 10:23:57 +08004804 struct sctp_chunk *repl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805
4806 /* The comment below says that we enter COOKIE-WAIT AFTER
4807 * sending the INIT, but that doesn't actually work in our
4808 * implementation...
4809 */
4810 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4811 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4812
4813 /* RFC 2960 5.1 Normal Establishment of an Association
4814 *
4815 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4816 * must provide its Verification Tag (Tag_A) in the Initiate
4817 * Tag field. Tag_A SHOULD be a random number in the range of
4818 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4819 */
4820
4821 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4822 if (!repl)
4823 goto nomem;
4824
Daniel Borkmann405426f2013-06-14 18:24:05 +02004825 /* Choose transport for INIT. */
4826 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4827 SCTP_CHUNK(repl));
4828
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 /* Cast away the const modifier, as we want to just
4830 * rerun it through as a sideffect.
4831 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004832 my_asoc = (struct sctp_association *)asoc;
4833 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
4835 /* After sending the INIT, "A" starts the T1-init timer and
4836 * enters the COOKIE-WAIT state.
4837 */
4838 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4839 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4840 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4841 return SCTP_DISPOSITION_CONSUME;
4842
4843nomem:
4844 return SCTP_DISPOSITION_NOMEM;
4845}
4846
4847/*
4848 * Process the SEND primitive.
4849 *
4850 * Section: 10.1 ULP-to-SCTP
4851 * E) Send
4852 *
4853 * Format: SEND(association id, buffer address, byte count [,context]
4854 * [,stream id] [,life time] [,destination transport address]
4855 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4856 * -> result
4857 *
4858 * This is the main method to send user data via SCTP.
4859 *
4860 * Mandatory attributes:
4861 *
4862 * o association id - local handle to the SCTP association
4863 *
4864 * o buffer address - the location where the user message to be
4865 * transmitted is stored;
4866 *
4867 * o byte count - The size of the user data in number of bytes;
4868 *
4869 * Optional attributes:
4870 *
4871 * o context - an optional 32 bit integer that will be carried in the
4872 * sending failure notification to the ULP if the transportation of
4873 * this User Message fails.
4874 *
4875 * o stream id - to indicate which stream to send the data on. If not
4876 * specified, stream 0 will be used.
4877 *
4878 * o life time - specifies the life time of the user data. The user data
4879 * will not be sent by SCTP after the life time expires. This
4880 * parameter can be used to avoid efforts to transmit stale
4881 * user messages. SCTP notifies the ULP if the data cannot be
4882 * initiated to transport (i.e. sent to the destination via SCTP's
4883 * send primitive) within the life time variable. However, the
4884 * user data will be transmitted if SCTP has attempted to transmit a
4885 * chunk before the life time expired.
4886 *
4887 * o destination transport address - specified as one of the destination
4888 * transport addresses of the peer endpoint to which this packet
4889 * should be sent. Whenever possible, SCTP should use this destination
4890 * transport address for sending the packets, instead of the current
4891 * primary path.
4892 *
4893 * o unorder flag - this flag, if present, indicates that the user
4894 * would like the data delivered in an unordered fashion to the peer
4895 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4896 * message).
4897 *
4898 * o no-bundle flag - instructs SCTP not to bundle this user data with
4899 * other outbound DATA chunks. SCTP MAY still bundle even when
4900 * this flag is present, when faced with network congestion.
4901 *
4902 * o payload protocol-id - A 32 bit unsigned integer that is to be
4903 * passed to the peer indicating the type of payload protocol data
4904 * being transmitted. This value is passed as opaque data by SCTP.
4905 *
4906 * The return value is the disposition.
4907 */
Xin Long172a1592017-08-11 10:23:57 +08004908enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4909 const struct sctp_endpoint *ep,
4910 const struct sctp_association *asoc,
4911 const union sctp_subtype type,
4912 void *arg,
4913 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004915 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004917 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 return SCTP_DISPOSITION_CONSUME;
4919}
4920
4921/*
4922 * Process the SHUTDOWN primitive.
4923 *
4924 * Section: 10.1:
4925 * C) Shutdown
4926 *
4927 * Format: SHUTDOWN(association id)
4928 * -> result
4929 *
4930 * Gracefully closes an association. Any locally queued user data
4931 * will be delivered to the peer. The association will be terminated only
4932 * after the peer acknowledges all the SCTP packets sent. A success code
4933 * will be returned on successful termination of the association. If
4934 * attempting to terminate the association results in a failure, an error
4935 * code shall be returned.
4936 *
4937 * Mandatory attributes:
4938 *
4939 * o association id - local handle to the SCTP association
4940 *
4941 * Optional attributes:
4942 *
4943 * None.
4944 *
4945 * The return value is the disposition.
4946 */
Xin Long172a1592017-08-11 10:23:57 +08004947enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
4948 struct net *net,
4949 const struct sctp_endpoint *ep,
4950 const struct sctp_association *asoc,
4951 const union sctp_subtype type,
4952 void *arg,
4953 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954{
Xin Long172a1592017-08-11 10:23:57 +08004955 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956
4957 /* From 9.2 Shutdown of an Association
4958 * Upon receipt of the SHUTDOWN primitive from its upper
4959 * layer, the endpoint enters SHUTDOWN-PENDING state and
4960 * remains there until all outstanding data has been
4961 * acknowledged by its peer. The endpoint accepts no new data
4962 * from its upper layer, but retransmits data to the far end
4963 * if necessary to fill gaps.
4964 */
4965 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4966 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4967
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 disposition = SCTP_DISPOSITION_CONSUME;
4969 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004970 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 arg, commands);
4972 }
Xin Long172a1592017-08-11 10:23:57 +08004973
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return disposition;
4975}
4976
4977/*
4978 * Process the ABORT primitive.
4979 *
4980 * Section: 10.1:
4981 * C) Abort
4982 *
4983 * Format: Abort(association id [, cause code])
4984 * -> result
4985 *
4986 * Ungracefully closes an association. Any locally queued user data
4987 * will be discarded and an ABORT chunk is sent to the peer. A success code
4988 * will be returned on successful abortion of the association. If
4989 * attempting to abort the association results in a failure, an error
4990 * code shall be returned.
4991 *
4992 * Mandatory attributes:
4993 *
4994 * o association id - local handle to the SCTP association
4995 *
4996 * Optional attributes:
4997 *
4998 * o cause code - reason of the abort to be passed to the peer
4999 *
5000 * None.
5001 *
5002 * The return value is the disposition.
5003 */
Xin Long172a1592017-08-11 10:23:57 +08005004enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5005 struct net *net,
5006 const struct sctp_endpoint *ep,
5007 const struct sctp_association *asoc,
5008 const union sctp_subtype type,
5009 void *arg,
5010 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011{
5012 /* From 9.1 Abort of an Association
5013 * Upon receipt of the ABORT primitive from its upper
5014 * layer, the endpoint enters CLOSED state and
5015 * discard all outstanding data has been
5016 * acknowledged by its peer. The endpoint accepts no new data
5017 * from its upper layer, but retransmits data to the far end
5018 * if necessary to fill gaps.
5019 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07005020 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021
Xin Long068d8bd2015-12-29 17:49:25 +08005022 if (abort)
5023 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
5025 /* Even if we can't send the ABORT due to low memory delete the
5026 * TCB. This is a departure from our typical NOMEM handling.
5027 */
5028
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005029 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5030 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 /* Delete the established association. */
5032 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005033 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005035 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5036 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005038 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039}
5040
5041/* We tried an illegal operation on an association which is closed. */
Xin Long172a1592017-08-11 10:23:57 +08005042enum sctp_disposition sctp_sf_error_closed(struct net *net,
5043 const struct sctp_endpoint *ep,
5044 const struct sctp_association *asoc,
5045 const union sctp_subtype type,
5046 void *arg,
5047 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048{
5049 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5050 return SCTP_DISPOSITION_CONSUME;
5051}
5052
5053/* We tried an illegal operation on an association which is shutting
5054 * down.
5055 */
Xin Long172a1592017-08-11 10:23:57 +08005056enum sctp_disposition sctp_sf_error_shutdown(
5057 struct net *net,
5058 const struct sctp_endpoint *ep,
5059 const struct sctp_association *asoc,
5060 const union sctp_subtype type,
5061 void *arg,
5062 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
5064 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5065 SCTP_ERROR(-ESHUTDOWN));
5066 return SCTP_DISPOSITION_CONSUME;
5067}
5068
5069/*
5070 * sctp_cookie_wait_prm_shutdown
5071 *
5072 * Section: 4 Note: 2
5073 * Verification Tag:
5074 * Inputs
5075 * (endpoint, asoc)
5076 *
5077 * The RFC does not explicitly address this issue, but is the route through the
5078 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5079 *
5080 * Outputs
5081 * (timers)
5082 */
Xin Long172a1592017-08-11 10:23:57 +08005083enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5084 struct net *net,
5085 const struct sctp_endpoint *ep,
5086 const struct sctp_association *asoc,
5087 const union sctp_subtype type,
5088 void *arg,
5089 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090{
5091 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5092 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5093
5094 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5095 SCTP_STATE(SCTP_STATE_CLOSED));
5096
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005097 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
5099 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5100
5101 return SCTP_DISPOSITION_DELETE_TCB;
5102}
5103
5104/*
5105 * sctp_cookie_echoed_prm_shutdown
5106 *
5107 * Section: 4 Note: 2
5108 * Verification Tag:
5109 * Inputs
5110 * (endpoint, asoc)
5111 *
5112 * The RFC does not explcitly address this issue, but is the route through the
5113 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5114 *
5115 * Outputs
5116 * (timers)
5117 */
Xin Long172a1592017-08-11 10:23:57 +08005118enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5119 struct net *net,
5120 const struct sctp_endpoint *ep,
5121 const struct sctp_association *asoc,
5122 const union sctp_subtype type,
5123 void *arg,
5124 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125{
5126 /* There is a single T1 timer, so we should be able to use
5127 * common function with the COOKIE-WAIT state.
5128 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005129 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
5132/*
5133 * sctp_sf_cookie_wait_prm_abort
5134 *
5135 * Section: 4 Note: 2
5136 * Verification Tag:
5137 * Inputs
5138 * (endpoint, asoc)
5139 *
5140 * The RFC does not explicitly address this issue, but is the route through the
5141 * state table when someone issues an abort while in COOKIE_WAIT state.
5142 *
5143 * Outputs
5144 * (timers)
5145 */
Xin Long172a1592017-08-11 10:23:57 +08005146enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5147 struct net *net,
5148 const struct sctp_endpoint *ep,
5149 const struct sctp_association *asoc,
5150 const union sctp_subtype type,
5151 void *arg,
5152 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07005154 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155
5156 /* Stop T1-init timer */
5157 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5158 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159
Xin Long068d8bd2015-12-29 17:49:25 +08005160 if (abort)
5161 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162
5163 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5164 SCTP_STATE(SCTP_STATE_CLOSED));
5165
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005166 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167
5168 /* Even if we can't send the ABORT due to low memory delete the
5169 * TCB. This is a departure from our typical NOMEM handling.
5170 */
5171
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005172 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5173 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 /* Delete the established association. */
5175 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005176 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005178 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
5180
5181/*
5182 * sctp_sf_cookie_echoed_prm_abort
5183 *
5184 * Section: 4 Note: 3
5185 * Verification Tag:
5186 * Inputs
5187 * (endpoint, asoc)
5188 *
5189 * The RFC does not explcitly address this issue, but is the route through the
5190 * state table when someone issues an abort while in COOKIE_ECHOED state.
5191 *
5192 * Outputs
5193 * (timers)
5194 */
Xin Long172a1592017-08-11 10:23:57 +08005195enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5196 struct net *net,
5197 const struct sctp_endpoint *ep,
5198 const struct sctp_association *asoc,
5199 const union sctp_subtype type,
5200 void *arg,
5201 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202{
5203 /* There is a single T1 timer, so we should be able to use
5204 * common function with the COOKIE-WAIT state.
5205 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005206 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207}
5208
5209/*
5210 * sctp_sf_shutdown_pending_prm_abort
5211 *
5212 * Inputs
5213 * (endpoint, asoc)
5214 *
5215 * The RFC does not explicitly address this issue, but is the route through the
5216 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5217 *
5218 * Outputs
5219 * (timers)
5220 */
Xin Long172a1592017-08-11 10:23:57 +08005221enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5222 struct net *net,
5223 const struct sctp_endpoint *ep,
5224 const struct sctp_association *asoc,
5225 const union sctp_subtype type,
5226 void *arg,
5227 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228{
5229 /* Stop the T5-shutdown guard timer. */
5230 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5231 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5232
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005233 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234}
5235
5236/*
5237 * sctp_sf_shutdown_sent_prm_abort
5238 *
5239 * Inputs
5240 * (endpoint, asoc)
5241 *
5242 * The RFC does not explicitly address this issue, but is the route through the
5243 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5244 *
5245 * Outputs
5246 * (timers)
5247 */
Xin Long172a1592017-08-11 10:23:57 +08005248enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5249 struct net *net,
5250 const struct sctp_endpoint *ep,
5251 const struct sctp_association *asoc,
5252 const union sctp_subtype type,
5253 void *arg,
5254 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255{
5256 /* Stop the T2-shutdown timer. */
5257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5258 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5259
5260 /* Stop the T5-shutdown guard timer. */
5261 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5262 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5263
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005264 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265}
5266
5267/*
5268 * sctp_sf_cookie_echoed_prm_abort
5269 *
5270 * Inputs
5271 * (endpoint, asoc)
5272 *
5273 * The RFC does not explcitly address this issue, but is the route through the
5274 * state table when someone issues an abort while in COOKIE_ECHOED state.
5275 *
5276 * Outputs
5277 * (timers)
5278 */
Xin Long172a1592017-08-11 10:23:57 +08005279enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5280 struct net *net,
5281 const struct sctp_endpoint *ep,
5282 const struct sctp_association *asoc,
5283 const union sctp_subtype type,
5284 void *arg,
5285 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286{
5287 /* The same T2 timer, so we should be able to use
5288 * common function with the SHUTDOWN-SENT state.
5289 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005290 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291}
5292
5293/*
5294 * Process the REQUESTHEARTBEAT primitive
5295 *
5296 * 10.1 ULP-to-SCTP
5297 * J) Request Heartbeat
5298 *
5299 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5300 *
5301 * -> result
5302 *
5303 * Instructs the local endpoint to perform a HeartBeat on the specified
5304 * destination transport address of the given association. The returned
5305 * result should indicate whether the transmission of the HEARTBEAT
5306 * chunk to the destination address is successful.
5307 *
5308 * Mandatory attributes:
5309 *
5310 * o association id - local handle to the SCTP association
5311 *
5312 * o destination transport address - the transport address of the
5313 * association on which a heartbeat should be issued.
5314 */
Xin Long172a1592017-08-11 10:23:57 +08005315enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005316 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 const struct sctp_endpoint *ep,
5318 const struct sctp_association *asoc,
Xin Longbfc6f822017-08-05 20:00:04 +08005319 const union sctp_subtype type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 void *arg,
Xin Longa85bbeb2017-08-11 10:23:52 +08005321 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005323 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5324 (struct sctp_transport *)arg, commands))
5325 return SCTP_DISPOSITION_NOMEM;
5326
5327 /*
5328 * RFC 2960 (bis), section 8.3
5329 *
5330 * D) Request an on-demand HEARTBEAT on a specific destination
5331 * transport address of a given association.
5332 *
5333 * The endpoint should increment the respective error counter of
5334 * the destination transport address each time a HEARTBEAT is sent
5335 * to that address and not acknowledged within one RTO.
5336 *
5337 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005338 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005339 SCTP_TRANSPORT(arg));
5340 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341}
5342
5343/*
5344 * ADDIP Section 4.1 ASCONF Chunk Procedures
5345 * When an endpoint has an ASCONF signaled change to be sent to the
5346 * remote endpoint it should do A1 to A9
5347 */
Xin Long172a1592017-08-11 10:23:57 +08005348enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5349 const struct sctp_endpoint *ep,
5350 const struct sctp_association *asoc,
5351 const union sctp_subtype type,
5352 void *arg,
5353 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354{
5355 struct sctp_chunk *chunk = arg;
5356
5357 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5358 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5359 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5360 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5361 return SCTP_DISPOSITION_CONSUME;
5362}
5363
Xin Long7a090b02017-01-18 00:44:44 +08005364/* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
Xin Long172a1592017-08-11 10:23:57 +08005365enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5366 const struct sctp_endpoint *ep,
5367 const struct sctp_association *asoc,
5368 const union sctp_subtype type,
5369 void *arg,
5370 struct sctp_cmd_seq *commands)
Xin Long7a090b02017-01-18 00:44:44 +08005371{
5372 struct sctp_chunk *chunk = arg;
5373
5374 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5375 return SCTP_DISPOSITION_CONSUME;
5376}
5377
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378/*
5379 * Ignore the primitive event
5380 *
5381 * The return value is the disposition of the primitive.
5382 */
Xin Long172a1592017-08-11 10:23:57 +08005383enum sctp_disposition sctp_sf_ignore_primitive(
5384 struct net *net,
5385 const struct sctp_endpoint *ep,
5386 const struct sctp_association *asoc,
5387 const union sctp_subtype type,
5388 void *arg,
5389 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005391 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5392 type.primitive);
5393
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 return SCTP_DISPOSITION_DISCARD;
5395}
5396
5397/***************************************************************************
5398 * These are the state functions for the OTHER events.
5399 ***************************************************************************/
5400
5401/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005402 * When the SCTP stack has no more user data to send or retransmit, this
5403 * notification is given to the user. Also, at the time when a user app
5404 * subscribes to this event, if there is no data to be sent or
5405 * retransmit, the stack will immediately send up this notification.
5406 */
Xin Long172a1592017-08-11 10:23:57 +08005407enum sctp_disposition sctp_sf_do_no_pending_tsn(
5408 struct net *net,
5409 const struct sctp_endpoint *ep,
5410 const struct sctp_association *asoc,
5411 const union sctp_subtype type,
5412 void *arg,
5413 struct sctp_cmd_seq *commands)
Wei Yongjune1cdd552011-04-17 17:29:03 +00005414{
5415 struct sctp_ulpevent *event;
5416
5417 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5418 if (!event)
5419 return SCTP_DISPOSITION_NOMEM;
5420
5421 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5422
5423 return SCTP_DISPOSITION_CONSUME;
5424}
5425
5426/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 * Start the shutdown negotiation.
5428 *
5429 * From Section 9.2:
5430 * Once all its outstanding data has been acknowledged, the endpoint
5431 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5432 * TSN Ack field the last sequential TSN it has received from the peer.
5433 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5434 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5435 * with the updated last sequential TSN received from its peer.
5436 *
5437 * The return value is the disposition.
5438 */
Xin Long172a1592017-08-11 10:23:57 +08005439enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5440 struct net *net,
5441 const struct sctp_endpoint *ep,
5442 const struct sctp_association *asoc,
5443 const union sctp_subtype type,
5444 void *arg,
5445 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446{
5447 struct sctp_chunk *reply;
5448
5449 /* Once all its outstanding data has been acknowledged, the
5450 * endpoint shall send a SHUTDOWN chunk to its peer including
5451 * in the Cumulative TSN Ack field the last sequential TSN it
5452 * has received from the peer.
5453 */
5454 reply = sctp_make_shutdown(asoc, NULL);
5455 if (!reply)
5456 goto nomem;
5457
5458 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5459 * T2-shutdown timer.
5460 */
5461 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5462
5463 /* It shall then start the T2-shutdown timer */
5464 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5465 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5466
Wei Yongjun536428a2008-09-05 08:55:26 +08005467 /* RFC 4960 Section 9.2
5468 * The sender of the SHUTDOWN MAY also start an overall guard timer
5469 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5470 */
Thomas Graff8d96052011-07-07 00:28:35 +00005471 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005472 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5473
Neil Horman9f70f462013-12-10 06:48:15 -05005474 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5476 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5477
5478 /* and enter the SHUTDOWN-SENT state. */
5479 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5480 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5481
5482 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5483 *
5484 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005485 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 */
5487 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5488
5489 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5490
5491 return SCTP_DISPOSITION_CONSUME;
5492
5493nomem:
5494 return SCTP_DISPOSITION_NOMEM;
5495}
5496
5497/*
5498 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5499 *
5500 * From Section 9.2:
5501 *
5502 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5503 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5504 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5505 * endpoint must re-send the SHUTDOWN ACK.
5506 *
5507 * The return value is the disposition.
5508 */
Xin Long172a1592017-08-11 10:23:57 +08005509enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5510 struct net *net,
5511 const struct sctp_endpoint *ep,
5512 const struct sctp_association *asoc,
5513 const union sctp_subtype type,
5514 void *arg,
5515 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516{
Xin Long172a1592017-08-11 10:23:57 +08005517 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 struct sctp_chunk *reply;
5519
5520 /* There are 2 ways of getting here:
5521 * 1) called in response to a SHUTDOWN chunk
5522 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5523 *
5524 * For the case (2), the arg parameter is set to NULL. We need
5525 * to check that we have a chunk before accessing it's fields.
5526 */
5527 if (chunk) {
5528 if (!sctp_vtag_verify(chunk, asoc))
Xin Longac23e682017-08-03 15:42:10 +08005529 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5530 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 /* Make sure that the SHUTDOWN chunk has a valid length. */
Xin Longac23e682017-08-03 15:42:10 +08005533 if (!sctp_chunk_length_valid(
5534 chunk, sizeof(struct sctp_shutdown_chunk)))
5535 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5536 arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 }
5538
5539 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5540 * shall send a SHUTDOWN ACK ...
5541 */
5542 reply = sctp_make_shutdown_ack(asoc, chunk);
5543 if (!reply)
5544 goto nomem;
5545
5546 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5547 * the T2-shutdown timer.
5548 */
5549 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5550
5551 /* and start/restart a T2-shutdown timer of its own, */
5552 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5553 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5554
Neil Horman9f70f462013-12-10 06:48:15 -05005555 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5557 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5558
5559 /* Enter the SHUTDOWN-ACK-SENT state. */
5560 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5561 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5562
5563 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5564 *
5565 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005566 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 */
5568 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5569
5570 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5571
5572 return SCTP_DISPOSITION_CONSUME;
5573
5574nomem:
5575 return SCTP_DISPOSITION_NOMEM;
5576}
5577
5578/*
5579 * Ignore the event defined as other
5580 *
5581 * The return value is the disposition of the event.
5582 */
Xin Long172a1592017-08-11 10:23:57 +08005583enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5584 const struct sctp_endpoint *ep,
5585 const struct sctp_association *asoc,
5586 const union sctp_subtype type,
5587 void *arg,
5588 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005590 pr_debug("%s: the event other type:%d is ignored\n",
5591 __func__, type.other);
5592
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 return SCTP_DISPOSITION_DISCARD;
5594}
5595
5596/************************************************************
5597 * These are the state functions for handling timeout events.
5598 ************************************************************/
5599
5600/*
5601 * RTX Timeout
5602 *
5603 * Section: 6.3.3 Handle T3-rtx Expiration
5604 *
5605 * Whenever the retransmission timer T3-rtx expires for a destination
5606 * address, do the following:
5607 * [See below]
5608 *
5609 * The return value is the disposition of the chunk.
5610 */
Xin Long172a1592017-08-11 10:23:57 +08005611enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5612 const struct sctp_endpoint *ep,
5613 const struct sctp_association *asoc,
5614 const union sctp_subtype type,
5615 void *arg,
5616 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617{
5618 struct sctp_transport *transport = arg;
5619
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005620 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005621
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005623 if (asoc->peer.zero_window_announced &&
5624 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005625 /*
5626 * We are here likely because the receiver had its rwnd
5627 * closed for a while and we have not been able to
5628 * transmit the locally queued data within the maximum
5629 * retransmission attempts limit. Start the T5
5630 * shutdown guard timer to give the receiver one last
5631 * chance and some additional time to recover before
5632 * aborting.
5633 */
5634 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5635 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5636 } else {
5637 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5638 SCTP_ERROR(ETIMEDOUT));
5639 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5640 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5641 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005642 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5643 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005644 return SCTP_DISPOSITION_DELETE_TCB;
5645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 }
5647
5648 /* E1) For the destination address for which the timer
5649 * expires, adjust its ssthresh with rules defined in Section
5650 * 7.2.3 and set the cwnd <- MTU.
5651 */
5652
5653 /* E2) For the destination address for which the timer
5654 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5655 * maximum value discussed in rule C7 above (RTO.max) may be
5656 * used to provide an upper bound to this doubling operation.
5657 */
5658
5659 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5660 * outstanding DATA chunks for the address for which the
5661 * T3-rtx has expired will fit into a single packet, subject
5662 * to the MTU constraint for the path corresponding to the
5663 * destination transport address to which the retransmission
5664 * is being sent (this may be different from the address for
5665 * which the timer expires [see Section 6.4]). Call this
5666 * value K. Bundle and retransmit those K DATA chunks in a
5667 * single packet to the destination endpoint.
5668 *
5669 * Note: Any DATA chunks that were sent to the address for
5670 * which the T3-rtx timer expired but did not fit in one MTU
5671 * (rule E3 above), should be marked for retransmission and
5672 * sent as soon as cwnd allows (normally when a SACK arrives).
5673 */
5674
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 /* Do some failure management (Section 8.2). */
5676 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5677
Vlad Yasevich1845a572007-02-21 02:06:19 -08005678 /* NB: Rules E4 and F1 are implicit in R1. */
5679 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 return SCTP_DISPOSITION_CONSUME;
5682}
5683
5684/*
5685 * Generate delayed SACK on timeout
5686 *
5687 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5688 *
5689 * The guidelines on delayed acknowledgement algorithm specified in
5690 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5691 * acknowledgement SHOULD be generated for at least every second packet
5692 * (not every second DATA chunk) received, and SHOULD be generated
5693 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5694 * some situations it may be beneficial for an SCTP transmitter to be
5695 * more conservative than the algorithms detailed in this document
5696 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5697 * the following algorithms allow.
5698 */
Xin Long172a1592017-08-11 10:23:57 +08005699enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5700 const struct sctp_endpoint *ep,
5701 const struct sctp_association *asoc,
5702 const union sctp_subtype type,
5703 void *arg,
5704 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005706 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5708 return SCTP_DISPOSITION_CONSUME;
5709}
5710
5711/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005712 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 *
5714 * Section: 4 Note: 2
5715 * Verification Tag:
5716 * Inputs
5717 * (endpoint, asoc)
5718 *
5719 * RFC 2960 Section 4 Notes
5720 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5721 * and re-start the T1-init timer without changing state. This MUST
5722 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5723 * endpoint MUST abort the initialization process and report the
5724 * error to SCTP user.
5725 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005726 * Outputs
5727 * (timers, events)
5728 *
5729 */
Xin Long172a1592017-08-11 10:23:57 +08005730enum sctp_disposition sctp_sf_t1_init_timer_expire(
5731 struct net *net,
5732 const struct sctp_endpoint *ep,
5733 const struct sctp_association *asoc,
5734 const union sctp_subtype type,
5735 void *arg,
5736 struct sctp_cmd_seq *commands)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005737{
Xin Long172a1592017-08-11 10:23:57 +08005738 int attempts = asoc->init_err_counter + 1;
Frank Filz3f7a87d2005-06-20 13:14:57 -07005739 struct sctp_chunk *repl = NULL;
5740 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07005741
Daniel Borkmannbb333812013-06-28 19:49:40 +02005742 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5743
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005744 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005745
Vlad Yasevich81845c22006-01-30 15:59:54 -08005746 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005747 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5748 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5749 if (!repl)
5750 return SCTP_DISPOSITION_NOMEM;
5751
5752 /* Choose transport for INIT. */
5753 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5754 SCTP_CHUNK(repl));
5755
5756 /* Issue a sideeffect to do the needed accounting. */
5757 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5758 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5759
5760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5761 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005762 pr_debug("%s: giving up on INIT, attempts:%d "
5763 "max_init_attempts:%d\n", __func__, attempts,
5764 asoc->max_init_attempts);
5765
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005766 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5767 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005768 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005769 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005770 return SCTP_DISPOSITION_DELETE_TCB;
5771 }
5772
5773 return SCTP_DISPOSITION_CONSUME;
5774}
5775
5776/*
5777 * sctp_sf_t1_cookie_timer_expire
5778 *
5779 * Section: 4 Note: 2
5780 * Verification Tag:
5781 * Inputs
5782 * (endpoint, asoc)
5783 *
5784 * RFC 2960 Section 4 Notes
5785 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 * COOKIE ECHO and re-start the T1-cookie timer without changing
5787 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5788 * After that, the endpoint MUST abort the initialization process and
5789 * report the error to SCTP user.
5790 *
5791 * Outputs
5792 * (timers, events)
5793 *
5794 */
Xin Long172a1592017-08-11 10:23:57 +08005795enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5796 struct net *net,
5797 const struct sctp_endpoint *ep,
5798 const struct sctp_association *asoc,
5799 const union sctp_subtype type,
5800 void *arg,
5801 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005803 int attempts = asoc->init_err_counter + 1;
Xin Long172a1592017-08-11 10:23:57 +08005804 struct sctp_chunk *repl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
Daniel Borkmannbb333812013-06-28 19:49:40 +02005806 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5807
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005808 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809
Vlad Yasevich81845c22006-01-30 15:59:54 -08005810 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005811 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005813 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005815 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5816 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005818 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5819 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5822 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005823 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5824 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005826 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 return SCTP_DISPOSITION_DELETE_TCB;
5828 }
5829
5830 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831}
5832
5833/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5834 * with the updated last sequential TSN received from its peer.
5835 *
5836 * An endpoint should limit the number of retransmissions of the
5837 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5838 * If this threshold is exceeded the endpoint should destroy the TCB and
5839 * MUST report the peer endpoint unreachable to the upper layer (and
5840 * thus the association enters the CLOSED state). The reception of any
5841 * packet from its peer (i.e. as the peer sends all of its queued DATA
5842 * chunks) should clear the endpoint's retransmission count and restart
5843 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5844 * all of its queued DATA chunks that have not yet been sent.
5845 */
Xin Long172a1592017-08-11 10:23:57 +08005846enum sctp_disposition sctp_sf_t2_timer_expire(
5847 struct net *net,
5848 const struct sctp_endpoint *ep,
5849 const struct sctp_association *asoc,
5850 const union sctp_subtype type,
5851 void *arg,
5852 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853{
5854 struct sctp_chunk *reply = NULL;
5855
Daniel Borkmannbb333812013-06-28 19:49:40 +02005856 pr_debug("%s: timer T2 expired\n", __func__);
5857
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005858 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005859
Neil Horman58fbbed2008-02-29 11:40:56 -08005860 ((struct sctp_association *)asoc)->shutdown_retries++;
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005863 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5864 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5866 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005867 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005868 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5869 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 return SCTP_DISPOSITION_DELETE_TCB;
5871 }
5872
5873 switch (asoc->state) {
5874 case SCTP_STATE_SHUTDOWN_SENT:
5875 reply = sctp_make_shutdown(asoc, NULL);
5876 break;
5877
5878 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5879 reply = sctp_make_shutdown_ack(asoc, NULL);
5880 break;
5881
5882 default:
5883 BUG();
5884 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
5887 if (!reply)
5888 goto nomem;
5889
Wei Yongjun6345b192009-04-26 23:13:35 +08005890 /* Do some failure management (Section 8.2).
5891 * If we remove the transport an SHUTDOWN was last sent to, don't
5892 * do failure management.
5893 */
5894 if (asoc->shutdown_last_sent_to)
5895 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5896 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
5898 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5899 * the T2-shutdown timer.
5900 */
5901 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5902
5903 /* Restart the T2-shutdown timer. */
5904 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5905 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5906 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5907 return SCTP_DISPOSITION_CONSUME;
5908
5909nomem:
5910 return SCTP_DISPOSITION_NOMEM;
5911}
5912
5913/*
5914 * ADDIP Section 4.1 ASCONF CHunk Procedures
5915 * If the T4 RTO timer expires the endpoint should do B1 to B5
5916 */
Xin Long172a1592017-08-11 10:23:57 +08005917enum sctp_disposition sctp_sf_t4_timer_expire(
5918 struct net *net,
5919 const struct sctp_endpoint *ep,
5920 const struct sctp_association *asoc,
5921 const union sctp_subtype type,
5922 void *arg,
5923 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924{
5925 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5926 struct sctp_transport *transport = chunk->transport;
5927
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005928 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005929
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5931 * detection on the appropriate destination address as defined in
5932 * RFC2960 [5] section 8.1 and 8.2.
5933 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005934 if (transport)
5935 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5936 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937
5938 /* Reconfig T4 timer and transport. */
5939 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5940
5941 /* ADDIP 4.1 B2) Increment the association error counters and perform
5942 * endpoint failure detection on the association as defined in
5943 * RFC2960 [5] section 8.1 and 8.2.
5944 * association error counter is incremented in SCTP_CMD_STRIKE.
5945 */
5946 if (asoc->overall_error_count >= asoc->max_retrans) {
5947 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5948 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005949 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5950 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005952 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005953 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5954 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return SCTP_DISPOSITION_ABORT;
5956 }
5957
5958 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5959 * the ASCONF chunk was sent by doubling the RTO timer value.
5960 * This is done in SCTP_CMD_STRIKE.
5961 */
5962
5963 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5964 * choose an alternate destination address (please refer to RFC2960
5965 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005966 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 * ASCONF sent.
5968 */
5969 sctp_chunk_hold(asoc->addip_last_asconf);
5970 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5971 SCTP_CHUNK(asoc->addip_last_asconf));
5972
5973 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5974 * destination is selected, then the RTO used will be that of the new
5975 * destination address.
5976 */
5977 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5978 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5979
5980 return SCTP_DISPOSITION_CONSUME;
5981}
5982
5983/* sctpimpguide-05 Section 2.12.2
5984 * The sender of the SHUTDOWN MAY also start an overall guard timer
5985 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5986 * At the expiration of this timer the sender SHOULD abort the association
5987 * by sending an ABORT chunk.
5988 */
Xin Long172a1592017-08-11 10:23:57 +08005989enum sctp_disposition sctp_sf_t5_timer_expire(
5990 struct net *net,
5991 const struct sctp_endpoint *ep,
5992 const struct sctp_association *asoc,
5993 const union sctp_subtype type,
5994 void *arg,
5995 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996{
5997 struct sctp_chunk *reply = NULL;
5998
Daniel Borkmannbb333812013-06-28 19:49:40 +02005999 pr_debug("%s: timer T5 expired\n", __func__);
6000
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006001 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002
6003 reply = sctp_make_abort(asoc, NULL, 0);
6004 if (!reply)
6005 goto nomem;
6006
6007 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006008 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6009 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006011 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006013 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6014 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07006015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 return SCTP_DISPOSITION_DELETE_TCB;
6017nomem:
6018 return SCTP_DISPOSITION_NOMEM;
6019}
6020
6021/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
6022 * the association is automatically closed by starting the shutdown process.
6023 * The work that needs to be done is same as when SHUTDOWN is initiated by
6024 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6025 */
Xin Long172a1592017-08-11 10:23:57 +08006026enum sctp_disposition sctp_sf_autoclose_timer_expire(
6027 struct net *net,
6028 const struct sctp_endpoint *ep,
6029 const struct sctp_association *asoc,
6030 const union sctp_subtype type,
6031 void *arg,
6032 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Xin Long172a1592017-08-11 10:23:57 +08006034 enum sctp_disposition disposition;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006036 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07006037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 /* From 9.2 Shutdown of an Association
6039 * Upon receipt of the SHUTDOWN primitive from its upper
6040 * layer, the endpoint enters SHUTDOWN-PENDING state and
6041 * remains there until all outstanding data has been
6042 * acknowledged by its peer. The endpoint accepts no new data
6043 * from its upper layer, but retransmits data to the far end
6044 * if necessary to fill gaps.
6045 */
6046 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6047 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6048
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 disposition = SCTP_DISPOSITION_CONSUME;
6050 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006051 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 arg, commands);
6053 }
Xin Long172a1592017-08-11 10:23:57 +08006054
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 return disposition;
6056}
6057
6058/*****************************************************************************
6059 * These are sa state functions which could apply to all types of events.
6060 ****************************************************************************/
6061
6062/*
6063 * This table entry is not implemented.
6064 *
6065 * Inputs
6066 * (endpoint, asoc, chunk)
6067 *
6068 * The return value is the disposition of the chunk.
6069 */
Xin Long172a1592017-08-11 10:23:57 +08006070enum sctp_disposition sctp_sf_not_impl(struct net *net,
6071 const struct sctp_endpoint *ep,
6072 const struct sctp_association *asoc,
6073 const union sctp_subtype type,
6074 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075{
6076 return SCTP_DISPOSITION_NOT_IMPL;
6077}
6078
6079/*
6080 * This table entry represents a bug.
6081 *
6082 * Inputs
6083 * (endpoint, asoc, chunk)
6084 *
6085 * The return value is the disposition of the chunk.
6086 */
Xin Long172a1592017-08-11 10:23:57 +08006087enum sctp_disposition sctp_sf_bug(struct net *net,
6088 const struct sctp_endpoint *ep,
6089 const struct sctp_association *asoc,
6090 const union sctp_subtype type,
6091 void *arg, struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092{
6093 return SCTP_DISPOSITION_BUG;
6094}
6095
6096/*
6097 * This table entry represents the firing of a timer in the wrong state.
6098 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6099 * when the association is in the wrong state. This event should
6100 * be ignored, so as to prevent any rearming of the timer.
6101 *
6102 * Inputs
6103 * (endpoint, asoc, chunk)
6104 *
6105 * The return value is the disposition of the chunk.
6106 */
Xin Long172a1592017-08-11 10:23:57 +08006107enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6108 const struct sctp_endpoint *ep,
6109 const struct sctp_association *asoc,
6110 const union sctp_subtype type,
6111 void *arg,
6112 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113{
Daniel Borkmannbb333812013-06-28 19:49:40 +02006114 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6115
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 return SCTP_DISPOSITION_CONSUME;
6117}
6118
6119/********************************************************************
6120 * 2nd Level Abstractions
6121 ********************************************************************/
6122
6123/* Pull the SACK chunk based on the SACK header. */
6124static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6125{
6126 struct sctp_sackhdr *sack;
Xin Long172a1592017-08-11 10:23:57 +08006127 __u16 num_dup_tsns;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 unsigned int len;
6129 __u16 num_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130
6131 /* Protect ourselves from reading too far into
6132 * the skb from a bogus sender.
6133 */
6134 sack = (struct sctp_sackhdr *) chunk->skb->data;
6135
6136 num_blocks = ntohs(sack->num_gap_ack_blocks);
6137 num_dup_tsns = ntohs(sack->num_dup_tsns);
6138 len = sizeof(struct sctp_sackhdr);
6139 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6140 if (len > chunk->skb->len)
6141 return NULL;
6142
6143 skb_pull(chunk->skb, len);
6144
6145 return sack;
6146}
6147
6148/* Create an ABORT packet to be sent as a response, with the specified
6149 * error causes.
6150 */
Xin Long172a1592017-08-11 10:23:57 +08006151static struct sctp_packet *sctp_abort_pkt_new(
6152 struct net *net,
6153 const struct sctp_endpoint *ep,
6154 const struct sctp_association *asoc,
6155 struct sctp_chunk *chunk,
6156 const void *payload, size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157{
6158 struct sctp_packet *packet;
6159 struct sctp_chunk *abort;
6160
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006161 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
6163 if (packet) {
6164 /* Make an ABORT.
6165 * The T bit will be set if the asoc is NULL.
6166 */
6167 abort = sctp_make_abort(asoc, chunk, paylen);
6168 if (!abort) {
6169 sctp_ootb_pkt_free(packet);
6170 return NULL;
6171 }
Jerome Forissier047a2422005-04-28 11:58:43 -07006172
6173 /* Reflect vtag if T-Bit is set */
6174 if (sctp_test_T_bit(abort))
6175 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6176
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177 /* Add specified error causes, i.e., payload, to the
6178 * end of the chunk.
6179 */
6180 sctp_addto_chunk(abort, paylen, payload);
6181
6182 /* Set the skb to the belonging sock for accounting. */
6183 abort->skb->sk = ep->base.sk;
6184
6185 sctp_packet_append_chunk(packet, abort);
6186
6187 }
6188
6189 return packet;
6190}
6191
6192/* Allocate a packet for responding in the OOTB conditions. */
Xin Long172a1592017-08-11 10:23:57 +08006193static struct sctp_packet *sctp_ootb_pkt_new(
6194 struct net *net,
6195 const struct sctp_association *asoc,
6196 const struct sctp_chunk *chunk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 struct sctp_transport *transport;
Xin Long172a1592017-08-11 10:23:57 +08006199 struct sctp_packet *packet;
6200 __u16 sport, dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 __u32 vtag;
6202
6203 /* Get the source and destination port from the inbound packet. */
6204 sport = ntohs(chunk->sctp_hdr->dest);
6205 dport = ntohs(chunk->sctp_hdr->source);
6206
6207 /* The V-tag is going to be the same as the inbound packet if no
6208 * association exists, otherwise, use the peer's vtag.
6209 */
6210 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006211 /* Special case the INIT-ACK as there is no peer's vtag
6212 * yet.
6213 */
wangweidongcb3f8372013-12-23 12:16:50 +08006214 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006215 case SCTP_CID_INIT_ACK:
6216 {
Xin Longcb1844c2017-07-23 09:34:26 +08006217 struct sctp_initack_chunk *initack;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006218
Xin Longcb1844c2017-07-23 09:34:26 +08006219 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
Wei Yongjun02c4e122007-08-31 10:03:58 +08006220 vtag = ntohl(initack->init_hdr.init_tag);
6221 break;
6222 }
6223 default:
6224 vtag = asoc->peer.i.init_tag;
6225 break;
6226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 } else {
6228 /* Special case the INIT and stale COOKIE_ECHO as there is no
6229 * vtag yet.
6230 */
wangweidongcb3f8372013-12-23 12:16:50 +08006231 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 case SCTP_CID_INIT:
6233 {
Xin Long01a992b2017-06-30 11:52:22 +08006234 struct sctp_init_chunk *init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
Xin Long01a992b2017-06-30 11:52:22 +08006236 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 vtag = ntohl(init->init_hdr.init_tag);
6238 break;
6239 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006240 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 vtag = ntohl(chunk->sctp_hdr->vtag);
6242 break;
6243 }
6244 }
6245
6246 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006247 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 if (!transport)
6249 goto nomem;
6250
6251 /* Cache a route for the transport with the chunk's destination as
6252 * the source address.
6253 */
Al Viro16b0a032006-11-20 17:13:38 -08006254 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006255 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006257 packet = &transport->packet;
6258 sctp_packet_init(packet, transport, sport, dport);
6259 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260
6261 return packet;
6262
6263nomem:
6264 return NULL;
6265}
6266
6267/* Free the packet allocated earlier for responding in the OOTB condition. */
6268void sctp_ootb_pkt_free(struct sctp_packet *packet)
6269{
6270 sctp_transport_free(packet->transport);
6271}
6272
6273/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006274static void sctp_send_stale_cookie_err(struct net *net,
6275 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 const struct sctp_association *asoc,
6277 const struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08006278 struct sctp_cmd_seq *commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 struct sctp_chunk *err_chunk)
6280{
6281 struct sctp_packet *packet;
6282
6283 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006284 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285 if (packet) {
6286 struct sctp_signed_cookie *cookie;
6287
6288 /* Override the OOTB vtag from the cookie. */
6289 cookie = chunk->subh.cookie_hdr;
6290 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006291
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 /* Set the skb to the belonging sock for accounting. */
6293 err_chunk->skb->sk = ep->base.sk;
6294 sctp_packet_append_chunk(packet, err_chunk);
6295 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6296 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006297 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 } else
6299 sctp_chunk_free (err_chunk);
6300 }
6301}
6302
6303
6304/* Process a data chunk */
6305static int sctp_eat_data(const struct sctp_association *asoc,
6306 struct sctp_chunk *chunk,
Xin Longa85bbeb2017-08-11 10:23:52 +08006307 struct sctp_cmd_seq *commands)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308{
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006309 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006310 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006311 struct net *net = sock_net(sk);
Xin Long172a1592017-08-11 10:23:57 +08006312 struct sctp_datahdr *data_hdr;
6313 struct sctp_chunk *err;
6314 enum sctp_verb deliver;
6315 size_t datalen;
Xin Long172a1592017-08-11 10:23:57 +08006316 __u32 tsn;
6317 int tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Xin Long3583df12017-06-30 11:52:19 +08006319 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6320 chunk->subh.data_hdr = data_hdr;
Xin Long9d4ceaf2017-12-08 21:04:03 +08006321 skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322
6323 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006324 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325
6326 /* ASSERT: Now skb->data is really the user data. */
6327
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 /* Process ECN based congestion.
6329 *
6330 * Since the chunk structure is reused for all chunks within
6331 * a packet, we use ecn_ce_done to track if we've already
6332 * done CE processing for this packet.
6333 *
6334 * We need to do ECN processing even if we plan to discard the
6335 * chunk later.
6336 */
6337
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006338 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006339 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 chunk->ecn_ce_done = 1;
6341
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006342 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 /* Do real work as sideffect. */
6344 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6345 SCTP_U32(tsn));
6346 }
6347 }
6348
6349 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6350 if (tmp < 0) {
6351 /* The TSN is too high--silently discard the chunk and
6352 * count on it getting retransmitted later.
6353 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006354 if (chunk->asoc)
6355 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 return SCTP_IERROR_HIGH_TSN;
6357 } else if (tmp > 0) {
6358 /* This is a duplicate. Record it. */
6359 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6360 return SCTP_IERROR_DUP_TSN;
6361 }
6362
6363 /* This is a new TSN. */
6364
6365 /* Discard if there is no room in the receive window.
6366 * Actually, allow a little bit of overflow (up to a MTU).
6367 */
6368 datalen = ntohs(chunk->chunk_hdr->length);
Xin Long9d4ceaf2017-12-08 21:04:03 +08006369 datalen -= sctp_datachk_len(&asoc->stream);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
6371 deliver = SCTP_CMD_CHUNK_ULP;
6372
6373 /* Think about partial delivery. */
6374 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6375
6376 /* Even if we don't accept this chunk there is
6377 * memory pressure.
6378 */
6379 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6380 }
6381
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006382 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 * seems a bit troublesome in that frag_point varies based on
6384 * PMTU. In cases, such as loopback, this might be a rather
6385 * large spill over.
6386 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006387 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006388 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
6390 /* If this is the next TSN, consider reneging to make
6391 * room. Note: Playing nice with a confused sender. A
6392 * malicious sender can still eat up all our buffer
6393 * space and in the future we may want to detect and
6394 * do more drastic reneging.
6395 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006396 if (sctp_tsnmap_has_gap(map) &&
6397 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006398 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 deliver = SCTP_CMD_RENEGE;
6400 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006401 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6402 __func__, tsn, datalen, asoc->rwnd);
6403
Linus Torvalds1da177e2005-04-16 15:20:36 -07006404 return SCTP_IERROR_IGNORE_TSN;
6405 }
6406 }
6407
6408 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006409 * Also try to renege to limit our memory usage in the event that
6410 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006411 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006412 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6413 * memory usage too much
6414 */
6415 if (*sk->sk_prot_creator->memory_pressure) {
6416 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006417 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006418 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6419 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006420 deliver = SCTP_CMD_RENEGE;
6421 }
6422 }
6423
6424 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 * Section 3.3.10.9 No User Data (9)
6426 *
6427 * Cause of error
6428 * ---------------
6429 * No User Data: This error cause is returned to the originator of a
6430 * DATA chunk if a received DATA chunk has no user data.
6431 */
6432 if (unlikely(0 == datalen)) {
6433 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6434 if (err) {
6435 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6436 SCTP_CHUNK(err));
6437 }
6438 /* We are going to ABORT, so we might as well stop
6439 * processing the rest of the chunks in the packet.
6440 */
wangweidongcb3f8372013-12-23 12:16:50 +08006441 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006442 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6443 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006444 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006445 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006446 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6447 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 return SCTP_IERROR_NO_DATA;
6449 }
6450
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006451 chunk->data_accepted = 1;
6452
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6454 * if we renege and the chunk arrives again.
6455 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006456 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006457 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006458 if (chunk->asoc)
6459 chunk->asoc->stats.iuodchunks++;
6460 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006461 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006462 if (chunk->asoc)
6463 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006465
6466 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6467 *
6468 * If an endpoint receive a DATA chunk with an invalid stream
6469 * identifier, it shall acknowledge the reception of the DATA chunk
6470 * following the normal procedure, immediately send an ERROR chunk
6471 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6472 * and discard the DATA chunk.
6473 */
Xin Long9d4ceaf2017-12-08 21:04:03 +08006474 if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006475 /* Mark tsn as received even though we drop it */
6476 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6477
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6479 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006480 sizeof(data_hdr->stream),
6481 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 if (err)
6483 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6484 SCTP_CHUNK(err));
6485 return SCTP_IERROR_BAD_STREAM;
6486 }
6487
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006488 /* Check to see if the SSN is possible for this TSN.
6489 * The biggest gap we can record is 4K wide. Since SSNs wrap
6490 * at an unsigned short, there is no way that an SSN can
6491 * wrap and for a valid TSN. We can simply check if the current
6492 * SSN is smaller then the next expected one. If it is, it wrapped
6493 * and is invalid.
6494 */
Xin Long9d4ceaf2017-12-08 21:04:03 +08006495 if (!asoc->stream.si->validate_data(chunk))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006496 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006497
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498 /* Send the data up to the user. Note: Schedule the
6499 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6500 * chunk needs the updated rwnd.
6501 */
6502 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6503
6504 return SCTP_IERROR_NO_ERROR;
6505}