blob: f863b5573e42d6f1c4908bf045af0079edc4b2d9 [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 Yongjunde6becd2011-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 Yongjunde6becd2011-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 Yongjunde6becd2011-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 Yongjunde6becd2011-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 Yongjunde6becd2011-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
Xin Long7e062972017-05-23 13:28:55 +08002091 /* Set temp so that it won't be added into hashtable */
2092 new_asoc->temp = 1;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /* Compare the tie_tag in cookie with the verification tag of
2095 * current association.
2096 */
2097 action = sctp_tietags_compare(new_asoc, asoc);
2098
2099 switch (action) {
2100 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002101 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 new_asoc);
2103 break;
2104
2105 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002106 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 new_asoc);
2108 break;
2109
2110 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002111 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 new_asoc);
2113 break;
2114
2115 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002116 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 new_asoc);
2118 break;
2119
2120 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002121 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002126 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2128
Max Matveevd5ccd492011-08-29 21:02:24 +00002129 /* Restore association pointer to provide SCTP command interpeter
2130 * with a valid context in case it needs to manipulate
2131 * the queues */
2132 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2133 SCTP_ASOC((struct sctp_association *)asoc));
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return retval;
2136
2137nomem:
2138 return SCTP_DISPOSITION_NOMEM;
2139}
2140
2141/*
2142 * Process an ABORT. (SHUTDOWN-PENDING state)
2143 *
2144 * See sctp_sf_do_9_1_abort().
2145 */
2146sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002147 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 const struct sctp_endpoint *ep,
2149 const struct sctp_association *asoc,
2150 const sctp_subtype_t type,
2151 void *arg,
2152 sctp_cmd_seq_t *commands)
2153{
2154 struct sctp_chunk *chunk = arg;
2155
2156 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002157 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 /* Make sure that the ABORT chunk has a valid length.
2160 * Since this is an ABORT chunk, we have to discard it
2161 * because of the following text:
2162 * RFC 2960, Section 3.3.7
2163 * If an endpoint receives an ABORT with a format error or for an
2164 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002165 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * as we do not know its true length. So, to be safe, discard the
2167 * packet.
2168 */
2169 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Vlad Yasevich75205f42007-12-20 14:12:59 -08002172 /* ADD-IP: Special case for ABORT chunks
2173 * F4) One special consideration is that ABORT Chunks arriving
2174 * destined to the IP address being deleted MUST be
2175 * ignored (see Section 5.3.1 for further details).
2176 */
2177 if (SCTP_ADDR_DEL ==
2178 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002179 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002180
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002181 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
2184/*
2185 * Process an ABORT. (SHUTDOWN-SENT state)
2186 *
2187 * See sctp_sf_do_9_1_abort().
2188 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002189sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2190 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 const struct sctp_association *asoc,
2192 const sctp_subtype_t type,
2193 void *arg,
2194 sctp_cmd_seq_t *commands)
2195{
2196 struct sctp_chunk *chunk = arg;
2197
2198 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002199 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /* Make sure that the ABORT chunk has a valid length.
2202 * Since this is an ABORT chunk, we have to discard it
2203 * because of the following text:
2204 * RFC 2960, Section 3.3.7
2205 * If an endpoint receives an ABORT with a format error or for an
2206 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002207 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * as we do not know its true length. So, to be safe, discard the
2209 * packet.
2210 */
2211 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002212 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Vlad Yasevich75205f42007-12-20 14:12:59 -08002214 /* ADD-IP: Special case for ABORT chunks
2215 * F4) One special consideration is that ABORT Chunks arriving
2216 * destined to the IP address being deleted MUST be
2217 * ignored (see Section 5.3.1 for further details).
2218 */
2219 if (SCTP_ADDR_DEL ==
2220 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002221 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* Stop the T2-shutdown timer. */
2224 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2225 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2226
2227 /* Stop the T5-shutdown guard timer. */
2228 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2229 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2230
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002231 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234/*
2235 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2236 *
2237 * See sctp_sf_do_9_1_abort().
2238 */
2239sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002240 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 const struct sctp_endpoint *ep,
2242 const struct sctp_association *asoc,
2243 const sctp_subtype_t type,
2244 void *arg,
2245 sctp_cmd_seq_t *commands)
2246{
2247 /* The same T2 timer, so we should be able to use
2248 * common function with the SHUTDOWN-SENT state.
2249 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002250 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
2253/*
2254 * Handle an Error received in COOKIE_ECHOED state.
2255 *
2256 * Only handle the error type of stale COOKIE Error, the other errors will
2257 * be ignored.
2258 *
2259 * Inputs
2260 * (endpoint, asoc, chunk)
2261 *
2262 * Outputs
2263 * (asoc, reply_msg, msg_up, timers, counters)
2264 *
2265 * The return value is the disposition of the chunk.
2266 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002267sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2268 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 const struct sctp_association *asoc,
2270 const sctp_subtype_t type,
2271 void *arg,
2272 sctp_cmd_seq_t *commands)
2273{
2274 struct sctp_chunk *chunk = arg;
2275 sctp_errhdr_t *err;
2276
2277 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002278 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 /* Make sure that the ERROR chunk has a valid length.
2281 * The parameter walking depends on this as well.
2282 */
2283 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002284 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 commands);
2286
2287 /* Process the error here */
2288 /* FUTURE FIXME: When PR-SCTP related and other optional
2289 * parms are emitted, this will have to change to handle multiple
2290 * errors.
2291 */
2292 sctp_walk_errors(err, chunk->chunk_hdr) {
2293 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002294 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 arg, commands);
2296 }
2297
2298 /* It is possible to have malformed error causes, and that
2299 * will cause us to end the walk early. However, since
2300 * we are discarding the packet, there should be no adverse
2301 * affects.
2302 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002303 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306/*
2307 * Handle a Stale COOKIE Error
2308 *
2309 * Section: 5.2.6 Handle Stale COOKIE Error
2310 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2311 * one of the following three alternatives.
2312 * ...
2313 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2314 * Preservative parameter requesting an extension to the lifetime of
2315 * the State Cookie. When calculating the time extension, an
2316 * implementation SHOULD use the RTT information measured based on the
2317 * previous COOKIE ECHO / ERROR exchange, and should add no more
2318 * than 1 second beyond the measured RTT, due to long State Cookie
2319 * lifetimes making the endpoint more subject to a replay attack.
2320 *
2321 * Verification Tag: Not explicit, but safe to ignore.
2322 *
2323 * Inputs
2324 * (endpoint, asoc, chunk)
2325 *
2326 * Outputs
2327 * (asoc, reply_msg, msg_up, timers, counters)
2328 *
2329 * The return value is the disposition of the chunk.
2330 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002331static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2332 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 const struct sctp_association *asoc,
2334 const sctp_subtype_t type,
2335 void *arg,
2336 sctp_cmd_seq_t *commands)
2337{
2338 struct sctp_chunk *chunk = arg;
Arnd Bergmann3ef0a252015-09-30 13:26:40 +02002339 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 sctp_cookie_preserve_param_t bht;
2341 sctp_errhdr_t *err;
2342 struct sctp_chunk *reply;
2343 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002344 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Vlad Yasevich81845c22006-01-30 15:59:54 -08002346 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002347 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2348 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002350 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return SCTP_DISPOSITION_DELETE_TCB;
2352 }
2353
2354 err = (sctp_errhdr_t *)(chunk->skb->data);
2355
2356 /* When calculating the time extension, an implementation
2357 * SHOULD use the RTT information measured based on the
2358 * previous COOKIE ECHO / ERROR exchange, and should add no
2359 * more than 1 second beyond the measured RTT, due to long
2360 * State Cookie lifetimes making the endpoint more subject to
2361 * a replay attack.
2362 * Measure of Staleness's unit is usec. (1/1000000 sec)
2363 * Suggested Cookie Life-span Increment's unit is msec.
2364 * (1/1000 sec)
2365 * In general, if you use the suggested cookie life, the value
2366 * found in the field of measure of staleness should be doubled
2367 * to give ample time to retransmit the new cookie and thus
2368 * yield a higher probability of success on the reattempt.
2369 */
Al Viro34bcca22006-11-20 17:27:15 -08002370 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 stale = (stale * 2) / 1000;
2372
2373 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2374 bht.param_hdr.length = htons(sizeof(bht));
2375 bht.lifespan_increment = htonl(stale);
2376
2377 /* Build that new INIT chunk. */
2378 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2379 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2380 if (!reply)
2381 goto nomem;
2382
2383 sctp_addto_chunk(reply, sizeof(bht), &bht);
2384
2385 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2386 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2387
2388 /* Stop pending T3-rtx and heartbeat timers */
2389 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2390 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2391
2392 /* Delete non-primary peer ip addresses since we are transitioning
2393 * back to the COOKIE-WAIT state
2394 */
2395 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2396
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002397 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2398 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002400 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 SCTP_TRANSPORT(asoc->peer.primary_path));
2402
2403 /* Cast away the const modifier, as we want to just
2404 * rerun it through as a sideffect.
2405 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002406 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
2408 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2409 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2410 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2411 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2412 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2413 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2414
2415 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2416
2417 return SCTP_DISPOSITION_CONSUME;
2418
2419nomem:
2420 return SCTP_DISPOSITION_NOMEM;
2421}
2422
2423/*
2424 * Process an ABORT.
2425 *
2426 * Section: 9.1
2427 * After checking the Verification Tag, the receiving endpoint shall
2428 * remove the association from its record, and shall report the
2429 * termination to its upper layer.
2430 *
2431 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2432 * B) Rules for packet carrying ABORT:
2433 *
2434 * - The endpoint shall always fill in the Verification Tag field of the
2435 * outbound packet with the destination endpoint's tag value if it
2436 * is known.
2437 *
2438 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2439 * MUST follow the procedure described in Section 8.4.
2440 *
2441 * - The receiver MUST accept the packet if the Verification Tag
2442 * matches either its own tag, OR the tag of its peer. Otherwise, the
2443 * receiver MUST silently discard the packet and take no further
2444 * action.
2445 *
2446 * Inputs
2447 * (endpoint, asoc, chunk)
2448 *
2449 * Outputs
2450 * (asoc, reply_msg, msg_up, timers, counters)
2451 *
2452 * The return value is the disposition of the chunk.
2453 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002454sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2455 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 const struct sctp_association *asoc,
2457 const sctp_subtype_t type,
2458 void *arg,
2459 sctp_cmd_seq_t *commands)
2460{
2461 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002464 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 /* Make sure that the ABORT chunk has a valid length.
2467 * Since this is an ABORT chunk, we have to discard it
2468 * because of the following text:
2469 * RFC 2960, Section 3.3.7
2470 * If an endpoint receives an ABORT with a format error or for an
2471 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002472 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 * as we do not know its true length. So, to be safe, discard the
2474 * packet.
2475 */
2476 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002477 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Vlad Yasevich75205f42007-12-20 14:12:59 -08002479 /* ADD-IP: Special case for ABORT chunks
2480 * F4) One special consideration is that ABORT Chunks arriving
2481 * destined to the IP address being deleted MUST be
2482 * ignored (see Section 5.3.1 for further details).
2483 */
2484 if (SCTP_ADDR_DEL ==
2485 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002486 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002487
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002488 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002489}
2490
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002491static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2492 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002493 const struct sctp_association *asoc,
2494 const sctp_subtype_t type,
2495 void *arg,
2496 sctp_cmd_seq_t *commands)
2497{
2498 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002499 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002500 __be16 error = SCTP_ERROR_NO_ERROR;
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 /* See if we have an error cause code in the chunk. */
2503 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002504 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2505
2506 sctp_errhdr_t *err;
2507 sctp_walk_errors(err, chunk->chunk_hdr);
2508 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002509 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002514 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002515 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002516 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002517 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2518 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 return SCTP_DISPOSITION_ABORT;
2521}
2522
2523/*
2524 * Process an ABORT. (COOKIE-WAIT state)
2525 *
2526 * See sctp_sf_do_9_1_abort() above.
2527 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002528sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2529 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 const struct sctp_association *asoc,
2531 const sctp_subtype_t type,
2532 void *arg,
2533 sctp_cmd_seq_t *commands)
2534{
2535 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002536 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002537 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002540 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 /* Make sure that the ABORT chunk has a valid length.
2543 * Since this is an ABORT chunk, we have to discard it
2544 * because of the following text:
2545 * RFC 2960, Section 3.3.7
2546 * If an endpoint receives an ABORT with a format error or for an
2547 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002548 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 * as we do not know its true length. So, to be safe, discard the
2550 * packet.
2551 */
2552 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002553 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /* See if we have an error cause code in the chunk. */
2556 len = ntohs(chunk->chunk_hdr->length);
2557 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2558 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2559
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002560 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002561 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
2564/*
2565 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2566 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002567sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2568 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 const struct sctp_association *asoc,
2570 const sctp_subtype_t type,
2571 void *arg,
2572 sctp_cmd_seq_t *commands)
2573{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002574 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002575 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002576 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579/*
2580 * Process an ABORT. (COOKIE-ECHOED state)
2581 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002582sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2583 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 const struct sctp_association *asoc,
2585 const sctp_subtype_t type,
2586 void *arg,
2587 sctp_cmd_seq_t *commands)
2588{
2589 /* There is a single T1 timer, so we should be able to use
2590 * common function with the COOKIE-WAIT state.
2591 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002592 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
2594
2595/*
2596 * Stop T1 timer and abort association with "INIT failed".
2597 *
2598 * This is common code called by several sctp_sf_*_abort() functions above.
2599 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002600static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2601 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002602 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002603 const struct sctp_association *asoc,
2604 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002606 pr_debug("%s: ABORT received (INIT)\n", __func__);
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2609 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002610 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2612 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002613 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* CMD_INIT_FAILED will DELETE_TCB. */
2615 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002616 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002617
Frank Filz3f7a87d2005-06-20 13:14:57 -07002618 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
2621/*
2622 * sctp_sf_do_9_2_shut
2623 *
2624 * Section: 9.2
2625 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2626 * - enter the SHUTDOWN-RECEIVED state,
2627 *
2628 * - stop accepting new data from its SCTP user
2629 *
2630 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2631 * that all its outstanding DATA chunks have been received by the
2632 * SHUTDOWN sender.
2633 *
2634 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2635 * send a SHUTDOWN in response to a ULP request. And should discard
2636 * subsequent SHUTDOWN chunks.
2637 *
2638 * If there are still outstanding DATA chunks left, the SHUTDOWN
2639 * receiver shall continue to follow normal data transmission
2640 * procedures defined in Section 6 until all outstanding DATA chunks
2641 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2642 * new data from its SCTP user.
2643 *
2644 * Verification Tag: 8.5 Verification Tag [Normal verification]
2645 *
2646 * Inputs
2647 * (endpoint, asoc, chunk)
2648 *
2649 * Outputs
2650 * (asoc, reply_msg, msg_up, timers, counters)
2651 *
2652 * The return value is the disposition of the chunk.
2653 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002654sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2655 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 const struct sctp_association *asoc,
2657 const sctp_subtype_t type,
2658 void *arg,
2659 sctp_cmd_seq_t *commands)
2660{
2661 struct sctp_chunk *chunk = arg;
2662 sctp_shutdownhdr_t *sdh;
2663 sctp_disposition_t disposition;
2664 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002665 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
2667 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002668 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 /* Make sure that the SHUTDOWN chunk has a valid length. */
2671 if (!sctp_chunk_length_valid(chunk,
2672 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002673 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 commands);
2675
2676 /* Convert the elaborate header. */
2677 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2678 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2679 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002680 ctsn = ntohl(sdh->cum_tsn_ack);
2681
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002682 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002683 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2684 asoc->ctsn_ack_point);
2685
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002686 return SCTP_DISPOSITION_DISCARD;
2687 }
2688
Wei Yongjundf10eec2008-10-23 01:00:21 -07002689 /* If Cumulative TSN Ack beyond the max tsn currently
2690 * send, terminating the association and respond to the
2691 * sender with an ABORT.
2692 */
2693 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002694 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002696 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2697 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2698 * inform the application that it should cease sending data.
2699 */
2700 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2701 if (!ev) {
2702 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002703 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002704 }
2705 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2708 * - enter the SHUTDOWN-RECEIVED state,
2709 * - stop accepting new data from its SCTP user
2710 *
2711 * [This is implicit in the new state.]
2712 */
2713 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2714 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2715 disposition = SCTP_DISPOSITION_CONSUME;
2716
2717 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002718 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 arg, commands);
2720 }
2721
2722 if (SCTP_DISPOSITION_NOMEM == disposition)
2723 goto out;
2724
2725 /* - verify, by checking the Cumulative TSN Ack field of the
2726 * chunk, that all its outstanding DATA chunks have been
2727 * received by the SHUTDOWN sender.
2728 */
2729 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002730 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732out:
2733 return disposition;
2734}
2735
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002736/*
2737 * sctp_sf_do_9_2_shut_ctsn
2738 *
2739 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2740 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2741 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2742 * MUST be processed.
2743 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002744sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2745 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002746 const struct sctp_association *asoc,
2747 const sctp_subtype_t type,
2748 void *arg,
2749 sctp_cmd_seq_t *commands)
2750{
2751 struct sctp_chunk *chunk = arg;
2752 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002753 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002754
2755 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002756 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002757
2758 /* Make sure that the SHUTDOWN chunk has a valid length. */
2759 if (!sctp_chunk_length_valid(chunk,
2760 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002761 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002762 commands);
2763
2764 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002765 ctsn = ntohl(sdh->cum_tsn_ack);
2766
2767 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002768 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2769 asoc->ctsn_ack_point);
2770
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002771 return SCTP_DISPOSITION_DISCARD;
2772 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002773
2774 /* If Cumulative TSN Ack beyond the max tsn currently
2775 * send, terminating the association and respond to the
2776 * sender with an ABORT.
2777 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002778 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002779 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002780
2781 /* verify, by checking the Cumulative TSN Ack field of the
2782 * chunk, that all its outstanding DATA chunks have been
2783 * received by the SHUTDOWN sender.
2784 */
2785 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2786 SCTP_BE32(sdh->cum_tsn_ack));
2787
2788 return SCTP_DISPOSITION_CONSUME;
2789}
2790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791/* RFC 2960 9.2
2792 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2793 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2794 * transport addresses (either in the IP addresses or in the INIT chunk)
2795 * that belong to this association, it should discard the INIT chunk and
2796 * retransmit the SHUTDOWN ACK chunk.
2797 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002798sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2799 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 const struct sctp_association *asoc,
2801 const sctp_subtype_t type,
2802 void *arg,
2803 sctp_cmd_seq_t *commands)
2804{
2805 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2806 struct sctp_chunk *reply;
2807
Vlad Yasevichece25df2007-09-07 16:30:54 -04002808 /* Make sure that the chunk has a valid length */
2809 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002810 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002811 commands);
2812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /* Since we are not going to really process this INIT, there
2814 * is no point in verifying chunk boundries. Just generate
2815 * the SHUTDOWN ACK.
2816 */
2817 reply = sctp_make_shutdown_ack(asoc, chunk);
2818 if (NULL == reply)
2819 goto nomem;
2820
2821 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2822 * the T2-SHUTDOWN timer.
2823 */
2824 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2825
2826 /* and restart the T2-shutdown timer. */
2827 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2828 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2829
2830 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2831
2832 return SCTP_DISPOSITION_CONSUME;
2833nomem:
2834 return SCTP_DISPOSITION_NOMEM;
2835}
2836
2837/*
2838 * sctp_sf_do_ecn_cwr
2839 *
2840 * Section: Appendix A: Explicit Congestion Notification
2841 *
2842 * CWR:
2843 *
2844 * RFC 2481 details a specific bit for a sender to send in the header of
2845 * its next outbound TCP segment to indicate to its peer that it has
2846 * reduced its congestion window. This is termed the CWR bit. For
2847 * SCTP the same indication is made by including the CWR chunk.
2848 * This chunk contains one data element, i.e. the TSN number that
2849 * was sent in the ECNE chunk. This element represents the lowest
2850 * TSN number in the datagram that was originally marked with the
2851 * CE bit.
2852 *
2853 * Verification Tag: 8.5 Verification Tag [Normal verification]
2854 * Inputs
2855 * (endpoint, asoc, chunk)
2856 *
2857 * Outputs
2858 * (asoc, reply_msg, msg_up, timers, counters)
2859 *
2860 * The return value is the disposition of the chunk.
2861 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002862sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2863 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 const struct sctp_association *asoc,
2865 const sctp_subtype_t type,
2866 void *arg,
2867 sctp_cmd_seq_t *commands)
2868{
2869 sctp_cwrhdr_t *cwr;
2870 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002871 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002874 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002877 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002879
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2881 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2882
Al Viro34bcca22006-11-20 17:27:15 -08002883 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002886 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* Stop sending ECNE. */
2888 sctp_add_cmd_sf(commands,
2889 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002890 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
2892 return SCTP_DISPOSITION_CONSUME;
2893}
2894
2895/*
2896 * sctp_sf_do_ecne
2897 *
2898 * Section: Appendix A: Explicit Congestion Notification
2899 *
2900 * ECN-Echo
2901 *
2902 * RFC 2481 details a specific bit for a receiver to send back in its
2903 * TCP acknowledgements to notify the sender of the Congestion
2904 * Experienced (CE) bit having arrived from the network. For SCTP this
2905 * same indication is made by including the ECNE chunk. This chunk
2906 * contains one data element, i.e. the lowest TSN associated with the IP
2907 * datagram marked with the CE bit.....
2908 *
2909 * Verification Tag: 8.5 Verification Tag [Normal verification]
2910 * Inputs
2911 * (endpoint, asoc, chunk)
2912 *
2913 * Outputs
2914 * (asoc, reply_msg, msg_up, timers, counters)
2915 *
2916 * The return value is the disposition of the chunk.
2917 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002918sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2919 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 const struct sctp_association *asoc,
2921 const sctp_subtype_t type,
2922 void *arg,
2923 sctp_cmd_seq_t *commands)
2924{
2925 sctp_ecnehdr_t *ecne;
2926 struct sctp_chunk *chunk = arg;
2927
2928 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002929 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002932 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 commands);
2934
2935 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2936 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2937
2938 /* If this is a newer ECNE than the last CWR packet we sent out */
2939 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2940 SCTP_U32(ntohl(ecne->lowest_tsn)));
2941
2942 return SCTP_DISPOSITION_CONSUME;
2943}
2944
2945/*
2946 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2947 *
2948 * The SCTP endpoint MUST always acknowledge the reception of each valid
2949 * DATA chunk.
2950 *
2951 * The guidelines on delayed acknowledgement algorithm specified in
2952 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2953 * acknowledgement SHOULD be generated for at least every second packet
2954 * (not every second DATA chunk) received, and SHOULD be generated within
2955 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2956 * situations it may be beneficial for an SCTP transmitter to be more
2957 * conservative than the algorithms detailed in this document allow.
2958 * However, an SCTP transmitter MUST NOT be more aggressive than the
2959 * following algorithms allow.
2960 *
2961 * A SCTP receiver MUST NOT generate more than one SACK for every
2962 * incoming packet, other than to update the offered window as the
2963 * receiving application consumes new data.
2964 *
2965 * Verification Tag: 8.5 Verification Tag [Normal verification]
2966 *
2967 * Inputs
2968 * (endpoint, asoc, chunk)
2969 *
2970 * Outputs
2971 * (asoc, reply_msg, msg_up, timers, counters)
2972 *
2973 * The return value is the disposition of the chunk.
2974 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002975sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2976 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 const struct sctp_association *asoc,
2978 const sctp_subtype_t type,
2979 void *arg,
2980 sctp_cmd_seq_t *commands)
2981{
2982 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002983 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 int error;
2985
2986 if (!sctp_vtag_verify(chunk, asoc)) {
2987 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2988 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002989 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002993 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 commands);
2995
wangweidongcb3f8372013-12-23 12:16:50 +08002996 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 switch (error) {
2998 case SCTP_IERROR_NO_ERROR:
2999 break;
3000 case SCTP_IERROR_HIGH_TSN:
3001 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003002 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 goto discard_noforce;
3004 case SCTP_IERROR_DUP_TSN:
3005 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003006 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 goto discard_force;
3008 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003009 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003010 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003011 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003012 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 default:
3014 BUG();
3015 }
3016
Wei Yongjun6dc76942009-11-23 15:53:53 -05003017 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3018 force = SCTP_FORCE();
3019
Neil Horman9f70f462013-12-10 06:48:15 -05003020 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3022 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3023 }
3024
3025 /* If this is the last chunk in a packet, we need to count it
3026 * toward sack generation. Note that we need to SACK every
3027 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3028 * THEM. We elect to NOT generate SACK's if the chunk fails
3029 * the verification tag test.
3030 *
3031 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3032 *
3033 * The SCTP endpoint MUST always acknowledge the reception of
3034 * each valid DATA chunk.
3035 *
3036 * The guidelines on delayed acknowledgement algorithm
3037 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3038 * Specifically, an acknowledgement SHOULD be generated for at
3039 * least every second packet (not every second DATA chunk)
3040 * received, and SHOULD be generated within 200 ms of the
3041 * arrival of any unacknowledged DATA chunk. In some
3042 * situations it may be beneficial for an SCTP transmitter to
3043 * be more conservative than the algorithms detailed in this
3044 * document allow. However, an SCTP transmitter MUST NOT be
3045 * more aggressive than the following algorithms allow.
3046 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003047 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003048 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return SCTP_DISPOSITION_CONSUME;
3051
3052discard_force:
3053 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3054 *
3055 * When a packet arrives with duplicate DATA chunk(s) and with
3056 * no new DATA chunk(s), the endpoint MUST immediately send a
3057 * SACK with no delay. If a packet arrives with duplicate
3058 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3059 * MAY immediately send a SACK. Normally receipt of duplicate
3060 * DATA chunks will occur when the original SACK chunk was lost
3061 * and the peer's RTO has expired. The duplicate TSN number(s)
3062 * SHOULD be reported in the SACK as duplicate.
3063 */
3064 /* In our case, we split the MAY SACK advice up whether or not
3065 * the last chunk is a duplicate.'
3066 */
3067 if (chunk->end_of_packet)
3068 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3069 return SCTP_DISPOSITION_DISCARD;
3070
3071discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003072 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003073 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
3078/*
3079 * sctp_sf_eat_data_fast_4_4
3080 *
3081 * Section: 4 (4)
3082 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3083 * DATA chunks without delay.
3084 *
3085 * Verification Tag: 8.5 Verification Tag [Normal verification]
3086 * Inputs
3087 * (endpoint, asoc, chunk)
3088 *
3089 * Outputs
3090 * (asoc, reply_msg, msg_up, timers, counters)
3091 *
3092 * The return value is the disposition of the chunk.
3093 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003094sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3095 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 const struct sctp_association *asoc,
3097 const sctp_subtype_t type,
3098 void *arg,
3099 sctp_cmd_seq_t *commands)
3100{
3101 struct sctp_chunk *chunk = arg;
3102 int error;
3103
3104 if (!sctp_vtag_verify(chunk, asoc)) {
3105 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3106 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003107 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
3109
3110 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003111 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 commands);
3113
wangweidongcb3f8372013-12-23 12:16:50 +08003114 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 switch (error) {
3116 case SCTP_IERROR_NO_ERROR:
3117 case SCTP_IERROR_HIGH_TSN:
3118 case SCTP_IERROR_DUP_TSN:
3119 case SCTP_IERROR_IGNORE_TSN:
3120 case SCTP_IERROR_BAD_STREAM:
3121 break;
3122 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003123 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003124 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003125 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003126 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 default:
3128 BUG();
3129 }
3130
3131 /* Go a head and force a SACK, since we are shutting down. */
3132
3133 /* Implementor's Guide.
3134 *
3135 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3136 * respond to each received packet containing one or more DATA chunk(s)
3137 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3138 */
3139 if (chunk->end_of_packet) {
3140 /* We must delay the chunk creation since the cumulative
3141 * TSN has not been updated yet.
3142 */
3143 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3144 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3145 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3146 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3147 }
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 return SCTP_DISPOSITION_CONSUME;
3150}
3151
3152/*
3153 * Section: 6.2 Processing a Received SACK
3154 * D) Any time a SACK arrives, the endpoint performs the following:
3155 *
3156 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3157 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3158 * increasing, a SACK whose Cumulative TSN Ack is less than the
3159 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3160 *
3161 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3162 * of bytes still outstanding after processing the Cumulative TSN Ack
3163 * and the Gap Ack Blocks.
3164 *
3165 * iii) If the SACK is missing a TSN that was previously
3166 * acknowledged via a Gap Ack Block (e.g., the data receiver
3167 * reneged on the data), then mark the corresponding DATA chunk
3168 * as available for retransmit: Mark it as missing for fast
3169 * retransmit as described in Section 7.2.4 and if no retransmit
3170 * timer is running for the destination address to which the DATA
3171 * chunk was originally transmitted, then T3-rtx is started for
3172 * that destination address.
3173 *
3174 * Verification Tag: 8.5 Verification Tag [Normal verification]
3175 *
3176 * Inputs
3177 * (endpoint, asoc, chunk)
3178 *
3179 * Outputs
3180 * (asoc, reply_msg, msg_up, timers, counters)
3181 *
3182 * The return value is the disposition of the chunk.
3183 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003184sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3185 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 const struct sctp_association *asoc,
3187 const sctp_subtype_t type,
3188 void *arg,
3189 sctp_cmd_seq_t *commands)
3190{
3191 struct sctp_chunk *chunk = arg;
3192 sctp_sackhdr_t *sackh;
3193 __u32 ctsn;
3194
3195 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003196 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
3198 /* Make sure that the SACK chunk has a valid length. */
3199 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003200 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 commands);
3202
3203 /* Pull the SACK chunk from the data buffer */
3204 sackh = sctp_sm_pull_sack(chunk);
3205 /* Was this a bogus SACK? */
3206 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003207 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 chunk->subh.sack_hdr = sackh;
3209 ctsn = ntohl(sackh->cum_tsn_ack);
3210
3211 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3212 * Ack Point, then drop the SACK. Since Cumulative TSN
3213 * Ack is monotonically increasing, a SACK whose
3214 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3215 * Point indicates an out-of-order SACK.
3216 */
3217 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003218 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3219 asoc->ctsn_ack_point);
3220
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return SCTP_DISPOSITION_DISCARD;
3222 }
3223
Wei Yongjunaecedea2007-08-02 16:57:44 +08003224 /* If Cumulative TSN Ack beyond the max tsn currently
3225 * send, terminating the association and respond to the
3226 * sender with an ABORT.
3227 */
3228 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003229 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003232 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 /* Note: We do the rest of the work on the PROCESS_SACK
3235 * sideeffect.
3236 */
3237 return SCTP_DISPOSITION_CONSUME;
3238}
3239
3240/*
3241 * Generate an ABORT in response to a packet.
3242 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003243 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003245 * 8) The receiver should respond to the sender of the OOTB packet with
3246 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3247 * MUST fill in the Verification Tag field of the outbound packet
3248 * with the value found in the Verification Tag field of the OOTB
3249 * packet and set the T-bit in the Chunk Flags to indicate that the
3250 * Verification Tag is reflected. After sending this ABORT, the
3251 * receiver of the OOTB packet shall discard the OOTB packet and take
3252 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 *
3254 * Verification Tag:
3255 *
3256 * The return value is the disposition of the chunk.
3257*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003258static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3259 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 const struct sctp_association *asoc,
3261 const sctp_subtype_t type,
3262 void *arg,
3263 sctp_cmd_seq_t *commands)
3264{
3265 struct sctp_packet *packet = NULL;
3266 struct sctp_chunk *chunk = arg;
3267 struct sctp_chunk *abort;
3268
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003269 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003270 if (!packet)
3271 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003273 /* Make an ABORT. The T bit will be set if the asoc
3274 * is NULL.
3275 */
3276 abort = sctp_make_abort(asoc, chunk, 0);
3277 if (!abort) {
3278 sctp_ootb_pkt_free(packet);
3279 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 }
3281
Marcelo Ricardo Leitnereab59072016-12-28 09:26:31 -02003282 /* Reflect vtag if T-Bit is set */
3283 if (sctp_test_T_bit(abort))
3284 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3285
3286 /* Set the skb to the belonging sock for accounting. */
3287 abort->skb->sk = ep->base.sk;
3288
3289 sctp_packet_append_chunk(packet, abort);
3290
3291 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3292 SCTP_PACKET(packet));
3293
3294 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3295
3296 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3297 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300/*
3301 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3302 * event as ULP notification for each cause included in the chunk.
3303 *
3304 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3305 *
3306 * The return value is the disposition of the chunk.
3307*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003308sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3309 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 const struct sctp_association *asoc,
3311 const sctp_subtype_t type,
3312 void *arg,
3313 sctp_cmd_seq_t *commands)
3314{
3315 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003316 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003319 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
3321 /* Make sure that the ERROR chunk has a valid length. */
3322 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003323 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003325 sctp_walk_errors(err, chunk->chunk_hdr);
3326 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003327 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003328 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Wei Yongjun3df26782009-03-02 06:46:51 +00003330 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3331 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
3336/*
3337 * Process an inbound SHUTDOWN ACK.
3338 *
3339 * From Section 9.2:
3340 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3341 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3342 * peer, and remove all record of the association.
3343 *
3344 * The return value is the disposition.
3345 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003346sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3347 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 const struct sctp_association *asoc,
3349 const sctp_subtype_t type,
3350 void *arg,
3351 sctp_cmd_seq_t *commands)
3352{
3353 struct sctp_chunk *chunk = arg;
3354 struct sctp_chunk *reply;
3355 struct sctp_ulpevent *ev;
3356
3357 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3361 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003362 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /* 10.2 H) SHUTDOWN COMPLETE notification
3365 *
3366 * When SCTP completes the shutdown procedures (section 9.2) this
3367 * notification is passed to the upper layer.
3368 */
3369 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003370 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (!ev)
3372 goto nomem;
3373
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003374 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3375 reply = sctp_make_shutdown_complete(asoc, chunk);
3376 if (!reply)
3377 goto nomem_chunk;
3378
3379 /* Do all the commands now (after allocation), so that we
3380 * have consistent state if memory allocation failes
3381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3383
3384 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3385 * stop the T2-shutdown timer,
3386 */
3387 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3388 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3389
3390 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3391 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3394 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003395 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3396 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3398
3399 /* ...and remove all record of the association. */
3400 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3401 return SCTP_DISPOSITION_DELETE_TCB;
3402
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003403nomem_chunk:
3404 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405nomem:
3406 return SCTP_DISPOSITION_NOMEM;
3407}
3408
3409/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003410 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3411 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3413 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3414 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3415 * packet must fill in the Verification Tag field of the outbound
3416 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003417 * set the T-bit in the Chunk Flags to indicate that the Verification
3418 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 *
3420 * 8) The receiver should respond to the sender of the OOTB packet with
3421 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3422 * MUST fill in the Verification Tag field of the outbound packet
3423 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003424 * packet and set the T-bit in the Chunk Flags to indicate that the
3425 * Verification Tag is reflected. After sending this ABORT, the
3426 * receiver of the OOTB packet shall discard the OOTB packet and take
3427 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003429sctp_disposition_t sctp_sf_ootb(struct net *net,
3430 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 const struct sctp_association *asoc,
3432 const sctp_subtype_t type,
3433 void *arg,
3434 sctp_cmd_seq_t *commands)
3435{
3436 struct sctp_chunk *chunk = arg;
3437 struct sk_buff *skb = chunk->skb;
3438 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003439 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 __u8 *ch_end;
3441 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003442 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003444 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445
3446 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3447 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003448 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003450 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003451 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003453 /* Report violation if chunk len overflows */
3454 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3455 if (ch_end > skb_tail_pointer(skb))
3456 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3457 commands);
3458
Vlad Yasevichece25df2007-09-07 16:30:54 -04003459 /* Now that we know we at least have a chunk header,
3460 * do things that are type appropriate.
3461 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3463 ootb_shut_ack = 1;
3464
3465 /* RFC 2960, Section 3.3.7
3466 * Moreover, under any circumstances, an endpoint that
3467 * receives an ABORT MUST NOT respond to that ABORT by
3468 * sending an ABORT of its own.
3469 */
3470 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003471 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003472
Shan Wei85c5ed42011-04-19 21:30:01 +00003473 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3474 * or a COOKIE ACK the SCTP Packet should be silently
3475 * discarded.
3476 */
3477
3478 if (SCTP_CID_COOKIE_ACK == ch->type)
3479 ootb_cookie_ack = 1;
3480
3481 if (SCTP_CID_ERROR == ch->type) {
3482 sctp_walk_errors(err, ch) {
3483 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3484 ootb_cookie_ack = 1;
3485 break;
3486 }
3487 }
3488 }
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003491 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003494 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003495 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003496 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003498 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
3501/*
3502 * Handle an "Out of the blue" SHUTDOWN ACK.
3503 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003504 * Section: 8.4 5, sctpimpguide 2.41.
3505 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003507 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3508 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3509 * packet must fill in the Verification Tag field of the outbound
3510 * packet with the Verification Tag received in the SHUTDOWN ACK and
3511 * set the T-bit in the Chunk Flags to indicate that the Verification
3512 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 *
3514 * Inputs
3515 * (endpoint, asoc, type, arg, commands)
3516 *
3517 * Outputs
3518 * (sctp_disposition_t)
3519 *
3520 * The return value is the disposition of the chunk.
3521 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003522static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3523 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 const struct sctp_association *asoc,
3525 const sctp_subtype_t type,
3526 void *arg,
3527 sctp_cmd_seq_t *commands)
3528{
3529 struct sctp_packet *packet = NULL;
3530 struct sctp_chunk *chunk = arg;
3531 struct sctp_chunk *shut;
3532
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003533 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003534 if (!packet)
3535 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003537 /* Make an SHUTDOWN_COMPLETE.
3538 * The T bit will be set if the asoc is NULL.
3539 */
3540 shut = sctp_make_shutdown_complete(asoc, chunk);
3541 if (!shut) {
3542 sctp_ootb_pkt_free(packet);
3543 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 }
3545
Marcelo Ricardo Leitner1ff01562016-12-28 09:26:32 -02003546 /* Reflect vtag if T-Bit is set */
3547 if (sctp_test_T_bit(shut))
3548 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3549
3550 /* Set the skb to the belonging sock for accounting. */
3551 shut->skb->sk = ep->base.sk;
3552
3553 sctp_packet_append_chunk(packet, shut);
3554
3555 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3556 SCTP_PACKET(packet));
3557
3558 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3559
3560 /* If the chunk length is invalid, we don't want to process
3561 * the reset of the packet.
3562 */
3563 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3564 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3565
3566 /* We need to discard the rest of the packet to prevent
3567 * potential bomming attacks from additional bundled chunks.
3568 * This is documented in SCTP Threats ID.
3569 */
3570 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
3573/*
3574 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3575 *
3576 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3577 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3578 * procedures in section 8.4 SHOULD be followed, in other words it
3579 * should be treated as an Out Of The Blue packet.
3580 * [This means that we do NOT check the Verification Tag on these
3581 * chunks. --piggy ]
3582 *
3583 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003584sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3585 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 const struct sctp_association *asoc,
3587 const sctp_subtype_t type,
3588 void *arg,
3589 sctp_cmd_seq_t *commands)
3590{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003591 struct sctp_chunk *chunk = arg;
3592
3593 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3594 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003595 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003596 commands);
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 /* Although we do have an association in this case, it corresponds
3599 * to a restarted association. So the packet is treated as an OOTB
3600 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3601 * called with a NULL association.
3602 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003603 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003604
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003605 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607
3608/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003609sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3610 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 const struct sctp_association *asoc,
3612 const sctp_subtype_t type, void *arg,
3613 sctp_cmd_seq_t *commands)
3614{
3615 struct sctp_chunk *chunk = arg;
3616 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003617 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 sctp_addiphdr_t *hdr;
3619 __u32 serial;
3620
3621 if (!sctp_vtag_verify(chunk, asoc)) {
3622 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3623 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003624 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003627 /* ADD-IP: Section 4.1.1
3628 * This chunk MUST be sent in an authenticated way by using
3629 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3630 * is received unauthenticated it MUST be silently discarded as
3631 * described in [I-D.ietf-tsvwg-sctp-auth].
3632 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003633 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003634 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3637 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003638 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 commands);
3640
3641 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3642 serial = ntohl(hdr->serial);
3643
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003644 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003645 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003646 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003647 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003648
Vlad Yasevicha08de642007-12-20 14:11:47 -08003649 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003651 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 */
3653 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003654 /* If this is the first instance of ASCONF in the packet,
3655 * we can clean our old ASCONF-ACKs.
3656 */
3657 if (!chunk->has_asconf)
3658 sctp_assoc_clean_asconf_ack_cache(asoc);
3659
3660 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3661 * expected, process the ASCONF as described below and after
3662 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3663 * the response packet and cache a copy of it (in the event it
3664 * later needs to be retransmitted).
3665 *
3666 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 */
3668 asconf_ack = sctp_process_asconf((struct sctp_association *)
3669 asoc, chunk);
3670 if (!asconf_ack)
3671 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003672 } else if (serial < asoc->peer.addip_serial + 1) {
3673 /* ADDIP 5.2 E2)
3674 * If the value found in the Sequence Number is less than the
3675 * ('Peer- Sequence-Number' + 1), simply skip to the next
3676 * ASCONF, and include in the outbound response packet
3677 * any previously cached ASCONF-ACK response that was
3678 * sent and saved that matches the Sequence Number of the
3679 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3680 * Chunk exists. This will occur when an older ASCONF
3681 * arrives out of order. In such a case, the receiver
3682 * should skip the ASCONF Chunk and not include ASCONF-ACK
3683 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003685 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3686 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003688
3689 /* Reset the transport so that we select the correct one
3690 * this time around. This is to make sure that we don't
3691 * accidentally use a stale transport that's been removed.
3692 */
3693 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003695 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return SCTP_DISPOSITION_DISCARD;
3699 }
3700
Vlad Yasevicha08de642007-12-20 14:11:47 -08003701 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3702 * containing the ASCONF-ACK Chunks MUST be the source address of
3703 * the SCTP packet that held the ASCONF Chunks.
3704 *
3705 * To do this properly, we'll set the destination address of the chunk
3706 * and at the transmit time, will try look up the transport to use.
3707 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003708 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003710 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003712 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003713 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003714 ((struct sctp_association *)asoc)->new_transport = NULL;
3715 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 return SCTP_DISPOSITION_CONSUME;
3718}
3719
3720/*
3721 * ADDIP Section 4.3 General rules for address manipulation
3722 * When building TLV parameters for the ASCONF Chunk that will add or
3723 * delete IP addresses the D0 to D13 rules should be applied:
3724 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003725sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3726 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003728 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 sctp_cmd_seq_t *commands)
3730{
3731 struct sctp_chunk *asconf_ack = arg;
3732 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3733 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003734 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 sctp_addiphdr_t *addip_hdr;
3736 __u32 sent_serial, rcvd_serial;
3737
3738 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3739 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3740 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003741 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
3743
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003744 /* ADD-IP, Section 4.1.2:
3745 * This chunk MUST be sent in an authenticated way by using
3746 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3747 * is received unauthenticated it MUST be silently discarded as
3748 * described in [I-D.ietf-tsvwg-sctp-auth].
3749 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003750 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003751 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* Make sure that the ADDIP chunk has a valid length. */
3754 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003755 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 commands);
3757
3758 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3759 rcvd_serial = ntohl(addip_hdr->serial);
3760
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003761 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003762 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003763 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003764 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 if (last_asconf) {
3767 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3768 sent_serial = ntohl(addip_hdr->serial);
3769 } else {
3770 sent_serial = asoc->addip_serial - 1;
3771 }
3772
3773 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3774 * equal to the next serial number to be used but no ASCONF chunk is
3775 * outstanding the endpoint MUST ABORT the association. Note that a
3776 * sequence number is greater than if it is no more than 2^^31-1
3777 * larger than the current sequence number (using serial arithmetic).
3778 */
3779 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3780 !(asoc->addip_last_asconf)) {
3781 abort = sctp_make_abort(asoc, asconf_ack,
3782 sizeof(sctp_errhdr_t));
3783 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003784 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3786 SCTP_CHUNK(abort));
3787 }
3788 /* We are going to ABORT, so we might as well stop
3789 * processing the rest of the chunks in the packet.
3790 */
3791 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3792 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003793 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003794 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003795 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003797 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003798 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3799 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 return SCTP_DISPOSITION_ABORT;
3801 }
3802
3803 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3804 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3805 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3806
3807 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003808 asconf_ack)) {
3809 /* Successfully processed ASCONF_ACK. We can
3810 * release the next asconf if we have one.
3811 */
3812 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3813 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817 abort = sctp_make_abort(asoc, asconf_ack,
3818 sizeof(sctp_errhdr_t));
3819 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003820 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3822 SCTP_CHUNK(abort));
3823 }
3824 /* We are going to ABORT, so we might as well stop
3825 * processing the rest of the chunks in the packet.
3826 */
wangweidongcb3f8372013-12-23 12:16:50 +08003827 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003828 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003829 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003831 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003832 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3833 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return SCTP_DISPOSITION_ABORT;
3835 }
3836
3837 return SCTP_DISPOSITION_DISCARD;
3838}
3839
Xin Long2040d3d2017-02-17 12:45:42 +08003840/* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3841sctp_disposition_t sctp_sf_do_reconf(struct net *net,
3842 const struct sctp_endpoint *ep,
3843 const struct sctp_association *asoc,
3844 const sctp_subtype_t type, void *arg,
3845 sctp_cmd_seq_t *commands)
3846{
3847 struct sctp_paramhdr *err_param = NULL;
3848 struct sctp_chunk *chunk = arg;
3849 struct sctp_reconf_chunk *hdr;
3850 union sctp_params param;
3851
3852 if (!sctp_vtag_verify(chunk, asoc)) {
3853 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3854 SCTP_NULL());
3855 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3856 }
3857
3858 /* Make sure that the RECONF chunk has a valid length. */
3859 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3860 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3861 commands);
3862
3863 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3864 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3865 (void *)err_param, commands);
3866
3867 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3868 sctp_walk_params(param, hdr, params) {
3869 struct sctp_chunk *reply = NULL;
3870 struct sctp_ulpevent *ev = NULL;
3871
3872 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3873 reply = sctp_process_strreset_outreq(
3874 (struct sctp_association *)asoc, param, &ev);
3875 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3876 reply = sctp_process_strreset_inreq(
3877 (struct sctp_association *)asoc, param, &ev);
Xin Long692787c2017-03-10 12:11:07 +08003878 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3879 reply = sctp_process_strreset_tsnreq(
3880 (struct sctp_association *)asoc, param, &ev);
Xin Long50a41592017-03-10 12:11:09 +08003881 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3882 reply = sctp_process_strreset_addstrm_out(
3883 (struct sctp_association *)asoc, param, &ev);
Xin Longc5c4ebb2017-03-10 12:11:10 +08003884 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3885 reply = sctp_process_strreset_addstrm_in(
3886 (struct sctp_association *)asoc, param, &ev);
Xin Long11ae76e2017-03-10 12:11:11 +08003887 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3888 reply = sctp_process_strreset_resp(
3889 (struct sctp_association *)asoc, param, &ev);
Xin Long2040d3d2017-02-17 12:45:42 +08003890
3891 if (ev)
3892 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3893 SCTP_ULPEVENT(ev));
3894
3895 if (reply)
3896 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3897 SCTP_CHUNK(reply));
3898 }
3899
3900 return SCTP_DISPOSITION_CONSUME;
3901}
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903/*
3904 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3905 *
3906 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3907 * its cumulative TSN point to the value carried in the FORWARD TSN
3908 * chunk, and then MUST further advance its cumulative TSN point locally
3909 * if possible.
3910 * After the above processing, the data receiver MUST stop reporting any
3911 * missing TSNs earlier than or equal to the new cumulative TSN point.
3912 *
3913 * Verification Tag: 8.5 Verification Tag [Normal verification]
3914 *
3915 * The return value is the disposition of the chunk.
3916 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003917sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3918 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 const struct sctp_association *asoc,
3920 const sctp_subtype_t type,
3921 void *arg,
3922 sctp_cmd_seq_t *commands)
3923{
3924 struct sctp_chunk *chunk = arg;
3925 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003926 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 __u16 len;
3928 __u32 tsn;
3929
3930 if (!sctp_vtag_verify(chunk, asoc)) {
3931 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3932 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003933 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
3935
Xin Longd15c9ed2017-02-03 17:37:06 +08003936 if (!asoc->peer.prsctp_capable)
3937 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
3938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 /* Make sure that the FORWARD_TSN chunk has valid length. */
3940 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003941 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 commands);
3943
3944 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3945 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3946 len = ntohs(chunk->chunk_hdr->length);
3947 len -= sizeof(struct sctp_chunkhdr);
3948 skb_pull(chunk->skb, len);
3949
3950 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003951 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
3953 /* The TSN is too high--silently discard the chunk and count on it
3954 * getting retransmitted later.
3955 */
3956 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3957 goto discard_noforce;
3958
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003959 /* Silently discard the chunk if stream-id is not valid */
3960 sctp_walk_fwdtsn(skip, chunk) {
Xin Longafe89962017-03-31 17:57:28 +08003961 if (ntohs(skip->stream) >= asoc->stream->incnt)
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003962 goto discard_noforce;
3963 }
3964
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3966 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003967 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003971 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3973 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3974 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003977 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 */
3979 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 return SCTP_DISPOSITION_CONSUME;
3982
3983discard_noforce:
3984 return SCTP_DISPOSITION_DISCARD;
3985}
3986
3987sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003988 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 const struct sctp_endpoint *ep,
3990 const struct sctp_association *asoc,
3991 const sctp_subtype_t type,
3992 void *arg,
3993 sctp_cmd_seq_t *commands)
3994{
3995 struct sctp_chunk *chunk = arg;
3996 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003997 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 __u16 len;
3999 __u32 tsn;
4000
4001 if (!sctp_vtag_verify(chunk, asoc)) {
4002 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4003 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004004 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 }
4006
Xin Longd15c9ed2017-02-03 17:37:06 +08004007 if (!asoc->peer.prsctp_capable)
4008 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4011 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004012 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 commands);
4014
4015 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4016 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4017 len = ntohs(chunk->chunk_hdr->length);
4018 len -= sizeof(struct sctp_chunkhdr);
4019 skb_pull(chunk->skb, len);
4020
4021 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02004022 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
4024 /* The TSN is too high--silently discard the chunk and count on it
4025 * getting retransmitted later.
4026 */
4027 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4028 goto gen_shutdown;
4029
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004030 /* Silently discard the chunk if stream-id is not valid */
4031 sctp_walk_fwdtsn(skip, chunk) {
Xin Longafe89962017-03-31 17:57:28 +08004032 if (ntohs(skip->stream) >= asoc->stream->incnt)
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08004033 goto gen_shutdown;
4034 }
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4037 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004038 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004040
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 /* Go a head and force a SACK, since we are shutting down. */
4042gen_shutdown:
4043 /* Implementor's Guide.
4044 *
4045 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4046 * respond to each received packet containing one or more DATA chunk(s)
4047 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4048 */
4049 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4050 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4051 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4052 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4053
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004054 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055}
4056
4057/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004058 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004059 *
4060 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4061 * Identifier field. If this algorithm was not specified by the
4062 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4063 * during association setup, the AUTH chunk and all chunks after it MUST
4064 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4065 * defined in Section 4.1.
4066 *
4067 * If an endpoint with no shared key receives a Shared Key Identifier
4068 * other than 0, it MUST silently discard all authenticated chunks. If
4069 * the endpoint has at least one endpoint pair shared key for the peer,
4070 * it MUST use the key specified by the Shared Key Identifier if a
4071 * key has been configured for that Shared Key Identifier. If no
4072 * endpoint pair shared key has been configured for that Shared Key
4073 * Identifier, all authenticated chunks MUST be silently discarded.
4074 *
4075 * Verification Tag: 8.5 Verification Tag [Normal verification]
4076 *
4077 * The return value is the disposition of the chunk.
4078 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004079static sctp_ierror_t sctp_sf_authenticate(struct net *net,
4080 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004081 const struct sctp_association *asoc,
4082 const sctp_subtype_t type,
4083 struct sctp_chunk *chunk)
4084{
4085 struct sctp_authhdr *auth_hdr;
4086 struct sctp_hmac *hmac;
4087 unsigned int sig_len;
4088 __u16 key_id;
4089 __u8 *save_digest;
4090 __u8 *digest;
4091
4092 /* Pull in the auth header, so we can do some more verification */
4093 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4094 chunk->subh.auth_hdr = auth_hdr;
4095 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4096
Masanari Iida02582e92012-08-22 19:11:26 +09004097 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004098 * chunk.
4099 */
4100 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4101 return SCTP_IERROR_AUTH_BAD_HMAC;
4102
4103 /* Make sure that the provided shared key identifier has been
4104 * configured
4105 */
4106 key_id = ntohs(auth_hdr->shkey_id);
4107 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4108 return SCTP_IERROR_AUTH_BAD_KEYID;
4109
4110
4111 /* Make sure that the length of the signature matches what
4112 * we expect.
4113 */
4114 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4115 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4116 if (sig_len != hmac->hmac_len)
4117 return SCTP_IERROR_PROTO_VIOLATION;
4118
4119 /* Now that we've done validation checks, we can compute and
4120 * verify the hmac. The steps involved are:
4121 * 1. Save the digest from the chunk.
4122 * 2. Zero out the digest in the chunk.
4123 * 3. Compute the new digest
4124 * 4. Compare saved and new digests.
4125 */
4126 digest = auth_hdr->hmac;
4127 skb_pull(chunk->skb, sig_len);
4128
4129 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4130 if (!save_digest)
4131 goto nomem;
4132
4133 memset(digest, 0, sig_len);
4134
4135 sctp_auth_calculate_hmac(asoc, chunk->skb,
4136 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4137 GFP_ATOMIC);
4138
4139 /* Discard the packet if the digests do not match */
4140 if (memcmp(save_digest, digest, sig_len)) {
4141 kfree(save_digest);
4142 return SCTP_IERROR_BAD_SIG;
4143 }
4144
4145 kfree(save_digest);
4146 chunk->auth = 1;
4147
4148 return SCTP_IERROR_NO_ERROR;
4149nomem:
4150 return SCTP_IERROR_NOMEM;
4151}
4152
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004153sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4154 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004155 const struct sctp_association *asoc,
4156 const sctp_subtype_t type,
4157 void *arg,
4158 sctp_cmd_seq_t *commands)
4159{
4160 struct sctp_authhdr *auth_hdr;
4161 struct sctp_chunk *chunk = arg;
4162 struct sctp_chunk *err_chunk;
4163 sctp_ierror_t error;
4164
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004165 /* Make sure that the peer has AUTH capable */
4166 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004167 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004168
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004169 if (!sctp_vtag_verify(chunk, asoc)) {
4170 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4171 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004172 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004173 }
4174
4175 /* Make sure that the AUTH chunk has valid length. */
4176 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004177 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004178 commands);
4179
4180 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004181 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004182 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004183 case SCTP_IERROR_AUTH_BAD_HMAC:
4184 /* Generate the ERROR chunk and discard the rest
4185 * of the packet
4186 */
4187 err_chunk = sctp_make_op_error(asoc, chunk,
4188 SCTP_ERROR_UNSUP_HMAC,
4189 &auth_hdr->hmac_id,
4190 sizeof(__u16), 0);
4191 if (err_chunk) {
4192 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4193 SCTP_CHUNK(err_chunk));
4194 }
4195 /* Fall Through */
4196 case SCTP_IERROR_AUTH_BAD_KEYID:
4197 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004198 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004199
4200 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004201 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004202 commands);
4203
4204 case SCTP_IERROR_NOMEM:
4205 return SCTP_DISPOSITION_NOMEM;
4206
4207 default: /* Prevent gcc warnings */
4208 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004209 }
4210
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004211 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4212 struct sctp_ulpevent *ev;
4213
4214 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4215 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4216
4217 if (!ev)
4218 return -ENOMEM;
4219
4220 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4221 SCTP_ULPEVENT(ev));
4222 }
4223
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004224 return SCTP_DISPOSITION_CONSUME;
4225}
4226
4227/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 * Process an unknown chunk.
4229 *
4230 * Section: 3.2. Also, 2.1 in the implementor's guide.
4231 *
4232 * Chunk Types are encoded such that the highest-order two bits specify
4233 * the action that must be taken if the processing endpoint does not
4234 * recognize the Chunk Type.
4235 *
4236 * 00 - Stop processing this SCTP packet and discard it, do not process
4237 * any further chunks within it.
4238 *
4239 * 01 - Stop processing this SCTP packet and discard it, do not process
4240 * any further chunks within it, and report the unrecognized
4241 * chunk in an 'Unrecognized Chunk Type'.
4242 *
4243 * 10 - Skip this chunk and continue processing.
4244 *
4245 * 11 - Skip this chunk and continue processing, but report in an ERROR
4246 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4247 *
4248 * The return value is the disposition of the chunk.
4249 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004250sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4251 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 const struct sctp_association *asoc,
4253 const sctp_subtype_t type,
4254 void *arg,
4255 sctp_cmd_seq_t *commands)
4256{
4257 struct sctp_chunk *unk_chunk = arg;
4258 struct sctp_chunk *err_chunk;
4259 sctp_chunkhdr_t *hdr;
4260
Daniel Borkmannbb333812013-06-28 19:49:40 +02004261 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262
4263 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004264 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
4266 /* Make sure that the chunk has a valid length.
4267 * Since we don't know the chunk type, we use a general
4268 * chunkhdr structure to make a comparison.
4269 */
4270 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004271 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 commands);
4273
4274 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4275 case SCTP_CID_ACTION_DISCARD:
4276 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004277 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 /* Generate an ERROR chunk as response. */
4280 hdr = unk_chunk->chunk_hdr;
4281 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4282 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004283 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004284 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 if (err_chunk) {
4286 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4287 SCTP_CHUNK(err_chunk));
4288 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004289
4290 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004291 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 case SCTP_CID_ACTION_SKIP:
4294 /* Skip the chunk. */
4295 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 case SCTP_CID_ACTION_SKIP_ERR:
4297 /* Generate an ERROR chunk as response. */
4298 hdr = unk_chunk->chunk_hdr;
4299 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4300 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004301 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004302 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (err_chunk) {
4304 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4305 SCTP_CHUNK(err_chunk));
4306 }
4307 /* Skip the chunk. */
4308 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 default:
4310 break;
4311 }
4312
4313 return SCTP_DISPOSITION_DISCARD;
4314}
4315
4316/*
4317 * Discard the chunk.
4318 *
4319 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4320 * [Too numerous to mention...]
4321 * Verification Tag: No verification needed.
4322 * Inputs
4323 * (endpoint, asoc, chunk)
4324 *
4325 * Outputs
4326 * (asoc, reply_msg, msg_up, timers, counters)
4327 *
4328 * The return value is the disposition of the chunk.
4329 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004330sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4331 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 const struct sctp_association *asoc,
4333 const sctp_subtype_t type,
4334 void *arg,
4335 sctp_cmd_seq_t *commands)
4336{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004337 struct sctp_chunk *chunk = arg;
4338
4339 /* Make sure that the chunk has a valid length.
4340 * Since we don't know the chunk type, we use a general
4341 * chunkhdr structure to make a comparison.
4342 */
4343 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004344 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004345 commands);
4346
Daniel Borkmannbb333812013-06-28 19:49:40 +02004347 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return SCTP_DISPOSITION_DISCARD;
4350}
4351
4352/*
4353 * Discard the whole packet.
4354 *
4355 * Section: 8.4 2)
4356 *
4357 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4358 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 *
4360 * Verification Tag: No verification necessary
4361 *
4362 * Inputs
4363 * (endpoint, asoc, chunk)
4364 *
4365 * Outputs
4366 * (asoc, reply_msg, msg_up, timers, counters)
4367 *
4368 * The return value is the disposition of the chunk.
4369 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004370sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4371 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 const struct sctp_association *asoc,
4373 const sctp_subtype_t type,
4374 void *arg,
4375 sctp_cmd_seq_t *commands)
4376{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004377 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4379
4380 return SCTP_DISPOSITION_CONSUME;
4381}
4382
4383
4384/*
4385 * The other end is violating protocol.
4386 *
4387 * Section: Not specified
4388 * Verification Tag: Not specified
4389 * Inputs
4390 * (endpoint, asoc, chunk)
4391 *
4392 * Outputs
4393 * (asoc, reply_msg, msg_up, timers, counters)
4394 *
4395 * We simply tag the chunk as a violation. The state machine will log
4396 * the violation and continue.
4397 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004398sctp_disposition_t sctp_sf_violation(struct net *net,
4399 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 const struct sctp_association *asoc,
4401 const sctp_subtype_t type,
4402 void *arg,
4403 sctp_cmd_seq_t *commands)
4404{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004405 struct sctp_chunk *chunk = arg;
4406
4407 /* Make sure that the chunk has a valid length. */
4408 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004409 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004410 commands);
4411
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 return SCTP_DISPOSITION_VIOLATION;
4413}
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004416 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004418static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004419 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004420 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004423 sctp_cmd_seq_t *commands,
4424 const __u8 *payload,
4425 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004427 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 struct sctp_chunk *chunk = arg;
4429 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004431 /* SCTP-AUTH, Section 6.3:
4432 * It should be noted that if the receiver wants to tear
4433 * down an association in an authenticated way only, the
4434 * handling of malformed packets should not result in
4435 * tearing down the association.
4436 *
4437 * This means that if we only want to abort associations
4438 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004439 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004440 * was malformed.
4441 */
4442 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4443 goto discard;
4444
Jesper Juhl9abed242007-11-11 23:57:49 +01004445 /* Make the abort chunk. */
4446 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4447 if (!abort)
4448 goto nomem;
4449
Vlad Yasevichece25df2007-09-07 16:30:54 -04004450 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004451 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4452 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4453 !asoc->peer.i.init_tag) {
4454 sctp_initack_chunk_t *initack;
4455
4456 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4457 if (!sctp_chunk_length_valid(chunk,
4458 sizeof(sctp_initack_chunk_t)))
4459 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4460 else {
4461 unsigned int inittag;
4462
4463 inittag = ntohl(initack->init_hdr.init_tag);
4464 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4465 SCTP_U32(inittag));
4466 }
4467 }
4468
Vlad Yasevichece25df2007-09-07 16:30:54 -04004469 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004470 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Vlad Yasevichece25df2007-09-07 16:30:54 -04004472 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4473 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4474 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4475 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4476 SCTP_ERROR(ECONNREFUSED));
4477 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4478 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4479 } else {
4480 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4481 SCTP_ERROR(ECONNABORTED));
4482 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4483 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004484 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004487 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004488
4489 if (!packet)
4490 goto nomem_pkt;
4491
4492 if (sctp_test_T_bit(abort))
4493 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4494
4495 abort->skb->sk = ep->base.sk;
4496
4497 sctp_packet_append_chunk(packet, abort);
4498
4499 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4500 SCTP_PACKET(packet));
4501
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004502 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 }
4504
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004505 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004506
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004507discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004508 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 return SCTP_DISPOSITION_ABORT;
4510
Vlad Yasevichece25df2007-09-07 16:30:54 -04004511nomem_pkt:
4512 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513nomem:
4514 return SCTP_DISPOSITION_NOMEM;
4515}
4516
Wei Yongjunaecedea2007-08-02 16:57:44 +08004517/*
4518 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004519 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004520 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004521 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004522 *
4523 * We inform the other end by sending an ABORT with a Protocol Violation
4524 * error code.
4525 *
4526 * Section: Not specified
4527 * Verification Tag: Nothing to do
4528 * Inputs
4529 * (endpoint, asoc, chunk)
4530 *
4531 * Outputs
4532 * (reply_msg, msg_up, counters)
4533 *
4534 * Generate an ABORT chunk and terminate the association.
4535 */
4536static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004537 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004538 const struct sctp_endpoint *ep,
4539 const struct sctp_association *asoc,
4540 const sctp_subtype_t type,
4541 void *arg,
4542 sctp_cmd_seq_t *commands)
4543{
wangweidongcb3f8372013-12-23 12:16:50 +08004544 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004545
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004546 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004547 sizeof(err_str));
4548}
4549
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004550/*
4551 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004552 * If the length is smaller than the minimum length of a given parameter,
4553 * or accumulated length in multi parameters exceeds the end of the chunk,
4554 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004555 */
4556static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004557 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004558 const struct sctp_endpoint *ep,
4559 const struct sctp_association *asoc,
4560 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004561 void *arg, void *ext,
4562 sctp_cmd_seq_t *commands)
4563{
4564 struct sctp_chunk *chunk = arg;
4565 struct sctp_paramhdr *param = ext;
4566 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004567
Wei Yongjunba016672008-09-30 05:32:24 -07004568 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4569 goto discard;
4570
4571 /* Make the abort chunk. */
4572 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4573 if (!abort)
4574 goto nomem;
4575
4576 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004577 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004578
4579 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4580 SCTP_ERROR(ECONNABORTED));
4581 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4582 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004583 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4584 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004585
4586discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004587 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004588 return SCTP_DISPOSITION_ABORT;
4589nomem:
4590 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004591}
4592
Wei Yongjunaecedea2007-08-02 16:57:44 +08004593/* Handle a protocol violation when the peer trying to advance the
4594 * cumulative tsn ack to a point beyond the max tsn currently sent.
4595 *
4596 * We inform the other end by sending an ABORT with a Protocol Violation
4597 * error code.
4598 */
4599static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004600 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004601 const struct sctp_endpoint *ep,
4602 const struct sctp_association *asoc,
4603 const sctp_subtype_t type,
4604 void *arg,
4605 sctp_cmd_seq_t *commands)
4606{
wangweidongcb3f8372013-12-23 12:16:50 +08004607 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004608
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004609 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004610 sizeof(err_str));
4611}
4612
Vlad Yasevichece25df2007-09-07 16:30:54 -04004613/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004614 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004615 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004616 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004617 * on the path and we may not want to continue this communication.
4618 */
4619static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004620 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004621 const struct sctp_endpoint *ep,
4622 const struct sctp_association *asoc,
4623 const sctp_subtype_t type,
4624 void *arg,
4625 sctp_cmd_seq_t *commands)
4626{
wangweidongcb3f8372013-12-23 12:16:50 +08004627 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004628
4629 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004630 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004631
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004632 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004633 sizeof(err_str));
4634}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635/***************************************************************************
4636 * These are the state functions for handling primitive (Section 10) events.
4637 ***************************************************************************/
4638/*
4639 * sctp_sf_do_prm_asoc
4640 *
4641 * Section: 10.1 ULP-to-SCTP
4642 * B) Associate
4643 *
4644 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4645 * outbound stream count)
4646 * -> association id [,destination transport addr list] [,outbound stream
4647 * count]
4648 *
4649 * This primitive allows the upper layer to initiate an association to a
4650 * specific peer endpoint.
4651 *
4652 * The peer endpoint shall be specified by one of the transport addresses
4653 * which defines the endpoint (see Section 1.4). If the local SCTP
4654 * instance has not been initialized, the ASSOCIATE is considered an
4655 * error.
4656 * [This is not relevant for the kernel implementation since we do all
4657 * initialization at boot time. It we hadn't initialized we wouldn't
4658 * get anywhere near this code.]
4659 *
4660 * An association id, which is a local handle to the SCTP association,
4661 * will be returned on successful establishment of the association. If
4662 * SCTP is not able to open an SCTP association with the peer endpoint,
4663 * an error is returned.
4664 * [In the kernel implementation, the struct sctp_association needs to
4665 * be created BEFORE causing this primitive to run.]
4666 *
4667 * Other association parameters may be returned, including the
4668 * complete destination transport addresses of the peer as well as the
4669 * outbound stream count of the local endpoint. One of the transport
4670 * address from the returned destination addresses will be selected by
4671 * the local endpoint as default primary path for sending SCTP packets
4672 * to this peer. The returned "destination transport addr list" can
4673 * be used by the ULP to change the default primary path or to force
4674 * sending a packet to a specific transport address. [All of this
4675 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4676 * function.]
4677 *
4678 * Mandatory attributes:
4679 *
4680 * o local SCTP instance name - obtained from the INITIALIZE operation.
4681 * [This is the argument asoc.]
4682 * o destination transport addr - specified as one of the transport
4683 * addresses of the peer endpoint with which the association is to be
4684 * established.
4685 * [This is asoc->peer.active_path.]
4686 * o outbound stream count - the number of outbound streams the ULP
4687 * would like to open towards this peer endpoint.
4688 * [BUG: This is not currently implemented.]
4689 * Optional attributes:
4690 *
4691 * None.
4692 *
4693 * The return value is a disposition.
4694 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004695sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4696 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 const struct sctp_association *asoc,
4698 const sctp_subtype_t type,
4699 void *arg,
4700 sctp_cmd_seq_t *commands)
4701{
4702 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004703 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
4705 /* The comment below says that we enter COOKIE-WAIT AFTER
4706 * sending the INIT, but that doesn't actually work in our
4707 * implementation...
4708 */
4709 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4710 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4711
4712 /* RFC 2960 5.1 Normal Establishment of an Association
4713 *
4714 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4715 * must provide its Verification Tag (Tag_A) in the Initiate
4716 * Tag field. Tag_A SHOULD be a random number in the range of
4717 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4718 */
4719
4720 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4721 if (!repl)
4722 goto nomem;
4723
Daniel Borkmann405426f2013-06-14 18:24:05 +02004724 /* Choose transport for INIT. */
4725 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4726 SCTP_CHUNK(repl));
4727
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 /* Cast away the const modifier, as we want to just
4729 * rerun it through as a sideffect.
4730 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004731 my_asoc = (struct sctp_association *)asoc;
4732 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /* After sending the INIT, "A" starts the T1-init timer and
4735 * enters the COOKIE-WAIT state.
4736 */
4737 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4738 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4739 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4740 return SCTP_DISPOSITION_CONSUME;
4741
4742nomem:
4743 return SCTP_DISPOSITION_NOMEM;
4744}
4745
4746/*
4747 * Process the SEND primitive.
4748 *
4749 * Section: 10.1 ULP-to-SCTP
4750 * E) Send
4751 *
4752 * Format: SEND(association id, buffer address, byte count [,context]
4753 * [,stream id] [,life time] [,destination transport address]
4754 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4755 * -> result
4756 *
4757 * This is the main method to send user data via SCTP.
4758 *
4759 * Mandatory attributes:
4760 *
4761 * o association id - local handle to the SCTP association
4762 *
4763 * o buffer address - the location where the user message to be
4764 * transmitted is stored;
4765 *
4766 * o byte count - The size of the user data in number of bytes;
4767 *
4768 * Optional attributes:
4769 *
4770 * o context - an optional 32 bit integer that will be carried in the
4771 * sending failure notification to the ULP if the transportation of
4772 * this User Message fails.
4773 *
4774 * o stream id - to indicate which stream to send the data on. If not
4775 * specified, stream 0 will be used.
4776 *
4777 * o life time - specifies the life time of the user data. The user data
4778 * will not be sent by SCTP after the life time expires. This
4779 * parameter can be used to avoid efforts to transmit stale
4780 * user messages. SCTP notifies the ULP if the data cannot be
4781 * initiated to transport (i.e. sent to the destination via SCTP's
4782 * send primitive) within the life time variable. However, the
4783 * user data will be transmitted if SCTP has attempted to transmit a
4784 * chunk before the life time expired.
4785 *
4786 * o destination transport address - specified as one of the destination
4787 * transport addresses of the peer endpoint to which this packet
4788 * should be sent. Whenever possible, SCTP should use this destination
4789 * transport address for sending the packets, instead of the current
4790 * primary path.
4791 *
4792 * o unorder flag - this flag, if present, indicates that the user
4793 * would like the data delivered in an unordered fashion to the peer
4794 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4795 * message).
4796 *
4797 * o no-bundle flag - instructs SCTP not to bundle this user data with
4798 * other outbound DATA chunks. SCTP MAY still bundle even when
4799 * this flag is present, when faced with network congestion.
4800 *
4801 * o payload protocol-id - A 32 bit unsigned integer that is to be
4802 * passed to the peer indicating the type of payload protocol data
4803 * being transmitted. This value is passed as opaque data by SCTP.
4804 *
4805 * The return value is the disposition.
4806 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004807sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4808 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 const struct sctp_association *asoc,
4810 const sctp_subtype_t type,
4811 void *arg,
4812 sctp_cmd_seq_t *commands)
4813{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004814 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004816 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 return SCTP_DISPOSITION_CONSUME;
4818}
4819
4820/*
4821 * Process the SHUTDOWN primitive.
4822 *
4823 * Section: 10.1:
4824 * C) Shutdown
4825 *
4826 * Format: SHUTDOWN(association id)
4827 * -> result
4828 *
4829 * Gracefully closes an association. Any locally queued user data
4830 * will be delivered to the peer. The association will be terminated only
4831 * after the peer acknowledges all the SCTP packets sent. A success code
4832 * will be returned on successful termination of the association. If
4833 * attempting to terminate the association results in a failure, an error
4834 * code shall be returned.
4835 *
4836 * Mandatory attributes:
4837 *
4838 * o association id - local handle to the SCTP association
4839 *
4840 * Optional attributes:
4841 *
4842 * None.
4843 *
4844 * The return value is the disposition.
4845 */
4846sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004847 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 const struct sctp_endpoint *ep,
4849 const struct sctp_association *asoc,
4850 const sctp_subtype_t type,
4851 void *arg,
4852 sctp_cmd_seq_t *commands)
4853{
4854 int disposition;
4855
4856 /* From 9.2 Shutdown of an Association
4857 * Upon receipt of the SHUTDOWN primitive from its upper
4858 * layer, the endpoint enters SHUTDOWN-PENDING state and
4859 * remains there until all outstanding data has been
4860 * acknowledged by its peer. The endpoint accepts no new data
4861 * from its upper layer, but retransmits data to the far end
4862 * if necessary to fill gaps.
4863 */
4864 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4865 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4866
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 disposition = SCTP_DISPOSITION_CONSUME;
4868 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004869 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 arg, commands);
4871 }
4872 return disposition;
4873}
4874
4875/*
4876 * Process the ABORT primitive.
4877 *
4878 * Section: 10.1:
4879 * C) Abort
4880 *
4881 * Format: Abort(association id [, cause code])
4882 * -> result
4883 *
4884 * Ungracefully closes an association. Any locally queued user data
4885 * will be discarded and an ABORT chunk is sent to the peer. A success code
4886 * will be returned on successful abortion of the association. If
4887 * attempting to abort the association results in a failure, an error
4888 * code shall be returned.
4889 *
4890 * Mandatory attributes:
4891 *
4892 * o association id - local handle to the SCTP association
4893 *
4894 * Optional attributes:
4895 *
4896 * o cause code - reason of the abort to be passed to the peer
4897 *
4898 * None.
4899 *
4900 * The return value is the disposition.
4901 */
4902sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004903 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 const struct sctp_endpoint *ep,
4905 const struct sctp_association *asoc,
4906 const sctp_subtype_t type,
4907 void *arg,
4908 sctp_cmd_seq_t *commands)
4909{
4910 /* From 9.1 Abort of an Association
4911 * Upon receipt of the ABORT primitive from its upper
4912 * layer, the endpoint enters CLOSED state and
4913 * discard all outstanding data has been
4914 * acknowledged by its peer. The endpoint accepts no new data
4915 * from its upper layer, but retransmits data to the far end
4916 * if necessary to fill gaps.
4917 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004918 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919
Xin Long068d8bd2015-12-29 17:49:25 +08004920 if (abort)
4921 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 /* Even if we can't send the ABORT due to low memory delete the
4924 * TCB. This is a departure from our typical NOMEM handling.
4925 */
4926
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004927 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4928 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 /* Delete the established association. */
4930 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004931 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004933 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4934 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004936 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
4939/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004940sctp_disposition_t sctp_sf_error_closed(struct net *net,
4941 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 const struct sctp_association *asoc,
4943 const sctp_subtype_t type,
4944 void *arg,
4945 sctp_cmd_seq_t *commands)
4946{
4947 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4948 return SCTP_DISPOSITION_CONSUME;
4949}
4950
4951/* We tried an illegal operation on an association which is shutting
4952 * down.
4953 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004954sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4955 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 const struct sctp_association *asoc,
4957 const sctp_subtype_t type,
4958 void *arg,
4959 sctp_cmd_seq_t *commands)
4960{
4961 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4962 SCTP_ERROR(-ESHUTDOWN));
4963 return SCTP_DISPOSITION_CONSUME;
4964}
4965
4966/*
4967 * sctp_cookie_wait_prm_shutdown
4968 *
4969 * Section: 4 Note: 2
4970 * Verification Tag:
4971 * Inputs
4972 * (endpoint, asoc)
4973 *
4974 * The RFC does not explicitly address this issue, but is the route through the
4975 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4976 *
4977 * Outputs
4978 * (timers)
4979 */
4980sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004981 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 const struct sctp_endpoint *ep,
4983 const struct sctp_association *asoc,
4984 const sctp_subtype_t type,
4985 void *arg,
4986 sctp_cmd_seq_t *commands)
4987{
4988 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4989 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4990
4991 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4992 SCTP_STATE(SCTP_STATE_CLOSED));
4993
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004994 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995
4996 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4997
4998 return SCTP_DISPOSITION_DELETE_TCB;
4999}
5000
5001/*
5002 * sctp_cookie_echoed_prm_shutdown
5003 *
5004 * Section: 4 Note: 2
5005 * Verification Tag:
5006 * Inputs
5007 * (endpoint, asoc)
5008 *
5009 * The RFC does not explcitly address this issue, but is the route through the
5010 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5011 *
5012 * Outputs
5013 * (timers)
5014 */
5015sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005016 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 const struct sctp_endpoint *ep,
5018 const struct sctp_association *asoc,
5019 const sctp_subtype_t type,
5020 void *arg, sctp_cmd_seq_t *commands)
5021{
5022 /* There is a single T1 timer, so we should be able to use
5023 * common function with the COOKIE-WAIT state.
5024 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005025 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026}
5027
5028/*
5029 * sctp_sf_cookie_wait_prm_abort
5030 *
5031 * Section: 4 Note: 2
5032 * Verification Tag:
5033 * Inputs
5034 * (endpoint, asoc)
5035 *
5036 * The RFC does not explicitly address this issue, but is the route through the
5037 * state table when someone issues an abort while in COOKIE_WAIT state.
5038 *
5039 * Outputs
5040 * (timers)
5041 */
5042sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005043 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 const struct sctp_endpoint *ep,
5045 const struct sctp_association *asoc,
5046 const sctp_subtype_t type,
5047 void *arg,
5048 sctp_cmd_seq_t *commands)
5049{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07005050 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051
5052 /* Stop T1-init timer */
5053 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5054 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055
Xin Long068d8bd2015-12-29 17:49:25 +08005056 if (abort)
5057 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058
5059 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5060 SCTP_STATE(SCTP_STATE_CLOSED));
5061
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005062 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063
5064 /* Even if we can't send the ABORT due to low memory delete the
5065 * TCB. This is a departure from our typical NOMEM handling.
5066 */
5067
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005068 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5069 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 /* Delete the established association. */
5071 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005072 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005074 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075}
5076
5077/*
5078 * sctp_sf_cookie_echoed_prm_abort
5079 *
5080 * Section: 4 Note: 3
5081 * Verification Tag:
5082 * Inputs
5083 * (endpoint, asoc)
5084 *
5085 * The RFC does not explcitly address this issue, but is the route through the
5086 * state table when someone issues an abort while in COOKIE_ECHOED state.
5087 *
5088 * Outputs
5089 * (timers)
5090 */
5091sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005092 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 const struct sctp_endpoint *ep,
5094 const struct sctp_association *asoc,
5095 const sctp_subtype_t type,
5096 void *arg,
5097 sctp_cmd_seq_t *commands)
5098{
5099 /* There is a single T1 timer, so we should be able to use
5100 * common function with the COOKIE-WAIT state.
5101 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005102 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
5105/*
5106 * sctp_sf_shutdown_pending_prm_abort
5107 *
5108 * Inputs
5109 * (endpoint, asoc)
5110 *
5111 * The RFC does not explicitly address this issue, but is the route through the
5112 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5113 *
5114 * Outputs
5115 * (timers)
5116 */
5117sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005118 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 const struct sctp_endpoint *ep,
5120 const struct sctp_association *asoc,
5121 const sctp_subtype_t type,
5122 void *arg,
5123 sctp_cmd_seq_t *commands)
5124{
5125 /* Stop the T5-shutdown guard timer. */
5126 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5127 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5128
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005129 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130}
5131
5132/*
5133 * sctp_sf_shutdown_sent_prm_abort
5134 *
5135 * Inputs
5136 * (endpoint, asoc)
5137 *
5138 * The RFC does not explicitly address this issue, but is the route through the
5139 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5140 *
5141 * Outputs
5142 * (timers)
5143 */
5144sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005145 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 const struct sctp_endpoint *ep,
5147 const struct sctp_association *asoc,
5148 const sctp_subtype_t type,
5149 void *arg,
5150 sctp_cmd_seq_t *commands)
5151{
5152 /* Stop the T2-shutdown timer. */
5153 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5154 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5155
5156 /* Stop the T5-shutdown guard timer. */
5157 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5158 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5159
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005160 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161}
5162
5163/*
5164 * sctp_sf_cookie_echoed_prm_abort
5165 *
5166 * Inputs
5167 * (endpoint, asoc)
5168 *
5169 * The RFC does not explcitly address this issue, but is the route through the
5170 * state table when someone issues an abort while in COOKIE_ECHOED state.
5171 *
5172 * Outputs
5173 * (timers)
5174 */
5175sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005176 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 const struct sctp_endpoint *ep,
5178 const struct sctp_association *asoc,
5179 const sctp_subtype_t type,
5180 void *arg,
5181 sctp_cmd_seq_t *commands)
5182{
5183 /* The same T2 timer, so we should be able to use
5184 * common function with the SHUTDOWN-SENT state.
5185 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005186 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187}
5188
5189/*
5190 * Process the REQUESTHEARTBEAT primitive
5191 *
5192 * 10.1 ULP-to-SCTP
5193 * J) Request Heartbeat
5194 *
5195 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5196 *
5197 * -> result
5198 *
5199 * Instructs the local endpoint to perform a HeartBeat on the specified
5200 * destination transport address of the given association. The returned
5201 * result should indicate whether the transmission of the HEARTBEAT
5202 * chunk to the destination address is successful.
5203 *
5204 * Mandatory attributes:
5205 *
5206 * o association id - local handle to the SCTP association
5207 *
5208 * o destination transport address - the transport address of the
5209 * association on which a heartbeat should be issued.
5210 */
5211sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005212 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 const struct sctp_endpoint *ep,
5214 const struct sctp_association *asoc,
5215 const sctp_subtype_t type,
5216 void *arg,
5217 sctp_cmd_seq_t *commands)
5218{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005219 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5220 (struct sctp_transport *)arg, commands))
5221 return SCTP_DISPOSITION_NOMEM;
5222
5223 /*
5224 * RFC 2960 (bis), section 8.3
5225 *
5226 * D) Request an on-demand HEARTBEAT on a specific destination
5227 * transport address of a given association.
5228 *
5229 * The endpoint should increment the respective error counter of
5230 * the destination transport address each time a HEARTBEAT is sent
5231 * to that address and not acknowledged within one RTO.
5232 *
5233 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005234 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005235 SCTP_TRANSPORT(arg));
5236 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237}
5238
5239/*
5240 * ADDIP Section 4.1 ASCONF Chunk Procedures
5241 * When an endpoint has an ASCONF signaled change to be sent to the
5242 * remote endpoint it should do A1 to A9
5243 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005244sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5245 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 const struct sctp_association *asoc,
5247 const sctp_subtype_t type,
5248 void *arg,
5249 sctp_cmd_seq_t *commands)
5250{
5251 struct sctp_chunk *chunk = arg;
5252
5253 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5254 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5255 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5256 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5257 return SCTP_DISPOSITION_CONSUME;
5258}
5259
Xin Long7a090b02017-01-18 00:44:44 +08005260/* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5261sctp_disposition_t sctp_sf_do_prm_reconf(struct net *net,
5262 const struct sctp_endpoint *ep,
5263 const struct sctp_association *asoc,
5264 const sctp_subtype_t type,
5265 void *arg, sctp_cmd_seq_t *commands)
5266{
5267 struct sctp_chunk *chunk = arg;
5268
5269 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5270 return SCTP_DISPOSITION_CONSUME;
5271}
5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273/*
5274 * Ignore the primitive event
5275 *
5276 * The return value is the disposition of the primitive.
5277 */
5278sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005279 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 const struct sctp_endpoint *ep,
5281 const struct sctp_association *asoc,
5282 const sctp_subtype_t type,
5283 void *arg,
5284 sctp_cmd_seq_t *commands)
5285{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005286 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5287 type.primitive);
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 return SCTP_DISPOSITION_DISCARD;
5290}
5291
5292/***************************************************************************
5293 * These are the state functions for the OTHER events.
5294 ***************************************************************************/
5295
5296/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005297 * When the SCTP stack has no more user data to send or retransmit, this
5298 * notification is given to the user. Also, at the time when a user app
5299 * subscribes to this event, if there is no data to be sent or
5300 * retransmit, the stack will immediately send up this notification.
5301 */
5302sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005303 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005304 const struct sctp_endpoint *ep,
5305 const struct sctp_association *asoc,
5306 const sctp_subtype_t type,
5307 void *arg,
5308 sctp_cmd_seq_t *commands)
5309{
5310 struct sctp_ulpevent *event;
5311
5312 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5313 if (!event)
5314 return SCTP_DISPOSITION_NOMEM;
5315
5316 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5317
5318 return SCTP_DISPOSITION_CONSUME;
5319}
5320
5321/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 * Start the shutdown negotiation.
5323 *
5324 * From Section 9.2:
5325 * Once all its outstanding data has been acknowledged, the endpoint
5326 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5327 * TSN Ack field the last sequential TSN it has received from the peer.
5328 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5329 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5330 * with the updated last sequential TSN received from its peer.
5331 *
5332 * The return value is the disposition.
5333 */
5334sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005335 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 const struct sctp_endpoint *ep,
5337 const struct sctp_association *asoc,
5338 const sctp_subtype_t type,
5339 void *arg,
5340 sctp_cmd_seq_t *commands)
5341{
5342 struct sctp_chunk *reply;
5343
5344 /* Once all its outstanding data has been acknowledged, the
5345 * endpoint shall send a SHUTDOWN chunk to its peer including
5346 * in the Cumulative TSN Ack field the last sequential TSN it
5347 * has received from the peer.
5348 */
5349 reply = sctp_make_shutdown(asoc, NULL);
5350 if (!reply)
5351 goto nomem;
5352
5353 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5354 * T2-shutdown timer.
5355 */
5356 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5357
5358 /* It shall then start the T2-shutdown timer */
5359 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5360 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5361
Wei Yongjun536428a2008-09-05 08:55:26 +08005362 /* RFC 4960 Section 9.2
5363 * The sender of the SHUTDOWN MAY also start an overall guard timer
5364 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5365 */
Thomas Graff8d96052011-07-07 00:28:35 +00005366 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005367 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5368
Neil Horman9f70f462013-12-10 06:48:15 -05005369 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5371 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5372
5373 /* and enter the SHUTDOWN-SENT state. */
5374 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5375 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5376
5377 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5378 *
5379 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005380 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 */
5382 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5383
5384 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5385
5386 return SCTP_DISPOSITION_CONSUME;
5387
5388nomem:
5389 return SCTP_DISPOSITION_NOMEM;
5390}
5391
5392/*
5393 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5394 *
5395 * From Section 9.2:
5396 *
5397 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5398 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5399 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5400 * endpoint must re-send the SHUTDOWN ACK.
5401 *
5402 * The return value is the disposition.
5403 */
5404sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005405 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 const struct sctp_endpoint *ep,
5407 const struct sctp_association *asoc,
5408 const sctp_subtype_t type,
5409 void *arg,
5410 sctp_cmd_seq_t *commands)
5411{
5412 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5413 struct sctp_chunk *reply;
5414
5415 /* There are 2 ways of getting here:
5416 * 1) called in response to a SHUTDOWN chunk
5417 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5418 *
5419 * For the case (2), the arg parameter is set to NULL. We need
5420 * to check that we have a chunk before accessing it's fields.
5421 */
5422 if (chunk) {
5423 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005424 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 /* Make sure that the SHUTDOWN chunk has a valid length. */
5427 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005428 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 commands);
5430 }
5431
5432 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5433 * shall send a SHUTDOWN ACK ...
5434 */
5435 reply = sctp_make_shutdown_ack(asoc, chunk);
5436 if (!reply)
5437 goto nomem;
5438
5439 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5440 * the T2-shutdown timer.
5441 */
5442 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5443
5444 /* and start/restart a T2-shutdown timer of its own, */
5445 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5446 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5447
Neil Horman9f70f462013-12-10 06:48:15 -05005448 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5450 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5451
5452 /* Enter the SHUTDOWN-ACK-SENT state. */
5453 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5454 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5455
5456 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5457 *
5458 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005459 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 */
5461 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5462
5463 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5464
5465 return SCTP_DISPOSITION_CONSUME;
5466
5467nomem:
5468 return SCTP_DISPOSITION_NOMEM;
5469}
5470
5471/*
5472 * Ignore the event defined as other
5473 *
5474 * The return value is the disposition of the event.
5475 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005476sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5477 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 const struct sctp_association *asoc,
5479 const sctp_subtype_t type,
5480 void *arg,
5481 sctp_cmd_seq_t *commands)
5482{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005483 pr_debug("%s: the event other type:%d is ignored\n",
5484 __func__, type.other);
5485
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 return SCTP_DISPOSITION_DISCARD;
5487}
5488
5489/************************************************************
5490 * These are the state functions for handling timeout events.
5491 ************************************************************/
5492
5493/*
5494 * RTX Timeout
5495 *
5496 * Section: 6.3.3 Handle T3-rtx Expiration
5497 *
5498 * Whenever the retransmission timer T3-rtx expires for a destination
5499 * address, do the following:
5500 * [See below]
5501 *
5502 * The return value is the disposition of the chunk.
5503 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005504sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5505 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 const struct sctp_association *asoc,
5507 const sctp_subtype_t type,
5508 void *arg,
5509 sctp_cmd_seq_t *commands)
5510{
5511 struct sctp_transport *transport = arg;
5512
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005513 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005516 if (asoc->peer.zero_window_announced &&
5517 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005518 /*
5519 * We are here likely because the receiver had its rwnd
5520 * closed for a while and we have not been able to
5521 * transmit the locally queued data within the maximum
5522 * retransmission attempts limit. Start the T5
5523 * shutdown guard timer to give the receiver one last
5524 * chance and some additional time to recover before
5525 * aborting.
5526 */
5527 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5528 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5529 } else {
5530 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5531 SCTP_ERROR(ETIMEDOUT));
5532 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5533 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5534 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005535 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5536 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005537 return SCTP_DISPOSITION_DELETE_TCB;
5538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 }
5540
5541 /* E1) For the destination address for which the timer
5542 * expires, adjust its ssthresh with rules defined in Section
5543 * 7.2.3 and set the cwnd <- MTU.
5544 */
5545
5546 /* E2) For the destination address for which the timer
5547 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5548 * maximum value discussed in rule C7 above (RTO.max) may be
5549 * used to provide an upper bound to this doubling operation.
5550 */
5551
5552 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5553 * outstanding DATA chunks for the address for which the
5554 * T3-rtx has expired will fit into a single packet, subject
5555 * to the MTU constraint for the path corresponding to the
5556 * destination transport address to which the retransmission
5557 * is being sent (this may be different from the address for
5558 * which the timer expires [see Section 6.4]). Call this
5559 * value K. Bundle and retransmit those K DATA chunks in a
5560 * single packet to the destination endpoint.
5561 *
5562 * Note: Any DATA chunks that were sent to the address for
5563 * which the T3-rtx timer expired but did not fit in one MTU
5564 * (rule E3 above), should be marked for retransmission and
5565 * sent as soon as cwnd allows (normally when a SACK arrives).
5566 */
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 /* Do some failure management (Section 8.2). */
5569 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5570
Vlad Yasevich1845a572007-02-21 02:06:19 -08005571 /* NB: Rules E4 and F1 are implicit in R1. */
5572 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 return SCTP_DISPOSITION_CONSUME;
5575}
5576
5577/*
5578 * Generate delayed SACK on timeout
5579 *
5580 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5581 *
5582 * The guidelines on delayed acknowledgement algorithm specified in
5583 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5584 * acknowledgement SHOULD be generated for at least every second packet
5585 * (not every second DATA chunk) received, and SHOULD be generated
5586 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5587 * some situations it may be beneficial for an SCTP transmitter to be
5588 * more conservative than the algorithms detailed in this document
5589 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5590 * the following algorithms allow.
5591 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005592sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5593 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 const struct sctp_association *asoc,
5595 const sctp_subtype_t type,
5596 void *arg,
5597 sctp_cmd_seq_t *commands)
5598{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005599 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5601 return SCTP_DISPOSITION_CONSUME;
5602}
5603
5604/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005605 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 *
5607 * Section: 4 Note: 2
5608 * Verification Tag:
5609 * Inputs
5610 * (endpoint, asoc)
5611 *
5612 * RFC 2960 Section 4 Notes
5613 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5614 * and re-start the T1-init timer without changing state. This MUST
5615 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5616 * endpoint MUST abort the initialization process and report the
5617 * error to SCTP user.
5618 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005619 * Outputs
5620 * (timers, events)
5621 *
5622 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005623sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5624 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005625 const struct sctp_association *asoc,
5626 const sctp_subtype_t type,
5627 void *arg,
5628 sctp_cmd_seq_t *commands)
5629{
5630 struct sctp_chunk *repl = NULL;
5631 struct sctp_bind_addr *bp;
5632 int attempts = asoc->init_err_counter + 1;
5633
Daniel Borkmannbb333812013-06-28 19:49:40 +02005634 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5635
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005636 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005637
Vlad Yasevich81845c22006-01-30 15:59:54 -08005638 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005639 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5640 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5641 if (!repl)
5642 return SCTP_DISPOSITION_NOMEM;
5643
5644 /* Choose transport for INIT. */
5645 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5646 SCTP_CHUNK(repl));
5647
5648 /* Issue a sideeffect to do the needed accounting. */
5649 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5650 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5651
5652 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5653 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005654 pr_debug("%s: giving up on INIT, attempts:%d "
5655 "max_init_attempts:%d\n", __func__, attempts,
5656 asoc->max_init_attempts);
5657
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005658 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5659 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005660 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005661 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005662 return SCTP_DISPOSITION_DELETE_TCB;
5663 }
5664
5665 return SCTP_DISPOSITION_CONSUME;
5666}
5667
5668/*
5669 * sctp_sf_t1_cookie_timer_expire
5670 *
5671 * Section: 4 Note: 2
5672 * Verification Tag:
5673 * Inputs
5674 * (endpoint, asoc)
5675 *
5676 * RFC 2960 Section 4 Notes
5677 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 * COOKIE ECHO and re-start the T1-cookie timer without changing
5679 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5680 * After that, the endpoint MUST abort the initialization process and
5681 * report the error to SCTP user.
5682 *
5683 * Outputs
5684 * (timers, events)
5685 *
5686 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005687sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5688 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 const struct sctp_association *asoc,
5690 const sctp_subtype_t type,
5691 void *arg,
5692 sctp_cmd_seq_t *commands)
5693{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005694 struct sctp_chunk *repl = NULL;
5695 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696
Daniel Borkmannbb333812013-06-28 19:49:40 +02005697 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5698
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005699 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
Vlad Yasevich81845c22006-01-30 15:59:54 -08005701 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005702 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005704 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005706 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5707 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005709 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5710 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5713 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005714 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5715 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005717 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 return SCTP_DISPOSITION_DELETE_TCB;
5719 }
5720
5721 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722}
5723
5724/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5725 * with the updated last sequential TSN received from its peer.
5726 *
5727 * An endpoint should limit the number of retransmissions of the
5728 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5729 * If this threshold is exceeded the endpoint should destroy the TCB and
5730 * MUST report the peer endpoint unreachable to the upper layer (and
5731 * thus the association enters the CLOSED state). The reception of any
5732 * packet from its peer (i.e. as the peer sends all of its queued DATA
5733 * chunks) should clear the endpoint's retransmission count and restart
5734 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5735 * all of its queued DATA chunks that have not yet been sent.
5736 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005737sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5738 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 const struct sctp_association *asoc,
5740 const sctp_subtype_t type,
5741 void *arg,
5742 sctp_cmd_seq_t *commands)
5743{
5744 struct sctp_chunk *reply = NULL;
5745
Daniel Borkmannbb333812013-06-28 19:49:40 +02005746 pr_debug("%s: timer T2 expired\n", __func__);
5747
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005748 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005749
Neil Horman58fbbed2008-02-29 11:40:56 -08005750 ((struct sctp_association *)asoc)->shutdown_retries++;
5751
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005753 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5754 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5756 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005757 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005758 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5759 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 return SCTP_DISPOSITION_DELETE_TCB;
5761 }
5762
5763 switch (asoc->state) {
5764 case SCTP_STATE_SHUTDOWN_SENT:
5765 reply = sctp_make_shutdown(asoc, NULL);
5766 break;
5767
5768 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5769 reply = sctp_make_shutdown_ack(asoc, NULL);
5770 break;
5771
5772 default:
5773 BUG();
5774 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
5777 if (!reply)
5778 goto nomem;
5779
Wei Yongjun6345b192009-04-26 23:13:35 +08005780 /* Do some failure management (Section 8.2).
5781 * If we remove the transport an SHUTDOWN was last sent to, don't
5782 * do failure management.
5783 */
5784 if (asoc->shutdown_last_sent_to)
5785 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5786 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
5788 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5789 * the T2-shutdown timer.
5790 */
5791 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5792
5793 /* Restart the T2-shutdown timer. */
5794 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5795 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5796 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5797 return SCTP_DISPOSITION_CONSUME;
5798
5799nomem:
5800 return SCTP_DISPOSITION_NOMEM;
5801}
5802
5803/*
5804 * ADDIP Section 4.1 ASCONF CHunk Procedures
5805 * If the T4 RTO timer expires the endpoint should do B1 to B5
5806 */
5807sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005808 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 const struct sctp_endpoint *ep,
5810 const struct sctp_association *asoc,
5811 const sctp_subtype_t type,
5812 void *arg,
5813 sctp_cmd_seq_t *commands)
5814{
5815 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5816 struct sctp_transport *transport = chunk->transport;
5817
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005818 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5821 * detection on the appropriate destination address as defined in
5822 * RFC2960 [5] section 8.1 and 8.2.
5823 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005824 if (transport)
5825 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5826 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
5828 /* Reconfig T4 timer and transport. */
5829 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5830
5831 /* ADDIP 4.1 B2) Increment the association error counters and perform
5832 * endpoint failure detection on the association as defined in
5833 * RFC2960 [5] section 8.1 and 8.2.
5834 * association error counter is incremented in SCTP_CMD_STRIKE.
5835 */
5836 if (asoc->overall_error_count >= asoc->max_retrans) {
5837 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5838 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005839 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5840 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005842 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005843 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5844 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 return SCTP_DISPOSITION_ABORT;
5846 }
5847
5848 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5849 * the ASCONF chunk was sent by doubling the RTO timer value.
5850 * This is done in SCTP_CMD_STRIKE.
5851 */
5852
5853 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5854 * choose an alternate destination address (please refer to RFC2960
5855 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005856 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 * ASCONF sent.
5858 */
5859 sctp_chunk_hold(asoc->addip_last_asconf);
5860 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5861 SCTP_CHUNK(asoc->addip_last_asconf));
5862
5863 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5864 * destination is selected, then the RTO used will be that of the new
5865 * destination address.
5866 */
5867 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5868 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5869
5870 return SCTP_DISPOSITION_CONSUME;
5871}
5872
5873/* sctpimpguide-05 Section 2.12.2
5874 * The sender of the SHUTDOWN MAY also start an overall guard timer
5875 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5876 * At the expiration of this timer the sender SHOULD abort the association
5877 * by sending an ABORT chunk.
5878 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005879sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5880 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 const struct sctp_association *asoc,
5882 const sctp_subtype_t type,
5883 void *arg,
5884 sctp_cmd_seq_t *commands)
5885{
5886 struct sctp_chunk *reply = NULL;
5887
Daniel Borkmannbb333812013-06-28 19:49:40 +02005888 pr_debug("%s: timer T5 expired\n", __func__);
5889
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005890 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 reply = sctp_make_abort(asoc, NULL, 0);
5893 if (!reply)
5894 goto nomem;
5895
5896 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005897 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5898 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005900 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005902 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5903 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005904
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 return SCTP_DISPOSITION_DELETE_TCB;
5906nomem:
5907 return SCTP_DISPOSITION_NOMEM;
5908}
5909
5910/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5911 * the association is automatically closed by starting the shutdown process.
5912 * The work that needs to be done is same as when SHUTDOWN is initiated by
5913 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5914 */
5915sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005916 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 const struct sctp_endpoint *ep,
5918 const struct sctp_association *asoc,
5919 const sctp_subtype_t type,
5920 void *arg,
5921 sctp_cmd_seq_t *commands)
5922{
5923 int disposition;
5924
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005925 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005926
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 /* From 9.2 Shutdown of an Association
5928 * Upon receipt of the SHUTDOWN primitive from its upper
5929 * layer, the endpoint enters SHUTDOWN-PENDING state and
5930 * remains there until all outstanding data has been
5931 * acknowledged by its peer. The endpoint accepts no new data
5932 * from its upper layer, but retransmits data to the far end
5933 * if necessary to fill gaps.
5934 */
5935 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5936 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 disposition = SCTP_DISPOSITION_CONSUME;
5939 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005940 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 arg, commands);
5942 }
5943 return disposition;
5944}
5945
5946/*****************************************************************************
5947 * These are sa state functions which could apply to all types of events.
5948 ****************************************************************************/
5949
5950/*
5951 * This table entry is not implemented.
5952 *
5953 * Inputs
5954 * (endpoint, asoc, chunk)
5955 *
5956 * The return value is the disposition of the chunk.
5957 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005958sctp_disposition_t sctp_sf_not_impl(struct net *net,
5959 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 const struct sctp_association *asoc,
5961 const sctp_subtype_t type,
5962 void *arg,
5963 sctp_cmd_seq_t *commands)
5964{
5965 return SCTP_DISPOSITION_NOT_IMPL;
5966}
5967
5968/*
5969 * This table entry represents a bug.
5970 *
5971 * Inputs
5972 * (endpoint, asoc, chunk)
5973 *
5974 * The return value is the disposition of the chunk.
5975 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005976sctp_disposition_t sctp_sf_bug(struct net *net,
5977 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 const struct sctp_association *asoc,
5979 const sctp_subtype_t type,
5980 void *arg,
5981 sctp_cmd_seq_t *commands)
5982{
5983 return SCTP_DISPOSITION_BUG;
5984}
5985
5986/*
5987 * This table entry represents the firing of a timer in the wrong state.
5988 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5989 * when the association is in the wrong state. This event should
5990 * be ignored, so as to prevent any rearming of the timer.
5991 *
5992 * Inputs
5993 * (endpoint, asoc, chunk)
5994 *
5995 * The return value is the disposition of the chunk.
5996 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005997sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5998 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 const struct sctp_association *asoc,
6000 const sctp_subtype_t type,
6001 void *arg,
6002 sctp_cmd_seq_t *commands)
6003{
Daniel Borkmannbb333812013-06-28 19:49:40 +02006004 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 return SCTP_DISPOSITION_CONSUME;
6007}
6008
6009/********************************************************************
6010 * 2nd Level Abstractions
6011 ********************************************************************/
6012
6013/* Pull the SACK chunk based on the SACK header. */
6014static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6015{
6016 struct sctp_sackhdr *sack;
6017 unsigned int len;
6018 __u16 num_blocks;
6019 __u16 num_dup_tsns;
6020
6021 /* Protect ourselves from reading too far into
6022 * the skb from a bogus sender.
6023 */
6024 sack = (struct sctp_sackhdr *) chunk->skb->data;
6025
6026 num_blocks = ntohs(sack->num_gap_ack_blocks);
6027 num_dup_tsns = ntohs(sack->num_dup_tsns);
6028 len = sizeof(struct sctp_sackhdr);
6029 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6030 if (len > chunk->skb->len)
6031 return NULL;
6032
6033 skb_pull(chunk->skb, len);
6034
6035 return sack;
6036}
6037
6038/* Create an ABORT packet to be sent as a response, with the specified
6039 * error causes.
6040 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006041static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
6042 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 const struct sctp_association *asoc,
6044 struct sctp_chunk *chunk,
6045 const void *payload,
6046 size_t paylen)
6047{
6048 struct sctp_packet *packet;
6049 struct sctp_chunk *abort;
6050
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006051 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
6053 if (packet) {
6054 /* Make an ABORT.
6055 * The T bit will be set if the asoc is NULL.
6056 */
6057 abort = sctp_make_abort(asoc, chunk, paylen);
6058 if (!abort) {
6059 sctp_ootb_pkt_free(packet);
6060 return NULL;
6061 }
Jerome Forissier047a2422005-04-28 11:58:43 -07006062
6063 /* Reflect vtag if T-Bit is set */
6064 if (sctp_test_T_bit(abort))
6065 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 /* Add specified error causes, i.e., payload, to the
6068 * end of the chunk.
6069 */
6070 sctp_addto_chunk(abort, paylen, payload);
6071
6072 /* Set the skb to the belonging sock for accounting. */
6073 abort->skb->sk = ep->base.sk;
6074
6075 sctp_packet_append_chunk(packet, abort);
6076
6077 }
6078
6079 return packet;
6080}
6081
6082/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006083static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6084 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 const struct sctp_chunk *chunk)
6086{
6087 struct sctp_packet *packet;
6088 struct sctp_transport *transport;
6089 __u16 sport;
6090 __u16 dport;
6091 __u32 vtag;
6092
6093 /* Get the source and destination port from the inbound packet. */
6094 sport = ntohs(chunk->sctp_hdr->dest);
6095 dport = ntohs(chunk->sctp_hdr->source);
6096
6097 /* The V-tag is going to be the same as the inbound packet if no
6098 * association exists, otherwise, use the peer's vtag.
6099 */
6100 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006101 /* Special case the INIT-ACK as there is no peer's vtag
6102 * yet.
6103 */
wangweidongcb3f8372013-12-23 12:16:50 +08006104 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006105 case SCTP_CID_INIT_ACK:
6106 {
6107 sctp_initack_chunk_t *initack;
6108
6109 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6110 vtag = ntohl(initack->init_hdr.init_tag);
6111 break;
6112 }
6113 default:
6114 vtag = asoc->peer.i.init_tag;
6115 break;
6116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 } else {
6118 /* Special case the INIT and stale COOKIE_ECHO as there is no
6119 * vtag yet.
6120 */
wangweidongcb3f8372013-12-23 12:16:50 +08006121 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 case SCTP_CID_INIT:
6123 {
6124 sctp_init_chunk_t *init;
6125
6126 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6127 vtag = ntohl(init->init_hdr.init_tag);
6128 break;
6129 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006130 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 vtag = ntohl(chunk->sctp_hdr->vtag);
6132 break;
6133 }
6134 }
6135
6136 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006137 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 if (!transport)
6139 goto nomem;
6140
6141 /* Cache a route for the transport with the chunk's destination as
6142 * the source address.
6143 */
Al Viro16b0a032006-11-20 17:13:38 -08006144 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006145 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Marcelo Ricardo Leitner66b91d22016-12-28 09:26:34 -02006147 packet = &transport->packet;
6148 sctp_packet_init(packet, transport, sport, dport);
6149 sctp_packet_config(packet, vtag, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
6151 return packet;
6152
6153nomem:
6154 return NULL;
6155}
6156
6157/* Free the packet allocated earlier for responding in the OOTB condition. */
6158void sctp_ootb_pkt_free(struct sctp_packet *packet)
6159{
6160 sctp_transport_free(packet->transport);
6161}
6162
6163/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006164static void sctp_send_stale_cookie_err(struct net *net,
6165 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 const struct sctp_association *asoc,
6167 const struct sctp_chunk *chunk,
6168 sctp_cmd_seq_t *commands,
6169 struct sctp_chunk *err_chunk)
6170{
6171 struct sctp_packet *packet;
6172
6173 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006174 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 if (packet) {
6176 struct sctp_signed_cookie *cookie;
6177
6178 /* Override the OOTB vtag from the cookie. */
6179 cookie = chunk->subh.cookie_hdr;
6180 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006181
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 /* Set the skb to the belonging sock for accounting. */
6183 err_chunk->skb->sk = ep->base.sk;
6184 sctp_packet_append_chunk(packet, err_chunk);
6185 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6186 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006187 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 } else
6189 sctp_chunk_free (err_chunk);
6190 }
6191}
6192
6193
6194/* Process a data chunk */
6195static int sctp_eat_data(const struct sctp_association *asoc,
6196 struct sctp_chunk *chunk,
6197 sctp_cmd_seq_t *commands)
6198{
6199 sctp_datahdr_t *data_hdr;
6200 struct sctp_chunk *err;
6201 size_t datalen;
6202 sctp_verb_t deliver;
6203 int tmp;
6204 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006205 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006206 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006207 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006208 u16 ssn;
6209 u16 sid;
6210 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211
6212 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6213 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6214
6215 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006216 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217
6218 /* ASSERT: Now skb->data is really the user data. */
6219
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 /* Process ECN based congestion.
6221 *
6222 * Since the chunk structure is reused for all chunks within
6223 * a packet, we use ecn_ce_done to track if we've already
6224 * done CE processing for this packet.
6225 *
6226 * We need to do ECN processing even if we plan to discard the
6227 * chunk later.
6228 */
6229
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006230 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006231 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 chunk->ecn_ce_done = 1;
6233
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006234 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 /* Do real work as sideffect. */
6236 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6237 SCTP_U32(tsn));
6238 }
6239 }
6240
6241 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6242 if (tmp < 0) {
6243 /* The TSN is too high--silently discard the chunk and
6244 * count on it getting retransmitted later.
6245 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006246 if (chunk->asoc)
6247 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 return SCTP_IERROR_HIGH_TSN;
6249 } else if (tmp > 0) {
6250 /* This is a duplicate. Record it. */
6251 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6252 return SCTP_IERROR_DUP_TSN;
6253 }
6254
6255 /* This is a new TSN. */
6256
6257 /* Discard if there is no room in the receive window.
6258 * Actually, allow a little bit of overflow (up to a MTU).
6259 */
6260 datalen = ntohs(chunk->chunk_hdr->length);
6261 datalen -= sizeof(sctp_data_chunk_t);
6262
6263 deliver = SCTP_CMD_CHUNK_ULP;
6264
6265 /* Think about partial delivery. */
6266 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6267
6268 /* Even if we don't accept this chunk there is
6269 * memory pressure.
6270 */
6271 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6272 }
6273
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006274 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 * seems a bit troublesome in that frag_point varies based on
6276 * PMTU. In cases, such as loopback, this might be a rather
6277 * large spill over.
6278 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006279 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006280 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282 /* If this is the next TSN, consider reneging to make
6283 * room. Note: Playing nice with a confused sender. A
6284 * malicious sender can still eat up all our buffer
6285 * space and in the future we may want to detect and
6286 * do more drastic reneging.
6287 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006288 if (sctp_tsnmap_has_gap(map) &&
6289 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006290 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 deliver = SCTP_CMD_RENEGE;
6292 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006293 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6294 __func__, tsn, datalen, asoc->rwnd);
6295
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 return SCTP_IERROR_IGNORE_TSN;
6297 }
6298 }
6299
6300 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006301 * Also try to renege to limit our memory usage in the event that
6302 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006303 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006304 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6305 * memory usage too much
6306 */
6307 if (*sk->sk_prot_creator->memory_pressure) {
6308 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006309 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006310 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6311 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006312 deliver = SCTP_CMD_RENEGE;
6313 }
6314 }
6315
6316 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 * Section 3.3.10.9 No User Data (9)
6318 *
6319 * Cause of error
6320 * ---------------
6321 * No User Data: This error cause is returned to the originator of a
6322 * DATA chunk if a received DATA chunk has no user data.
6323 */
6324 if (unlikely(0 == datalen)) {
6325 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6326 if (err) {
6327 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6328 SCTP_CHUNK(err));
6329 }
6330 /* We are going to ABORT, so we might as well stop
6331 * processing the rest of the chunks in the packet.
6332 */
wangweidongcb3f8372013-12-23 12:16:50 +08006333 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006334 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6335 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006337 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006338 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6339 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 return SCTP_IERROR_NO_DATA;
6341 }
6342
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006343 chunk->data_accepted = 1;
6344
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6346 * if we renege and the chunk arrives again.
6347 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006348 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006349 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006350 if (chunk->asoc)
6351 chunk->asoc->stats.iuodchunks++;
6352 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006353 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006354 if (chunk->asoc)
6355 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006356 ordered = 1;
6357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6360 *
6361 * If an endpoint receive a DATA chunk with an invalid stream
6362 * identifier, it shall acknowledge the reception of the DATA chunk
6363 * following the normal procedure, immediately send an ERROR chunk
6364 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6365 * and discard the DATA chunk.
6366 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006367 sid = ntohs(data_hdr->stream);
Xin Longafe89962017-03-31 17:57:28 +08006368 if (sid >= asoc->stream->incnt) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006369 /* Mark tsn as received even though we drop it */
6370 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6371
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6373 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006374 sizeof(data_hdr->stream),
6375 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 if (err)
6377 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6378 SCTP_CHUNK(err));
6379 return SCTP_IERROR_BAD_STREAM;
6380 }
6381
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006382 /* Check to see if the SSN is possible for this TSN.
6383 * The biggest gap we can record is 4K wide. Since SSNs wrap
6384 * at an unsigned short, there is no way that an SSN can
6385 * wrap and for a valid TSN. We can simply check if the current
6386 * SSN is smaller then the next expected one. If it is, it wrapped
6387 * and is invalid.
6388 */
6389 ssn = ntohs(data_hdr->ssn);
Xin Longa8386312017-01-06 22:18:33 +08006390 if (ordered && SSN_lt(ssn, sctp_ssn_peek(asoc->stream, in, sid)))
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006391 return SCTP_IERROR_PROTO_VIOLATION;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006392
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 /* Send the data up to the user. Note: Schedule the
6394 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6395 * chunk needs the updated rwnd.
6396 */
6397 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6398
6399 return SCTP_IERROR_NO_ERROR;
6400}