blob: d8798ddda726176ac66ed36f8234ed89109e8c20 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080025 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020030 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
Joe Perches145ce502010-08-24 13:21:08 +000046#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/ip.h>
51#include <linux/ipv6.h>
52#include <linux/net.h>
53#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/inet_ecn.h>
57#include <linux/skbuff.h>
58#include <net/sctp/sctp.h>
59#include <net/sctp/sm.h>
60#include <net/sctp/structs.h>
61
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000062static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
63 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 const struct sctp_association *asoc,
65 struct sctp_chunk *chunk,
66 const void *payload,
67 size_t paylen);
68static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 sctp_cmd_seq_t *commands);
Eric W. Biederman2ce95502012-08-06 08:43:06 +000071static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
72 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000074static void sctp_send_stale_cookie_err(struct net *net,
75 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk,
78 sctp_cmd_seq_t *commands,
79 struct sctp_chunk *err_chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000080static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
81 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000086static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
87 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 const struct sctp_association *asoc,
89 const sctp_subtype_t type,
90 void *arg,
91 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000092static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
93 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -040094 const struct sctp_association *asoc,
95 const sctp_subtype_t type,
96 void *arg,
97 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
99
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000100static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
101 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800102 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
105
Wei Yongjunaecedea2007-08-02 16:57:44 +0800106static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000107 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400108 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800109 const struct sctp_association *asoc,
110 void *arg,
111 sctp_cmd_seq_t *commands,
112 const __u8 *payload,
113 const size_t paylen);
114
Adrian Bunk52c1da32005-06-23 22:05:33 -0700115static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000116 struct net *net,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const sctp_subtype_t type,
120 void *arg,
121 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000124 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700128 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800129 sctp_cmd_seq_t *commands);
130
Wei Yongjunaecedea2007-08-02 16:57:44 +0800131static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000132 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichece25df2007-09-07 16:30:54 -0400139static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000140 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const sctp_subtype_t type,
144 void *arg,
145 sctp_cmd_seq_t *commands);
146
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000147static sctp_ierror_t sctp_sf_authenticate(struct net *net,
148 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 struct sctp_chunk *chunk);
152
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000153static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
154 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800155 const struct sctp_association *asoc,
156 const sctp_subtype_t type,
157 void *arg,
158 sctp_cmd_seq_t *commands);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Small helper function that checks if the chunk length
161 * is of the appropriate length. The 'required_length' argument
162 * is set to be the size of a specific chunk we are testing.
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200163 * Return Values: true = Valid length
164 * false = Invalid length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
166 */
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200167static inline bool
168sctp_chunk_length_valid(struct sctp_chunk *chunk, __u16 required_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
171
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200172 /* Previously already marked? */
173 if (unlikely(chunk->pdiscard))
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200174 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (unlikely(chunk_length < required_length))
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200176 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Marcelo Ricardo Leitner509e7a32016-12-28 09:26:35 -0200178 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/**********************************************************
182 * These are the state functions for handling chunk events.
183 **********************************************************/
184
185/*
186 * Process the final SHUTDOWN COMPLETE.
187 *
188 * Section: 4 (C) (diagram), 9.2
189 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
190 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
191 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
192 * should stop the T2-shutdown timer and remove all knowledge of the
193 * association (and thus the association enters the CLOSED state).
194 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700195 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * C) Rules for packet carrying SHUTDOWN COMPLETE:
197 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700198 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
199 * if the Verification Tag field of the packet matches its own tag and
200 * the T bit is not set
201 * OR
202 * it is set to its peer's tag and the T bit is set in the Chunk
203 * Flags.
204 * Otherwise, the receiver MUST silently discard the packet
205 * and take no further action. An endpoint MUST ignore the
206 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 *
208 * Inputs
209 * (endpoint, asoc, chunk)
210 *
211 * Outputs
212 * (asoc, reply_msg, msg_up, timers, counters)
213 *
214 * The return value is the disposition of the chunk.
215 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000216sctp_disposition_t sctp_sf_do_4_C(struct net *net,
217 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 const struct sctp_association *asoc,
219 const sctp_subtype_t type,
220 void *arg,
221 sctp_cmd_seq_t *commands)
222{
223 struct sctp_chunk *chunk = arg;
224 struct sctp_ulpevent *ev;
225
Vlad Yasevichece25df2007-09-07 16:30:54 -0400226 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000227 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* RFC 2960 6.10 Bundling
230 *
231 * An endpoint MUST NOT bundle INIT, INIT ACK or
232 * SHUTDOWN COMPLETE with any other chunks.
233 */
234 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000235 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Vlad Yasevichece25df2007-09-07 16:30:54 -0400237 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
238 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000239 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400240 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* RFC 2960 10.2 SCTP-to-ULP
243 *
244 * H) SHUTDOWN COMPLETE notification
245 *
246 * When SCTP completes the shutdown procedures (section 9.2) this
247 * notification is passed to the upper layer.
248 */
249 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700250 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700251 if (ev)
252 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900253 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
256 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
257 * not the chunk should be discarded. If the endpoint is in
258 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
259 * T2-shutdown timer and remove all knowledge of the
260 * association (and thus the association enters the CLOSED
261 * state).
262 */
263 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
264 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
265
266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
267 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
268
269 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
270 SCTP_STATE(SCTP_STATE_CLOSED));
271
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000272 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
273 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
276
277 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/*
281 * Respond to a normal INIT chunk.
282 * We are the side that is being asked for an association.
283 *
284 * Section: 5.1 Normal Establishment of an Association, B
285 * B) "Z" shall respond immediately with an INIT ACK chunk. The
286 * destination IP address of the INIT ACK MUST be set to the source
287 * IP address of the INIT to which this INIT ACK is responding. In
288 * the response, besides filling in other parameters, "Z" must set the
289 * Verification Tag field to Tag_A, and also provide its own
290 * Verification Tag (Tag_Z) in the Initiate Tag field.
291 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900292 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 *
294 * Inputs
295 * (endpoint, asoc, chunk)
296 *
297 * Outputs
298 * (asoc, reply_msg, msg_up, timers, counters)
299 *
300 * The return value is the disposition of the chunk.
301 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000302sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
303 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 const struct sctp_association *asoc,
305 const sctp_subtype_t type,
306 void *arg,
307 sctp_cmd_seq_t *commands)
308{
309 struct sctp_chunk *chunk = arg;
310 struct sctp_chunk *repl;
311 struct sctp_association *new_asoc;
312 struct sctp_chunk *err_chunk;
313 struct sctp_packet *packet;
314 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int len;
316
317 /* 6.10 Bundling
318 * An endpoint MUST NOT bundle INIT, INIT ACK or
319 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900320 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * IG Section 2.11.2
322 * Furthermore, we require that the receiver of an INIT chunk MUST
323 * enforce these rules by silently discarding an arriving packet
324 * with an INIT chunk that is bundled with other chunks.
325 */
326 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000327 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 /* If the packet is an OOTB packet which is temporarily on the
330 * control endpoint, respond with an ABORT.
331 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000332 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000333 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000334 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900338 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000341 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Make sure that the INIT chunk has a valid length.
344 * Normally, this would cause an ABORT with a Protocol Violation
345 * error, but since we don't have an association, we'll
346 * just discard the packet.
347 */
348 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000349 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Vlad Yasevichbec96402009-07-30 18:08:28 -0400351 /* If the INIT is coming toward a closing socket, we'll send back
352 * and ABORT. Essentially, this catches the race of INIT being
353 * backloged to the socket at the same time as the user isses close().
354 * Since the socket and all its associations are going away, we
355 * can treat this OOTB
356 */
357 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000358 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Verify the INIT chunk before processing it. */
361 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200362 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
364 &err_chunk)) {
365 /* This chunk contains fatal error. It is to be discarded.
366 * Send an ABORT, with causes if there is any.
367 */
368 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000369 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 (__u8 *)(err_chunk->chunk_hdr) +
371 sizeof(sctp_chunkhdr_t),
372 ntohs(err_chunk->chunk_hdr->length) -
373 sizeof(sctp_chunkhdr_t));
374
375 sctp_chunk_free(err_chunk);
376
377 if (packet) {
378 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
379 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000380 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return SCTP_DISPOSITION_CONSUME;
382 } else {
383 return SCTP_DISPOSITION_NOMEM;
384 }
385 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000386 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 commands);
388 }
389 }
390
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900391 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
393
394 /* Tag the variable length parameters. */
395 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
396
397 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
398 if (!new_asoc)
399 goto nomem;
400
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000401 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
402 sctp_scope(sctp_source(chunk)),
403 GFP_ATOMIC) < 0)
404 goto nomem_init;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000407 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 (sctp_init_chunk_t *)chunk->chunk_hdr,
409 GFP_ATOMIC))
410 goto nomem_init;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
413
414 /* If there are errors need to be reported for unknown parameters,
415 * make sure to reserve enough room in the INIT ACK for them.
416 */
417 len = 0;
418 if (err_chunk)
419 len = ntohs(err_chunk->chunk_hdr->length) -
420 sizeof(sctp_chunkhdr_t);
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
423 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700424 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 /* If there are errors need to be reported for unknown parameters,
427 * include them in the outgoing INIT ACK as "Unrecognized parameter"
428 * parameter.
429 */
430 if (err_chunk) {
431 /* Get the "Unrecognized parameter" parameter(s) out of the
432 * ERROR chunk generated by sctp_verify_init(). Since the
433 * error cause code for "unknown parameter" and the
434 * "Unrecognized parameter" type is the same, we can
435 * construct the parameters in INIT ACK by copying the
436 * ERROR causes over.
437 */
438 unk_param = (sctp_unrecognized_param_t *)
439 ((__u8 *)(err_chunk->chunk_hdr) +
440 sizeof(sctp_chunkhdr_t));
441 /* Replace the cause code with the "Unrecognized parameter"
442 * parameter type.
443 */
444 sctp_addto_chunk(repl, len, unk_param);
445 sctp_chunk_free(err_chunk);
446 }
447
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700448 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
451
452 /*
453 * Note: After sending out INIT ACK with the State Cookie parameter,
454 * "Z" MUST NOT allocate any resources, nor keep any states for the
455 * new association. Otherwise, "Z" will be vulnerable to resource
456 * attacks.
457 */
458 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
459
460 return SCTP_DISPOSITION_DELETE_TCB;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462nomem_init:
463 sctp_association_free(new_asoc);
464nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700465 if (err_chunk)
466 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return SCTP_DISPOSITION_NOMEM;
468}
469
470/*
471 * Respond to a normal INIT ACK chunk.
472 * We are the side that is initiating the association.
473 *
474 * Section: 5.1 Normal Establishment of an Association, C
475 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
476 * timer and leave COOKIE-WAIT state. "A" shall then send the State
477 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
478 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
479 *
480 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
481 * DATA chunks, but it MUST be the first chunk in the packet and
482 * until the COOKIE ACK is returned the sender MUST NOT send any
483 * other packets to the peer.
484 *
485 * Verification Tag: 3.3.3
486 * If the value of the Initiate Tag in a received INIT ACK chunk is
487 * found to be 0, the receiver MUST treat it as an error and close the
488 * association by transmitting an ABORT.
489 *
490 * Inputs
491 * (endpoint, asoc, chunk)
492 *
493 * Outputs
494 * (asoc, reply_msg, msg_up, timers, counters)
495 *
496 * The return value is the disposition of the chunk.
497 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000498sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
499 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 const struct sctp_association *asoc,
501 const sctp_subtype_t type,
502 void *arg,
503 sctp_cmd_seq_t *commands)
504{
505 struct sctp_chunk *chunk = arg;
506 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 struct sctp_chunk *err_chunk;
508 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000511 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* 6.10 Bundling
514 * An endpoint MUST NOT bundle INIT, INIT ACK or
515 * SHUTDOWN COMPLETE with any other chunks.
516 */
517 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000518 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Vlad Yasevichece25df2007-09-07 16:30:54 -0400520 /* Make sure that the INIT-ACK chunk has a valid length */
521 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000522 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400523 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Grab the INIT header. */
525 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* Verify the INIT chunk before processing it. */
528 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200529 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
531 &err_chunk)) {
532
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800533 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800536 * Send an ABORT, with causes. If there are no causes,
537 * then there wasn't enough memory. Just terminate
538 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000541 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 (__u8 *)(err_chunk->chunk_hdr) +
543 sizeof(sctp_chunkhdr_t),
544 ntohs(err_chunk->chunk_hdr->length) -
545 sizeof(sctp_chunkhdr_t));
546
547 sctp_chunk_free(err_chunk);
548
549 if (packet) {
550 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
551 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000552 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700553 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700556
557 /* SCTP-AUTH, Section 6.3:
558 * It should be noted that if the receiver wants to tear
559 * down an association in an authenticated way only, the
560 * handling of malformed packets should not result in
561 * tearing down the association.
562 *
563 * This means that if we only want to abort associations
564 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300565 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700566 * was malformed.
567 */
568 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000569 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700570
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000571 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
572 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700573 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575
576 /* Tag the variable length parameters. Note that we never
577 * convert the parameters in an INIT chunk.
578 */
579 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
580
581 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
582
583 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
584 SCTP_PEER_INIT(initchunk));
585
Frank Filz3f7a87d2005-06-20 13:14:57 -0700586 /* Reset init error count upon receipt of INIT-ACK. */
587 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* 5.1 C) "A" shall stop the T1-init timer and leave
590 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
591 * timer, and enter the COOKIE-ECHOED state.
592 */
593 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
594 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
595 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
596 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
597 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
598 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
599
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700600 /* SCTP-AUTH: genereate the assocition shared keys so that
601 * we can potentially signe the COOKIE-ECHO.
602 */
603 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* 5.1 C) "A" shall then send the State Cookie received in the
606 * INIT ACK chunk in a COOKIE ECHO chunk, ...
607 */
608 /* If there is any errors to report, send the ERROR chunk generated
609 * for unknown parameters as well.
610 */
611 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
612 SCTP_CHUNK(err_chunk));
613
614 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/*
618 * Respond to a normal COOKIE ECHO chunk.
619 * We are the side that is being asked for an association.
620 *
621 * Section: 5.1 Normal Establishment of an Association, D
622 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
623 * with a COOKIE ACK chunk after building a TCB and moving to
624 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
625 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
626 * chunk MUST be the first chunk in the packet.
627 *
628 * IMPLEMENTATION NOTE: An implementation may choose to send the
629 * Communication Up notification to the SCTP user upon reception
630 * of a valid COOKIE ECHO chunk.
631 *
632 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
633 * D) Rules for packet carrying a COOKIE ECHO
634 *
635 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
636 * Initial Tag received in the INIT ACK.
637 *
638 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
639 *
640 * Inputs
641 * (endpoint, asoc, chunk)
642 *
643 * Outputs
644 * (asoc, reply_msg, msg_up, timers, counters)
645 *
646 * The return value is the disposition of the chunk.
647 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000648sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
649 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 const struct sctp_association *asoc,
651 const sctp_subtype_t type, void *arg,
652 sctp_cmd_seq_t *commands)
653{
654 struct sctp_chunk *chunk = arg;
655 struct sctp_association *new_asoc;
656 sctp_init_chunk_t *peer_init;
657 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700658 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 int error = 0;
660 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900661 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /* If the packet is an OOTB packet which is temporarily on the
664 * control endpoint, respond with an ABORT.
665 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000666 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000667 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000668 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* Make sure that the COOKIE_ECHO chunk has a valid length.
672 * In this case, we check that we have enough for at least a
673 * chunk header. More detailed verification is done
674 * in sctp_unpack_cookie().
675 */
676 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000677 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Vlad Yasevich609ee462007-08-31 03:10:59 +0900679 /* If the endpoint is not listening or if the number of associations
680 * on the TCP-style socket exceed the max backlog, respond with an
681 * ABORT.
682 */
683 sk = ep->base.sk;
684 if (!sctp_sstate(sk, LISTENING) ||
685 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000686 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 /* "Decode" the chunk. We have no optional parameters so we
689 * are in good shape.
690 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900691 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700693 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
694 sizeof(sctp_chunkhdr_t)))
695 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
698 * "Z" will reply with a COOKIE ACK chunk after building a TCB
699 * and moving to the ESTABLISHED state.
700 */
701 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
702 &err_chk_p);
703
704 /* FIXME:
705 * If the re-build failed, what is the proper error path
706 * from here?
707 *
708 * [We should abort the association. --piggy]
709 */
710 if (!new_asoc) {
711 /* FIXME: Several errors are possible. A bad cookie should
712 * be silently discarded, but think about logging it too.
713 */
714 switch (error) {
715 case -SCTP_IERROR_NOMEM:
716 goto nomem;
717
718 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000719 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000721 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 case -SCTP_IERROR_BAD_SIG:
724 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000725 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700730 /* Delay state machine commands until later.
731 *
732 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 * the sctp_unpack_cookie() already.
734 */
735 /* This is a brand-new association, so these are not yet side
736 * effects--it is safe to run them here.
737 */
738 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
739
Wei Yongjunde6becdc02011-04-19 21:30:51 +0000740 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800741 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 peer_init, GFP_ATOMIC))
743 goto nomem_init;
744
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700745 /* SCTP-AUTH: Now that we've populate required fields in
746 * sctp_process_init, set up the assocaition shared keys as
747 * necessary so that we can potentially authenticate the ACK
748 */
749 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
750 if (error)
751 goto nomem_init;
752
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700753 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
754 * is supposed to be authenticated and we have to do delayed
755 * authentication. We've just recreated the association using
756 * the information in the cookie and now it's much easier to
757 * do the authentication.
758 */
759 if (chunk->auth_chunk) {
760 struct sctp_chunk auth;
761 sctp_ierror_t ret;
762
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100763 /* Make sure that we and the peer are AUTH capable */
764 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100765 sctp_association_free(new_asoc);
766 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
767 }
768
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700769 /* set-up our fake chunk so that we can process it */
770 auth.skb = chunk->auth_chunk;
771 auth.asoc = chunk->asoc;
772 auth.sctp_hdr = chunk->sctp_hdr;
773 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
774 sizeof(sctp_chunkhdr_t));
775 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
776 auth.transport = chunk->transport;
777
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000778 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700779 if (ret != SCTP_IERROR_NO_ERROR) {
780 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000781 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700782 }
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 repl = sctp_make_cookie_ack(new_asoc, chunk);
786 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700787 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* RFC 2960 5.1 Normal Establishment of an Association
790 *
791 * D) IMPLEMENTATION NOTE: An implementation may choose to
792 * send the Communication Up notification to the SCTP user
793 * upon reception of a valid COOKIE ECHO chunk.
794 */
795 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
796 new_asoc->c.sinit_num_ostreams,
797 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700798 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!ev)
800 goto nomem_ev;
801
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900802 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800803 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800805 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800807 if (new_asoc->peer.adaptation_ind) {
808 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700810 if (!ai_ev)
811 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
813
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700814 /* Add all the state machine commands now since we've created
815 * everything. This way we don't introduce memory corruptions
816 * during side-effect processing and correclty count established
817 * associations.
818 */
819 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
820 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
821 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000822 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
823 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700824 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
825
Neil Horman9f70f462013-12-10 06:48:15 -0500826 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700827 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
828 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
829
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700830 /* This will send the COOKIE ACK */
831 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
832
833 /* Queue the ASSOC_CHANGE event */
834 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
835
836 /* Send up the Adaptation Layer Indication event */
837 if (ai_ev)
838 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
839 SCTP_ULPEVENT(ai_ev));
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return SCTP_DISPOSITION_CONSUME;
842
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700843nomem_aiev:
844 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845nomem_ev:
846 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847nomem_init:
848 sctp_association_free(new_asoc);
849nomem:
850 return SCTP_DISPOSITION_NOMEM;
851}
852
853/*
854 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300855 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 *
857 * RFC 2960 5.1 Normal Establishment of an Association
858 *
859 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
860 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
861 * timer. It may also notify its ULP about the successful
862 * establishment of the association with a Communication Up
863 * notification (see Section 10).
864 *
865 * Verification Tag:
866 * Inputs
867 * (endpoint, asoc, chunk)
868 *
869 * Outputs
870 * (asoc, reply_msg, msg_up, timers, counters)
871 *
872 * The return value is the disposition of the chunk.
873 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000874sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
875 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 const struct sctp_association *asoc,
877 const sctp_subtype_t type, void *arg,
878 sctp_cmd_seq_t *commands)
879{
880 struct sctp_chunk *chunk = arg;
881 struct sctp_ulpevent *ev;
882
883 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000884 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* Verify that the chunk length for the COOKIE-ACK is OK.
887 * If we don't do this, any bundled chunks may be junked.
888 */
889 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000890 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 commands);
892
893 /* Reset init error count upon receipt of COOKIE-ACK,
894 * to avoid problems with the managemement of this
895 * counter in stale cookie situations when a transition back
896 * from the COOKIE-ECHOED state to the COOKIE-WAIT
897 * state is performed.
898 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700899 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* RFC 2960 5.1 Normal Establishment of an Association
902 *
903 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
904 * from the COOKIE-ECHOED state to the ESTABLISHED state,
905 * stopping the T1-cookie timer.
906 */
907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
908 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
909 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
910 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000911 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
912 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500914 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
916 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /* It may also notify its ULP about the successful
919 * establishment of the association with a Communication Up
920 * notification (see Section 10).
921 */
922 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
923 0, asoc->c.sinit_num_ostreams,
924 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700925 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 if (!ev)
928 goto nomem;
929
930 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
931
932 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800933 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800935 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800937 if (asoc->peer.adaptation_ind) {
938 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (!ev)
940 goto nomem;
941
942 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
943 SCTP_ULPEVENT(ev));
944 }
945
946 return SCTP_DISPOSITION_CONSUME;
947nomem:
948 return SCTP_DISPOSITION_NOMEM;
949}
950
951/* Generate and sendout a heartbeat packet. */
952static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
953 const struct sctp_association *asoc,
954 const sctp_subtype_t type,
955 void *arg,
956 sctp_cmd_seq_t *commands)
957{
958 struct sctp_transport *transport = (struct sctp_transport *) arg;
959 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000962 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (!reply)
964 return SCTP_DISPOSITION_NOMEM;
965
966 /* Set rto_pending indicating that an RTT measurement
967 * is started with this heartbeat chunk.
968 */
969 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
970 SCTP_TRANSPORT(transport));
971
972 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
973 return SCTP_DISPOSITION_CONSUME;
974}
975
976/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000977sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
978 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 const struct sctp_association *asoc,
980 const sctp_subtype_t type,
981 void *arg,
982 sctp_cmd_seq_t *commands)
983{
984 struct sctp_transport *transport = (struct sctp_transport *) arg;
985
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400986 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700987 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
988 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
990 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800991 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000992 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
993 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return SCTP_DISPOSITION_DELETE_TCB;
995 }
996
997 /* Section 3.3.5.
998 * The Sender-specific Heartbeat Info field should normally include
999 * information about the sender's current time when this HEARTBEAT
1000 * chunk is sent and the destination transport address to which this
1001 * HEARTBEAT is sent (see Section 8.3).
1002 */
1003
Frank Filz52ccb8e2005-12-22 11:36:46 -08001004 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (SCTP_DISPOSITION_NOMEM ==
1006 sctp_sf_heartbeat(ep, asoc, type, arg,
1007 commands))
1008 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* Set transport error counter and association error counter
1011 * when sending heartbeat.
1012 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001013 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 SCTP_TRANSPORT(transport));
1015 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001016 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1017 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1019 SCTP_TRANSPORT(transport));
1020
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001021 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Xin Long7b9438d2017-01-18 00:44:43 +08001024/* resend asoc strreset_chunk. */
1025sctp_disposition_t sctp_sf_send_reconf(struct net *net,
1026 const struct sctp_endpoint *ep,
1027 const struct sctp_association *asoc,
1028 const sctp_subtype_t type, void *arg,
1029 sctp_cmd_seq_t *commands)
1030{
1031 struct sctp_transport *transport = arg;
1032
1033 if (asoc->overall_error_count >= asoc->max_retrans) {
1034 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1035 SCTP_ERROR(ETIMEDOUT));
1036 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1037 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1038 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1039 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1040 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1041 return SCTP_DISPOSITION_DELETE_TCB;
1042 }
1043
1044 sctp_chunk_hold(asoc->strreset_chunk);
1045 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1046 SCTP_CHUNK(asoc->strreset_chunk));
1047 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1048
1049 return SCTP_DISPOSITION_CONSUME;
1050}
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * Process an heartbeat request.
1054 *
1055 * Section: 8.3 Path Heartbeat
1056 * The receiver of the HEARTBEAT should immediately respond with a
1057 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1058 * from the received HEARTBEAT chunk.
1059 *
1060 * Verification Tag: 8.5 Verification Tag [Normal verification]
1061 * When receiving an SCTP packet, the endpoint MUST ensure that the
1062 * value in the Verification Tag field of the received SCTP packet
1063 * matches its own Tag. If the received Verification Tag value does not
1064 * match the receiver's own tag value, the receiver shall silently
1065 * discard the packet and shall not process it any further except for
1066 * those cases listed in Section 8.5.1 below.
1067 *
1068 * Inputs
1069 * (endpoint, asoc, chunk)
1070 *
1071 * Outputs
1072 * (asoc, reply_msg, msg_up, timers, counters)
1073 *
1074 * The return value is the disposition of the chunk.
1075 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001076sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1077 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 const struct sctp_association *asoc,
1079 const sctp_subtype_t type,
1080 void *arg,
1081 sctp_cmd_seq_t *commands)
1082{
Thomas Graf06a31e22012-11-30 02:16:27 +00001083 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 struct sctp_chunk *chunk = arg;
1085 struct sctp_chunk *reply;
1086 size_t paylen = 0;
1087
1088 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001089 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 /* Make sure that the HEARTBEAT chunk has a valid length. */
1092 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001093 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 commands);
1095
1096 /* 8.3 The receiver of the HEARTBEAT should immediately
1097 * respond with a HEARTBEAT ACK that contains the Heartbeat
1098 * Information field copied from the received HEARTBEAT chunk.
1099 */
1100 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001101 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001103
1104 if (ntohs(param_hdr->length) > paylen)
1105 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1106 param_hdr, commands);
1107
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001108 if (!pskb_pull(chunk->skb, paylen))
1109 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Thomas Graf06a31e22012-11-30 02:16:27 +00001111 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!reply)
1113 goto nomem;
1114
1115 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1116 return SCTP_DISPOSITION_CONSUME;
1117
1118nomem:
1119 return SCTP_DISPOSITION_NOMEM;
1120}
1121
1122/*
1123 * Process the returning HEARTBEAT ACK.
1124 *
1125 * Section: 8.3 Path Heartbeat
1126 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1127 * should clear the error counter of the destination transport
1128 * address to which the HEARTBEAT was sent, and mark the destination
1129 * transport address as active if it is not so marked. The endpoint may
1130 * optionally report to the upper layer when an inactive destination
1131 * address is marked as active due to the reception of the latest
1132 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1133 * clear the association overall error count as well (as defined
1134 * in section 8.1).
1135 *
1136 * The receiver of the HEARTBEAT ACK should also perform an RTT
1137 * measurement for that destination transport address using the time
1138 * value carried in the HEARTBEAT ACK chunk.
1139 *
1140 * Verification Tag: 8.5 Verification Tag [Normal verification]
1141 *
1142 * Inputs
1143 * (endpoint, asoc, chunk)
1144 *
1145 * Outputs
1146 * (asoc, reply_msg, msg_up, timers, counters)
1147 *
1148 * The return value is the disposition of the chunk.
1149 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001150sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1151 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 const struct sctp_association *asoc,
1153 const sctp_subtype_t type,
1154 void *arg,
1155 sctp_cmd_seq_t *commands)
1156{
1157 struct sctp_chunk *chunk = arg;
1158 union sctp_addr from_addr;
1159 struct sctp_transport *link;
1160 sctp_sender_hb_info_t *hbinfo;
1161 unsigned long max_interval;
1162
1163 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001164 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001167 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1168 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001169 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 commands);
1171
1172 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001173 /* Make sure that the length of the parameter is what we expect */
1174 if (ntohs(hbinfo->param_hdr.length) !=
1175 sizeof(sctp_sender_hb_info_t)) {
1176 return SCTP_DISPOSITION_DISCARD;
1177 }
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001180 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001183 if (unlikely(!link)) {
1184 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001185 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1186 __func__,
1187 asoc,
1188 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001189 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001190 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1191 __func__,
1192 asoc,
1193 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return SCTP_DISPOSITION_DISCARD;
1196 }
1197
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001198 /* Validate the 64-bit random nonce. */
1199 if (hbinfo->hb_nonce != link->hb_nonce)
1200 return SCTP_DISPOSITION_DISCARD;
1201
Frank Filz52ccb8e2005-12-22 11:36:46 -08001202 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 /* Check if the timestamp looks valid. */
1205 if (time_after(hbinfo->sent_at, jiffies) ||
1206 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001207 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1208 "for transport:%p\n", __func__, link);
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return SCTP_DISPOSITION_DISCARD;
1211 }
1212
1213 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1214 * the HEARTBEAT should clear the error counter of the
1215 * destination transport address to which the HEARTBEAT was
1216 * sent and mark the destination transport address as active if
1217 * it is not so marked.
1218 */
1219 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1220
1221 return SCTP_DISPOSITION_CONSUME;
1222}
1223
1224/* Helper function to send out an abort for the restart
1225 * condition.
1226 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001227static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 struct sctp_chunk *init,
1229 sctp_cmd_seq_t *commands)
1230{
1231 int len;
1232 struct sctp_packet *pkt;
1233 union sctp_addr_param *addrparm;
1234 struct sctp_errhdr *errhdr;
1235 struct sctp_endpoint *ep;
1236 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1237 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1238
1239 /* Build the error on the stack. We are way to malloc crazy
1240 * throughout the code today.
1241 */
1242 errhdr = (struct sctp_errhdr *)buffer;
1243 addrparm = (union sctp_addr_param *)errhdr->variable;
1244
1245 /* Copy into a parm format. */
1246 len = af->to_addr_param(ssa, addrparm);
1247 len += sizeof(sctp_errhdr_t);
1248
1249 errhdr->cause = SCTP_ERROR_RESTART;
1250 errhdr->length = htons(len);
1251
1252 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001253 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255 /* Association is NULL since this may be a restart attack and we
1256 * want to send back the attacker's vtag.
1257 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001258 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (!pkt)
1261 goto out;
1262 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1263
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001264 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /* Discard the rest of the inbound packet. */
1267 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1268
1269out:
1270 /* Even if there is no memory, treat as a failure so
1271 * the packet will get dropped.
1272 */
1273 return 0;
1274}
1275
Joe Perches123031c2010-09-08 11:04:21 +00001276static bool list_has_sctp_addr(const struct list_head *list,
1277 union sctp_addr *ipaddr)
1278{
1279 struct sctp_transport *addr;
1280
1281 list_for_each_entry(addr, list, transports) {
1282 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1283 return true;
1284 }
1285
1286 return false;
1287}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/* A restart is occurring, check to make sure no new addresses
1289 * are being added as we may be under a takeover attack.
1290 */
1291static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1292 const struct sctp_association *asoc,
1293 struct sctp_chunk *init,
1294 sctp_cmd_seq_t *commands)
1295{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001296 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001297 struct sctp_transport *new_addr;
1298 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Joe Perches123031c2010-09-08 11:04:21 +00001300 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 * ...
1302 * Before responding the endpoint MUST check to see if the
1303 * unexpected INIT adds new addresses to the association. If new
1304 * addresses are added to the association, the endpoint MUST respond
1305 * with an ABORT..
1306 */
1307
1308 /* Search through all current addresses and make sure
1309 * we aren't adding any new ones.
1310 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001311 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001312 transports) {
1313 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1314 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001315 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001316 commands);
1317 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 break;
Joe Perches123031c2010-09-08 11:04:21 +00001319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
1322 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001323 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326/* Populate the verification/tie tags based on overlapping INIT
1327 * scenario.
1328 *
1329 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1330 */
1331static void sctp_tietags_populate(struct sctp_association *new_asoc,
1332 const struct sctp_association *asoc)
1333{
1334 switch (asoc->state) {
1335
1336 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1337
1338 case SCTP_STATE_COOKIE_WAIT:
1339 new_asoc->c.my_vtag = asoc->c.my_vtag;
1340 new_asoc->c.my_ttag = asoc->c.my_vtag;
1341 new_asoc->c.peer_ttag = 0;
1342 break;
1343
1344 case SCTP_STATE_COOKIE_ECHOED:
1345 new_asoc->c.my_vtag = asoc->c.my_vtag;
1346 new_asoc->c.my_ttag = asoc->c.my_vtag;
1347 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1348 break;
1349
1350 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1351 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1352 */
1353 default:
1354 new_asoc->c.my_ttag = asoc->c.my_vtag;
1355 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1356 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* Other parameters for the endpoint SHOULD be copied from the
1360 * existing parameters of the association (e.g. number of
1361 * outbound streams) into the INIT ACK and cookie.
1362 */
1363 new_asoc->rwnd = asoc->rwnd;
1364 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1365 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1366 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1367}
1368
1369/*
1370 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1371 * handling action.
1372 *
1373 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1374 *
1375 * Returns value representing action to be taken. These action values
1376 * correspond to Action/Description values in RFC 2960, Table 2.
1377 */
1378static char sctp_tietags_compare(struct sctp_association *new_asoc,
1379 const struct sctp_association *asoc)
1380{
1381 /* In this case, the peer may have restarted. */
1382 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1383 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1384 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1385 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1386 return 'A';
1387
1388 /* Collision case B. */
1389 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1390 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1391 (0 == asoc->c.peer_vtag))) {
1392 return 'B';
1393 }
1394
1395 /* Collision case D. */
1396 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1397 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1398 return 'D';
1399
1400 /* Collision case C. */
1401 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1402 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1403 (0 == new_asoc->c.my_ttag) &&
1404 (0 == new_asoc->c.peer_ttag))
1405 return 'C';
1406
1407 /* No match to any of the special cases; discard this packet. */
1408 return 'E';
1409}
1410
1411/* Common helper routine for both duplicate and simulataneous INIT
1412 * chunk handling.
1413 */
1414static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001415 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 const struct sctp_endpoint *ep,
1417 const struct sctp_association *asoc,
1418 const sctp_subtype_t type,
1419 void *arg, sctp_cmd_seq_t *commands)
1420{
1421 sctp_disposition_t retval;
1422 struct sctp_chunk *chunk = arg;
1423 struct sctp_chunk *repl;
1424 struct sctp_association *new_asoc;
1425 struct sctp_chunk *err_chunk;
1426 struct sctp_packet *packet;
1427 sctp_unrecognized_param_t *unk_param;
1428 int len;
1429
1430 /* 6.10 Bundling
1431 * An endpoint MUST NOT bundle INIT, INIT ACK or
1432 * SHUTDOWN COMPLETE with any other chunks.
1433 *
1434 * IG Section 2.11.2
1435 * Furthermore, we require that the receiver of an INIT chunk MUST
1436 * enforce these rules by silently discarding an arriving packet
1437 * with an INIT chunk that is bundled with other chunks.
1438 */
1439 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001440 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001443 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 */
1445 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001446 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 /* Make sure that the INIT chunk has a valid length.
1449 * In this case, we generate a protocol violation since we have
1450 * an association established.
1451 */
1452 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001453 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 commands);
1455 /* Grab the INIT header. */
1456 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1457
1458 /* Tag the variable length parameters. */
1459 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1460
1461 /* Verify the INIT chunk before processing it. */
1462 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001463 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1465 &err_chunk)) {
1466 /* This chunk contains fatal error. It is to be discarded.
1467 * Send an ABORT, with causes if there is any.
1468 */
1469 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001470 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 (__u8 *)(err_chunk->chunk_hdr) +
1472 sizeof(sctp_chunkhdr_t),
1473 ntohs(err_chunk->chunk_hdr->length) -
1474 sizeof(sctp_chunkhdr_t));
1475
1476 if (packet) {
1477 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1478 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001479 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 retval = SCTP_DISPOSITION_CONSUME;
1481 } else {
1482 retval = SCTP_DISPOSITION_NOMEM;
1483 }
1484 goto cleanup;
1485 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001486 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 commands);
1488 }
1489 }
1490
1491 /*
1492 * Other parameters for the endpoint SHOULD be copied from the
1493 * existing parameters of the association (e.g. number of
1494 * outbound streams) into the INIT ACK and cookie.
1495 * FIXME: We are copying parameters from the endpoint not the
1496 * association.
1497 */
1498 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1499 if (!new_asoc)
1500 goto nomem;
1501
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001502 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1503 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1504 goto nomem;
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /* In the outbound INIT ACK the endpoint MUST copy its current
1507 * Verification Tag and Peers Verification tag into a reserved
1508 * place (local tie-tag and per tie-tag) within the state cookie.
1509 */
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001510 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001512 GFP_ATOMIC))
1513 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /* Make sure no new addresses are being added during the
1516 * restart. Do not do this check for COOKIE-WAIT state,
1517 * since there are no peer addresses to check against.
1518 * Upon return an ABORT will have been sent if needed.
1519 */
1520 if (!sctp_state(asoc, COOKIE_WAIT)) {
1521 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1522 commands)) {
1523 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001524 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
1526 }
1527
1528 sctp_tietags_populate(new_asoc, asoc);
1529
1530 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1531
1532 /* If there are errors need to be reported for unknown parameters,
1533 * make sure to reserve enough room in the INIT ACK for them.
1534 */
1535 len = 0;
1536 if (err_chunk) {
1537 len = ntohs(err_chunk->chunk_hdr->length) -
1538 sizeof(sctp_chunkhdr_t);
1539 }
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1542 if (!repl)
1543 goto nomem;
1544
1545 /* If there are errors need to be reported for unknown parameters,
1546 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1547 * parameter.
1548 */
1549 if (err_chunk) {
1550 /* Get the "Unrecognized parameter" parameter(s) out of the
1551 * ERROR chunk generated by sctp_verify_init(). Since the
1552 * error cause code for "unknown parameter" and the
1553 * "Unrecognized parameter" type is the same, we can
1554 * construct the parameters in INIT ACK by copying the
1555 * ERROR causes over.
1556 */
1557 unk_param = (sctp_unrecognized_param_t *)
1558 ((__u8 *)(err_chunk->chunk_hdr) +
1559 sizeof(sctp_chunkhdr_t));
1560 /* Replace the cause code with the "Unrecognized parameter"
1561 * parameter type.
1562 */
1563 sctp_addto_chunk(repl, len, unk_param);
1564 }
1565
1566 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1567 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1568
1569 /*
1570 * Note: After sending out INIT ACK with the State Cookie parameter,
1571 * "Z" MUST NOT allocate any resources for this new association.
1572 * Otherwise, "Z" will be vulnerable to resource attacks.
1573 */
1574 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1575 retval = SCTP_DISPOSITION_CONSUME;
1576
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001577 return retval;
1578
1579nomem:
1580 retval = SCTP_DISPOSITION_NOMEM;
1581nomem_retval:
1582 if (new_asoc)
1583 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584cleanup:
1585 if (err_chunk)
1586 sctp_chunk_free(err_chunk);
1587 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
1590/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001591 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 * This means we started an INIT and then we got an INIT request from
1593 * our peer.
1594 *
1595 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1596 * This usually indicates an initialization collision, i.e., each
1597 * endpoint is attempting, at about the same time, to establish an
1598 * association with the other endpoint.
1599 *
1600 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1601 * endpoint MUST respond with an INIT ACK using the same parameters it
1602 * sent in its original INIT chunk (including its Verification Tag,
1603 * unchanged). These original parameters are combined with those from the
1604 * newly received INIT chunk. The endpoint shall also generate a State
1605 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1606 * INIT to calculate the State Cookie.
1607 *
1608 * After that, the endpoint MUST NOT change its state, the T1-init
1609 * timer shall be left running and the corresponding TCB MUST NOT be
1610 * destroyed. The normal procedures for handling State Cookies when
1611 * a TCB exists will resolve the duplicate INITs to a single association.
1612 *
1613 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1614 * its Tie-Tags with the Tag information of itself and its peer (see
1615 * section 5.2.2 for a description of the Tie-Tags).
1616 *
1617 * Verification Tag: Not explicit, but an INIT can not have a valid
1618 * verification tag, so we skip the check.
1619 *
1620 * Inputs
1621 * (endpoint, asoc, chunk)
1622 *
1623 * Outputs
1624 * (asoc, reply_msg, msg_up, timers, counters)
1625 *
1626 * The return value is the disposition of the chunk.
1627 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001628sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1629 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 const struct sctp_association *asoc,
1631 const sctp_subtype_t type,
1632 void *arg,
1633 sctp_cmd_seq_t *commands)
1634{
1635 /* Call helper to do the real work for both simulataneous and
1636 * duplicate INIT chunk handling.
1637 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001638 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
1641/*
1642 * Handle duplicated INIT messages. These are usually delayed
1643 * restransmissions.
1644 *
1645 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1646 * COOKIE-ECHOED and COOKIE-WAIT
1647 *
1648 * Unless otherwise stated, upon reception of an unexpected INIT for
1649 * this association, the endpoint shall generate an INIT ACK with a
1650 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1651 * current Verification Tag and peer's Verification Tag into a reserved
1652 * place within the state cookie. We shall refer to these locations as
1653 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1654 * containing this INIT ACK MUST carry a Verification Tag value equal to
1655 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1656 * MUST contain a new Initiation Tag (randomly generated see Section
1657 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1658 * existing parameters of the association (e.g. number of outbound
1659 * streams) into the INIT ACK and cookie.
1660 *
1661 * After sending out the INIT ACK, the endpoint shall take no further
1662 * actions, i.e., the existing association, including its current state,
1663 * and the corresponding TCB MUST NOT be changed.
1664 *
1665 * Note: Only when a TCB exists and the association is not in a COOKIE-
1666 * WAIT state are the Tie-Tags populated. For a normal association INIT
1667 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1668 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1669 * State Cookie are populated as specified in section 5.2.1.
1670 *
1671 * Verification Tag: Not specified, but an INIT has no way of knowing
1672 * what the verification tag could be, so we ignore it.
1673 *
1674 * Inputs
1675 * (endpoint, asoc, chunk)
1676 *
1677 * Outputs
1678 * (asoc, reply_msg, msg_up, timers, counters)
1679 *
1680 * The return value is the disposition of the chunk.
1681 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001682sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1683 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 const struct sctp_association *asoc,
1685 const sctp_subtype_t type,
1686 void *arg,
1687 sctp_cmd_seq_t *commands)
1688{
1689 /* Call helper to do the real work for both simulataneous and
1690 * duplicate INIT chunk handling.
1691 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001692 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695
Vlad Yasevich610ab732007-01-15 19:18:30 -08001696/*
1697 * Unexpected INIT-ACK handler.
1698 *
1699 * Section 5.2.3
1700 * If an INIT ACK received by an endpoint in any state other than the
1701 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1702 * An unexpected INIT ACK usually indicates the processing of an old or
1703 * duplicated INIT chunk.
1704*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001705sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1706 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001707 const struct sctp_association *asoc,
1708 const sctp_subtype_t type,
1709 void *arg, sctp_cmd_seq_t *commands)
1710{
1711 /* Per the above section, we'll discard the chunk if we have an
1712 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1713 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001714 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001715 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001716 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001717 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1721 *
1722 * Section 5.2.4
1723 * A) In this case, the peer may have restarted.
1724 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001725static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1726 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 const struct sctp_association *asoc,
1728 struct sctp_chunk *chunk,
1729 sctp_cmd_seq_t *commands,
1730 struct sctp_association *new_asoc)
1731{
1732 sctp_init_chunk_t *peer_init;
1733 struct sctp_ulpevent *ev;
1734 struct sctp_chunk *repl;
1735 struct sctp_chunk *err;
1736 sctp_disposition_t disposition;
1737
1738 /* new_asoc is a brand-new association, so these are not yet
1739 * side effects--it is safe to run them here.
1740 */
1741 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1742
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001743 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 GFP_ATOMIC))
1745 goto nomem;
1746
1747 /* Make sure no new addresses are being added during the
1748 * restart. Though this is a pretty complicated attack
1749 * since you'd have to get inside the cookie.
1750 */
1751 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1752 return SCTP_DISPOSITION_CONSUME;
1753 }
1754
1755 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1756 * the peer has restarted (Action A), it MUST NOT setup a new
1757 * association but instead resend the SHUTDOWN ACK and send an ERROR
1758 * chunk with a "Cookie Received while Shutting Down" error cause to
1759 * its peer.
1760 */
1761 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001762 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1764 chunk, commands);
1765 if (SCTP_DISPOSITION_NOMEM == disposition)
1766 goto nomem;
1767
1768 err = sctp_make_op_error(asoc, chunk,
1769 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001770 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (err)
1772 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1773 SCTP_CHUNK(err));
1774
1775 return SCTP_DISPOSITION_CONSUME;
1776 }
1777
Wei Yongjuna000c012011-05-29 23:23:36 +00001778 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1779 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001781 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1782 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1783 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1785
Wei Yongjuna000c012011-05-29 23:23:36 +00001786 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1787 * and ASCONF-ACK cache.
1788 */
1789 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1790 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1791 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 repl = sctp_make_cookie_ack(new_asoc, chunk);
1794 if (!repl)
1795 goto nomem;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /* Report association restart to upper layer. */
1798 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1799 new_asoc->c.sinit_num_ostreams,
1800 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001801 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (!ev)
1803 goto nomem_ev;
1804
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001805 /* Update the content of current association. */
1806 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001808 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1809 (sctp_sstate(asoc->base.sk, CLOSING) ||
1810 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1811 /* if were currently in SHUTDOWN_PENDING, but the socket
1812 * has been closed by user, don't transition to ESTABLISHED.
1813 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1814 */
1815 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1816 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1817 SCTP_ST_CHUNK(0), NULL,
1818 commands);
1819 } else {
1820 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1821 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1822 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return SCTP_DISPOSITION_CONSUME;
1825
1826nomem_ev:
1827 sctp_chunk_free(repl);
1828nomem:
1829 return SCTP_DISPOSITION_NOMEM;
1830}
1831
1832/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1833 *
1834 * Section 5.2.4
1835 * B) In this case, both sides may be attempting to start an association
1836 * at about the same time but the peer endpoint started its INIT
1837 * after responding to the local endpoint's INIT
1838 */
1839/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001840static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1841 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 const struct sctp_association *asoc,
1843 struct sctp_chunk *chunk,
1844 sctp_cmd_seq_t *commands,
1845 struct sctp_association *new_asoc)
1846{
1847 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct sctp_chunk *repl;
1849
1850 /* new_asoc is a brand-new association, so these are not yet
1851 * side effects--it is safe to run them here.
1852 */
1853 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becdc02011-04-19 21:30:51 +00001854 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 GFP_ATOMIC))
1856 goto nomem;
1857
1858 /* Update the content of current association. */
1859 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1860 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1861 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001862 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1864
1865 repl = sctp_make_cookie_ack(new_asoc, chunk);
1866 if (!repl)
1867 goto nomem;
1868
1869 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /* RFC 2960 5.1 Normal Establishment of an Association
1872 *
1873 * D) IMPLEMENTATION NOTE: An implementation may choose to
1874 * send the Communication Up notification to the SCTP user
1875 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001876 *
1877 * Sadly, this needs to be implemented as a side-effect, because
1878 * we are not guaranteed to have set the association id of the real
1879 * association and so these notifications need to be delayed until
1880 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Vlad Yasevich07d93962007-05-04 13:55:27 -07001883 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001886 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001888 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001889 *
1890 * This also needs to be done as a side effect for the same reason as
1891 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001893 if (asoc->peer.adaptation_ind)
1894 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 return SCTP_DISPOSITION_CONSUME;
1897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898nomem:
1899 return SCTP_DISPOSITION_NOMEM;
1900}
1901
1902/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1903 *
1904 * Section 5.2.4
1905 * C) In this case, the local endpoint's cookie has arrived late.
1906 * Before it arrived, the local endpoint sent an INIT and received an
1907 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1908 * but a new tag of its own.
1909 */
1910/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001911static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1912 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 const struct sctp_association *asoc,
1914 struct sctp_chunk *chunk,
1915 sctp_cmd_seq_t *commands,
1916 struct sctp_association *new_asoc)
1917{
1918 /* The cookie should be silently discarded.
1919 * The endpoint SHOULD NOT change states and should leave
1920 * any timers running.
1921 */
1922 return SCTP_DISPOSITION_DISCARD;
1923}
1924
1925/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1926 *
1927 * Section 5.2.4
1928 *
1929 * D) When both local and remote tags match the endpoint should always
1930 * enter the ESTABLISHED state, if it has not already done so.
1931 */
1932/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001933static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1934 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 const struct sctp_association *asoc,
1936 struct sctp_chunk *chunk,
1937 sctp_cmd_seq_t *commands,
1938 struct sctp_association *new_asoc)
1939{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001940 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 struct sctp_chunk *repl;
1942
1943 /* Clarification from Implementor's Guide:
1944 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001945 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1946 * It should stop any cookie timer that may be running and send
1947 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
1949
1950 /* Don't accidentally move back into established state. */
1951 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1952 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1953 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1954 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1955 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001956 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1958 SCTP_NULL());
1959
1960 /* RFC 2960 5.1 Normal Establishment of an Association
1961 *
1962 * D) IMPLEMENTATION NOTE: An implementation may choose
1963 * to send the Communication Up notification to the
1964 * SCTP user upon reception of a valid COOKIE
1965 * ECHO chunk.
1966 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001967 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001969 asoc->c.sinit_num_ostreams,
1970 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001971 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (!ev)
1973 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001976 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001978 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001980 if (asoc->peer.adaptation_ind) {
1981 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001983 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 goto nomem;
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 repl = sctp_make_cookie_ack(new_asoc, chunk);
1990 if (!repl)
1991 goto nomem;
1992
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001993 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1994
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001995 if (ev)
1996 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1997 SCTP_ULPEVENT(ev));
1998 if (ai_ev)
1999 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2000 SCTP_ULPEVENT(ai_ev));
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return SCTP_DISPOSITION_CONSUME;
2003
2004nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002005 if (ai_ev)
2006 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (ev)
2008 sctp_ulpevent_free(ev);
2009 return SCTP_DISPOSITION_NOMEM;
2010}
2011
2012/*
2013 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2014 * chunk was retransmitted and then delayed in the network.
2015 *
2016 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2017 *
2018 * Verification Tag: None. Do cookie validation.
2019 *
2020 * Inputs
2021 * (endpoint, asoc, chunk)
2022 *
2023 * Outputs
2024 * (asoc, reply_msg, msg_up, timers, counters)
2025 *
2026 * The return value is the disposition of the chunk.
2027 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002028sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2029 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 const struct sctp_association *asoc,
2031 const sctp_subtype_t type,
2032 void *arg,
2033 sctp_cmd_seq_t *commands)
2034{
2035 sctp_disposition_t retval;
2036 struct sctp_chunk *chunk = arg;
2037 struct sctp_association *new_asoc;
2038 int error = 0;
2039 char action;
2040 struct sctp_chunk *err_chk_p;
2041
2042 /* Make sure that the chunk has a valid length from the protocol
2043 * perspective. In this case check to make sure we have at least
2044 * enough for the chunk header. Cookie length verification is
2045 * done later.
2046 */
2047 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002048 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 commands);
2050
2051 /* "Decode" the chunk. We have no optional parameters so we
2052 * are in good shape.
2053 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002054 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002055 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2056 sizeof(sctp_chunkhdr_t)))
2057 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2060 * of a duplicate COOKIE ECHO match the Verification Tags of the
2061 * current association, consider the State Cookie valid even if
2062 * the lifespan is exceeded.
2063 */
2064 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2065 &err_chk_p);
2066
2067 /* FIXME:
2068 * If the re-build failed, what is the proper error path
2069 * from here?
2070 *
2071 * [We should abort the association. --piggy]
2072 */
2073 if (!new_asoc) {
2074 /* FIXME: Several errors are possible. A bad cookie should
2075 * be silently discarded, but think about logging it too.
2076 */
2077 switch (error) {
2078 case -SCTP_IERROR_NOMEM:
2079 goto nomem;
2080
2081 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002082 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002084 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 case -SCTP_IERROR_BAD_SIG:
2086 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002087 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090
2091 /* Compare the tie_tag in cookie with the verification tag of
2092 * current association.
2093 */
2094 action = sctp_tietags_compare(new_asoc, asoc);
2095
2096 switch (action) {
2097 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002098 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 new_asoc);
2100 break;
2101
2102 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002103 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 new_asoc);
2105 break;
2106
2107 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002108 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 new_asoc);
2110 break;
2111
2112 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002113 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 new_asoc);
2115 break;
2116
2117 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002118 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002123 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2125
Max Matveevd5ccd492011-08-29 21:02:24 +00002126 /* Restore association pointer to provide SCTP command interpeter
2127 * with a valid context in case it needs to manipulate
2128 * the queues */
2129 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2130 SCTP_ASOC((struct sctp_association *)asoc));
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return retval;
2133
2134nomem:
2135 return SCTP_DISPOSITION_NOMEM;
2136}
2137
2138/*
2139 * Process an ABORT. (SHUTDOWN-PENDING state)
2140 *
2141 * See sctp_sf_do_9_1_abort().
2142 */
2143sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002144 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 const struct sctp_endpoint *ep,
2146 const struct sctp_association *asoc,
2147 const sctp_subtype_t type,
2148 void *arg,
2149 sctp_cmd_seq_t *commands)
2150{
2151 struct sctp_chunk *chunk = arg;
2152
2153 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002154 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Make sure that the ABORT chunk has a valid length.
2157 * Since this is an ABORT chunk, we have to discard it
2158 * because of the following text:
2159 * RFC 2960, Section 3.3.7
2160 * If an endpoint receives an ABORT with a format error or for an
2161 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002162 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * as we do not know its true length. So, to be safe, discard the
2164 * packet.
2165 */
2166 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002167 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Vlad Yasevich75205f42007-12-20 14:12:59 -08002169 /* ADD-IP: Special case for ABORT chunks
2170 * F4) One special consideration is that ABORT Chunks arriving
2171 * destined to the IP address being deleted MUST be
2172 * ignored (see Section 5.3.1 for further details).
2173 */
2174 if (SCTP_ADDR_DEL ==
2175 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002176 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002177
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002178 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181/*
2182 * Process an ABORT. (SHUTDOWN-SENT state)
2183 *
2184 * See sctp_sf_do_9_1_abort().
2185 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002186sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2187 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 const struct sctp_association *asoc,
2189 const sctp_subtype_t type,
2190 void *arg,
2191 sctp_cmd_seq_t *commands)
2192{
2193 struct sctp_chunk *chunk = arg;
2194
2195 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002196 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /* Make sure that the ABORT chunk has a valid length.
2199 * Since this is an ABORT chunk, we have to discard it
2200 * because of the following text:
2201 * RFC 2960, Section 3.3.7
2202 * If an endpoint receives an ABORT with a format error or for an
2203 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002204 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 * as we do not know its true length. So, to be safe, discard the
2206 * packet.
2207 */
2208 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002209 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Vlad Yasevich75205f42007-12-20 14:12:59 -08002211 /* ADD-IP: Special case for ABORT chunks
2212 * F4) One special consideration is that ABORT Chunks arriving
2213 * destined to the IP address being deleted MUST be
2214 * ignored (see Section 5.3.1 for further details).
2215 */
2216 if (SCTP_ADDR_DEL ==
2217 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002218 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* Stop the T2-shutdown timer. */
2221 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2222 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2223
2224 /* Stop the T5-shutdown guard timer. */
2225 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2226 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2227
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002228 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229}
2230
2231/*
2232 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2233 *
2234 * See sctp_sf_do_9_1_abort().
2235 */
2236sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002237 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 const struct sctp_endpoint *ep,
2239 const struct sctp_association *asoc,
2240 const sctp_subtype_t type,
2241 void *arg,
2242 sctp_cmd_seq_t *commands)
2243{
2244 /* The same T2 timer, so we should be able to use
2245 * common function with the SHUTDOWN-SENT state.
2246 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002247 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248}
2249
2250/*
2251 * Handle an Error received in COOKIE_ECHOED state.
2252 *
2253 * Only handle the error type of stale COOKIE Error, the other errors will
2254 * be ignored.
2255 *
2256 * Inputs
2257 * (endpoint, asoc, chunk)
2258 *
2259 * Outputs
2260 * (asoc, reply_msg, msg_up, timers, counters)
2261 *
2262 * The return value is the disposition of the chunk.
2263 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002264sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2265 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 const struct sctp_association *asoc,
2267 const sctp_subtype_t type,
2268 void *arg,
2269 sctp_cmd_seq_t *commands)
2270{
2271 struct sctp_chunk *chunk = arg;
2272 sctp_errhdr_t *err;
2273
2274 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002275 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 /* Make sure that the ERROR chunk has a valid length.
2278 * The parameter walking depends on this as well.
2279 */
2280 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002281 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 commands);
2283
2284 /* Process the error here */
2285 /* FUTURE FIXME: When PR-SCTP related and other optional
2286 * parms are emitted, this will have to change to handle multiple
2287 * errors.
2288 */
2289 sctp_walk_errors(err, chunk->chunk_hdr) {
2290 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002291 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 arg, commands);
2293 }
2294
2295 /* It is possible to have malformed error causes, and that
2296 * will cause us to end the walk early. However, since
2297 * we are discarding the packet, there should be no adverse
2298 * affects.
2299 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002300 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/*
2304 * Handle a Stale COOKIE Error
2305 *
2306 * Section: 5.2.6 Handle Stale COOKIE Error
2307 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2308 * one of the following three alternatives.
2309 * ...
2310 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2311 * Preservative parameter requesting an extension to the lifetime of
2312 * the State Cookie. When calculating the time extension, an
2313 * implementation SHOULD use the RTT information measured based on the
2314 * previous COOKIE ECHO / ERROR exchange, and should add no more
2315 * than 1 second beyond the measured RTT, due to long State Cookie
2316 * lifetimes making the endpoint more subject to a replay attack.
2317 *
2318 * Verification Tag: Not explicit, but safe to ignore.
2319 *
2320 * Inputs
2321 * (endpoint, asoc, chunk)
2322 *
2323 * Outputs
2324 * (asoc, reply_msg, msg_up, timers, counters)
2325 *
2326 * The return value is the disposition of the chunk.
2327 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002328static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2329 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 const struct sctp_association *asoc,
2331 const sctp_subtype_t type,
2332 void *arg,
2333 sctp_cmd_seq_t *commands)
2334{
2335 struct sctp_chunk *chunk = arg;
Arnd Bergmann3ef0a252015-09-30 13:26:40 +02002336 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 sctp_cookie_preserve_param_t bht;
2338 sctp_errhdr_t *err;
2339 struct sctp_chunk *reply;
2340 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002341 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Vlad Yasevich81845c22006-01-30 15:59:54 -08002343 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002344 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2345 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002347 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return SCTP_DISPOSITION_DELETE_TCB;
2349 }
2350
2351 err = (sctp_errhdr_t *)(chunk->skb->data);
2352
2353 /* When calculating the time extension, an implementation
2354 * SHOULD use the RTT information measured based on the
2355 * previous COOKIE ECHO / ERROR exchange, and should add no
2356 * more than 1 second beyond the measured RTT, due to long
2357 * State Cookie lifetimes making the endpoint more subject to
2358 * a replay attack.
2359 * Measure of Staleness's unit is usec. (1/1000000 sec)
2360 * Suggested Cookie Life-span Increment's unit is msec.
2361 * (1/1000 sec)
2362 * In general, if you use the suggested cookie life, the value
2363 * found in the field of measure of staleness should be doubled
2364 * to give ample time to retransmit the new cookie and thus
2365 * yield a higher probability of success on the reattempt.
2366 */
Al Viro34bcca22006-11-20 17:27:15 -08002367 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 stale = (stale * 2) / 1000;
2369
2370 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2371 bht.param_hdr.length = htons(sizeof(bht));
2372 bht.lifespan_increment = htonl(stale);
2373
2374 /* Build that new INIT chunk. */
2375 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2376 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2377 if (!reply)
2378 goto nomem;
2379
2380 sctp_addto_chunk(reply, sizeof(bht), &bht);
2381
2382 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2383 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2384
2385 /* Stop pending T3-rtx and heartbeat timers */
2386 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2387 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2388
2389 /* Delete non-primary peer ip addresses since we are transitioning
2390 * back to the COOKIE-WAIT state
2391 */
2392 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2393
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002394 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2395 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002397 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 SCTP_TRANSPORT(asoc->peer.primary_path));
2399
2400 /* Cast away the const modifier, as we want to just
2401 * rerun it through as a sideffect.
2402 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002403 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2406 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2407 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2408 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2409 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2410 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2411
2412 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2413
2414 return SCTP_DISPOSITION_CONSUME;
2415
2416nomem:
2417 return SCTP_DISPOSITION_NOMEM;
2418}
2419
2420/*
2421 * Process an ABORT.
2422 *
2423 * Section: 9.1
2424 * After checking the Verification Tag, the receiving endpoint shall
2425 * remove the association from its record, and shall report the
2426 * termination to its upper layer.
2427 *
2428 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2429 * B) Rules for packet carrying ABORT:
2430 *
2431 * - The endpoint shall always fill in the Verification Tag field of the
2432 * outbound packet with the destination endpoint's tag value if it
2433 * is known.
2434 *
2435 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2436 * MUST follow the procedure described in Section 8.4.
2437 *
2438 * - The receiver MUST accept the packet if the Verification Tag
2439 * matches either its own tag, OR the tag of its peer. Otherwise, the
2440 * receiver MUST silently discard the packet and take no further
2441 * action.
2442 *
2443 * Inputs
2444 * (endpoint, asoc, chunk)
2445 *
2446 * Outputs
2447 * (asoc, reply_msg, msg_up, timers, counters)
2448 *
2449 * The return value is the disposition of the chunk.
2450 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002451sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2452 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 const struct sctp_association *asoc,
2454 const sctp_subtype_t type,
2455 void *arg,
2456 sctp_cmd_seq_t *commands)
2457{
2458 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
2460 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 /* Make sure that the ABORT chunk has a valid length.
2464 * Since this is an ABORT chunk, we have to discard it
2465 * because of the following text:
2466 * RFC 2960, Section 3.3.7
2467 * If an endpoint receives an ABORT with a format error or for an
2468 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002469 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 * as we do not know its true length. So, to be safe, discard the
2471 * packet.
2472 */
2473 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002474 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Vlad Yasevich75205f42007-12-20 14:12:59 -08002476 /* ADD-IP: Special case for ABORT chunks
2477 * F4) One special consideration is that ABORT Chunks arriving
2478 * destined to the IP address being deleted MUST be
2479 * ignored (see Section 5.3.1 for further details).
2480 */
2481 if (SCTP_ADDR_DEL ==
2482 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002483 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002484
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002485 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002486}
2487
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002488static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2489 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002490 const struct sctp_association *asoc,
2491 const sctp_subtype_t type,
2492 void *arg,
2493 sctp_cmd_seq_t *commands)
2494{
2495 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002496 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002497 __be16 error = SCTP_ERROR_NO_ERROR;
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 /* See if we have an error cause code in the chunk. */
2500 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002501 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2502
2503 sctp_errhdr_t *err;
2504 sctp_walk_errors(err, chunk->chunk_hdr);
2505 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002506 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002511 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002512 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002513 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002514 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2515 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517 return SCTP_DISPOSITION_ABORT;
2518}
2519
2520/*
2521 * Process an ABORT. (COOKIE-WAIT state)
2522 *
2523 * See sctp_sf_do_9_1_abort() above.
2524 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002525sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2526 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 const struct sctp_association *asoc,
2528 const sctp_subtype_t type,
2529 void *arg,
2530 sctp_cmd_seq_t *commands)
2531{
2532 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002533 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002534 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002537 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 /* Make sure that the ABORT chunk has a valid length.
2540 * Since this is an ABORT chunk, we have to discard it
2541 * because of the following text:
2542 * RFC 2960, Section 3.3.7
2543 * If an endpoint receives an ABORT with a format error or for an
2544 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002545 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 * as we do not know its true length. So, to be safe, discard the
2547 * packet.
2548 */
2549 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002550 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 /* See if we have an error cause code in the chunk. */
2553 len = ntohs(chunk->chunk_hdr->length);
2554 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2555 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2556
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002557 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002558 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559}
2560
2561/*
2562 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2563 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002564sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2565 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 const struct sctp_association *asoc,
2567 const sctp_subtype_t type,
2568 void *arg,
2569 sctp_cmd_seq_t *commands)
2570{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002571 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002572 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002573 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576/*
2577 * Process an ABORT. (COOKIE-ECHOED state)
2578 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002579sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2580 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 const struct sctp_association *asoc,
2582 const sctp_subtype_t type,
2583 void *arg,
2584 sctp_cmd_seq_t *commands)
2585{
2586 /* There is a single T1 timer, so we should be able to use
2587 * common function with the COOKIE-WAIT state.
2588 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002589 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
2591
2592/*
2593 * Stop T1 timer and abort association with "INIT failed".
2594 *
2595 * This is common code called by several sctp_sf_*_abort() functions above.
2596 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002597static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2598 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002599 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002600 const struct sctp_association *asoc,
2601 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002603 pr_debug("%s: ABORT received (INIT)\n", __func__);
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2606 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002607 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2609 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002610 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* CMD_INIT_FAILED will DELETE_TCB. */
2612 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002613 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002614
Frank Filz3f7a87d2005-06-20 13:14:57 -07002615 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
2618/*
2619 * sctp_sf_do_9_2_shut
2620 *
2621 * Section: 9.2
2622 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2623 * - enter the SHUTDOWN-RECEIVED state,
2624 *
2625 * - stop accepting new data from its SCTP user
2626 *
2627 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2628 * that all its outstanding DATA chunks have been received by the
2629 * SHUTDOWN sender.
2630 *
2631 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2632 * send a SHUTDOWN in response to a ULP request. And should discard
2633 * subsequent SHUTDOWN chunks.
2634 *
2635 * If there are still outstanding DATA chunks left, the SHUTDOWN
2636 * receiver shall continue to follow normal data transmission
2637 * procedures defined in Section 6 until all outstanding DATA chunks
2638 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2639 * new data from its SCTP user.
2640 *
2641 * Verification Tag: 8.5 Verification Tag [Normal verification]
2642 *
2643 * Inputs
2644 * (endpoint, asoc, chunk)
2645 *
2646 * Outputs
2647 * (asoc, reply_msg, msg_up, timers, counters)
2648 *
2649 * The return value is the disposition of the chunk.
2650 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002651sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2652 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 const struct sctp_association *asoc,
2654 const sctp_subtype_t type,
2655 void *arg,
2656 sctp_cmd_seq_t *commands)
2657{
2658 struct sctp_chunk *chunk = arg;
2659 sctp_shutdownhdr_t *sdh;
2660 sctp_disposition_t disposition;
2661 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002662 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002665 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 /* Make sure that the SHUTDOWN chunk has a valid length. */
2668 if (!sctp_chunk_length_valid(chunk,
2669 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002670 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 commands);
2672
2673 /* Convert the elaborate header. */
2674 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2675 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2676 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002677 ctsn = ntohl(sdh->cum_tsn_ack);
2678
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002679 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002680 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2681 asoc->ctsn_ack_point);
2682
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002683 return SCTP_DISPOSITION_DISCARD;
2684 }
2685
Wei Yongjundf10eec2008-10-23 01:00:21 -07002686 /* If Cumulative TSN Ack beyond the max tsn currently
2687 * send, terminating the association and respond to the
2688 * sender with an ABORT.
2689 */
2690 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002691 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002693 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2694 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2695 * inform the application that it should cease sending data.
2696 */
2697 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2698 if (!ev) {
2699 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002700 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002701 }
2702 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2705 * - enter the SHUTDOWN-RECEIVED state,
2706 * - stop accepting new data from its SCTP user
2707 *
2708 * [This is implicit in the new state.]
2709 */
2710 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2711 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2712 disposition = SCTP_DISPOSITION_CONSUME;
2713
2714 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002715 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 arg, commands);
2717 }
2718
2719 if (SCTP_DISPOSITION_NOMEM == disposition)
2720 goto out;
2721
2722 /* - verify, by checking the Cumulative TSN Ack field of the
2723 * chunk, that all its outstanding DATA chunks have been
2724 * received by the SHUTDOWN sender.
2725 */
2726 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002727 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729out:
2730 return disposition;
2731}
2732
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002733/*
2734 * sctp_sf_do_9_2_shut_ctsn
2735 *
2736 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2737 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2738 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2739 * MUST be processed.
2740 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002741sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2742 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002743 const struct sctp_association *asoc,
2744 const sctp_subtype_t type,
2745 void *arg,
2746 sctp_cmd_seq_t *commands)
2747{
2748 struct sctp_chunk *chunk = arg;
2749 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002750 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002751
2752 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002753 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002754
2755 /* Make sure that the SHUTDOWN chunk has a valid length. */
2756 if (!sctp_chunk_length_valid(chunk,
2757 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002758 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002759 commands);
2760
2761 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002762 ctsn = ntohl(sdh->cum_tsn_ack);
2763
2764 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002765 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2766 asoc->ctsn_ack_point);
2767
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002768 return SCTP_DISPOSITION_DISCARD;
2769 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002770
2771 /* If Cumulative TSN Ack beyond the max tsn currently
2772 * send, terminating the association and respond to the
2773 * sender with an ABORT.
2774 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002775 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002776 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002777
2778 /* verify, by checking the Cumulative TSN Ack field of the
2779 * chunk, that all its outstanding DATA chunks have been
2780 * received by the SHUTDOWN sender.
2781 */
2782 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2783 SCTP_BE32(sdh->cum_tsn_ack));
2784
2785 return SCTP_DISPOSITION_CONSUME;
2786}
2787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788/* RFC 2960 9.2
2789 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2790 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2791 * transport addresses (either in the IP addresses or in the INIT chunk)
2792 * that belong to this association, it should discard the INIT chunk and
2793 * retransmit the SHUTDOWN ACK chunk.
2794 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002795sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2796 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 const struct sctp_association *asoc,
2798 const sctp_subtype_t type,
2799 void *arg,
2800 sctp_cmd_seq_t *commands)
2801{
2802 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2803 struct sctp_chunk *reply;
2804
Vlad Yasevichece25df2007-09-07 16:30:54 -04002805 /* Make sure that the chunk has a valid length */
2806 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002807 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002808 commands);
2809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /* Since we are not going to really process this INIT, there
2811 * is no point in verifying chunk boundries. Just generate
2812 * the SHUTDOWN ACK.
2813 */
2814 reply = sctp_make_shutdown_ack(asoc, chunk);
2815 if (NULL == reply)
2816 goto nomem;
2817
2818 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2819 * the T2-SHUTDOWN timer.
2820 */
2821 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2822
2823 /* and restart the T2-shutdown timer. */
2824 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2825 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2826
2827 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2828
2829 return SCTP_DISPOSITION_CONSUME;
2830nomem:
2831 return SCTP_DISPOSITION_NOMEM;
2832}
2833
2834/*
2835 * sctp_sf_do_ecn_cwr
2836 *
2837 * Section: Appendix A: Explicit Congestion Notification
2838 *
2839 * CWR:
2840 *
2841 * RFC 2481 details a specific bit for a sender to send in the header of
2842 * its next outbound TCP segment to indicate to its peer that it has
2843 * reduced its congestion window. This is termed the CWR bit. For
2844 * SCTP the same indication is made by including the CWR chunk.
2845 * This chunk contains one data element, i.e. the TSN number that
2846 * was sent in the ECNE chunk. This element represents the lowest
2847 * TSN number in the datagram that was originally marked with the
2848 * CE bit.
2849 *
2850 * Verification Tag: 8.5 Verification Tag [Normal verification]
2851 * Inputs
2852 * (endpoint, asoc, chunk)
2853 *
2854 * Outputs
2855 * (asoc, reply_msg, msg_up, timers, counters)
2856 *
2857 * The return value is the disposition of the chunk.
2858 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002859sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2860 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 const struct sctp_association *asoc,
2862 const sctp_subtype_t type,
2863 void *arg,
2864 sctp_cmd_seq_t *commands)
2865{
2866 sctp_cwrhdr_t *cwr;
2867 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002868 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002871 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002874 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2878 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2879
Al Viro34bcca22006-11-20 17:27:15 -08002880 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002883 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /* Stop sending ECNE. */
2885 sctp_add_cmd_sf(commands,
2886 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002887 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
2889 return SCTP_DISPOSITION_CONSUME;
2890}
2891
2892/*
2893 * sctp_sf_do_ecne
2894 *
2895 * Section: Appendix A: Explicit Congestion Notification
2896 *
2897 * ECN-Echo
2898 *
2899 * RFC 2481 details a specific bit for a receiver to send back in its
2900 * TCP acknowledgements to notify the sender of the Congestion
2901 * Experienced (CE) bit having arrived from the network. For SCTP this
2902 * same indication is made by including the ECNE chunk. This chunk
2903 * contains one data element, i.e. the lowest TSN associated with the IP
2904 * datagram marked with the CE bit.....
2905 *
2906 * Verification Tag: 8.5 Verification Tag [Normal verification]
2907 * Inputs
2908 * (endpoint, asoc, chunk)
2909 *
2910 * Outputs
2911 * (asoc, reply_msg, msg_up, timers, counters)
2912 *
2913 * The return value is the disposition of the chunk.
2914 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002915sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2916 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 const struct sctp_association *asoc,
2918 const sctp_subtype_t type,
2919 void *arg,
2920 sctp_cmd_seq_t *commands)
2921{
2922 sctp_ecnehdr_t *ecne;
2923 struct sctp_chunk *chunk = arg;
2924
2925 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002926 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002929 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 commands);
2931
2932 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2933 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2934
2935 /* If this is a newer ECNE than the last CWR packet we sent out */
2936 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2937 SCTP_U32(ntohl(ecne->lowest_tsn)));
2938
2939 return SCTP_DISPOSITION_CONSUME;
2940}
2941
2942/*
2943 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2944 *
2945 * The SCTP endpoint MUST always acknowledge the reception of each valid
2946 * DATA chunk.
2947 *
2948 * The guidelines on delayed acknowledgement algorithm specified in
2949 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2950 * acknowledgement SHOULD be generated for at least every second packet
2951 * (not every second DATA chunk) received, and SHOULD be generated within
2952 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2953 * situations it may be beneficial for an SCTP transmitter to be more
2954 * conservative than the algorithms detailed in this document allow.
2955 * However, an SCTP transmitter MUST NOT be more aggressive than the
2956 * following algorithms allow.
2957 *
2958 * A SCTP receiver MUST NOT generate more than one SACK for every
2959 * incoming packet, other than to update the offered window as the
2960 * receiving application consumes new data.
2961 *
2962 * Verification Tag: 8.5 Verification Tag [Normal verification]
2963 *
2964 * Inputs
2965 * (endpoint, asoc, chunk)
2966 *
2967 * Outputs
2968 * (asoc, reply_msg, msg_up, timers, counters)
2969 *
2970 * The return value is the disposition of the chunk.
2971 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002972sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2973 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 const struct sctp_association *asoc,
2975 const sctp_subtype_t type,
2976 void *arg,
2977 sctp_cmd_seq_t *commands)
2978{
2979 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002980 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 int error;
2982
2983 if (!sctp_vtag_verify(chunk, asoc)) {
2984 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2985 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002986 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
2989 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002990 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 commands);
2992
wangweidongcb3f8372013-12-23 12:16:50 +08002993 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 switch (error) {
2995 case SCTP_IERROR_NO_ERROR:
2996 break;
2997 case SCTP_IERROR_HIGH_TSN:
2998 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002999 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 goto discard_noforce;
3001 case SCTP_IERROR_DUP_TSN:
3002 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003003 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 goto discard_force;
3005 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003006 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003007 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003008 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003009 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 default:
3011 BUG();
3012 }
3013
Wei Yongjun6dc76942009-11-23 15:53:53 -05003014 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3015 force = SCTP_FORCE();
3016
Neil Horman9f70f462013-12-10 06:48:15 -05003017 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3019 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3020 }
3021
3022 /* If this is the last chunk in a packet, we need to count it
3023 * toward sack generation. Note that we need to SACK every
3024 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3025 * THEM. We elect to NOT generate SACK's if the chunk fails
3026 * the verification tag test.
3027 *
3028 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3029 *
3030 * The SCTP endpoint MUST always acknowledge the reception of
3031 * each valid DATA chunk.
3032 *
3033 * The guidelines on delayed acknowledgement algorithm
3034 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3035 * Specifically, an acknowledgement SHOULD be generated for at
3036 * least every second packet (not every second DATA chunk)
3037 * received, and SHOULD be generated within 200 ms of the
3038 * arrival of any unacknowledged DATA chunk. In some
3039 * situations it may be beneficial for an SCTP transmitter to
3040 * be more conservative than the algorithms detailed in this
3041 * document allow. However, an SCTP transmitter MUST NOT be
3042 * more aggressive than the following algorithms allow.
3043 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003044 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003045 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return SCTP_DISPOSITION_CONSUME;
3048
3049discard_force:
3050 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3051 *
3052 * When a packet arrives with duplicate DATA chunk(s) and with
3053 * no new DATA chunk(s), the endpoint MUST immediately send a
3054 * SACK with no delay. If a packet arrives with duplicate
3055 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3056 * MAY immediately send a SACK. Normally receipt of duplicate
3057 * DATA chunks will occur when the original SACK chunk was lost
3058 * and the peer's RTO has expired. The duplicate TSN number(s)
3059 * SHOULD be reported in the SACK as duplicate.
3060 */
3061 /* In our case, we split the MAY SACK advice up whether or not
3062 * the last chunk is a duplicate.'
3063 */
3064 if (chunk->end_of_packet)
3065 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3066 return SCTP_DISPOSITION_DISCARD;
3067
3068discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003069 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003070 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
3075/*
3076 * sctp_sf_eat_data_fast_4_4
3077 *
3078 * Section: 4 (4)
3079 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3080 * DATA chunks without delay.
3081 *
3082 * Verification Tag: 8.5 Verification Tag [Normal verification]
3083 * Inputs
3084 * (endpoint, asoc, chunk)
3085 *
3086 * Outputs
3087 * (asoc, reply_msg, msg_up, timers, counters)
3088 *
3089 * The return value is the disposition of the chunk.
3090 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003091sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3092 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 const struct sctp_association *asoc,
3094 const sctp_subtype_t type,
3095 void *arg,
3096 sctp_cmd_seq_t *commands)
3097{
3098 struct sctp_chunk *chunk = arg;
3099 int error;
3100
3101 if (!sctp_vtag_verify(chunk, asoc)) {
3102 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3103 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003104 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106
3107 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003108 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 commands);
3110
wangweidongcb3f8372013-12-23 12:16:50 +08003111 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 switch (error) {
3113 case SCTP_IERROR_NO_ERROR:
3114 case SCTP_IERROR_HIGH_TSN:
3115 case SCTP_IERROR_DUP_TSN:
3116 case SCTP_IERROR_IGNORE_TSN:
3117 case SCTP_IERROR_BAD_STREAM:
3118 break;
3119 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003120 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003121 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003122 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003123 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 default:
3125 BUG();
3126 }
3127
3128 /* Go a head and force a SACK, since we are shutting down. */
3129
3130 /* Implementor's Guide.
3131 *
3132 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3133 * respond to each received packet containing one or more DATA chunk(s)
3134 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3135 */
3136 if (chunk->end_of_packet) {
3137 /* We must delay the chunk creation since the cumulative
3138 * TSN has not been updated yet.
3139 */
3140 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3141 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3142 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3143 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3144 }
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return SCTP_DISPOSITION_CONSUME;
3147}
3148
3149/*
3150 * Section: 6.2 Processing a Received SACK
3151 * D) Any time a SACK arrives, the endpoint performs the following:
3152 *
3153 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3154 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3155 * increasing, a SACK whose Cumulative TSN Ack is less than the
3156 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3157 *
3158 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3159 * of bytes still outstanding after processing the Cumulative TSN Ack
3160 * and the Gap Ack Blocks.
3161 *
3162 * iii) If the SACK is missing a TSN that was previously
3163 * acknowledged via a Gap Ack Block (e.g., the data receiver
3164 * reneged on the data), then mark the corresponding DATA chunk
3165 * as available for retransmit: Mark it as missing for fast
3166 * retransmit as described in Section 7.2.4 and if no retransmit
3167 * timer is running for the destination address to which the DATA
3168 * chunk was originally transmitted, then T3-rtx is started for
3169 * that destination address.
3170 *
3171 * Verification Tag: 8.5 Verification Tag [Normal verification]
3172 *
3173 * Inputs
3174 * (endpoint, asoc, chunk)
3175 *
3176 * Outputs
3177 * (asoc, reply_msg, msg_up, timers, counters)
3178 *
3179 * The return value is the disposition of the chunk.
3180 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003181sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3182 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 const struct sctp_association *asoc,
3184 const sctp_subtype_t type,
3185 void *arg,
3186 sctp_cmd_seq_t *commands)
3187{
3188 struct sctp_chunk *chunk = arg;
3189 sctp_sackhdr_t *sackh;
3190 __u32 ctsn;
3191
3192 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003193 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194
3195 /* Make sure that the SACK chunk has a valid length. */
3196 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003197 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 commands);
3199
3200 /* Pull the SACK chunk from the data buffer */
3201 sackh = sctp_sm_pull_sack(chunk);
3202 /* Was this a bogus SACK? */
3203 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003204 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 chunk->subh.sack_hdr = sackh;
3206 ctsn = ntohl(sackh->cum_tsn_ack);
3207
3208 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3209 * Ack Point, then drop the SACK. Since Cumulative TSN
3210 * Ack is monotonically increasing, a SACK whose
3211 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3212 * Point indicates an out-of-order SACK.
3213 */
3214 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003215 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3216 asoc->ctsn_ack_point);
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return SCTP_DISPOSITION_DISCARD;
3219 }
3220
Wei Yongjunaecedea2007-08-02 16:57:44 +08003221 /* If Cumulative TSN Ack beyond the max tsn currently
3222 * send, terminating the association and respond to the
3223 * sender with an ABORT.
3224 */
3225 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003226 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003229 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 /* Note: We do the rest of the work on the PROCESS_SACK
3232 * sideeffect.
3233 */
3234 return SCTP_DISPOSITION_CONSUME;
3235}
3236
3237/*
3238 * Generate an ABORT in response to a packet.
3239 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003240 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003242 * 8) The receiver should respond to the sender of the OOTB packet with
3243 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3244 * MUST fill in the Verification Tag field of the outbound packet
3245 * with the value found in the Verification Tag field of the OOTB
3246 * packet and set the T-bit in the Chunk Flags to indicate that the
3247 * Verification Tag is reflected. After sending this ABORT, the
3248 * receiver of the OOTB packet shall discard the OOTB packet and take
3249 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 *
3251 * Verification Tag:
3252 *
3253 * The return value is the disposition of the chunk.
3254*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003255static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3256 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 const struct sctp_association *asoc,
3258 const sctp_subtype_t type,
3259 void *arg,
3260 sctp_cmd_seq_t *commands)
3261{
3262 struct sctp_packet *packet = NULL;
3263 struct sctp_chunk *chunk = arg;
3264 struct sctp_chunk *abort;
3265
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003266 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003267 if (!packet)
3268 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003270 /* Make an ABORT. The T bit will be set if the asoc
3271 * is NULL.
3272 */
3273 abort = sctp_make_abort(asoc, chunk, 0);
3274 if (!abort) {
3275 sctp_ootb_pkt_free(packet);
3276 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 }
3278
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003279 /* Reflect vtag if T-Bit is set */
3280 if (sctp_test_T_bit(abort))
3281 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3282
3283 /* Set the skb to the belonging sock for accounting. */
3284 abort->skb->sk = ep->base.sk;
3285
3286 sctp_packet_append_chunk(packet, abort);
3287
3288 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3289 SCTP_PACKET(packet));
3290
3291 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3292
3293 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3294 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
3297/*
3298 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3299 * event as ULP notification for each cause included in the chunk.
3300 *
3301 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3302 *
3303 * The return value is the disposition of the chunk.
3304*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003305sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3306 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 const struct sctp_association *asoc,
3308 const sctp_subtype_t type,
3309 void *arg,
3310 sctp_cmd_seq_t *commands)
3311{
3312 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003313 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 /* Make sure that the ERROR chunk has a valid length. */
3319 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003320 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003322 sctp_walk_errors(err, chunk->chunk_hdr);
3323 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003324 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003325 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Wei Yongjun3df26782009-03-02 06:46:51 +00003327 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3328 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
3333/*
3334 * Process an inbound SHUTDOWN ACK.
3335 *
3336 * From Section 9.2:
3337 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3338 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3339 * peer, and remove all record of the association.
3340 *
3341 * The return value is the disposition.
3342 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003343sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3344 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 const struct sctp_association *asoc,
3346 const sctp_subtype_t type,
3347 void *arg,
3348 sctp_cmd_seq_t *commands)
3349{
3350 struct sctp_chunk *chunk = arg;
3351 struct sctp_chunk *reply;
3352 struct sctp_ulpevent *ev;
3353
3354 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003355 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3358 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003359 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 /* 10.2 H) SHUTDOWN COMPLETE notification
3362 *
3363 * When SCTP completes the shutdown procedures (section 9.2) this
3364 * notification is passed to the upper layer.
3365 */
3366 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003367 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (!ev)
3369 goto nomem;
3370
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003371 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3372 reply = sctp_make_shutdown_complete(asoc, chunk);
3373 if (!reply)
3374 goto nomem_chunk;
3375
3376 /* Do all the commands now (after allocation), so that we
3377 * have consistent state if memory allocation failes
3378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3380
3381 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3382 * stop the T2-shutdown timer,
3383 */
3384 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3385 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3386
3387 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3388 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3391 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003392 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3393 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3395
3396 /* ...and remove all record of the association. */
3397 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3398 return SCTP_DISPOSITION_DELETE_TCB;
3399
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003400nomem_chunk:
3401 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402nomem:
3403 return SCTP_DISPOSITION_NOMEM;
3404}
3405
3406/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003407 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3408 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3410 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3411 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3412 * packet must fill in the Verification Tag field of the outbound
3413 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003414 * set the T-bit in the Chunk Flags to indicate that the Verification
3415 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 *
3417 * 8) The receiver should respond to the sender of the OOTB packet with
3418 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3419 * MUST fill in the Verification Tag field of the outbound packet
3420 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003421 * packet and set the T-bit in the Chunk Flags to indicate that the
3422 * Verification Tag is reflected. After sending this ABORT, the
3423 * receiver of the OOTB packet shall discard the OOTB packet and take
3424 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003426sctp_disposition_t sctp_sf_ootb(struct net *net,
3427 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 const struct sctp_association *asoc,
3429 const sctp_subtype_t type,
3430 void *arg,
3431 sctp_cmd_seq_t *commands)
3432{
3433 struct sctp_chunk *chunk = arg;
3434 struct sk_buff *skb = chunk->skb;
3435 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003436 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 __u8 *ch_end;
3438 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003439 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003441 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3444 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003445 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003447 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003448 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003450 /* Report violation if chunk len overflows */
3451 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3452 if (ch_end > skb_tail_pointer(skb))
3453 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3454 commands);
3455
Vlad Yasevichece25df2007-09-07 16:30:54 -04003456 /* Now that we know we at least have a chunk header,
3457 * do things that are type appropriate.
3458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3460 ootb_shut_ack = 1;
3461
3462 /* RFC 2960, Section 3.3.7
3463 * Moreover, under any circumstances, an endpoint that
3464 * receives an ABORT MUST NOT respond to that ABORT by
3465 * sending an ABORT of its own.
3466 */
3467 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003468 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003469
Shan Wei85c5ed42011-04-19 21:30:01 +00003470 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3471 * or a COOKIE ACK the SCTP Packet should be silently
3472 * discarded.
3473 */
3474
3475 if (SCTP_CID_COOKIE_ACK == ch->type)
3476 ootb_cookie_ack = 1;
3477
3478 if (SCTP_CID_ERROR == ch->type) {
3479 sctp_walk_errors(err, ch) {
3480 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3481 ootb_cookie_ack = 1;
3482 break;
3483 }
3484 }
3485 }
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003488 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003491 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003492 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003493 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003495 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
3498/*
3499 * Handle an "Out of the blue" SHUTDOWN ACK.
3500 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003501 * Section: 8.4 5, sctpimpguide 2.41.
3502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003504 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3505 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3506 * packet must fill in the Verification Tag field of the outbound
3507 * packet with the Verification Tag received in the SHUTDOWN ACK and
3508 * set the T-bit in the Chunk Flags to indicate that the Verification
3509 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 *
3511 * Inputs
3512 * (endpoint, asoc, type, arg, commands)
3513 *
3514 * Outputs
3515 * (sctp_disposition_t)
3516 *
3517 * The return value is the disposition of the chunk.
3518 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003519static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3520 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 const struct sctp_association *asoc,
3522 const sctp_subtype_t type,
3523 void *arg,
3524 sctp_cmd_seq_t *commands)
3525{
3526 struct sctp_packet *packet = NULL;
3527 struct sctp_chunk *chunk = arg;
3528 struct sctp_chunk *shut;
3529
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003530 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003531 if (!packet)
3532 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003534 /* Make an SHUTDOWN_COMPLETE.
3535 * The T bit will be set if the asoc is NULL.
3536 */
3537 shut = sctp_make_shutdown_complete(asoc, chunk);
3538 if (!shut) {
3539 sctp_ootb_pkt_free(packet);
3540 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
3542
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003543 /* Reflect vtag if T-Bit is set */
3544 if (sctp_test_T_bit(shut))
3545 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3546
3547 /* Set the skb to the belonging sock for accounting. */
3548 shut->skb->sk = ep->base.sk;
3549
3550 sctp_packet_append_chunk(packet, shut);
3551
3552 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3553 SCTP_PACKET(packet));
3554
3555 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3556
3557 /* If the chunk length is invalid, we don't want to process
3558 * the reset of the packet.
3559 */
3560 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3561 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3562
3563 /* We need to discard the rest of the packet to prevent
3564 * potential bomming attacks from additional bundled chunks.
3565 * This is documented in SCTP Threats ID.
3566 */
3567 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
3570/*
3571 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3572 *
3573 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3574 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3575 * procedures in section 8.4 SHOULD be followed, in other words it
3576 * should be treated as an Out Of The Blue packet.
3577 * [This means that we do NOT check the Verification Tag on these
3578 * chunks. --piggy ]
3579 *
3580 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003581sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3582 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 const struct sctp_association *asoc,
3584 const sctp_subtype_t type,
3585 void *arg,
3586 sctp_cmd_seq_t *commands)
3587{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003588 struct sctp_chunk *chunk = arg;
3589
3590 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3591 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003592 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003593 commands);
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 /* Although we do have an association in this case, it corresponds
3596 * to a restarted association. So the packet is treated as an OOTB
3597 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3598 * called with a NULL association.
3599 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003600 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003601
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003602 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
3605/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003606sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3607 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 const struct sctp_association *asoc,
3609 const sctp_subtype_t type, void *arg,
3610 sctp_cmd_seq_t *commands)
3611{
3612 struct sctp_chunk *chunk = arg;
3613 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003614 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 sctp_addiphdr_t *hdr;
3616 __u32 serial;
3617
3618 if (!sctp_vtag_verify(chunk, asoc)) {
3619 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3620 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003621 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 }
3623
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003624 /* ADD-IP: Section 4.1.1
3625 * This chunk MUST be sent in an authenticated way by using
3626 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3627 * is received unauthenticated it MUST be silently discarded as
3628 * described in [I-D.ietf-tsvwg-sctp-auth].
3629 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003630 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003631 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3634 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003635 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 commands);
3637
3638 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3639 serial = ntohl(hdr->serial);
3640
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003641 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003642 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003643 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003644 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003645
Vlad Yasevicha08de642007-12-20 14:11:47 -08003646 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003648 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 */
3650 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003651 /* If this is the first instance of ASCONF in the packet,
3652 * we can clean our old ASCONF-ACKs.
3653 */
3654 if (!chunk->has_asconf)
3655 sctp_assoc_clean_asconf_ack_cache(asoc);
3656
3657 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3658 * expected, process the ASCONF as described below and after
3659 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3660 * the response packet and cache a copy of it (in the event it
3661 * later needs to be retransmitted).
3662 *
3663 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 */
3665 asconf_ack = sctp_process_asconf((struct sctp_association *)
3666 asoc, chunk);
3667 if (!asconf_ack)
3668 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003669 } else if (serial < asoc->peer.addip_serial + 1) {
3670 /* ADDIP 5.2 E2)
3671 * If the value found in the Sequence Number is less than the
3672 * ('Peer- Sequence-Number' + 1), simply skip to the next
3673 * ASCONF, and include in the outbound response packet
3674 * any previously cached ASCONF-ACK response that was
3675 * sent and saved that matches the Sequence Number of the
3676 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3677 * Chunk exists. This will occur when an older ASCONF
3678 * arrives out of order. In such a case, the receiver
3679 * should skip the ASCONF Chunk and not include ASCONF-ACK
3680 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003682 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3683 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003685
3686 /* Reset the transport so that we select the correct one
3687 * this time around. This is to make sure that we don't
3688 * accidentally use a stale transport that's been removed.
3689 */
3690 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003692 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return SCTP_DISPOSITION_DISCARD;
3696 }
3697
Vlad Yasevicha08de642007-12-20 14:11:47 -08003698 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3699 * containing the ASCONF-ACK Chunks MUST be the source address of
3700 * the SCTP packet that held the ASCONF Chunks.
3701 *
3702 * To do this properly, we'll set the destination address of the chunk
3703 * and at the transmit time, will try look up the transport to use.
3704 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003705 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003707 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003709 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003710 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003711 ((struct sctp_association *)asoc)->new_transport = NULL;
3712 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return SCTP_DISPOSITION_CONSUME;
3715}
3716
3717/*
3718 * ADDIP Section 4.3 General rules for address manipulation
3719 * When building TLV parameters for the ASCONF Chunk that will add or
3720 * delete IP addresses the D0 to D13 rules should be applied:
3721 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003722sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3723 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003725 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 sctp_cmd_seq_t *commands)
3727{
3728 struct sctp_chunk *asconf_ack = arg;
3729 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3730 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003731 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 sctp_addiphdr_t *addip_hdr;
3733 __u32 sent_serial, rcvd_serial;
3734
3735 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3736 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3737 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003738 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
3740
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003741 /* ADD-IP, Section 4.1.2:
3742 * This chunk MUST be sent in an authenticated way by using
3743 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3744 * is received unauthenticated it MUST be silently discarded as
3745 * described in [I-D.ietf-tsvwg-sctp-auth].
3746 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003747 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003748 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003749
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 /* Make sure that the ADDIP chunk has a valid length. */
3751 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003752 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 commands);
3754
3755 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3756 rcvd_serial = ntohl(addip_hdr->serial);
3757
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003758 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003759 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003760 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003761 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003762
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (last_asconf) {
3764 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3765 sent_serial = ntohl(addip_hdr->serial);
3766 } else {
3767 sent_serial = asoc->addip_serial - 1;
3768 }
3769
3770 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3771 * equal to the next serial number to be used but no ASCONF chunk is
3772 * outstanding the endpoint MUST ABORT the association. Note that a
3773 * sequence number is greater than if it is no more than 2^^31-1
3774 * larger than the current sequence number (using serial arithmetic).
3775 */
3776 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3777 !(asoc->addip_last_asconf)) {
3778 abort = sctp_make_abort(asoc, asconf_ack,
3779 sizeof(sctp_errhdr_t));
3780 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003781 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3783 SCTP_CHUNK(abort));
3784 }
3785 /* We are going to ABORT, so we might as well stop
3786 * processing the rest of the chunks in the packet.
3787 */
3788 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3789 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003790 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003791 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003792 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003794 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003795 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3796 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 return SCTP_DISPOSITION_ABORT;
3798 }
3799
3800 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3801 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3802 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3803
3804 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003805 asconf_ack)) {
3806 /* Successfully processed ASCONF_ACK. We can
3807 * release the next asconf if we have one.
3808 */
3809 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3810 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
3814 abort = sctp_make_abort(asoc, asconf_ack,
3815 sizeof(sctp_errhdr_t));
3816 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003817 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3819 SCTP_CHUNK(abort));
3820 }
3821 /* We are going to ABORT, so we might as well stop
3822 * processing the rest of the chunks in the packet.
3823 */
wangweidongcb3f8372013-12-23 12:16:50 +08003824 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003825 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003826 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003828 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003829 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3830 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return SCTP_DISPOSITION_ABORT;
3832 }
3833
3834 return SCTP_DISPOSITION_DISCARD;
3835}
3836
3837/*
3838 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3839 *
3840 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3841 * its cumulative TSN point to the value carried in the FORWARD TSN
3842 * chunk, and then MUST further advance its cumulative TSN point locally
3843 * if possible.
3844 * After the above processing, the data receiver MUST stop reporting any
3845 * missing TSNs earlier than or equal to the new cumulative TSN point.
3846 *
3847 * Verification Tag: 8.5 Verification Tag [Normal verification]
3848 *
3849 * The return value is the disposition of the chunk.
3850 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003851sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3852 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 const struct sctp_association *asoc,
3854 const sctp_subtype_t type,
3855 void *arg,
3856 sctp_cmd_seq_t *commands)
3857{
3858 struct sctp_chunk *chunk = arg;
3859 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003860 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 __u16 len;
3862 __u32 tsn;
3863
3864 if (!sctp_vtag_verify(chunk, asoc)) {
3865 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3866 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003867 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 }
3869
Xin Longd15c9ed2017-02-03 17:37:06 +08003870 if (!asoc->peer.prsctp_capable)
3871 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 /* Make sure that the FORWARD_TSN chunk has valid length. */
3874 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003875 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 commands);
3877
3878 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3879 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3880 len = ntohs(chunk->chunk_hdr->length);
3881 len -= sizeof(struct sctp_chunkhdr);
3882 skb_pull(chunk->skb, len);
3883
3884 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003885 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887 /* The TSN is too high--silently discard the chunk and count on it
3888 * getting retransmitted later.
3889 */
3890 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3891 goto discard_noforce;
3892
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003893 /* Silently discard the chunk if stream-id is not valid */
3894 sctp_walk_fwdtsn(skip, chunk) {
3895 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3896 goto discard_noforce;
3897 }
3898
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3900 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003901 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003905 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3907 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3908 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003909
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003911 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 */
3913 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915 return SCTP_DISPOSITION_CONSUME;
3916
3917discard_noforce:
3918 return SCTP_DISPOSITION_DISCARD;
3919}
3920
3921sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003922 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 const struct sctp_endpoint *ep,
3924 const struct sctp_association *asoc,
3925 const sctp_subtype_t type,
3926 void *arg,
3927 sctp_cmd_seq_t *commands)
3928{
3929 struct sctp_chunk *chunk = arg;
3930 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003931 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 __u16 len;
3933 __u32 tsn;
3934
3935 if (!sctp_vtag_verify(chunk, asoc)) {
3936 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3937 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003938 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 }
3940
Xin Longd15c9ed2017-02-03 17:37:06 +08003941 if (!asoc->peer.prsctp_capable)
3942 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
3943
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3945 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003946 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 commands);
3948
3949 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3950 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3951 len = ntohs(chunk->chunk_hdr->length);
3952 len -= sizeof(struct sctp_chunkhdr);
3953 skb_pull(chunk->skb, len);
3954
3955 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003956 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 /* The TSN is too high--silently discard the chunk and count on it
3959 * getting retransmitted later.
3960 */
3961 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3962 goto gen_shutdown;
3963
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003964 /* Silently discard the chunk if stream-id is not valid */
3965 sctp_walk_fwdtsn(skip, chunk) {
3966 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3967 goto gen_shutdown;
3968 }
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3971 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003972 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 /* Go a head and force a SACK, since we are shutting down. */
3976gen_shutdown:
3977 /* Implementor's Guide.
3978 *
3979 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3980 * respond to each received packet containing one or more DATA chunk(s)
3981 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3982 */
3983 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3984 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3985 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3986 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3987
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003988 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989}
3990
3991/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003992 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003993 *
3994 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3995 * Identifier field. If this algorithm was not specified by the
3996 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3997 * during association setup, the AUTH chunk and all chunks after it MUST
3998 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3999 * defined in Section 4.1.
4000 *
4001 * If an endpoint with no shared key receives a Shared Key Identifier
4002 * other than 0, it MUST silently discard all authenticated chunks. If
4003 * the endpoint has at least one endpoint pair shared key for the peer,
4004 * it MUST use the key specified by the Shared Key Identifier if a
4005 * key has been configured for that Shared Key Identifier. If no
4006 * endpoint pair shared key has been configured for that Shared Key
4007 * Identifier, all authenticated chunks MUST be silently discarded.
4008 *
4009 * Verification Tag: 8.5 Verification Tag [Normal verification]
4010 *
4011 * The return value is the disposition of the chunk.
4012 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004013static sctp_ierror_t sctp_sf_authenticate(struct net *net,
4014 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004015 const struct sctp_association *asoc,
4016 const sctp_subtype_t type,
4017 struct sctp_chunk *chunk)
4018{
4019 struct sctp_authhdr *auth_hdr;
4020 struct sctp_hmac *hmac;
4021 unsigned int sig_len;
4022 __u16 key_id;
4023 __u8 *save_digest;
4024 __u8 *digest;
4025
4026 /* Pull in the auth header, so we can do some more verification */
4027 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4028 chunk->subh.auth_hdr = auth_hdr;
4029 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4030
Masanari Iida02582e92012-08-22 19:11:26 +09004031 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004032 * chunk.
4033 */
4034 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4035 return SCTP_IERROR_AUTH_BAD_HMAC;
4036
4037 /* Make sure that the provided shared key identifier has been
4038 * configured
4039 */
4040 key_id = ntohs(auth_hdr->shkey_id);
4041 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4042 return SCTP_IERROR_AUTH_BAD_KEYID;
4043
4044
4045 /* Make sure that the length of the signature matches what
4046 * we expect.
4047 */
4048 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4049 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4050 if (sig_len != hmac->hmac_len)
4051 return SCTP_IERROR_PROTO_VIOLATION;
4052
4053 /* Now that we've done validation checks, we can compute and
4054 * verify the hmac. The steps involved are:
4055 * 1. Save the digest from the chunk.
4056 * 2. Zero out the digest in the chunk.
4057 * 3. Compute the new digest
4058 * 4. Compare saved and new digests.
4059 */
4060 digest = auth_hdr->hmac;
4061 skb_pull(chunk->skb, sig_len);
4062
4063 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4064 if (!save_digest)
4065 goto nomem;
4066
4067 memset(digest, 0, sig_len);
4068
4069 sctp_auth_calculate_hmac(asoc, chunk->skb,
4070 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4071 GFP_ATOMIC);
4072
4073 /* Discard the packet if the digests do not match */
4074 if (memcmp(save_digest, digest, sig_len)) {
4075 kfree(save_digest);
4076 return SCTP_IERROR_BAD_SIG;
4077 }
4078
4079 kfree(save_digest);
4080 chunk->auth = 1;
4081
4082 return SCTP_IERROR_NO_ERROR;
4083nomem:
4084 return SCTP_IERROR_NOMEM;
4085}
4086
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004087sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4088 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004089 const struct sctp_association *asoc,
4090 const sctp_subtype_t type,
4091 void *arg,
4092 sctp_cmd_seq_t *commands)
4093{
4094 struct sctp_authhdr *auth_hdr;
4095 struct sctp_chunk *chunk = arg;
4096 struct sctp_chunk *err_chunk;
4097 sctp_ierror_t error;
4098
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004099 /* Make sure that the peer has AUTH capable */
4100 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004101 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004102
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004103 if (!sctp_vtag_verify(chunk, asoc)) {
4104 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4105 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004106 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004107 }
4108
4109 /* Make sure that the AUTH chunk has valid length. */
4110 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004111 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004112 commands);
4113
4114 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004115 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004116 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004117 case SCTP_IERROR_AUTH_BAD_HMAC:
4118 /* Generate the ERROR chunk and discard the rest
4119 * of the packet
4120 */
4121 err_chunk = sctp_make_op_error(asoc, chunk,
4122 SCTP_ERROR_UNSUP_HMAC,
4123 &auth_hdr->hmac_id,
4124 sizeof(__u16), 0);
4125 if (err_chunk) {
4126 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4127 SCTP_CHUNK(err_chunk));
4128 }
4129 /* Fall Through */
4130 case SCTP_IERROR_AUTH_BAD_KEYID:
4131 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004132 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004133
4134 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004135 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004136 commands);
4137
4138 case SCTP_IERROR_NOMEM:
4139 return SCTP_DISPOSITION_NOMEM;
4140
4141 default: /* Prevent gcc warnings */
4142 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004143 }
4144
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004145 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4146 struct sctp_ulpevent *ev;
4147
4148 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4149 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4150
4151 if (!ev)
4152 return -ENOMEM;
4153
4154 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4155 SCTP_ULPEVENT(ev));
4156 }
4157
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004158 return SCTP_DISPOSITION_CONSUME;
4159}
4160
4161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 * Process an unknown chunk.
4163 *
4164 * Section: 3.2. Also, 2.1 in the implementor's guide.
4165 *
4166 * Chunk Types are encoded such that the highest-order two bits specify
4167 * the action that must be taken if the processing endpoint does not
4168 * recognize the Chunk Type.
4169 *
4170 * 00 - Stop processing this SCTP packet and discard it, do not process
4171 * any further chunks within it.
4172 *
4173 * 01 - Stop processing this SCTP packet and discard it, do not process
4174 * any further chunks within it, and report the unrecognized
4175 * chunk in an 'Unrecognized Chunk Type'.
4176 *
4177 * 10 - Skip this chunk and continue processing.
4178 *
4179 * 11 - Skip this chunk and continue processing, but report in an ERROR
4180 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4181 *
4182 * The return value is the disposition of the chunk.
4183 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004184sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4185 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 const struct sctp_association *asoc,
4187 const sctp_subtype_t type,
4188 void *arg,
4189 sctp_cmd_seq_t *commands)
4190{
4191 struct sctp_chunk *unk_chunk = arg;
4192 struct sctp_chunk *err_chunk;
4193 sctp_chunkhdr_t *hdr;
4194
Daniel Borkmannbb333812013-06-28 19:49:40 +02004195 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
4197 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004198 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 /* Make sure that the chunk has a valid length.
4201 * Since we don't know the chunk type, we use a general
4202 * chunkhdr structure to make a comparison.
4203 */
4204 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004205 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 commands);
4207
4208 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4209 case SCTP_CID_ACTION_DISCARD:
4210 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004211 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 /* Generate an ERROR chunk as response. */
4214 hdr = unk_chunk->chunk_hdr;
4215 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4216 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004217 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004218 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 if (err_chunk) {
4220 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4221 SCTP_CHUNK(err_chunk));
4222 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004223
4224 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004225 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 case SCTP_CID_ACTION_SKIP:
4228 /* Skip the chunk. */
4229 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 case SCTP_CID_ACTION_SKIP_ERR:
4231 /* Generate an ERROR chunk as response. */
4232 hdr = unk_chunk->chunk_hdr;
4233 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4234 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004235 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004236 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 if (err_chunk) {
4238 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4239 SCTP_CHUNK(err_chunk));
4240 }
4241 /* Skip the chunk. */
4242 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 default:
4244 break;
4245 }
4246
4247 return SCTP_DISPOSITION_DISCARD;
4248}
4249
4250/*
4251 * Discard the chunk.
4252 *
4253 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4254 * [Too numerous to mention...]
4255 * Verification Tag: No verification needed.
4256 * Inputs
4257 * (endpoint, asoc, chunk)
4258 *
4259 * Outputs
4260 * (asoc, reply_msg, msg_up, timers, counters)
4261 *
4262 * The return value is the disposition of the chunk.
4263 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004264sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4265 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 const struct sctp_association *asoc,
4267 const sctp_subtype_t type,
4268 void *arg,
4269 sctp_cmd_seq_t *commands)
4270{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004271 struct sctp_chunk *chunk = arg;
4272
4273 /* Make sure that the chunk has a valid length.
4274 * Since we don't know the chunk type, we use a general
4275 * chunkhdr structure to make a comparison.
4276 */
4277 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004278 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004279 commands);
4280
Daniel Borkmannbb333812013-06-28 19:49:40 +02004281 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 return SCTP_DISPOSITION_DISCARD;
4284}
4285
4286/*
4287 * Discard the whole packet.
4288 *
4289 * Section: 8.4 2)
4290 *
4291 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4292 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 *
4294 * Verification Tag: No verification necessary
4295 *
4296 * Inputs
4297 * (endpoint, asoc, chunk)
4298 *
4299 * Outputs
4300 * (asoc, reply_msg, msg_up, timers, counters)
4301 *
4302 * The return value is the disposition of the chunk.
4303 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004304sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4305 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 const struct sctp_association *asoc,
4307 const sctp_subtype_t type,
4308 void *arg,
4309 sctp_cmd_seq_t *commands)
4310{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004311 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4313
4314 return SCTP_DISPOSITION_CONSUME;
4315}
4316
4317
4318/*
4319 * The other end is violating protocol.
4320 *
4321 * Section: Not specified
4322 * Verification Tag: Not specified
4323 * Inputs
4324 * (endpoint, asoc, chunk)
4325 *
4326 * Outputs
4327 * (asoc, reply_msg, msg_up, timers, counters)
4328 *
4329 * We simply tag the chunk as a violation. The state machine will log
4330 * the violation and continue.
4331 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004332sctp_disposition_t sctp_sf_violation(struct net *net,
4333 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 const struct sctp_association *asoc,
4335 const sctp_subtype_t type,
4336 void *arg,
4337 sctp_cmd_seq_t *commands)
4338{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004339 struct sctp_chunk *chunk = arg;
4340
4341 /* Make sure that the chunk has a valid length. */
4342 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004343 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004344 commands);
4345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return SCTP_DISPOSITION_VIOLATION;
4347}
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004350 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004352static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004353 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004354 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004357 sctp_cmd_seq_t *commands,
4358 const __u8 *payload,
4359 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004361 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 struct sctp_chunk *chunk = arg;
4363 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004365 /* SCTP-AUTH, Section 6.3:
4366 * It should be noted that if the receiver wants to tear
4367 * down an association in an authenticated way only, the
4368 * handling of malformed packets should not result in
4369 * tearing down the association.
4370 *
4371 * This means that if we only want to abort associations
4372 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004373 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004374 * was malformed.
4375 */
4376 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4377 goto discard;
4378
Jesper Juhl9abed242007-11-11 23:57:49 +01004379 /* Make the abort chunk. */
4380 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4381 if (!abort)
4382 goto nomem;
4383
Vlad Yasevichece25df2007-09-07 16:30:54 -04004384 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004385 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4386 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4387 !asoc->peer.i.init_tag) {
4388 sctp_initack_chunk_t *initack;
4389
4390 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4391 if (!sctp_chunk_length_valid(chunk,
4392 sizeof(sctp_initack_chunk_t)))
4393 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4394 else {
4395 unsigned int inittag;
4396
4397 inittag = ntohl(initack->init_hdr.init_tag);
4398 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4399 SCTP_U32(inittag));
4400 }
4401 }
4402
Vlad Yasevichece25df2007-09-07 16:30:54 -04004403 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004404 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Vlad Yasevichece25df2007-09-07 16:30:54 -04004406 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4407 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4408 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4409 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4410 SCTP_ERROR(ECONNREFUSED));
4411 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4412 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4413 } else {
4414 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4415 SCTP_ERROR(ECONNABORTED));
4416 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4417 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004418 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004421 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004422
4423 if (!packet)
4424 goto nomem_pkt;
4425
4426 if (sctp_test_T_bit(abort))
4427 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4428
4429 abort->skb->sk = ep->base.sk;
4430
4431 sctp_packet_append_chunk(packet, abort);
4432
4433 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4434 SCTP_PACKET(packet));
4435
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004436 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 }
4438
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004439 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004440
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004441discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004442 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 return SCTP_DISPOSITION_ABORT;
4444
Vlad Yasevichece25df2007-09-07 16:30:54 -04004445nomem_pkt:
4446 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447nomem:
4448 return SCTP_DISPOSITION_NOMEM;
4449}
4450
Wei Yongjunaecedea2007-08-02 16:57:44 +08004451/*
4452 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004453 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004454 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004455 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004456 *
4457 * We inform the other end by sending an ABORT with a Protocol Violation
4458 * error code.
4459 *
4460 * Section: Not specified
4461 * Verification Tag: Nothing to do
4462 * Inputs
4463 * (endpoint, asoc, chunk)
4464 *
4465 * Outputs
4466 * (reply_msg, msg_up, counters)
4467 *
4468 * Generate an ABORT chunk and terminate the association.
4469 */
4470static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004471 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004472 const struct sctp_endpoint *ep,
4473 const struct sctp_association *asoc,
4474 const sctp_subtype_t type,
4475 void *arg,
4476 sctp_cmd_seq_t *commands)
4477{
wangweidongcb3f8372013-12-23 12:16:50 +08004478 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004479
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004480 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004481 sizeof(err_str));
4482}
4483
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004484/*
4485 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004486 * If the length is smaller than the minimum length of a given parameter,
4487 * or accumulated length in multi parameters exceeds the end of the chunk,
4488 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004489 */
4490static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004491 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004492 const struct sctp_endpoint *ep,
4493 const struct sctp_association *asoc,
4494 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004495 void *arg, void *ext,
4496 sctp_cmd_seq_t *commands)
4497{
4498 struct sctp_chunk *chunk = arg;
4499 struct sctp_paramhdr *param = ext;
4500 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004501
Wei Yongjunba016672008-09-30 05:32:24 -07004502 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4503 goto discard;
4504
4505 /* Make the abort chunk. */
4506 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4507 if (!abort)
4508 goto nomem;
4509
4510 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004511 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004512
4513 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4514 SCTP_ERROR(ECONNABORTED));
4515 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4516 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004517 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4518 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004519
4520discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004521 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004522 return SCTP_DISPOSITION_ABORT;
4523nomem:
4524 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004525}
4526
Wei Yongjunaecedea2007-08-02 16:57:44 +08004527/* Handle a protocol violation when the peer trying to advance the
4528 * cumulative tsn ack to a point beyond the max tsn currently sent.
4529 *
4530 * We inform the other end by sending an ABORT with a Protocol Violation
4531 * error code.
4532 */
4533static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004534 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004535 const struct sctp_endpoint *ep,
4536 const struct sctp_association *asoc,
4537 const sctp_subtype_t type,
4538 void *arg,
4539 sctp_cmd_seq_t *commands)
4540{
wangweidongcb3f8372013-12-23 12:16:50 +08004541 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004542
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004543 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004544 sizeof(err_str));
4545}
4546
Vlad Yasevichece25df2007-09-07 16:30:54 -04004547/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004548 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004549 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004550 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004551 * on the path and we may not want to continue this communication.
4552 */
4553static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004554 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004555 const struct sctp_endpoint *ep,
4556 const struct sctp_association *asoc,
4557 const sctp_subtype_t type,
4558 void *arg,
4559 sctp_cmd_seq_t *commands)
4560{
wangweidongcb3f8372013-12-23 12:16:50 +08004561 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004562
4563 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004564 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004565
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004566 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004567 sizeof(err_str));
4568}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569/***************************************************************************
4570 * These are the state functions for handling primitive (Section 10) events.
4571 ***************************************************************************/
4572/*
4573 * sctp_sf_do_prm_asoc
4574 *
4575 * Section: 10.1 ULP-to-SCTP
4576 * B) Associate
4577 *
4578 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4579 * outbound stream count)
4580 * -> association id [,destination transport addr list] [,outbound stream
4581 * count]
4582 *
4583 * This primitive allows the upper layer to initiate an association to a
4584 * specific peer endpoint.
4585 *
4586 * The peer endpoint shall be specified by one of the transport addresses
4587 * which defines the endpoint (see Section 1.4). If the local SCTP
4588 * instance has not been initialized, the ASSOCIATE is considered an
4589 * error.
4590 * [This is not relevant for the kernel implementation since we do all
4591 * initialization at boot time. It we hadn't initialized we wouldn't
4592 * get anywhere near this code.]
4593 *
4594 * An association id, which is a local handle to the SCTP association,
4595 * will be returned on successful establishment of the association. If
4596 * SCTP is not able to open an SCTP association with the peer endpoint,
4597 * an error is returned.
4598 * [In the kernel implementation, the struct sctp_association needs to
4599 * be created BEFORE causing this primitive to run.]
4600 *
4601 * Other association parameters may be returned, including the
4602 * complete destination transport addresses of the peer as well as the
4603 * outbound stream count of the local endpoint. One of the transport
4604 * address from the returned destination addresses will be selected by
4605 * the local endpoint as default primary path for sending SCTP packets
4606 * to this peer. The returned "destination transport addr list" can
4607 * be used by the ULP to change the default primary path or to force
4608 * sending a packet to a specific transport address. [All of this
4609 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4610 * function.]
4611 *
4612 * Mandatory attributes:
4613 *
4614 * o local SCTP instance name - obtained from the INITIALIZE operation.
4615 * [This is the argument asoc.]
4616 * o destination transport addr - specified as one of the transport
4617 * addresses of the peer endpoint with which the association is to be
4618 * established.
4619 * [This is asoc->peer.active_path.]
4620 * o outbound stream count - the number of outbound streams the ULP
4621 * would like to open towards this peer endpoint.
4622 * [BUG: This is not currently implemented.]
4623 * Optional attributes:
4624 *
4625 * None.
4626 *
4627 * The return value is a disposition.
4628 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004629sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4630 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 const struct sctp_association *asoc,
4632 const sctp_subtype_t type,
4633 void *arg,
4634 sctp_cmd_seq_t *commands)
4635{
4636 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004637 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
4639 /* The comment below says that we enter COOKIE-WAIT AFTER
4640 * sending the INIT, but that doesn't actually work in our
4641 * implementation...
4642 */
4643 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4644 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4645
4646 /* RFC 2960 5.1 Normal Establishment of an Association
4647 *
4648 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4649 * must provide its Verification Tag (Tag_A) in the Initiate
4650 * Tag field. Tag_A SHOULD be a random number in the range of
4651 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4652 */
4653
4654 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4655 if (!repl)
4656 goto nomem;
4657
Daniel Borkmann405426f2013-06-14 18:24:05 +02004658 /* Choose transport for INIT. */
4659 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4660 SCTP_CHUNK(repl));
4661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 /* Cast away the const modifier, as we want to just
4663 * rerun it through as a sideffect.
4664 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004665 my_asoc = (struct sctp_association *)asoc;
4666 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
4668 /* After sending the INIT, "A" starts the T1-init timer and
4669 * enters the COOKIE-WAIT state.
4670 */
4671 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4672 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4673 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4674 return SCTP_DISPOSITION_CONSUME;
4675
4676nomem:
4677 return SCTP_DISPOSITION_NOMEM;
4678}
4679
4680/*
4681 * Process the SEND primitive.
4682 *
4683 * Section: 10.1 ULP-to-SCTP
4684 * E) Send
4685 *
4686 * Format: SEND(association id, buffer address, byte count [,context]
4687 * [,stream id] [,life time] [,destination transport address]
4688 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4689 * -> result
4690 *
4691 * This is the main method to send user data via SCTP.
4692 *
4693 * Mandatory attributes:
4694 *
4695 * o association id - local handle to the SCTP association
4696 *
4697 * o buffer address - the location where the user message to be
4698 * transmitted is stored;
4699 *
4700 * o byte count - The size of the user data in number of bytes;
4701 *
4702 * Optional attributes:
4703 *
4704 * o context - an optional 32 bit integer that will be carried in the
4705 * sending failure notification to the ULP if the transportation of
4706 * this User Message fails.
4707 *
4708 * o stream id - to indicate which stream to send the data on. If not
4709 * specified, stream 0 will be used.
4710 *
4711 * o life time - specifies the life time of the user data. The user data
4712 * will not be sent by SCTP after the life time expires. This
4713 * parameter can be used to avoid efforts to transmit stale
4714 * user messages. SCTP notifies the ULP if the data cannot be
4715 * initiated to transport (i.e. sent to the destination via SCTP's
4716 * send primitive) within the life time variable. However, the
4717 * user data will be transmitted if SCTP has attempted to transmit a
4718 * chunk before the life time expired.
4719 *
4720 * o destination transport address - specified as one of the destination
4721 * transport addresses of the peer endpoint to which this packet
4722 * should be sent. Whenever possible, SCTP should use this destination
4723 * transport address for sending the packets, instead of the current
4724 * primary path.
4725 *
4726 * o unorder flag - this flag, if present, indicates that the user
4727 * would like the data delivered in an unordered fashion to the peer
4728 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4729 * message).
4730 *
4731 * o no-bundle flag - instructs SCTP not to bundle this user data with
4732 * other outbound DATA chunks. SCTP MAY still bundle even when
4733 * this flag is present, when faced with network congestion.
4734 *
4735 * o payload protocol-id - A 32 bit unsigned integer that is to be
4736 * passed to the peer indicating the type of payload protocol data
4737 * being transmitted. This value is passed as opaque data by SCTP.
4738 *
4739 * The return value is the disposition.
4740 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004741sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4742 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 const struct sctp_association *asoc,
4744 const sctp_subtype_t type,
4745 void *arg,
4746 sctp_cmd_seq_t *commands)
4747{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004748 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004750 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 return SCTP_DISPOSITION_CONSUME;
4752}
4753
4754/*
4755 * Process the SHUTDOWN primitive.
4756 *
4757 * Section: 10.1:
4758 * C) Shutdown
4759 *
4760 * Format: SHUTDOWN(association id)
4761 * -> result
4762 *
4763 * Gracefully closes an association. Any locally queued user data
4764 * will be delivered to the peer. The association will be terminated only
4765 * after the peer acknowledges all the SCTP packets sent. A success code
4766 * will be returned on successful termination of the association. If
4767 * attempting to terminate the association results in a failure, an error
4768 * code shall be returned.
4769 *
4770 * Mandatory attributes:
4771 *
4772 * o association id - local handle to the SCTP association
4773 *
4774 * Optional attributes:
4775 *
4776 * None.
4777 *
4778 * The return value is the disposition.
4779 */
4780sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004781 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 const struct sctp_endpoint *ep,
4783 const struct sctp_association *asoc,
4784 const sctp_subtype_t type,
4785 void *arg,
4786 sctp_cmd_seq_t *commands)
4787{
4788 int disposition;
4789
4790 /* From 9.2 Shutdown of an Association
4791 * Upon receipt of the SHUTDOWN primitive from its upper
4792 * layer, the endpoint enters SHUTDOWN-PENDING state and
4793 * remains there until all outstanding data has been
4794 * acknowledged by its peer. The endpoint accepts no new data
4795 * from its upper layer, but retransmits data to the far end
4796 * if necessary to fill gaps.
4797 */
4798 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4799 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 disposition = SCTP_DISPOSITION_CONSUME;
4802 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004803 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 arg, commands);
4805 }
4806 return disposition;
4807}
4808
4809/*
4810 * Process the ABORT primitive.
4811 *
4812 * Section: 10.1:
4813 * C) Abort
4814 *
4815 * Format: Abort(association id [, cause code])
4816 * -> result
4817 *
4818 * Ungracefully closes an association. Any locally queued user data
4819 * will be discarded and an ABORT chunk is sent to the peer. A success code
4820 * will be returned on successful abortion of the association. If
4821 * attempting to abort the association results in a failure, an error
4822 * code shall be returned.
4823 *
4824 * Mandatory attributes:
4825 *
4826 * o association id - local handle to the SCTP association
4827 *
4828 * Optional attributes:
4829 *
4830 * o cause code - reason of the abort to be passed to the peer
4831 *
4832 * None.
4833 *
4834 * The return value is the disposition.
4835 */
4836sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004837 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 const struct sctp_endpoint *ep,
4839 const struct sctp_association *asoc,
4840 const sctp_subtype_t type,
4841 void *arg,
4842 sctp_cmd_seq_t *commands)
4843{
4844 /* From 9.1 Abort of an Association
4845 * Upon receipt of the ABORT primitive from its upper
4846 * layer, the endpoint enters CLOSED state and
4847 * discard all outstanding data has been
4848 * acknowledged by its peer. The endpoint accepts no new data
4849 * from its upper layer, but retransmits data to the far end
4850 * if necessary to fill gaps.
4851 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004852 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
Xin Long068d8bd2015-12-29 17:49:25 +08004854 if (abort)
4855 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856
4857 /* Even if we can't send the ABORT due to low memory delete the
4858 * TCB. This is a departure from our typical NOMEM handling.
4859 */
4860
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004861 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4862 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 /* Delete the established association. */
4864 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004865 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004867 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4868 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004870 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871}
4872
4873/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004874sctp_disposition_t sctp_sf_error_closed(struct net *net,
4875 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 const struct sctp_association *asoc,
4877 const sctp_subtype_t type,
4878 void *arg,
4879 sctp_cmd_seq_t *commands)
4880{
4881 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4882 return SCTP_DISPOSITION_CONSUME;
4883}
4884
4885/* We tried an illegal operation on an association which is shutting
4886 * down.
4887 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004888sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4889 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 const struct sctp_association *asoc,
4891 const sctp_subtype_t type,
4892 void *arg,
4893 sctp_cmd_seq_t *commands)
4894{
4895 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4896 SCTP_ERROR(-ESHUTDOWN));
4897 return SCTP_DISPOSITION_CONSUME;
4898}
4899
4900/*
4901 * sctp_cookie_wait_prm_shutdown
4902 *
4903 * Section: 4 Note: 2
4904 * Verification Tag:
4905 * Inputs
4906 * (endpoint, asoc)
4907 *
4908 * The RFC does not explicitly address this issue, but is the route through the
4909 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4910 *
4911 * Outputs
4912 * (timers)
4913 */
4914sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004915 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 const struct sctp_endpoint *ep,
4917 const struct sctp_association *asoc,
4918 const sctp_subtype_t type,
4919 void *arg,
4920 sctp_cmd_seq_t *commands)
4921{
4922 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4923 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4924
4925 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4926 SCTP_STATE(SCTP_STATE_CLOSED));
4927
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004928 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
4930 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4931
4932 return SCTP_DISPOSITION_DELETE_TCB;
4933}
4934
4935/*
4936 * sctp_cookie_echoed_prm_shutdown
4937 *
4938 * Section: 4 Note: 2
4939 * Verification Tag:
4940 * Inputs
4941 * (endpoint, asoc)
4942 *
4943 * The RFC does not explcitly address this issue, but is the route through the
4944 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4945 *
4946 * Outputs
4947 * (timers)
4948 */
4949sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004950 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 const struct sctp_endpoint *ep,
4952 const struct sctp_association *asoc,
4953 const sctp_subtype_t type,
4954 void *arg, sctp_cmd_seq_t *commands)
4955{
4956 /* There is a single T1 timer, so we should be able to use
4957 * common function with the COOKIE-WAIT state.
4958 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004959 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960}
4961
4962/*
4963 * sctp_sf_cookie_wait_prm_abort
4964 *
4965 * Section: 4 Note: 2
4966 * Verification Tag:
4967 * Inputs
4968 * (endpoint, asoc)
4969 *
4970 * The RFC does not explicitly address this issue, but is the route through the
4971 * state table when someone issues an abort while in COOKIE_WAIT state.
4972 *
4973 * Outputs
4974 * (timers)
4975 */
4976sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004977 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 const struct sctp_endpoint *ep,
4979 const struct sctp_association *asoc,
4980 const sctp_subtype_t type,
4981 void *arg,
4982 sctp_cmd_seq_t *commands)
4983{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004984 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 /* Stop T1-init timer */
4987 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4988 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Xin Long068d8bd2015-12-29 17:49:25 +08004990 if (abort)
4991 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
4993 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4994 SCTP_STATE(SCTP_STATE_CLOSED));
4995
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004996 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
4998 /* Even if we can't send the ABORT due to low memory delete the
4999 * TCB. This is a departure from our typical NOMEM handling.
5000 */
5001
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005002 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5003 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 /* Delete the established association. */
5005 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005006 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005008 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
5011/*
5012 * sctp_sf_cookie_echoed_prm_abort
5013 *
5014 * Section: 4 Note: 3
5015 * Verification Tag:
5016 * Inputs
5017 * (endpoint, asoc)
5018 *
5019 * The RFC does not explcitly address this issue, but is the route through the
5020 * state table when someone issues an abort while in COOKIE_ECHOED state.
5021 *
5022 * Outputs
5023 * (timers)
5024 */
5025sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005026 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 const struct sctp_endpoint *ep,
5028 const struct sctp_association *asoc,
5029 const sctp_subtype_t type,
5030 void *arg,
5031 sctp_cmd_seq_t *commands)
5032{
5033 /* There is a single T1 timer, so we should be able to use
5034 * common function with the COOKIE-WAIT state.
5035 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005036 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
5039/*
5040 * sctp_sf_shutdown_pending_prm_abort
5041 *
5042 * Inputs
5043 * (endpoint, asoc)
5044 *
5045 * The RFC does not explicitly address this issue, but is the route through the
5046 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5047 *
5048 * Outputs
5049 * (timers)
5050 */
5051sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005052 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 const struct sctp_endpoint *ep,
5054 const struct sctp_association *asoc,
5055 const sctp_subtype_t type,
5056 void *arg,
5057 sctp_cmd_seq_t *commands)
5058{
5059 /* Stop the T5-shutdown guard timer. */
5060 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5061 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5062
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005063 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
5066/*
5067 * sctp_sf_shutdown_sent_prm_abort
5068 *
5069 * Inputs
5070 * (endpoint, asoc)
5071 *
5072 * The RFC does not explicitly address this issue, but is the route through the
5073 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5074 *
5075 * Outputs
5076 * (timers)
5077 */
5078sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005079 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 const struct sctp_endpoint *ep,
5081 const struct sctp_association *asoc,
5082 const sctp_subtype_t type,
5083 void *arg,
5084 sctp_cmd_seq_t *commands)
5085{
5086 /* Stop the T2-shutdown timer. */
5087 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5088 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5089
5090 /* Stop the T5-shutdown guard timer. */
5091 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5092 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5093
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005094 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095}
5096
5097/*
5098 * sctp_sf_cookie_echoed_prm_abort
5099 *
5100 * Inputs
5101 * (endpoint, asoc)
5102 *
5103 * The RFC does not explcitly address this issue, but is the route through the
5104 * state table when someone issues an abort while in COOKIE_ECHOED state.
5105 *
5106 * Outputs
5107 * (timers)
5108 */
5109sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005110 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 const struct sctp_endpoint *ep,
5112 const struct sctp_association *asoc,
5113 const sctp_subtype_t type,
5114 void *arg,
5115 sctp_cmd_seq_t *commands)
5116{
5117 /* The same T2 timer, so we should be able to use
5118 * common function with the SHUTDOWN-SENT state.
5119 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005120 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121}
5122
5123/*
5124 * Process the REQUESTHEARTBEAT primitive
5125 *
5126 * 10.1 ULP-to-SCTP
5127 * J) Request Heartbeat
5128 *
5129 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5130 *
5131 * -> result
5132 *
5133 * Instructs the local endpoint to perform a HeartBeat on the specified
5134 * destination transport address of the given association. The returned
5135 * result should indicate whether the transmission of the HEARTBEAT
5136 * chunk to the destination address is successful.
5137 *
5138 * Mandatory attributes:
5139 *
5140 * o association id - local handle to the SCTP association
5141 *
5142 * o destination transport address - the transport address of the
5143 * association on which a heartbeat should be issued.
5144 */
5145sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005146 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 const struct sctp_endpoint *ep,
5148 const struct sctp_association *asoc,
5149 const sctp_subtype_t type,
5150 void *arg,
5151 sctp_cmd_seq_t *commands)
5152{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005153 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5154 (struct sctp_transport *)arg, commands))
5155 return SCTP_DISPOSITION_NOMEM;
5156
5157 /*
5158 * RFC 2960 (bis), section 8.3
5159 *
5160 * D) Request an on-demand HEARTBEAT on a specific destination
5161 * transport address of a given association.
5162 *
5163 * The endpoint should increment the respective error counter of
5164 * the destination transport address each time a HEARTBEAT is sent
5165 * to that address and not acknowledged within one RTO.
5166 *
5167 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005168 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005169 SCTP_TRANSPORT(arg));
5170 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171}
5172
5173/*
5174 * ADDIP Section 4.1 ASCONF Chunk Procedures
5175 * When an endpoint has an ASCONF signaled change to be sent to the
5176 * remote endpoint it should do A1 to A9
5177 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005178sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5179 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 const struct sctp_association *asoc,
5181 const sctp_subtype_t type,
5182 void *arg,
5183 sctp_cmd_seq_t *commands)
5184{
5185 struct sctp_chunk *chunk = arg;
5186
5187 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5188 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5189 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5190 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5191 return SCTP_DISPOSITION_CONSUME;
5192}
5193
Xin Long7a090b02017-01-18 00:44:44 +08005194/* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5195sctp_disposition_t sctp_sf_do_prm_reconf(struct net *net,
5196 const struct sctp_endpoint *ep,
5197 const struct sctp_association *asoc,
5198 const sctp_subtype_t type,
5199 void *arg, sctp_cmd_seq_t *commands)
5200{
5201 struct sctp_chunk *chunk = arg;
5202
5203 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5204 return SCTP_DISPOSITION_CONSUME;
5205}
5206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207/*
5208 * Ignore the primitive event
5209 *
5210 * The return value is the disposition of the primitive.
5211 */
5212sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005213 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 const struct sctp_endpoint *ep,
5215 const struct sctp_association *asoc,
5216 const sctp_subtype_t type,
5217 void *arg,
5218 sctp_cmd_seq_t *commands)
5219{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005220 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5221 type.primitive);
5222
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 return SCTP_DISPOSITION_DISCARD;
5224}
5225
5226/***************************************************************************
5227 * These are the state functions for the OTHER events.
5228 ***************************************************************************/
5229
5230/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005231 * When the SCTP stack has no more user data to send or retransmit, this
5232 * notification is given to the user. Also, at the time when a user app
5233 * subscribes to this event, if there is no data to be sent or
5234 * retransmit, the stack will immediately send up this notification.
5235 */
5236sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005237 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005238 const struct sctp_endpoint *ep,
5239 const struct sctp_association *asoc,
5240 const sctp_subtype_t type,
5241 void *arg,
5242 sctp_cmd_seq_t *commands)
5243{
5244 struct sctp_ulpevent *event;
5245
5246 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5247 if (!event)
5248 return SCTP_DISPOSITION_NOMEM;
5249
5250 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5251
5252 return SCTP_DISPOSITION_CONSUME;
5253}
5254
5255/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 * Start the shutdown negotiation.
5257 *
5258 * From Section 9.2:
5259 * Once all its outstanding data has been acknowledged, the endpoint
5260 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5261 * TSN Ack field the last sequential TSN it has received from the peer.
5262 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5263 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5264 * with the updated last sequential TSN received from its peer.
5265 *
5266 * The return value is the disposition.
5267 */
5268sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005269 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 const struct sctp_endpoint *ep,
5271 const struct sctp_association *asoc,
5272 const sctp_subtype_t type,
5273 void *arg,
5274 sctp_cmd_seq_t *commands)
5275{
5276 struct sctp_chunk *reply;
5277
5278 /* Once all its outstanding data has been acknowledged, the
5279 * endpoint shall send a SHUTDOWN chunk to its peer including
5280 * in the Cumulative TSN Ack field the last sequential TSN it
5281 * has received from the peer.
5282 */
5283 reply = sctp_make_shutdown(asoc, NULL);
5284 if (!reply)
5285 goto nomem;
5286
5287 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5288 * T2-shutdown timer.
5289 */
5290 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5291
5292 /* It shall then start the T2-shutdown timer */
5293 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5294 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5295
Wei Yongjun536428a2008-09-05 08:55:26 +08005296 /* RFC 4960 Section 9.2
5297 * The sender of the SHUTDOWN MAY also start an overall guard timer
5298 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5299 */
Thomas Graff8d96052011-07-07 00:28:35 +00005300 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005301 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5302
Neil Horman9f70f462013-12-10 06:48:15 -05005303 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5305 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5306
5307 /* and enter the SHUTDOWN-SENT state. */
5308 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5309 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5310
5311 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5312 *
5313 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005314 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 */
5316 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5317
5318 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5319
5320 return SCTP_DISPOSITION_CONSUME;
5321
5322nomem:
5323 return SCTP_DISPOSITION_NOMEM;
5324}
5325
5326/*
5327 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5328 *
5329 * From Section 9.2:
5330 *
5331 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5332 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5333 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5334 * endpoint must re-send the SHUTDOWN ACK.
5335 *
5336 * The return value is the disposition.
5337 */
5338sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005339 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 const struct sctp_endpoint *ep,
5341 const struct sctp_association *asoc,
5342 const sctp_subtype_t type,
5343 void *arg,
5344 sctp_cmd_seq_t *commands)
5345{
5346 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5347 struct sctp_chunk *reply;
5348
5349 /* There are 2 ways of getting here:
5350 * 1) called in response to a SHUTDOWN chunk
5351 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5352 *
5353 * For the case (2), the arg parameter is set to NULL. We need
5354 * to check that we have a chunk before accessing it's fields.
5355 */
5356 if (chunk) {
5357 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
5360 /* Make sure that the SHUTDOWN chunk has a valid length. */
5361 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005362 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 commands);
5364 }
5365
5366 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5367 * shall send a SHUTDOWN ACK ...
5368 */
5369 reply = sctp_make_shutdown_ack(asoc, chunk);
5370 if (!reply)
5371 goto nomem;
5372
5373 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5374 * the T2-shutdown timer.
5375 */
5376 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5377
5378 /* and start/restart a T2-shutdown timer of its own, */
5379 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5380 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5381
Neil Horman9f70f462013-12-10 06:48:15 -05005382 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5384 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5385
5386 /* Enter the SHUTDOWN-ACK-SENT state. */
5387 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5388 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5389
5390 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5391 *
5392 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005393 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 */
5395 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5396
5397 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5398
5399 return SCTP_DISPOSITION_CONSUME;
5400
5401nomem:
5402 return SCTP_DISPOSITION_NOMEM;
5403}
5404
5405/*
5406 * Ignore the event defined as other
5407 *
5408 * The return value is the disposition of the event.
5409 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005410sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5411 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 const struct sctp_association *asoc,
5413 const sctp_subtype_t type,
5414 void *arg,
5415 sctp_cmd_seq_t *commands)
5416{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005417 pr_debug("%s: the event other type:%d is ignored\n",
5418 __func__, type.other);
5419
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 return SCTP_DISPOSITION_DISCARD;
5421}
5422
5423/************************************************************
5424 * These are the state functions for handling timeout events.
5425 ************************************************************/
5426
5427/*
5428 * RTX Timeout
5429 *
5430 * Section: 6.3.3 Handle T3-rtx Expiration
5431 *
5432 * Whenever the retransmission timer T3-rtx expires for a destination
5433 * address, do the following:
5434 * [See below]
5435 *
5436 * The return value is the disposition of the chunk.
5437 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005438sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5439 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 const struct sctp_association *asoc,
5441 const sctp_subtype_t type,
5442 void *arg,
5443 sctp_cmd_seq_t *commands)
5444{
5445 struct sctp_transport *transport = arg;
5446
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005447 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005448
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005450 if (asoc->peer.zero_window_announced &&
5451 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005452 /*
5453 * We are here likely because the receiver had its rwnd
5454 * closed for a while and we have not been able to
5455 * transmit the locally queued data within the maximum
5456 * retransmission attempts limit. Start the T5
5457 * shutdown guard timer to give the receiver one last
5458 * chance and some additional time to recover before
5459 * aborting.
5460 */
5461 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5462 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5463 } else {
5464 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5465 SCTP_ERROR(ETIMEDOUT));
5466 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5467 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5468 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005469 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5470 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005471 return SCTP_DISPOSITION_DELETE_TCB;
5472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 }
5474
5475 /* E1) For the destination address for which the timer
5476 * expires, adjust its ssthresh with rules defined in Section
5477 * 7.2.3 and set the cwnd <- MTU.
5478 */
5479
5480 /* E2) For the destination address for which the timer
5481 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5482 * maximum value discussed in rule C7 above (RTO.max) may be
5483 * used to provide an upper bound to this doubling operation.
5484 */
5485
5486 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5487 * outstanding DATA chunks for the address for which the
5488 * T3-rtx has expired will fit into a single packet, subject
5489 * to the MTU constraint for the path corresponding to the
5490 * destination transport address to which the retransmission
5491 * is being sent (this may be different from the address for
5492 * which the timer expires [see Section 6.4]). Call this
5493 * value K. Bundle and retransmit those K DATA chunks in a
5494 * single packet to the destination endpoint.
5495 *
5496 * Note: Any DATA chunks that were sent to the address for
5497 * which the T3-rtx timer expired but did not fit in one MTU
5498 * (rule E3 above), should be marked for retransmission and
5499 * sent as soon as cwnd allows (normally when a SACK arrives).
5500 */
5501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 /* Do some failure management (Section 8.2). */
5503 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5504
Vlad Yasevich1845a572007-02-21 02:06:19 -08005505 /* NB: Rules E4 and F1 are implicit in R1. */
5506 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5507
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 return SCTP_DISPOSITION_CONSUME;
5509}
5510
5511/*
5512 * Generate delayed SACK on timeout
5513 *
5514 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5515 *
5516 * The guidelines on delayed acknowledgement algorithm specified in
5517 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5518 * acknowledgement SHOULD be generated for at least every second packet
5519 * (not every second DATA chunk) received, and SHOULD be generated
5520 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5521 * some situations it may be beneficial for an SCTP transmitter to be
5522 * more conservative than the algorithms detailed in this document
5523 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5524 * the following algorithms allow.
5525 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005526sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5527 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 const struct sctp_association *asoc,
5529 const sctp_subtype_t type,
5530 void *arg,
5531 sctp_cmd_seq_t *commands)
5532{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005533 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5535 return SCTP_DISPOSITION_CONSUME;
5536}
5537
5538/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005539 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 *
5541 * Section: 4 Note: 2
5542 * Verification Tag:
5543 * Inputs
5544 * (endpoint, asoc)
5545 *
5546 * RFC 2960 Section 4 Notes
5547 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5548 * and re-start the T1-init timer without changing state. This MUST
5549 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5550 * endpoint MUST abort the initialization process and report the
5551 * error to SCTP user.
5552 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005553 * Outputs
5554 * (timers, events)
5555 *
5556 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005557sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5558 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005559 const struct sctp_association *asoc,
5560 const sctp_subtype_t type,
5561 void *arg,
5562 sctp_cmd_seq_t *commands)
5563{
5564 struct sctp_chunk *repl = NULL;
5565 struct sctp_bind_addr *bp;
5566 int attempts = asoc->init_err_counter + 1;
5567
Daniel Borkmannbb333812013-06-28 19:49:40 +02005568 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5569
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005570 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005571
Vlad Yasevich81845c22006-01-30 15:59:54 -08005572 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005573 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5574 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5575 if (!repl)
5576 return SCTP_DISPOSITION_NOMEM;
5577
5578 /* Choose transport for INIT. */
5579 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5580 SCTP_CHUNK(repl));
5581
5582 /* Issue a sideeffect to do the needed accounting. */
5583 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5584 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5585
5586 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5587 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005588 pr_debug("%s: giving up on INIT, attempts:%d "
5589 "max_init_attempts:%d\n", __func__, attempts,
5590 asoc->max_init_attempts);
5591
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005592 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5593 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005594 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005595 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005596 return SCTP_DISPOSITION_DELETE_TCB;
5597 }
5598
5599 return SCTP_DISPOSITION_CONSUME;
5600}
5601
5602/*
5603 * sctp_sf_t1_cookie_timer_expire
5604 *
5605 * Section: 4 Note: 2
5606 * Verification Tag:
5607 * Inputs
5608 * (endpoint, asoc)
5609 *
5610 * RFC 2960 Section 4 Notes
5611 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 * COOKIE ECHO and re-start the T1-cookie timer without changing
5613 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5614 * After that, the endpoint MUST abort the initialization process and
5615 * report the error to SCTP user.
5616 *
5617 * Outputs
5618 * (timers, events)
5619 *
5620 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005621sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5622 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 const struct sctp_association *asoc,
5624 const sctp_subtype_t type,
5625 void *arg,
5626 sctp_cmd_seq_t *commands)
5627{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005628 struct sctp_chunk *repl = NULL;
5629 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
Daniel Borkmannbb333812013-06-28 19:49:40 +02005631 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5632
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005633 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634
Vlad Yasevich81845c22006-01-30 15:59:54 -08005635 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005636 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005638 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005640 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5641 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005643 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5644 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5647 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005648 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5649 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005651 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 return SCTP_DISPOSITION_DELETE_TCB;
5653 }
5654
5655 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656}
5657
5658/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5659 * with the updated last sequential TSN received from its peer.
5660 *
5661 * An endpoint should limit the number of retransmissions of the
5662 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5663 * If this threshold is exceeded the endpoint should destroy the TCB and
5664 * MUST report the peer endpoint unreachable to the upper layer (and
5665 * thus the association enters the CLOSED state). The reception of any
5666 * packet from its peer (i.e. as the peer sends all of its queued DATA
5667 * chunks) should clear the endpoint's retransmission count and restart
5668 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5669 * all of its queued DATA chunks that have not yet been sent.
5670 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005671sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5672 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 const struct sctp_association *asoc,
5674 const sctp_subtype_t type,
5675 void *arg,
5676 sctp_cmd_seq_t *commands)
5677{
5678 struct sctp_chunk *reply = NULL;
5679
Daniel Borkmannbb333812013-06-28 19:49:40 +02005680 pr_debug("%s: timer T2 expired\n", __func__);
5681
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005682 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005683
Neil Horman58fbbed2008-02-29 11:40:56 -08005684 ((struct sctp_association *)asoc)->shutdown_retries++;
5685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005687 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5688 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5690 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005691 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005692 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5693 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return SCTP_DISPOSITION_DELETE_TCB;
5695 }
5696
5697 switch (asoc->state) {
5698 case SCTP_STATE_SHUTDOWN_SENT:
5699 reply = sctp_make_shutdown(asoc, NULL);
5700 break;
5701
5702 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5703 reply = sctp_make_shutdown_ack(asoc, NULL);
5704 break;
5705
5706 default:
5707 BUG();
5708 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
5711 if (!reply)
5712 goto nomem;
5713
Wei Yongjun6345b192009-04-26 23:13:35 +08005714 /* Do some failure management (Section 8.2).
5715 * If we remove the transport an SHUTDOWN was last sent to, don't
5716 * do failure management.
5717 */
5718 if (asoc->shutdown_last_sent_to)
5719 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5720 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
5722 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5723 * the T2-shutdown timer.
5724 */
5725 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5726
5727 /* Restart the T2-shutdown timer. */
5728 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5729 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5730 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5731 return SCTP_DISPOSITION_CONSUME;
5732
5733nomem:
5734 return SCTP_DISPOSITION_NOMEM;
5735}
5736
5737/*
5738 * ADDIP Section 4.1 ASCONF CHunk Procedures
5739 * If the T4 RTO timer expires the endpoint should do B1 to B5
5740 */
5741sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005742 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 const struct sctp_endpoint *ep,
5744 const struct sctp_association *asoc,
5745 const sctp_subtype_t type,
5746 void *arg,
5747 sctp_cmd_seq_t *commands)
5748{
5749 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5750 struct sctp_transport *transport = chunk->transport;
5751
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005752 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005753
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5755 * detection on the appropriate destination address as defined in
5756 * RFC2960 [5] section 8.1 and 8.2.
5757 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005758 if (transport)
5759 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5760 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 /* Reconfig T4 timer and transport. */
5763 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5764
5765 /* ADDIP 4.1 B2) Increment the association error counters and perform
5766 * endpoint failure detection on the association as defined in
5767 * RFC2960 [5] section 8.1 and 8.2.
5768 * association error counter is incremented in SCTP_CMD_STRIKE.
5769 */
5770 if (asoc->overall_error_count >= asoc->max_retrans) {
5771 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5772 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005773 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5774 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005776 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005777 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5778 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 return SCTP_DISPOSITION_ABORT;
5780 }
5781
5782 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5783 * the ASCONF chunk was sent by doubling the RTO timer value.
5784 * This is done in SCTP_CMD_STRIKE.
5785 */
5786
5787 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5788 * choose an alternate destination address (please refer to RFC2960
5789 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005790 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 * ASCONF sent.
5792 */
5793 sctp_chunk_hold(asoc->addip_last_asconf);
5794 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5795 SCTP_CHUNK(asoc->addip_last_asconf));
5796
5797 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5798 * destination is selected, then the RTO used will be that of the new
5799 * destination address.
5800 */
5801 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5802 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5803
5804 return SCTP_DISPOSITION_CONSUME;
5805}
5806
5807/* sctpimpguide-05 Section 2.12.2
5808 * The sender of the SHUTDOWN MAY also start an overall guard timer
5809 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5810 * At the expiration of this timer the sender SHOULD abort the association
5811 * by sending an ABORT chunk.
5812 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005813sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5814 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815 const struct sctp_association *asoc,
5816 const sctp_subtype_t type,
5817 void *arg,
5818 sctp_cmd_seq_t *commands)
5819{
5820 struct sctp_chunk *reply = NULL;
5821
Daniel Borkmannbb333812013-06-28 19:49:40 +02005822 pr_debug("%s: timer T5 expired\n", __func__);
5823
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005824 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
5826 reply = sctp_make_abort(asoc, NULL, 0);
5827 if (!reply)
5828 goto nomem;
5829
5830 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005831 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5832 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005834 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005836 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5837 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 return SCTP_DISPOSITION_DELETE_TCB;
5840nomem:
5841 return SCTP_DISPOSITION_NOMEM;
5842}
5843
5844/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5845 * the association is automatically closed by starting the shutdown process.
5846 * The work that needs to be done is same as when SHUTDOWN is initiated by
5847 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5848 */
5849sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005850 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 const struct sctp_endpoint *ep,
5852 const struct sctp_association *asoc,
5853 const sctp_subtype_t type,
5854 void *arg,
5855 sctp_cmd_seq_t *commands)
5856{
5857 int disposition;
5858
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005859 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005860
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 /* From 9.2 Shutdown of an Association
5862 * Upon receipt of the SHUTDOWN primitive from its upper
5863 * layer, the endpoint enters SHUTDOWN-PENDING state and
5864 * remains there until all outstanding data has been
5865 * acknowledged by its peer. The endpoint accepts no new data
5866 * from its upper layer, but retransmits data to the far end
5867 * if necessary to fill gaps.
5868 */
5869 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5870 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5871
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 disposition = SCTP_DISPOSITION_CONSUME;
5873 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005874 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 arg, commands);
5876 }
5877 return disposition;
5878}
5879
5880/*****************************************************************************
5881 * These are sa state functions which could apply to all types of events.
5882 ****************************************************************************/
5883
5884/*
5885 * This table entry is not implemented.
5886 *
5887 * Inputs
5888 * (endpoint, asoc, chunk)
5889 *
5890 * The return value is the disposition of the chunk.
5891 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005892sctp_disposition_t sctp_sf_not_impl(struct net *net,
5893 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 const struct sctp_association *asoc,
5895 const sctp_subtype_t type,
5896 void *arg,
5897 sctp_cmd_seq_t *commands)
5898{
5899 return SCTP_DISPOSITION_NOT_IMPL;
5900}
5901
5902/*
5903 * This table entry represents a bug.
5904 *
5905 * Inputs
5906 * (endpoint, asoc, chunk)
5907 *
5908 * The return value is the disposition of the chunk.
5909 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005910sctp_disposition_t sctp_sf_bug(struct net *net,
5911 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 const struct sctp_association *asoc,
5913 const sctp_subtype_t type,
5914 void *arg,
5915 sctp_cmd_seq_t *commands)
5916{
5917 return SCTP_DISPOSITION_BUG;
5918}
5919
5920/*
5921 * This table entry represents the firing of a timer in the wrong state.
5922 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5923 * when the association is in the wrong state. This event should
5924 * be ignored, so as to prevent any rearming of the timer.
5925 *
5926 * Inputs
5927 * (endpoint, asoc, chunk)
5928 *
5929 * The return value is the disposition of the chunk.
5930 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005931sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5932 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 const struct sctp_association *asoc,
5934 const sctp_subtype_t type,
5935 void *arg,
5936 sctp_cmd_seq_t *commands)
5937{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005938 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5939
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 return SCTP_DISPOSITION_CONSUME;
5941}
5942
5943/********************************************************************
5944 * 2nd Level Abstractions
5945 ********************************************************************/
5946
5947/* Pull the SACK chunk based on the SACK header. */
5948static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5949{
5950 struct sctp_sackhdr *sack;
5951 unsigned int len;
5952 __u16 num_blocks;
5953 __u16 num_dup_tsns;
5954
5955 /* Protect ourselves from reading too far into
5956 * the skb from a bogus sender.
5957 */
5958 sack = (struct sctp_sackhdr *) chunk->skb->data;
5959
5960 num_blocks = ntohs(sack->num_gap_ack_blocks);
5961 num_dup_tsns = ntohs(sack->num_dup_tsns);
5962 len = sizeof(struct sctp_sackhdr);
5963 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5964 if (len > chunk->skb->len)
5965 return NULL;
5966
5967 skb_pull(chunk->skb, len);
5968
5969 return sack;
5970}
5971
5972/* Create an ABORT packet to be sent as a response, with the specified
5973 * error causes.
5974 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005975static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5976 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 const struct sctp_association *asoc,
5978 struct sctp_chunk *chunk,
5979 const void *payload,
5980 size_t paylen)
5981{
5982 struct sctp_packet *packet;
5983 struct sctp_chunk *abort;
5984
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005985 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
5987 if (packet) {
5988 /* Make an ABORT.
5989 * The T bit will be set if the asoc is NULL.
5990 */
5991 abort = sctp_make_abort(asoc, chunk, paylen);
5992 if (!abort) {
5993 sctp_ootb_pkt_free(packet);
5994 return NULL;
5995 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005996
5997 /* Reflect vtag if T-Bit is set */
5998 if (sctp_test_T_bit(abort))
5999 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6000
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 /* Add specified error causes, i.e., payload, to the
6002 * end of the chunk.
6003 */
6004 sctp_addto_chunk(abort, paylen, payload);
6005
6006 /* Set the skb to the belonging sock for accounting. */
6007 abort->skb->sk = ep->base.sk;
6008
6009 sctp_packet_append_chunk(packet, abort);
6010
6011 }
6012
6013 return packet;
6014}
6015
6016/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006017static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6018 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 const struct sctp_chunk *chunk)
6020{
6021 struct sctp_packet *packet;
6022 struct sctp_transport *transport;
6023 __u16 sport;
6024 __u16 dport;
6025 __u32 vtag;
6026
6027 /* Get the source and destination port from the inbound packet. */
6028 sport = ntohs(chunk->sctp_hdr->dest);
6029 dport = ntohs(chunk->sctp_hdr->source);
6030
6031 /* The V-tag is going to be the same as the inbound packet if no
6032 * association exists, otherwise, use the peer's vtag.
6033 */
6034 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006035 /* Special case the INIT-ACK as there is no peer's vtag
6036 * yet.
6037 */
wangweidongcb3f8372013-12-23 12:16:50 +08006038 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006039 case SCTP_CID_INIT_ACK:
6040 {
6041 sctp_initack_chunk_t *initack;
6042
6043 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6044 vtag = ntohl(initack->init_hdr.init_tag);
6045 break;
6046 }
6047 default:
6048 vtag = asoc->peer.i.init_tag;
6049 break;
6050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 } else {
6052 /* Special case the INIT and stale COOKIE_ECHO as there is no
6053 * vtag yet.
6054 */
wangweidongcb3f8372013-12-23 12:16:50 +08006055 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 case SCTP_CID_INIT:
6057 {
6058 sctp_init_chunk_t *init;
6059
6060 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6061 vtag = ntohl(init->init_hdr.init_tag);
6062 break;
6063 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006064 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 vtag = ntohl(chunk->sctp_hdr->vtag);
6066 break;
6067 }
6068 }
6069
6070 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006071 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 if (!transport)
6073 goto nomem;
6074
6075 /* Cache a route for the transport with the chunk's destination as
6076 * the source address.
6077 */
Al Viro16b0a032006-11-20 17:13:38 -08006078 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006079 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006081 packet = &transport->packet;
6082 sctp_packet_init(packet, transport, sport, dport);
6083 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
6085 return packet;
6086
6087nomem:
6088 return NULL;
6089}
6090
6091/* Free the packet allocated earlier for responding in the OOTB condition. */
6092void sctp_ootb_pkt_free(struct sctp_packet *packet)
6093{
6094 sctp_transport_free(packet->transport);
6095}
6096
6097/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006098static void sctp_send_stale_cookie_err(struct net *net,
6099 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100 const struct sctp_association *asoc,
6101 const struct sctp_chunk *chunk,
6102 sctp_cmd_seq_t *commands,
6103 struct sctp_chunk *err_chunk)
6104{
6105 struct sctp_packet *packet;
6106
6107 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006108 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 if (packet) {
6110 struct sctp_signed_cookie *cookie;
6111
6112 /* Override the OOTB vtag from the cookie. */
6113 cookie = chunk->subh.cookie_hdr;
6114 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006115
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 /* Set the skb to the belonging sock for accounting. */
6117 err_chunk->skb->sk = ep->base.sk;
6118 sctp_packet_append_chunk(packet, err_chunk);
6119 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6120 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006121 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 } else
6123 sctp_chunk_free (err_chunk);
6124 }
6125}
6126
6127
6128/* Process a data chunk */
6129static int sctp_eat_data(const struct sctp_association *asoc,
6130 struct sctp_chunk *chunk,
6131 sctp_cmd_seq_t *commands)
6132{
6133 sctp_datahdr_t *data_hdr;
6134 struct sctp_chunk *err;
6135 size_t datalen;
6136 sctp_verb_t deliver;
6137 int tmp;
6138 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006139 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006140 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006141 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006142 u16 ssn;
6143 u16 sid;
6144 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145
6146 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6147 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6148
6149 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006150 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151
6152 /* ASSERT: Now skb->data is really the user data. */
6153
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 /* Process ECN based congestion.
6155 *
6156 * Since the chunk structure is reused for all chunks within
6157 * a packet, we use ecn_ce_done to track if we've already
6158 * done CE processing for this packet.
6159 *
6160 * We need to do ECN processing even if we plan to discard the
6161 * chunk later.
6162 */
6163
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006164 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006165 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 chunk->ecn_ce_done = 1;
6167
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006168 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 /* Do real work as sideffect. */
6170 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6171 SCTP_U32(tsn));
6172 }
6173 }
6174
6175 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6176 if (tmp < 0) {
6177 /* The TSN is too high--silently discard the chunk and
6178 * count on it getting retransmitted later.
6179 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006180 if (chunk->asoc)
6181 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 return SCTP_IERROR_HIGH_TSN;
6183 } else if (tmp > 0) {
6184 /* This is a duplicate. Record it. */
6185 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6186 return SCTP_IERROR_DUP_TSN;
6187 }
6188
6189 /* This is a new TSN. */
6190
6191 /* Discard if there is no room in the receive window.
6192 * Actually, allow a little bit of overflow (up to a MTU).
6193 */
6194 datalen = ntohs(chunk->chunk_hdr->length);
6195 datalen -= sizeof(sctp_data_chunk_t);
6196
6197 deliver = SCTP_CMD_CHUNK_ULP;
6198
6199 /* Think about partial delivery. */
6200 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6201
6202 /* Even if we don't accept this chunk there is
6203 * memory pressure.
6204 */
6205 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6206 }
6207
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006208 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209 * seems a bit troublesome in that frag_point varies based on
6210 * PMTU. In cases, such as loopback, this might be a rather
6211 * large spill over.
6212 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006213 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006214 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215
6216 /* If this is the next TSN, consider reneging to make
6217 * room. Note: Playing nice with a confused sender. A
6218 * malicious sender can still eat up all our buffer
6219 * space and in the future we may want to detect and
6220 * do more drastic reneging.
6221 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006222 if (sctp_tsnmap_has_gap(map) &&
6223 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006224 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 deliver = SCTP_CMD_RENEGE;
6226 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006227 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6228 __func__, tsn, datalen, asoc->rwnd);
6229
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 return SCTP_IERROR_IGNORE_TSN;
6231 }
6232 }
6233
6234 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006235 * Also try to renege to limit our memory usage in the event that
6236 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006237 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006238 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6239 * memory usage too much
6240 */
6241 if (*sk->sk_prot_creator->memory_pressure) {
6242 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006243 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006244 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6245 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006246 deliver = SCTP_CMD_RENEGE;
6247 }
6248 }
6249
6250 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 * Section 3.3.10.9 No User Data (9)
6252 *
6253 * Cause of error
6254 * ---------------
6255 * No User Data: This error cause is returned to the originator of a
6256 * DATA chunk if a received DATA chunk has no user data.
6257 */
6258 if (unlikely(0 == datalen)) {
6259 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6260 if (err) {
6261 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6262 SCTP_CHUNK(err));
6263 }
6264 /* We are going to ABORT, so we might as well stop
6265 * processing the rest of the chunks in the packet.
6266 */
wangweidongcb3f8372013-12-23 12:16:50 +08006267 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006268 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6269 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006271 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006272 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6273 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 return SCTP_IERROR_NO_DATA;
6275 }
6276
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006277 chunk->data_accepted = 1;
6278
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6280 * if we renege and the chunk arrives again.
6281 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006282 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006283 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006284 if (chunk->asoc)
6285 chunk->asoc->stats.iuodchunks++;
6286 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006287 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006288 if (chunk->asoc)
6289 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006290 ordered = 1;
6291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292
6293 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6294 *
6295 * If an endpoint receive a DATA chunk with an invalid stream
6296 * identifier, it shall acknowledge the reception of the DATA chunk
6297 * following the normal procedure, immediately send an ERROR chunk
6298 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6299 * and discard the DATA chunk.
6300 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006301 sid = ntohs(data_hdr->stream);
6302 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006303 /* Mark tsn as received even though we drop it */
6304 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6305
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6307 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006308 sizeof(data_hdr->stream),
6309 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 if (err)
6311 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6312 SCTP_CHUNK(err));
6313 return SCTP_IERROR_BAD_STREAM;
6314 }
6315
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006316 /* Check to see if the SSN is possible for this TSN.
6317 * The biggest gap we can record is 4K wide. Since SSNs wrap
6318 * at an unsigned short, there is no way that an SSN can
6319 * wrap and for a valid TSN. We can simply check if the current
6320 * SSN is smaller then the next expected one. If it is, it wrapped
6321 * and is invalid.
6322 */
6323 ssn = ntohs(data_hdr->ssn);
Xin Longa8386312017-01-06 22:18:33 +08006324 if (ordered && SSN_lt(ssn, sctp_ssn_peek(asoc->stream, in, sid)))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006325 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006326
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 /* Send the data up to the user. Note: Schedule the
6328 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6329 * chunk needs the updated rwnd.
6330 */
6331 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6332
6333 return SCTP_IERROR_NO_ERROR;
6334}