blob: 146b568962e01da492ffd864fe8b57b2db8ebb8e [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
Xin Long0e67ad52018-05-05 14:59:47 +0800147static sctp_ierror_t sctp_sf_authenticate(
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700148 const struct sctp_association *asoc,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700149 struct sctp_chunk *chunk);
150
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000151static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
152 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800153 const struct sctp_association *asoc,
154 const sctp_subtype_t type,
155 void *arg,
156 sctp_cmd_seq_t *commands);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/* Small helper function that checks if the chunk length
159 * is of the appropriate length. The 'required_length' argument
160 * is set to be the size of a specific chunk we are testing.
161 * Return Values: 1 = Valid length
162 * 0 = Invalid length
163 *
164 */
165static inline int
166sctp_chunk_length_valid(struct sctp_chunk *chunk,
167 __u16 required_length)
168{
169 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
170
Daniel Borkmann26b87c72014-10-09 22:55:33 +0200171 /* Previously already marked? */
172 if (unlikely(chunk->pdiscard))
173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (unlikely(chunk_length < required_length))
175 return 0;
176
177 return 1;
178}
179
Xin Long548e23f2020-02-18 12:07:53 +0800180/* Check for format error in an ABORT chunk */
181static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
182{
183 struct sctp_errhdr *err;
184
185 sctp_walk_errors(err, chunk->chunk_hdr);
186
187 return (void *)err == (void *)chunk->chunk_end;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/**********************************************************
191 * These are the state functions for handling chunk events.
192 **********************************************************/
193
194/*
195 * Process the final SHUTDOWN COMPLETE.
196 *
197 * Section: 4 (C) (diagram), 9.2
198 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
199 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
200 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
201 * should stop the T2-shutdown timer and remove all knowledge of the
202 * association (and thus the association enters the CLOSED state).
203 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700204 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * C) Rules for packet carrying SHUTDOWN COMPLETE:
206 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700207 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
208 * if the Verification Tag field of the packet matches its own tag and
209 * the T bit is not set
210 * OR
211 * it is set to its peer's tag and the T bit is set in the Chunk
212 * Flags.
213 * Otherwise, the receiver MUST silently discard the packet
214 * and take no further action. An endpoint MUST ignore the
215 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * Inputs
218 * (endpoint, asoc, chunk)
219 *
220 * Outputs
221 * (asoc, reply_msg, msg_up, timers, counters)
222 *
223 * The return value is the disposition of the chunk.
224 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000225sctp_disposition_t sctp_sf_do_4_C(struct net *net,
226 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 const struct sctp_association *asoc,
228 const sctp_subtype_t type,
229 void *arg,
230 sctp_cmd_seq_t *commands)
231{
232 struct sctp_chunk *chunk = arg;
233 struct sctp_ulpevent *ev;
234
Vlad Yasevichece25df2007-09-07 16:30:54 -0400235 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000236 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* RFC 2960 6.10 Bundling
239 *
240 * An endpoint MUST NOT bundle INIT, INIT ACK or
241 * SHUTDOWN COMPLETE with any other chunks.
242 */
243 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000244 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Vlad Yasevichece25df2007-09-07 16:30:54 -0400246 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
247 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000248 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400249 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* RFC 2960 10.2 SCTP-to-ULP
252 *
253 * H) SHUTDOWN COMPLETE notification
254 *
255 * When SCTP completes the shutdown procedures (section 9.2) this
256 * notification is passed to the upper layer.
257 */
258 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700259 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700260 if (ev)
261 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900262 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
265 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
266 * not the chunk should be discarded. If the endpoint is in
267 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
268 * T2-shutdown timer and remove all knowledge of the
269 * association (and thus the association enters the CLOSED
270 * state).
271 */
272 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
273 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
274
275 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
276 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
277
278 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
279 SCTP_STATE(SCTP_STATE_CLOSED));
280
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000281 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
282 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
285
286 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * Respond to a normal INIT chunk.
291 * We are the side that is being asked for an association.
292 *
293 * Section: 5.1 Normal Establishment of an Association, B
294 * B) "Z" shall respond immediately with an INIT ACK chunk. The
295 * destination IP address of the INIT ACK MUST be set to the source
296 * IP address of the INIT to which this INIT ACK is responding. In
297 * the response, besides filling in other parameters, "Z" must set the
298 * Verification Tag field to Tag_A, and also provide its own
299 * Verification Tag (Tag_Z) in the Initiate Tag field.
300 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900301 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
303 * Inputs
304 * (endpoint, asoc, chunk)
305 *
306 * Outputs
307 * (asoc, reply_msg, msg_up, timers, counters)
308 *
309 * The return value is the disposition of the chunk.
310 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000311sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
312 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 const struct sctp_association *asoc,
314 const sctp_subtype_t type,
315 void *arg,
316 sctp_cmd_seq_t *commands)
317{
318 struct sctp_chunk *chunk = arg;
319 struct sctp_chunk *repl;
320 struct sctp_association *new_asoc;
321 struct sctp_chunk *err_chunk;
322 struct sctp_packet *packet;
323 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int len;
325
326 /* 6.10 Bundling
327 * An endpoint MUST NOT bundle INIT, INIT ACK or
328 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900329 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * IG Section 2.11.2
331 * Furthermore, we require that the receiver of an INIT chunk MUST
332 * enforce these rules by silently discarding an arriving packet
333 * with an INIT chunk that is bundled with other chunks.
334 */
335 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* If the packet is an OOTB packet which is temporarily on the
339 * control endpoint, respond with an ABORT.
340 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000341 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000342 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000343 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900347 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
349 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000350 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 /* Make sure that the INIT chunk has a valid length.
353 * Normally, this would cause an ABORT with a Protocol Violation
354 * error, but since we don't have an association, we'll
355 * just discard the packet.
356 */
357 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Vlad Yasevichbec96402009-07-30 18:08:28 -0400360 /* If the INIT is coming toward a closing socket, we'll send back
361 * and ABORT. Essentially, this catches the race of INIT being
362 * backloged to the socket at the same time as the user isses close().
363 * Since the socket and all its associations are going away, we
364 * can treat this OOTB
365 */
366 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000367 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* Verify the INIT chunk before processing it. */
370 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200371 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
373 &err_chunk)) {
374 /* This chunk contains fatal error. It is to be discarded.
375 * Send an ABORT, with causes if there is any.
376 */
377 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000378 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 (__u8 *)(err_chunk->chunk_hdr) +
380 sizeof(sctp_chunkhdr_t),
381 ntohs(err_chunk->chunk_hdr->length) -
382 sizeof(sctp_chunkhdr_t));
383
384 sctp_chunk_free(err_chunk);
385
386 if (packet) {
387 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
388 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000389 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return SCTP_DISPOSITION_CONSUME;
391 } else {
392 return SCTP_DISPOSITION_NOMEM;
393 }
394 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000395 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 commands);
397 }
398 }
399
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900400 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
402
403 /* Tag the variable length parameters. */
404 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
405
406 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
407 if (!new_asoc)
408 goto nomem;
409
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000410 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
411 sctp_scope(sctp_source(chunk)),
412 GFP_ATOMIC) < 0)
413 goto nomem_init;
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000416 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 (sctp_init_chunk_t *)chunk->chunk_hdr,
418 GFP_ATOMIC))
419 goto nomem_init;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
422
423 /* If there are errors need to be reported for unknown parameters,
424 * make sure to reserve enough room in the INIT ACK for them.
425 */
426 len = 0;
427 if (err_chunk)
428 len = ntohs(err_chunk->chunk_hdr->length) -
429 sizeof(sctp_chunkhdr_t);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
432 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700433 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* If there are errors need to be reported for unknown parameters,
436 * include them in the outgoing INIT ACK as "Unrecognized parameter"
437 * parameter.
438 */
439 if (err_chunk) {
440 /* Get the "Unrecognized parameter" parameter(s) out of the
441 * ERROR chunk generated by sctp_verify_init(). Since the
442 * error cause code for "unknown parameter" and the
443 * "Unrecognized parameter" type is the same, we can
444 * construct the parameters in INIT ACK by copying the
445 * ERROR causes over.
446 */
447 unk_param = (sctp_unrecognized_param_t *)
448 ((__u8 *)(err_chunk->chunk_hdr) +
449 sizeof(sctp_chunkhdr_t));
450 /* Replace the cause code with the "Unrecognized parameter"
451 * parameter type.
452 */
453 sctp_addto_chunk(repl, len, unk_param);
454 sctp_chunk_free(err_chunk);
455 }
456
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700457 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
460
461 /*
462 * Note: After sending out INIT ACK with the State Cookie parameter,
463 * "Z" MUST NOT allocate any resources, nor keep any states for the
464 * new association. Otherwise, "Z" will be vulnerable to resource
465 * attacks.
466 */
467 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
468
469 return SCTP_DISPOSITION_DELETE_TCB;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471nomem_init:
472 sctp_association_free(new_asoc);
473nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700474 if (err_chunk)
475 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return SCTP_DISPOSITION_NOMEM;
477}
478
479/*
480 * Respond to a normal INIT ACK chunk.
481 * We are the side that is initiating the association.
482 *
483 * Section: 5.1 Normal Establishment of an Association, C
484 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
485 * timer and leave COOKIE-WAIT state. "A" shall then send the State
486 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
487 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
488 *
489 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
490 * DATA chunks, but it MUST be the first chunk in the packet and
491 * until the COOKIE ACK is returned the sender MUST NOT send any
492 * other packets to the peer.
493 *
494 * Verification Tag: 3.3.3
495 * If the value of the Initiate Tag in a received INIT ACK chunk is
496 * found to be 0, the receiver MUST treat it as an error and close the
497 * association by transmitting an ABORT.
498 *
499 * Inputs
500 * (endpoint, asoc, chunk)
501 *
502 * Outputs
503 * (asoc, reply_msg, msg_up, timers, counters)
504 *
505 * The return value is the disposition of the chunk.
506 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000507sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
508 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 const struct sctp_association *asoc,
510 const sctp_subtype_t type,
511 void *arg,
512 sctp_cmd_seq_t *commands)
513{
514 struct sctp_chunk *chunk = arg;
515 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 struct sctp_chunk *err_chunk;
517 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000520 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* 6.10 Bundling
523 * An endpoint MUST NOT bundle INIT, INIT ACK or
524 * SHUTDOWN COMPLETE with any other chunks.
525 */
526 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000527 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Vlad Yasevichece25df2007-09-07 16:30:54 -0400529 /* Make sure that the INIT-ACK chunk has a valid length */
530 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000531 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400532 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Grab the INIT header. */
534 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* Verify the INIT chunk before processing it. */
537 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200538 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
540 &err_chunk)) {
541
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800542 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800545 * Send an ABORT, with causes. If there are no causes,
546 * then there wasn't enough memory. Just terminate
547 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
549 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000550 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 (__u8 *)(err_chunk->chunk_hdr) +
552 sizeof(sctp_chunkhdr_t),
553 ntohs(err_chunk->chunk_hdr->length) -
554 sizeof(sctp_chunkhdr_t));
555
556 sctp_chunk_free(err_chunk);
557
558 if (packet) {
559 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
560 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000561 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700562 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700565
566 /* SCTP-AUTH, Section 6.3:
567 * It should be noted that if the receiver wants to tear
568 * down an association in an authenticated way only, the
569 * handling of malformed packets should not result in
570 * tearing down the association.
571 *
572 * This means that if we only want to abort associations
573 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300574 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700575 * was malformed.
576 */
577 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000578 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700579
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000580 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
581 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700582 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 /* Tag the variable length parameters. Note that we never
586 * convert the parameters in an INIT chunk.
587 */
588 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
589
590 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
591
592 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
593 SCTP_PEER_INIT(initchunk));
594
Frank Filz3f7a87d2005-06-20 13:14:57 -0700595 /* Reset init error count upon receipt of INIT-ACK. */
596 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /* 5.1 C) "A" shall stop the T1-init timer and leave
599 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
600 * timer, and enter the COOKIE-ECHOED state.
601 */
602 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
603 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
606 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
607 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
608
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700609 /* SCTP-AUTH: genereate the assocition shared keys so that
610 * we can potentially signe the COOKIE-ECHO.
611 */
612 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* 5.1 C) "A" shall then send the State Cookie received in the
615 * INIT ACK chunk in a COOKIE ECHO chunk, ...
616 */
617 /* If there is any errors to report, send the ERROR chunk generated
618 * for unknown parameters as well.
619 */
620 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
621 SCTP_CHUNK(err_chunk));
622
623 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Xin Long0e67ad52018-05-05 14:59:47 +0800626static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
627 const struct sctp_association *asoc)
628{
629 struct sctp_chunk auth;
630
631 if (!chunk->auth_chunk)
632 return true;
633
634 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
635 * is supposed to be authenticated and we have to do delayed
636 * authentication. We've just recreated the association using
637 * the information in the cookie and now it's much easier to
638 * do the authentication.
639 */
640
641 /* Make sure that we and the peer are AUTH capable */
642 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
643 return false;
644
645 /* set-up our fake chunk so that we can process it */
646 auth.skb = chunk->auth_chunk;
647 auth.asoc = chunk->asoc;
648 auth.sctp_hdr = chunk->sctp_hdr;
649 auth.chunk_hdr = (struct sctp_chunkhdr *)
650 skb_push(chunk->auth_chunk,
651 sizeof(struct sctp_chunkhdr));
652 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
653 auth.transport = chunk->transport;
654
655 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * Respond to a normal COOKIE ECHO chunk.
660 * We are the side that is being asked for an association.
661 *
662 * Section: 5.1 Normal Establishment of an Association, D
663 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
664 * with a COOKIE ACK chunk after building a TCB and moving to
665 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
666 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
667 * chunk MUST be the first chunk in the packet.
668 *
669 * IMPLEMENTATION NOTE: An implementation may choose to send the
670 * Communication Up notification to the SCTP user upon reception
671 * of a valid COOKIE ECHO chunk.
672 *
673 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
674 * D) Rules for packet carrying a COOKIE ECHO
675 *
676 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
677 * Initial Tag received in the INIT ACK.
678 *
679 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
680 *
681 * Inputs
682 * (endpoint, asoc, chunk)
683 *
684 * Outputs
685 * (asoc, reply_msg, msg_up, timers, counters)
686 *
687 * The return value is the disposition of the chunk.
688 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000689sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
690 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 const struct sctp_association *asoc,
692 const sctp_subtype_t type, void *arg,
693 sctp_cmd_seq_t *commands)
694{
695 struct sctp_chunk *chunk = arg;
696 struct sctp_association *new_asoc;
697 sctp_init_chunk_t *peer_init;
698 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700699 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 int error = 0;
701 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900702 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* If the packet is an OOTB packet which is temporarily on the
705 * control endpoint, respond with an ABORT.
706 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000707 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000708 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000709 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712 /* Make sure that the COOKIE_ECHO chunk has a valid length.
713 * In this case, we check that we have enough for at least a
714 * chunk header. More detailed verification is done
715 * in sctp_unpack_cookie().
716 */
717 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000718 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Vlad Yasevich609ee462007-08-31 03:10:59 +0900720 /* If the endpoint is not listening or if the number of associations
721 * on the TCP-style socket exceed the max backlog, respond with an
722 * ABORT.
723 */
724 sk = ep->base.sk;
725 if (!sctp_sstate(sk, LISTENING) ||
726 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000727 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* "Decode" the chunk. We have no optional parameters so we
730 * are in good shape.
731 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900732 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700734 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
735 sizeof(sctp_chunkhdr_t)))
736 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
739 * "Z" will reply with a COOKIE ACK chunk after building a TCB
740 * and moving to the ESTABLISHED state.
741 */
742 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
743 &err_chk_p);
744
745 /* FIXME:
746 * If the re-build failed, what is the proper error path
747 * from here?
748 *
749 * [We should abort the association. --piggy]
750 */
751 if (!new_asoc) {
752 /* FIXME: Several errors are possible. A bad cookie should
753 * be silently discarded, but think about logging it too.
754 */
755 switch (error) {
756 case -SCTP_IERROR_NOMEM:
757 goto nomem;
758
759 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000760 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000762 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 case -SCTP_IERROR_BAD_SIG:
765 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000766 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700771 /* Delay state machine commands until later.
772 *
773 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * the sctp_unpack_cookie() already.
775 */
776 /* This is a brand-new association, so these are not yet side
777 * effects--it is safe to run them here.
778 */
779 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
780
Wei Yongjunde6becd2011-04-19 21:30:51 +0000781 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800782 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 peer_init, GFP_ATOMIC))
784 goto nomem_init;
785
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700786 /* SCTP-AUTH: Now that we've populate required fields in
787 * sctp_process_init, set up the assocaition shared keys as
788 * necessary so that we can potentially authenticate the ACK
789 */
790 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
791 if (error)
792 goto nomem_init;
793
Xin Long0e67ad52018-05-05 14:59:47 +0800794 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
795 sctp_association_free(new_asoc);
796 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 repl = sctp_make_cookie_ack(new_asoc, chunk);
800 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700801 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 /* RFC 2960 5.1 Normal Establishment of an Association
804 *
805 * D) IMPLEMENTATION NOTE: An implementation may choose to
806 * send the Communication Up notification to the SCTP user
807 * upon reception of a valid COOKIE ECHO chunk.
808 */
809 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
810 new_asoc->c.sinit_num_ostreams,
811 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700812 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (!ev)
814 goto nomem_ev;
815
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900816 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800817 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800819 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800821 if (new_asoc->peer.adaptation_ind) {
822 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700824 if (!ai_ev)
825 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700828 /* Add all the state machine commands now since we've created
829 * everything. This way we don't introduce memory corruptions
830 * during side-effect processing and correclty count established
831 * associations.
832 */
833 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
834 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
835 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000836 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
837 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700838 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
839
Neil Horman9f70f462013-12-10 06:48:15 -0500840 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700841 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
842 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
843
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700844 /* This will send the COOKIE ACK */
845 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
846
847 /* Queue the ASSOC_CHANGE event */
848 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
849
850 /* Send up the Adaptation Layer Indication event */
851 if (ai_ev)
852 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
853 SCTP_ULPEVENT(ai_ev));
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return SCTP_DISPOSITION_CONSUME;
856
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700857nomem_aiev:
858 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859nomem_ev:
860 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861nomem_init:
862 sctp_association_free(new_asoc);
863nomem:
864 return SCTP_DISPOSITION_NOMEM;
865}
866
867/*
868 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300869 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
871 * RFC 2960 5.1 Normal Establishment of an Association
872 *
873 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
874 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
875 * timer. It may also notify its ULP about the successful
876 * establishment of the association with a Communication Up
877 * notification (see Section 10).
878 *
879 * Verification Tag:
880 * Inputs
881 * (endpoint, asoc, chunk)
882 *
883 * Outputs
884 * (asoc, reply_msg, msg_up, timers, counters)
885 *
886 * The return value is the disposition of the chunk.
887 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000888sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
889 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 const struct sctp_association *asoc,
891 const sctp_subtype_t type, void *arg,
892 sctp_cmd_seq_t *commands)
893{
894 struct sctp_chunk *chunk = arg;
895 struct sctp_ulpevent *ev;
896
897 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000898 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* Verify that the chunk length for the COOKIE-ACK is OK.
901 * If we don't do this, any bundled chunks may be junked.
902 */
903 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000904 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 commands);
906
907 /* Reset init error count upon receipt of COOKIE-ACK,
908 * to avoid problems with the managemement of this
909 * counter in stale cookie situations when a transition back
910 * from the COOKIE-ECHOED state to the COOKIE-WAIT
911 * state is performed.
912 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700913 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* RFC 2960 5.1 Normal Establishment of an Association
916 *
917 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
918 * from the COOKIE-ECHOED state to the ESTABLISHED state,
919 * stopping the T1-cookie timer.
920 */
921 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
922 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
923 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
924 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000925 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
926 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500928 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
930 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* It may also notify its ULP about the successful
933 * establishment of the association with a Communication Up
934 * notification (see Section 10).
935 */
936 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
937 0, asoc->c.sinit_num_ostreams,
938 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700939 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (!ev)
942 goto nomem;
943
944 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
945
946 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800947 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800949 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800951 if (asoc->peer.adaptation_ind) {
952 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!ev)
954 goto nomem;
955
956 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
957 SCTP_ULPEVENT(ev));
958 }
959
960 return SCTP_DISPOSITION_CONSUME;
961nomem:
962 return SCTP_DISPOSITION_NOMEM;
963}
964
965/* Generate and sendout a heartbeat packet. */
966static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
967 const struct sctp_association *asoc,
968 const sctp_subtype_t type,
969 void *arg,
970 sctp_cmd_seq_t *commands)
971{
972 struct sctp_transport *transport = (struct sctp_transport *) arg;
973 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000976 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!reply)
978 return SCTP_DISPOSITION_NOMEM;
979
980 /* Set rto_pending indicating that an RTT measurement
981 * is started with this heartbeat chunk.
982 */
983 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
984 SCTP_TRANSPORT(transport));
985
986 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
987 return SCTP_DISPOSITION_CONSUME;
988}
989
990/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000991sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
992 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 const struct sctp_association *asoc,
994 const sctp_subtype_t type,
995 void *arg,
996 sctp_cmd_seq_t *commands)
997{
998 struct sctp_transport *transport = (struct sctp_transport *) arg;
999
Vlad Yasevichb9f84782009-08-26 09:36:25 -04001000 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07001001 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1002 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1004 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08001005 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001006 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1007 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 return SCTP_DISPOSITION_DELETE_TCB;
1009 }
1010
1011 /* Section 3.3.5.
1012 * The Sender-specific Heartbeat Info field should normally include
1013 * information about the sender's current time when this HEARTBEAT
1014 * chunk is sent and the destination transport address to which this
1015 * HEARTBEAT is sent (see Section 8.3).
1016 */
1017
Frank Filz52ccb8e2005-12-22 11:36:46 -08001018 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (SCTP_DISPOSITION_NOMEM ==
1020 sctp_sf_heartbeat(ep, asoc, type, arg,
1021 commands))
1022 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /* Set transport error counter and association error counter
1025 * when sending heartbeat.
1026 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001027 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 SCTP_TRANSPORT(transport));
1029 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001030 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1031 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1033 SCTP_TRANSPORT(transport));
1034
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001035 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
1039 * Process an heartbeat request.
1040 *
1041 * Section: 8.3 Path Heartbeat
1042 * The receiver of the HEARTBEAT should immediately respond with a
1043 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1044 * from the received HEARTBEAT chunk.
1045 *
1046 * Verification Tag: 8.5 Verification Tag [Normal verification]
1047 * When receiving an SCTP packet, the endpoint MUST ensure that the
1048 * value in the Verification Tag field of the received SCTP packet
1049 * matches its own Tag. If the received Verification Tag value does not
1050 * match the receiver's own tag value, the receiver shall silently
1051 * discard the packet and shall not process it any further except for
1052 * those cases listed in Section 8.5.1 below.
1053 *
1054 * Inputs
1055 * (endpoint, asoc, chunk)
1056 *
1057 * Outputs
1058 * (asoc, reply_msg, msg_up, timers, counters)
1059 *
1060 * The return value is the disposition of the chunk.
1061 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001062sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1063 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 const struct sctp_association *asoc,
1065 const sctp_subtype_t type,
1066 void *arg,
1067 sctp_cmd_seq_t *commands)
1068{
Thomas Graf06a31e22012-11-30 02:16:27 +00001069 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct sctp_chunk *chunk = arg;
1071 struct sctp_chunk *reply;
1072 size_t paylen = 0;
1073
1074 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001075 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /* Make sure that the HEARTBEAT chunk has a valid length. */
1078 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001079 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 commands);
1081
1082 /* 8.3 The receiver of the HEARTBEAT should immediately
1083 * respond with a HEARTBEAT ACK that contains the Heartbeat
1084 * Information field copied from the received HEARTBEAT chunk.
1085 */
1086 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001087 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001089
1090 if (ntohs(param_hdr->length) > paylen)
1091 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1092 param_hdr, commands);
1093
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001094 if (!pskb_pull(chunk->skb, paylen))
1095 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Thomas Graf06a31e22012-11-30 02:16:27 +00001097 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 if (!reply)
1099 goto nomem;
1100
1101 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1102 return SCTP_DISPOSITION_CONSUME;
1103
1104nomem:
1105 return SCTP_DISPOSITION_NOMEM;
1106}
1107
1108/*
1109 * Process the returning HEARTBEAT ACK.
1110 *
1111 * Section: 8.3 Path Heartbeat
1112 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1113 * should clear the error counter of the destination transport
1114 * address to which the HEARTBEAT was sent, and mark the destination
1115 * transport address as active if it is not so marked. The endpoint may
1116 * optionally report to the upper layer when an inactive destination
1117 * address is marked as active due to the reception of the latest
1118 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1119 * clear the association overall error count as well (as defined
1120 * in section 8.1).
1121 *
1122 * The receiver of the HEARTBEAT ACK should also perform an RTT
1123 * measurement for that destination transport address using the time
1124 * value carried in the HEARTBEAT ACK chunk.
1125 *
1126 * Verification Tag: 8.5 Verification Tag [Normal verification]
1127 *
1128 * Inputs
1129 * (endpoint, asoc, chunk)
1130 *
1131 * Outputs
1132 * (asoc, reply_msg, msg_up, timers, counters)
1133 *
1134 * The return value is the disposition of the chunk.
1135 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001136sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1137 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 const struct sctp_association *asoc,
1139 const sctp_subtype_t type,
1140 void *arg,
1141 sctp_cmd_seq_t *commands)
1142{
1143 struct sctp_chunk *chunk = arg;
1144 union sctp_addr from_addr;
1145 struct sctp_transport *link;
1146 sctp_sender_hb_info_t *hbinfo;
1147 unsigned long max_interval;
1148
1149 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001150 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001153 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1154 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001155 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 commands);
1157
1158 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001159 /* Make sure that the length of the parameter is what we expect */
1160 if (ntohs(hbinfo->param_hdr.length) !=
1161 sizeof(sctp_sender_hb_info_t)) {
1162 return SCTP_DISPOSITION_DISCARD;
1163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001166 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 if (unlikely(!link)) {
1170 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001171 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1172 __func__,
1173 asoc,
1174 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001175 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001176 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1177 __func__,
1178 asoc,
1179 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return SCTP_DISPOSITION_DISCARD;
1182 }
1183
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001184 /* Validate the 64-bit random nonce. */
1185 if (hbinfo->hb_nonce != link->hb_nonce)
1186 return SCTP_DISPOSITION_DISCARD;
1187
Frank Filz52ccb8e2005-12-22 11:36:46 -08001188 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* Check if the timestamp looks valid. */
1191 if (time_after(hbinfo->sent_at, jiffies) ||
1192 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001193 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1194 "for transport:%p\n", __func__, link);
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return SCTP_DISPOSITION_DISCARD;
1197 }
1198
1199 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1200 * the HEARTBEAT should clear the error counter of the
1201 * destination transport address to which the HEARTBEAT was
1202 * sent and mark the destination transport address as active if
1203 * it is not so marked.
1204 */
1205 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1206
1207 return SCTP_DISPOSITION_CONSUME;
1208}
1209
1210/* Helper function to send out an abort for the restart
1211 * condition.
1212 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001213static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 struct sctp_chunk *init,
1215 sctp_cmd_seq_t *commands)
1216{
1217 int len;
1218 struct sctp_packet *pkt;
1219 union sctp_addr_param *addrparm;
1220 struct sctp_errhdr *errhdr;
1221 struct sctp_endpoint *ep;
1222 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1223 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1224
1225 /* Build the error on the stack. We are way to malloc crazy
1226 * throughout the code today.
1227 */
1228 errhdr = (struct sctp_errhdr *)buffer;
1229 addrparm = (union sctp_addr_param *)errhdr->variable;
1230
1231 /* Copy into a parm format. */
1232 len = af->to_addr_param(ssa, addrparm);
1233 len += sizeof(sctp_errhdr_t);
1234
1235 errhdr->cause = SCTP_ERROR_RESTART;
1236 errhdr->length = htons(len);
1237
1238 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001239 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Association is NULL since this may be a restart attack and we
1242 * want to send back the attacker's vtag.
1243 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001244 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 if (!pkt)
1247 goto out;
1248 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1249
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001250 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /* Discard the rest of the inbound packet. */
1253 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1254
1255out:
1256 /* Even if there is no memory, treat as a failure so
1257 * the packet will get dropped.
1258 */
1259 return 0;
1260}
1261
Joe Perches123031c2010-09-08 11:04:21 +00001262static bool list_has_sctp_addr(const struct list_head *list,
1263 union sctp_addr *ipaddr)
1264{
1265 struct sctp_transport *addr;
1266
1267 list_for_each_entry(addr, list, transports) {
1268 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1269 return true;
1270 }
1271
1272 return false;
1273}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274/* A restart is occurring, check to make sure no new addresses
1275 * are being added as we may be under a takeover attack.
1276 */
1277static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1278 const struct sctp_association *asoc,
1279 struct sctp_chunk *init,
1280 sctp_cmd_seq_t *commands)
1281{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001282 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001283 struct sctp_transport *new_addr;
1284 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Joe Perches123031c2010-09-08 11:04:21 +00001286 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 * ...
1288 * Before responding the endpoint MUST check to see if the
1289 * unexpected INIT adds new addresses to the association. If new
1290 * addresses are added to the association, the endpoint MUST respond
1291 * with an ABORT..
1292 */
1293
1294 /* Search through all current addresses and make sure
1295 * we aren't adding any new ones.
1296 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001297 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001298 transports) {
1299 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1300 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001301 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001302 commands);
1303 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 break;
Joe Perches123031c2010-09-08 11:04:21 +00001305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312/* Populate the verification/tie tags based on overlapping INIT
1313 * scenario.
1314 *
1315 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1316 */
1317static void sctp_tietags_populate(struct sctp_association *new_asoc,
1318 const struct sctp_association *asoc)
1319{
1320 switch (asoc->state) {
1321
1322 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1323
1324 case SCTP_STATE_COOKIE_WAIT:
1325 new_asoc->c.my_vtag = asoc->c.my_vtag;
1326 new_asoc->c.my_ttag = asoc->c.my_vtag;
1327 new_asoc->c.peer_ttag = 0;
1328 break;
1329
1330 case SCTP_STATE_COOKIE_ECHOED:
1331 new_asoc->c.my_vtag = asoc->c.my_vtag;
1332 new_asoc->c.my_ttag = asoc->c.my_vtag;
1333 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1334 break;
1335
1336 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1337 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1338 */
1339 default:
1340 new_asoc->c.my_ttag = asoc->c.my_vtag;
1341 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1342 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /* Other parameters for the endpoint SHOULD be copied from the
1346 * existing parameters of the association (e.g. number of
1347 * outbound streams) into the INIT ACK and cookie.
1348 */
1349 new_asoc->rwnd = asoc->rwnd;
1350 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1351 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1352 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1353}
1354
1355/*
1356 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1357 * handling action.
1358 *
1359 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1360 *
1361 * Returns value representing action to be taken. These action values
1362 * correspond to Action/Description values in RFC 2960, Table 2.
1363 */
1364static char sctp_tietags_compare(struct sctp_association *new_asoc,
1365 const struct sctp_association *asoc)
1366{
1367 /* In this case, the peer may have restarted. */
1368 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1369 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1370 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1371 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1372 return 'A';
1373
1374 /* Collision case B. */
1375 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1376 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1377 (0 == asoc->c.peer_vtag))) {
1378 return 'B';
1379 }
1380
1381 /* Collision case D. */
1382 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1383 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1384 return 'D';
1385
1386 /* Collision case C. */
1387 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1388 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1389 (0 == new_asoc->c.my_ttag) &&
1390 (0 == new_asoc->c.peer_ttag))
1391 return 'C';
1392
1393 /* No match to any of the special cases; discard this packet. */
1394 return 'E';
1395}
1396
1397/* Common helper routine for both duplicate and simulataneous INIT
1398 * chunk handling.
1399 */
1400static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001401 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 const struct sctp_endpoint *ep,
1403 const struct sctp_association *asoc,
1404 const sctp_subtype_t type,
1405 void *arg, sctp_cmd_seq_t *commands)
1406{
1407 sctp_disposition_t retval;
1408 struct sctp_chunk *chunk = arg;
1409 struct sctp_chunk *repl;
1410 struct sctp_association *new_asoc;
1411 struct sctp_chunk *err_chunk;
1412 struct sctp_packet *packet;
1413 sctp_unrecognized_param_t *unk_param;
1414 int len;
1415
1416 /* 6.10 Bundling
1417 * An endpoint MUST NOT bundle INIT, INIT ACK or
1418 * SHUTDOWN COMPLETE with any other chunks.
1419 *
1420 * IG Section 2.11.2
1421 * Furthermore, we require that the receiver of an INIT chunk MUST
1422 * enforce these rules by silently discarding an arriving packet
1423 * with an INIT chunk that is bundled with other chunks.
1424 */
1425 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001426 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001429 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
1431 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001432 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 /* Make sure that the INIT chunk has a valid length.
1435 * In this case, we generate a protocol violation since we have
1436 * an association established.
1437 */
1438 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001439 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 commands);
1441 /* Grab the INIT header. */
1442 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1443
1444 /* Tag the variable length parameters. */
1445 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1446
1447 /* Verify the INIT chunk before processing it. */
1448 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001449 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1451 &err_chunk)) {
1452 /* This chunk contains fatal error. It is to be discarded.
1453 * Send an ABORT, with causes if there is any.
1454 */
1455 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001456 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 (__u8 *)(err_chunk->chunk_hdr) +
1458 sizeof(sctp_chunkhdr_t),
1459 ntohs(err_chunk->chunk_hdr->length) -
1460 sizeof(sctp_chunkhdr_t));
1461
1462 if (packet) {
1463 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1464 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001465 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 retval = SCTP_DISPOSITION_CONSUME;
1467 } else {
1468 retval = SCTP_DISPOSITION_NOMEM;
1469 }
1470 goto cleanup;
1471 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001472 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 commands);
1474 }
1475 }
1476
1477 /*
1478 * Other parameters for the endpoint SHOULD be copied from the
1479 * existing parameters of the association (e.g. number of
1480 * outbound streams) into the INIT ACK and cookie.
1481 * FIXME: We are copying parameters from the endpoint not the
1482 * association.
1483 */
1484 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1485 if (!new_asoc)
1486 goto nomem;
1487
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001488 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1489 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1490 goto nomem;
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 /* In the outbound INIT ACK the endpoint MUST copy its current
1493 * Verification Tag and Peers Verification tag into a reserved
1494 * place (local tie-tag and per tie-tag) within the state cookie.
1495 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001496 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001498 GFP_ATOMIC))
1499 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /* Make sure no new addresses are being added during the
1502 * restart. Do not do this check for COOKIE-WAIT state,
1503 * since there are no peer addresses to check against.
1504 * Upon return an ABORT will have been sent if needed.
1505 */
1506 if (!sctp_state(asoc, COOKIE_WAIT)) {
1507 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1508 commands)) {
1509 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001510 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 }
1513
1514 sctp_tietags_populate(new_asoc, asoc);
1515
1516 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1517
1518 /* If there are errors need to be reported for unknown parameters,
1519 * make sure to reserve enough room in the INIT ACK for them.
1520 */
1521 len = 0;
1522 if (err_chunk) {
1523 len = ntohs(err_chunk->chunk_hdr->length) -
1524 sizeof(sctp_chunkhdr_t);
1525 }
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1528 if (!repl)
1529 goto nomem;
1530
1531 /* If there are errors need to be reported for unknown parameters,
1532 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1533 * parameter.
1534 */
1535 if (err_chunk) {
1536 /* Get the "Unrecognized parameter" parameter(s) out of the
1537 * ERROR chunk generated by sctp_verify_init(). Since the
1538 * error cause code for "unknown parameter" and the
1539 * "Unrecognized parameter" type is the same, we can
1540 * construct the parameters in INIT ACK by copying the
1541 * ERROR causes over.
1542 */
1543 unk_param = (sctp_unrecognized_param_t *)
1544 ((__u8 *)(err_chunk->chunk_hdr) +
1545 sizeof(sctp_chunkhdr_t));
1546 /* Replace the cause code with the "Unrecognized parameter"
1547 * parameter type.
1548 */
1549 sctp_addto_chunk(repl, len, unk_param);
1550 }
1551
1552 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1553 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1554
1555 /*
1556 * Note: After sending out INIT ACK with the State Cookie parameter,
1557 * "Z" MUST NOT allocate any resources for this new association.
1558 * Otherwise, "Z" will be vulnerable to resource attacks.
1559 */
1560 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1561 retval = SCTP_DISPOSITION_CONSUME;
1562
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001563 return retval;
1564
1565nomem:
1566 retval = SCTP_DISPOSITION_NOMEM;
1567nomem_retval:
1568 if (new_asoc)
1569 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570cleanup:
1571 if (err_chunk)
1572 sctp_chunk_free(err_chunk);
1573 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001577 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 * This means we started an INIT and then we got an INIT request from
1579 * our peer.
1580 *
1581 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1582 * This usually indicates an initialization collision, i.e., each
1583 * endpoint is attempting, at about the same time, to establish an
1584 * association with the other endpoint.
1585 *
1586 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1587 * endpoint MUST respond with an INIT ACK using the same parameters it
1588 * sent in its original INIT chunk (including its Verification Tag,
1589 * unchanged). These original parameters are combined with those from the
1590 * newly received INIT chunk. The endpoint shall also generate a State
1591 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1592 * INIT to calculate the State Cookie.
1593 *
1594 * After that, the endpoint MUST NOT change its state, the T1-init
1595 * timer shall be left running and the corresponding TCB MUST NOT be
1596 * destroyed. The normal procedures for handling State Cookies when
1597 * a TCB exists will resolve the duplicate INITs to a single association.
1598 *
1599 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1600 * its Tie-Tags with the Tag information of itself and its peer (see
1601 * section 5.2.2 for a description of the Tie-Tags).
1602 *
1603 * Verification Tag: Not explicit, but an INIT can not have a valid
1604 * verification tag, so we skip the check.
1605 *
1606 * Inputs
1607 * (endpoint, asoc, chunk)
1608 *
1609 * Outputs
1610 * (asoc, reply_msg, msg_up, timers, counters)
1611 *
1612 * The return value is the disposition of the chunk.
1613 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001614sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1615 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 const struct sctp_association *asoc,
1617 const sctp_subtype_t type,
1618 void *arg,
1619 sctp_cmd_seq_t *commands)
1620{
1621 /* Call helper to do the real work for both simulataneous and
1622 * duplicate INIT chunk handling.
1623 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001624 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625}
1626
1627/*
1628 * Handle duplicated INIT messages. These are usually delayed
1629 * restransmissions.
1630 *
1631 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1632 * COOKIE-ECHOED and COOKIE-WAIT
1633 *
1634 * Unless otherwise stated, upon reception of an unexpected INIT for
1635 * this association, the endpoint shall generate an INIT ACK with a
1636 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1637 * current Verification Tag and peer's Verification Tag into a reserved
1638 * place within the state cookie. We shall refer to these locations as
1639 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1640 * containing this INIT ACK MUST carry a Verification Tag value equal to
1641 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1642 * MUST contain a new Initiation Tag (randomly generated see Section
1643 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1644 * existing parameters of the association (e.g. number of outbound
1645 * streams) into the INIT ACK and cookie.
1646 *
1647 * After sending out the INIT ACK, the endpoint shall take no further
1648 * actions, i.e., the existing association, including its current state,
1649 * and the corresponding TCB MUST NOT be changed.
1650 *
1651 * Note: Only when a TCB exists and the association is not in a COOKIE-
1652 * WAIT state are the Tie-Tags populated. For a normal association INIT
1653 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1654 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1655 * State Cookie are populated as specified in section 5.2.1.
1656 *
1657 * Verification Tag: Not specified, but an INIT has no way of knowing
1658 * what the verification tag could be, so we ignore it.
1659 *
1660 * Inputs
1661 * (endpoint, asoc, chunk)
1662 *
1663 * Outputs
1664 * (asoc, reply_msg, msg_up, timers, counters)
1665 *
1666 * The return value is the disposition of the chunk.
1667 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001668sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1669 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 const struct sctp_association *asoc,
1671 const sctp_subtype_t type,
1672 void *arg,
1673 sctp_cmd_seq_t *commands)
1674{
1675 /* Call helper to do the real work for both simulataneous and
1676 * duplicate INIT chunk handling.
1677 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001678 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681
Vlad Yasevich610ab732007-01-15 19:18:30 -08001682/*
1683 * Unexpected INIT-ACK handler.
1684 *
1685 * Section 5.2.3
1686 * If an INIT ACK received by an endpoint in any state other than the
1687 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1688 * An unexpected INIT ACK usually indicates the processing of an old or
1689 * duplicated INIT chunk.
1690*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001691sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1692 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001693 const struct sctp_association *asoc,
1694 const sctp_subtype_t type,
1695 void *arg, sctp_cmd_seq_t *commands)
1696{
1697 /* Per the above section, we'll discard the chunk if we have an
1698 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1699 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001700 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001701 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001702 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001703 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001704}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1707 *
1708 * Section 5.2.4
1709 * A) In this case, the peer may have restarted.
1710 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001711static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1712 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 const struct sctp_association *asoc,
1714 struct sctp_chunk *chunk,
1715 sctp_cmd_seq_t *commands,
1716 struct sctp_association *new_asoc)
1717{
1718 sctp_init_chunk_t *peer_init;
1719 struct sctp_ulpevent *ev;
1720 struct sctp_chunk *repl;
1721 struct sctp_chunk *err;
1722 sctp_disposition_t disposition;
1723
1724 /* new_asoc is a brand-new association, so these are not yet
1725 * side effects--it is safe to run them here.
1726 */
1727 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1728
Wei Yongjunde6becd2011-04-19 21:30:51 +00001729 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 GFP_ATOMIC))
1731 goto nomem;
1732
Xin Long0e67ad52018-05-05 14:59:47 +08001733 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1734 return SCTP_DISPOSITION_DISCARD;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* Make sure no new addresses are being added during the
1737 * restart. Though this is a pretty complicated attack
1738 * since you'd have to get inside the cookie.
1739 */
Xin Long0e67ad52018-05-05 14:59:47 +08001740 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1744 * the peer has restarted (Action A), it MUST NOT setup a new
1745 * association but instead resend the SHUTDOWN ACK and send an ERROR
1746 * chunk with a "Cookie Received while Shutting Down" error cause to
1747 * its peer.
1748 */
1749 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001750 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1752 chunk, commands);
1753 if (SCTP_DISPOSITION_NOMEM == disposition)
1754 goto nomem;
1755
1756 err = sctp_make_op_error(asoc, chunk,
1757 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001758 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 if (err)
1760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1761 SCTP_CHUNK(err));
1762
1763 return SCTP_DISPOSITION_CONSUME;
1764 }
1765
Wei Yongjuna000c012011-05-29 23:23:36 +00001766 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1767 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001769 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1770 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1771 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1773
Wei Yongjuna000c012011-05-29 23:23:36 +00001774 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1775 * and ASCONF-ACK cache.
1776 */
1777 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1778 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1779 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 repl = sctp_make_cookie_ack(new_asoc, chunk);
1782 if (!repl)
1783 goto nomem;
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Report association restart to upper layer. */
1786 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1787 new_asoc->c.sinit_num_ostreams,
1788 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001789 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (!ev)
1791 goto nomem_ev;
1792
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001793 /* Update the content of current association. */
1794 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Jere Leppänen5e350e72020-05-20 18:15:31 +03001796 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1797 sctp_state(asoc, SHUTDOWN_SENT)) &&
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001798 (sctp_sstate(asoc->base.sk, CLOSING) ||
1799 sock_flag(asoc->base.sk, SOCK_DEAD))) {
Jere Leppänen5e350e72020-05-20 18:15:31 +03001800 /* If the socket has been closed by user, don't
1801 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1802 * bundled with COOKIE_ACK.
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001803 */
1804 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1805 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1806 SCTP_ST_CHUNK(0), NULL,
1807 commands);
1808 } else {
1809 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1810 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1811 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return SCTP_DISPOSITION_CONSUME;
1814
1815nomem_ev:
1816 sctp_chunk_free(repl);
1817nomem:
1818 return SCTP_DISPOSITION_NOMEM;
1819}
1820
1821/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1822 *
1823 * Section 5.2.4
1824 * B) In this case, both sides may be attempting to start an association
1825 * at about the same time but the peer endpoint started its INIT
1826 * after responding to the local endpoint's INIT
1827 */
1828/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001829static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1830 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 const struct sctp_association *asoc,
1832 struct sctp_chunk *chunk,
1833 sctp_cmd_seq_t *commands,
1834 struct sctp_association *new_asoc)
1835{
1836 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct sctp_chunk *repl;
1838
1839 /* new_asoc is a brand-new association, so these are not yet
1840 * side effects--it is safe to run them here.
1841 */
1842 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001843 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 GFP_ATOMIC))
1845 goto nomem;
1846
Xin Long0e67ad52018-05-05 14:59:47 +08001847 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1848 return SCTP_DISPOSITION_DISCARD;
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /* Update the content of current association. */
1851 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1852 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1853 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001854 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1856
1857 repl = sctp_make_cookie_ack(new_asoc, chunk);
1858 if (!repl)
1859 goto nomem;
1860
1861 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* RFC 2960 5.1 Normal Establishment of an Association
1864 *
1865 * D) IMPLEMENTATION NOTE: An implementation may choose to
1866 * send the Communication Up notification to the SCTP user
1867 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001868 *
1869 * Sadly, this needs to be implemented as a side-effect, because
1870 * we are not guaranteed to have set the association id of the real
1871 * association and so these notifications need to be delayed until
1872 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Vlad Yasevich07d93962007-05-04 13:55:27 -07001875 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001878 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001880 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001881 *
1882 * This also needs to be done as a side effect for the same reason as
1883 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001885 if (asoc->peer.adaptation_ind)
1886 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 return SCTP_DISPOSITION_CONSUME;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890nomem:
1891 return SCTP_DISPOSITION_NOMEM;
1892}
1893
1894/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1895 *
1896 * Section 5.2.4
1897 * C) In this case, the local endpoint's cookie has arrived late.
1898 * Before it arrived, the local endpoint sent an INIT and received an
1899 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1900 * but a new tag of its own.
1901 */
1902/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001903static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1904 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 const struct sctp_association *asoc,
1906 struct sctp_chunk *chunk,
1907 sctp_cmd_seq_t *commands,
1908 struct sctp_association *new_asoc)
1909{
1910 /* The cookie should be silently discarded.
1911 * The endpoint SHOULD NOT change states and should leave
1912 * any timers running.
1913 */
1914 return SCTP_DISPOSITION_DISCARD;
1915}
1916
1917/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1918 *
1919 * Section 5.2.4
1920 *
1921 * D) When both local and remote tags match the endpoint should always
1922 * enter the ESTABLISHED state, if it has not already done so.
1923 */
1924/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001925static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1926 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 const struct sctp_association *asoc,
1928 struct sctp_chunk *chunk,
1929 sctp_cmd_seq_t *commands,
1930 struct sctp_association *new_asoc)
1931{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001932 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct sctp_chunk *repl;
1934
1935 /* Clarification from Implementor's Guide:
1936 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001937 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1938 * It should stop any cookie timer that may be running and send
1939 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 */
1941
Xin Long0e67ad52018-05-05 14:59:47 +08001942 if (!sctp_auth_chunk_verify(net, chunk, asoc))
1943 return SCTP_DISPOSITION_DISCARD;
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 /* Don't accidentally move back into established state. */
1946 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1947 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1948 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1949 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1950 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001951 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1953 SCTP_NULL());
1954
1955 /* RFC 2960 5.1 Normal Establishment of an Association
1956 *
1957 * D) IMPLEMENTATION NOTE: An implementation may choose
1958 * to send the Communication Up notification to the
1959 * SCTP user upon reception of a valid COOKIE
1960 * ECHO chunk.
1961 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001962 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001964 asoc->c.sinit_num_ostreams,
1965 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001966 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!ev)
1968 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001971 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001973 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001975 if (asoc->peer.adaptation_ind) {
1976 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001978 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto nomem;
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Xin Longc832ac42018-05-02 13:39:46 +08001984 repl = sctp_make_cookie_ack(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (!repl)
1986 goto nomem;
1987
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001988 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1989
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001990 if (ev)
1991 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1992 SCTP_ULPEVENT(ev));
1993 if (ai_ev)
1994 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1995 SCTP_ULPEVENT(ai_ev));
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return SCTP_DISPOSITION_CONSUME;
1998
1999nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07002000 if (ai_ev)
2001 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (ev)
2003 sctp_ulpevent_free(ev);
2004 return SCTP_DISPOSITION_NOMEM;
2005}
2006
2007/*
2008 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2009 * chunk was retransmitted and then delayed in the network.
2010 *
2011 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2012 *
2013 * Verification Tag: None. Do cookie validation.
2014 *
2015 * Inputs
2016 * (endpoint, asoc, chunk)
2017 *
2018 * Outputs
2019 * (asoc, reply_msg, msg_up, timers, counters)
2020 *
2021 * The return value is the disposition of the chunk.
2022 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002023sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2024 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 const struct sctp_association *asoc,
2026 const sctp_subtype_t type,
2027 void *arg,
2028 sctp_cmd_seq_t *commands)
2029{
2030 sctp_disposition_t retval;
2031 struct sctp_chunk *chunk = arg;
2032 struct sctp_association *new_asoc;
2033 int error = 0;
2034 char action;
2035 struct sctp_chunk *err_chk_p;
2036
2037 /* Make sure that the chunk has a valid length from the protocol
2038 * perspective. In this case check to make sure we have at least
2039 * enough for the chunk header. Cookie length verification is
2040 * done later.
2041 */
2042 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002043 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 commands);
2045
2046 /* "Decode" the chunk. We have no optional parameters so we
2047 * are in good shape.
2048 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002049 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002050 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2051 sizeof(sctp_chunkhdr_t)))
2052 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2055 * of a duplicate COOKIE ECHO match the Verification Tags of the
2056 * current association, consider the State Cookie valid even if
2057 * the lifespan is exceeded.
2058 */
2059 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2060 &err_chk_p);
2061
2062 /* FIXME:
2063 * If the re-build failed, what is the proper error path
2064 * from here?
2065 *
2066 * [We should abort the association. --piggy]
2067 */
2068 if (!new_asoc) {
2069 /* FIXME: Several errors are possible. A bad cookie should
2070 * be silently discarded, but think about logging it too.
2071 */
2072 switch (error) {
2073 case -SCTP_IERROR_NOMEM:
2074 goto nomem;
2075
2076 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002077 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002079 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 case -SCTP_IERROR_BAD_SIG:
2081 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002082 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085
2086 /* Compare the tie_tag in cookie with the verification tag of
2087 * current association.
2088 */
2089 action = sctp_tietags_compare(new_asoc, asoc);
2090
2091 switch (action) {
2092 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002093 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 new_asoc);
2095 break;
2096
2097 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002098 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 new_asoc);
2100 break;
2101
2102 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002103 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 new_asoc);
2105 break;
2106
2107 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002108 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 new_asoc);
2110 break;
2111
2112 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002113 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002118 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2120
Max Matveevd5ccd492011-08-29 21:02:24 +00002121 /* Restore association pointer to provide SCTP command interpeter
2122 * with a valid context in case it needs to manipulate
2123 * the queues */
2124 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2125 SCTP_ASOC((struct sctp_association *)asoc));
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 return retval;
2128
2129nomem:
2130 return SCTP_DISPOSITION_NOMEM;
2131}
2132
2133/*
2134 * Process an ABORT. (SHUTDOWN-PENDING state)
2135 *
2136 * See sctp_sf_do_9_1_abort().
2137 */
2138sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002139 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 const struct sctp_endpoint *ep,
2141 const struct sctp_association *asoc,
2142 const sctp_subtype_t type,
2143 void *arg,
2144 sctp_cmd_seq_t *commands)
2145{
2146 struct sctp_chunk *chunk = arg;
2147
2148 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002149 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 /* Make sure that the ABORT chunk has a valid length.
2152 * Since this is an ABORT chunk, we have to discard it
2153 * because of the following text:
2154 * RFC 2960, Section 3.3.7
2155 * If an endpoint receives an ABORT with a format error or for an
2156 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002157 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 * as we do not know its true length. So, to be safe, discard the
2159 * packet.
2160 */
2161 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002162 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Vlad Yasevich75205f42007-12-20 14:12:59 -08002164 /* ADD-IP: Special case for ABORT chunks
2165 * F4) One special consideration is that ABORT Chunks arriving
2166 * destined to the IP address being deleted MUST be
2167 * ignored (see Section 5.3.1 for further details).
2168 */
2169 if (SCTP_ADDR_DEL ==
2170 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002171 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002172
Xin Long548e23f2020-02-18 12:07:53 +08002173 if (!sctp_err_chunk_valid(chunk))
2174 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2175
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002176 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179/*
2180 * Process an ABORT. (SHUTDOWN-SENT state)
2181 *
2182 * See sctp_sf_do_9_1_abort().
2183 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002184sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2185 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 const struct sctp_association *asoc,
2187 const sctp_subtype_t type,
2188 void *arg,
2189 sctp_cmd_seq_t *commands)
2190{
2191 struct sctp_chunk *chunk = arg;
2192
2193 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002194 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 /* Make sure that the ABORT chunk has a valid length.
2197 * Since this is an ABORT chunk, we have to discard it
2198 * because of the following text:
2199 * RFC 2960, Section 3.3.7
2200 * If an endpoint receives an ABORT with a format error or for an
2201 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002202 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 * as we do not know its true length. So, to be safe, discard the
2204 * packet.
2205 */
2206 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002207 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Vlad Yasevich75205f42007-12-20 14:12:59 -08002209 /* ADD-IP: Special case for ABORT chunks
2210 * F4) One special consideration is that ABORT Chunks arriving
2211 * destined to the IP address being deleted MUST be
2212 * ignored (see Section 5.3.1 for further details).
2213 */
2214 if (SCTP_ADDR_DEL ==
2215 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002216 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002217
Xin Long548e23f2020-02-18 12:07:53 +08002218 if (!sctp_err_chunk_valid(chunk))
2219 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 /* Stop the T2-shutdown timer. */
2222 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2223 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2224
2225 /* Stop the T5-shutdown guard timer. */
2226 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2227 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2228
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002229 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232/*
2233 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2234 *
2235 * See sctp_sf_do_9_1_abort().
2236 */
2237sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002238 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 const struct sctp_endpoint *ep,
2240 const struct sctp_association *asoc,
2241 const sctp_subtype_t type,
2242 void *arg,
2243 sctp_cmd_seq_t *commands)
2244{
2245 /* The same T2 timer, so we should be able to use
2246 * common function with the SHUTDOWN-SENT state.
2247 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002248 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249}
2250
2251/*
2252 * Handle an Error received in COOKIE_ECHOED state.
2253 *
2254 * Only handle the error type of stale COOKIE Error, the other errors will
2255 * be ignored.
2256 *
2257 * Inputs
2258 * (endpoint, asoc, chunk)
2259 *
2260 * Outputs
2261 * (asoc, reply_msg, msg_up, timers, counters)
2262 *
2263 * The return value is the disposition of the chunk.
2264 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002265sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2266 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 const struct sctp_association *asoc,
2268 const sctp_subtype_t type,
2269 void *arg,
2270 sctp_cmd_seq_t *commands)
2271{
2272 struct sctp_chunk *chunk = arg;
2273 sctp_errhdr_t *err;
2274
2275 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002276 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 /* Make sure that the ERROR chunk has a valid length.
2279 * The parameter walking depends on this as well.
2280 */
2281 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002282 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 commands);
2284
2285 /* Process the error here */
2286 /* FUTURE FIXME: When PR-SCTP related and other optional
2287 * parms are emitted, this will have to change to handle multiple
2288 * errors.
2289 */
2290 sctp_walk_errors(err, chunk->chunk_hdr) {
2291 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002292 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 arg, commands);
2294 }
2295
2296 /* It is possible to have malformed error causes, and that
2297 * will cause us to end the walk early. However, since
2298 * we are discarding the packet, there should be no adverse
2299 * affects.
2300 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002301 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
2303
2304/*
2305 * Handle a Stale COOKIE Error
2306 *
2307 * Section: 5.2.6 Handle Stale COOKIE Error
2308 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2309 * one of the following three alternatives.
2310 * ...
2311 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2312 * Preservative parameter requesting an extension to the lifetime of
2313 * the State Cookie. When calculating the time extension, an
2314 * implementation SHOULD use the RTT information measured based on the
2315 * previous COOKIE ECHO / ERROR exchange, and should add no more
2316 * than 1 second beyond the measured RTT, due to long State Cookie
2317 * lifetimes making the endpoint more subject to a replay attack.
2318 *
2319 * Verification Tag: Not explicit, but safe to ignore.
2320 *
2321 * Inputs
2322 * (endpoint, asoc, chunk)
2323 *
2324 * Outputs
2325 * (asoc, reply_msg, msg_up, timers, counters)
2326 *
2327 * The return value is the disposition of the chunk.
2328 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002329static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2330 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 const struct sctp_association *asoc,
2332 const sctp_subtype_t type,
2333 void *arg,
2334 sctp_cmd_seq_t *commands)
2335{
2336 struct sctp_chunk *chunk = arg;
Arnd Bergmann3ef0a252015-09-30 13:26:40 +02002337 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 sctp_cookie_preserve_param_t bht;
2339 sctp_errhdr_t *err;
2340 struct sctp_chunk *reply;
2341 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002342 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Vlad Yasevich81845c22006-01-30 15:59:54 -08002344 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002345 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2346 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002348 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return SCTP_DISPOSITION_DELETE_TCB;
2350 }
2351
2352 err = (sctp_errhdr_t *)(chunk->skb->data);
2353
2354 /* When calculating the time extension, an implementation
2355 * SHOULD use the RTT information measured based on the
2356 * previous COOKIE ECHO / ERROR exchange, and should add no
2357 * more than 1 second beyond the measured RTT, due to long
2358 * State Cookie lifetimes making the endpoint more subject to
2359 * a replay attack.
2360 * Measure of Staleness's unit is usec. (1/1000000 sec)
2361 * Suggested Cookie Life-span Increment's unit is msec.
2362 * (1/1000 sec)
2363 * In general, if you use the suggested cookie life, the value
2364 * found in the field of measure of staleness should be doubled
2365 * to give ample time to retransmit the new cookie and thus
2366 * yield a higher probability of success on the reattempt.
2367 */
Al Viro34bcca22006-11-20 17:27:15 -08002368 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 stale = (stale * 2) / 1000;
2370
2371 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2372 bht.param_hdr.length = htons(sizeof(bht));
2373 bht.lifespan_increment = htonl(stale);
2374
2375 /* Build that new INIT chunk. */
2376 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2377 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2378 if (!reply)
2379 goto nomem;
2380
2381 sctp_addto_chunk(reply, sizeof(bht), &bht);
2382
2383 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2384 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2385
2386 /* Stop pending T3-rtx and heartbeat timers */
2387 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2388 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2389
2390 /* Delete non-primary peer ip addresses since we are transitioning
2391 * back to the COOKIE-WAIT state
2392 */
2393 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2394
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002395 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2396 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002398 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 SCTP_TRANSPORT(asoc->peer.primary_path));
2400
2401 /* Cast away the const modifier, as we want to just
2402 * rerun it through as a sideffect.
2403 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002404 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2407 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2408 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2409 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2410 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2411 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2412
2413 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2414
2415 return SCTP_DISPOSITION_CONSUME;
2416
2417nomem:
2418 return SCTP_DISPOSITION_NOMEM;
2419}
2420
2421/*
2422 * Process an ABORT.
2423 *
2424 * Section: 9.1
2425 * After checking the Verification Tag, the receiving endpoint shall
2426 * remove the association from its record, and shall report the
2427 * termination to its upper layer.
2428 *
2429 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2430 * B) Rules for packet carrying ABORT:
2431 *
2432 * - The endpoint shall always fill in the Verification Tag field of the
2433 * outbound packet with the destination endpoint's tag value if it
2434 * is known.
2435 *
2436 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2437 * MUST follow the procedure described in Section 8.4.
2438 *
2439 * - The receiver MUST accept the packet if the Verification Tag
2440 * matches either its own tag, OR the tag of its peer. Otherwise, the
2441 * receiver MUST silently discard the packet and take no further
2442 * action.
2443 *
2444 * Inputs
2445 * (endpoint, asoc, chunk)
2446 *
2447 * Outputs
2448 * (asoc, reply_msg, msg_up, timers, counters)
2449 *
2450 * The return value is the disposition of the chunk.
2451 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002452sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2453 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 const struct sctp_association *asoc,
2455 const sctp_subtype_t type,
2456 void *arg,
2457 sctp_cmd_seq_t *commands)
2458{
2459 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
2461 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002462 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* Make sure that the ABORT chunk has a valid length.
2465 * Since this is an ABORT chunk, we have to discard it
2466 * because of the following text:
2467 * RFC 2960, Section 3.3.7
2468 * If an endpoint receives an ABORT with a format error or for an
2469 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002470 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 * as we do not know its true length. So, to be safe, discard the
2472 * packet.
2473 */
2474 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002475 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Vlad Yasevich75205f42007-12-20 14:12:59 -08002477 /* ADD-IP: Special case for ABORT chunks
2478 * F4) One special consideration is that ABORT Chunks arriving
2479 * destined to the IP address being deleted MUST be
2480 * ignored (see Section 5.3.1 for further details).
2481 */
2482 if (SCTP_ADDR_DEL ==
2483 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002484 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002485
Xin Long548e23f2020-02-18 12:07:53 +08002486 if (!sctp_err_chunk_valid(chunk))
2487 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2488
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002489 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002490}
2491
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002492static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2493 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002494 const struct sctp_association *asoc,
2495 const sctp_subtype_t type,
2496 void *arg,
2497 sctp_cmd_seq_t *commands)
2498{
2499 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002500 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002501 __be16 error = SCTP_ERROR_NO_ERROR;
2502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 /* See if we have an error cause code in the chunk. */
2504 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002505
Xin Long548e23f2020-02-18 12:07:53 +08002506 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2508
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002509 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002510 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002511 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002512 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2513 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 return SCTP_DISPOSITION_ABORT;
2516}
2517
2518/*
2519 * Process an ABORT. (COOKIE-WAIT state)
2520 *
2521 * See sctp_sf_do_9_1_abort() above.
2522 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002523sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2524 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 const struct sctp_association *asoc,
2526 const sctp_subtype_t type,
2527 void *arg,
2528 sctp_cmd_seq_t *commands)
2529{
2530 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002531 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002532 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002535 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 /* Make sure that the ABORT chunk has a valid length.
2538 * Since this is an ABORT chunk, we have to discard it
2539 * because of the following text:
2540 * RFC 2960, Section 3.3.7
2541 * If an endpoint receives an ABORT with a format error or for an
2542 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002543 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 * as we do not know its true length. So, to be safe, discard the
2545 * packet.
2546 */
2547 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002548 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
2550 /* See if we have an error cause code in the chunk. */
2551 len = ntohs(chunk->chunk_hdr->length);
2552 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2553 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2554
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002555 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002556 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
2558
2559/*
2560 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2561 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002562sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2563 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 const struct sctp_association *asoc,
2565 const sctp_subtype_t type,
2566 void *arg,
2567 sctp_cmd_seq_t *commands)
2568{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002569 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002570 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002571 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574/*
2575 * Process an ABORT. (COOKIE-ECHOED state)
2576 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002577sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2578 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 const struct sctp_association *asoc,
2580 const sctp_subtype_t type,
2581 void *arg,
2582 sctp_cmd_seq_t *commands)
2583{
2584 /* There is a single T1 timer, so we should be able to use
2585 * common function with the COOKIE-WAIT state.
2586 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002587 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
2590/*
2591 * Stop T1 timer and abort association with "INIT failed".
2592 *
2593 * This is common code called by several sctp_sf_*_abort() functions above.
2594 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002595static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2596 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002597 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002598 const struct sctp_association *asoc,
2599 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002601 pr_debug("%s: ABORT received (INIT)\n", __func__);
2602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2604 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002605 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2607 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002608 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /* CMD_INIT_FAILED will DELETE_TCB. */
2610 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002611 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002612
Frank Filz3f7a87d2005-06-20 13:14:57 -07002613 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614}
2615
2616/*
2617 * sctp_sf_do_9_2_shut
2618 *
2619 * Section: 9.2
2620 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2621 * - enter the SHUTDOWN-RECEIVED state,
2622 *
2623 * - stop accepting new data from its SCTP user
2624 *
2625 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2626 * that all its outstanding DATA chunks have been received by the
2627 * SHUTDOWN sender.
2628 *
2629 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2630 * send a SHUTDOWN in response to a ULP request. And should discard
2631 * subsequent SHUTDOWN chunks.
2632 *
2633 * If there are still outstanding DATA chunks left, the SHUTDOWN
2634 * receiver shall continue to follow normal data transmission
2635 * procedures defined in Section 6 until all outstanding DATA chunks
2636 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2637 * new data from its SCTP user.
2638 *
2639 * Verification Tag: 8.5 Verification Tag [Normal verification]
2640 *
2641 * Inputs
2642 * (endpoint, asoc, chunk)
2643 *
2644 * Outputs
2645 * (asoc, reply_msg, msg_up, timers, counters)
2646 *
2647 * The return value is the disposition of the chunk.
2648 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002649sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2650 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 const struct sctp_association *asoc,
2652 const sctp_subtype_t type,
2653 void *arg,
2654 sctp_cmd_seq_t *commands)
2655{
2656 struct sctp_chunk *chunk = arg;
2657 sctp_shutdownhdr_t *sdh;
2658 sctp_disposition_t disposition;
2659 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002660 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
2662 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002663 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
2665 /* Make sure that the SHUTDOWN chunk has a valid length. */
2666 if (!sctp_chunk_length_valid(chunk,
2667 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002668 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 commands);
2670
2671 /* Convert the elaborate header. */
2672 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2673 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2674 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002675 ctsn = ntohl(sdh->cum_tsn_ack);
2676
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002677 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002678 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2679 asoc->ctsn_ack_point);
2680
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002681 return SCTP_DISPOSITION_DISCARD;
2682 }
2683
Wei Yongjundf10eec2008-10-23 01:00:21 -07002684 /* If Cumulative TSN Ack beyond the max tsn currently
2685 * send, terminating the association and respond to the
2686 * sender with an ABORT.
2687 */
2688 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002689 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002691 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2692 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2693 * inform the application that it should cease sending data.
2694 */
2695 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2696 if (!ev) {
2697 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002698 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002699 }
2700 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2703 * - enter the SHUTDOWN-RECEIVED state,
2704 * - stop accepting new data from its SCTP user
2705 *
2706 * [This is implicit in the new state.]
2707 */
2708 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2709 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2710 disposition = SCTP_DISPOSITION_CONSUME;
2711
2712 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002713 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 arg, commands);
2715 }
2716
2717 if (SCTP_DISPOSITION_NOMEM == disposition)
2718 goto out;
2719
2720 /* - verify, by checking the Cumulative TSN Ack field of the
2721 * chunk, that all its outstanding DATA chunks have been
2722 * received by the SHUTDOWN sender.
2723 */
2724 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002725 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727out:
2728 return disposition;
2729}
2730
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002731/*
2732 * sctp_sf_do_9_2_shut_ctsn
2733 *
2734 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2735 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2736 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2737 * MUST be processed.
2738 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002739sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2740 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002741 const struct sctp_association *asoc,
2742 const sctp_subtype_t type,
2743 void *arg,
2744 sctp_cmd_seq_t *commands)
2745{
2746 struct sctp_chunk *chunk = arg;
2747 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002748 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002749
2750 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002751 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002752
2753 /* Make sure that the SHUTDOWN chunk has a valid length. */
2754 if (!sctp_chunk_length_valid(chunk,
2755 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002756 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002757 commands);
2758
2759 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002760 ctsn = ntohl(sdh->cum_tsn_ack);
2761
2762 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002763 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2764 asoc->ctsn_ack_point);
2765
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002766 return SCTP_DISPOSITION_DISCARD;
2767 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002768
2769 /* If Cumulative TSN Ack beyond the max tsn currently
2770 * send, terminating the association and respond to the
2771 * sender with an ABORT.
2772 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002773 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002774 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002775
2776 /* verify, by checking the Cumulative TSN Ack field of the
2777 * chunk, that all its outstanding DATA chunks have been
2778 * received by the SHUTDOWN sender.
2779 */
2780 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2781 SCTP_BE32(sdh->cum_tsn_ack));
2782
2783 return SCTP_DISPOSITION_CONSUME;
2784}
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786/* RFC 2960 9.2
2787 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2788 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2789 * transport addresses (either in the IP addresses or in the INIT chunk)
2790 * that belong to this association, it should discard the INIT chunk and
2791 * retransmit the SHUTDOWN ACK chunk.
2792 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002793sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2794 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 const struct sctp_association *asoc,
2796 const sctp_subtype_t type,
2797 void *arg,
2798 sctp_cmd_seq_t *commands)
2799{
2800 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2801 struct sctp_chunk *reply;
2802
Vlad Yasevichece25df2007-09-07 16:30:54 -04002803 /* Make sure that the chunk has a valid length */
2804 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002805 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002806 commands);
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 /* Since we are not going to really process this INIT, there
2809 * is no point in verifying chunk boundries. Just generate
2810 * the SHUTDOWN ACK.
2811 */
2812 reply = sctp_make_shutdown_ack(asoc, chunk);
2813 if (NULL == reply)
2814 goto nomem;
2815
2816 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2817 * the T2-SHUTDOWN timer.
2818 */
2819 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2820
2821 /* and restart the T2-shutdown timer. */
2822 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2823 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2824
2825 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2826
2827 return SCTP_DISPOSITION_CONSUME;
2828nomem:
2829 return SCTP_DISPOSITION_NOMEM;
2830}
2831
2832/*
2833 * sctp_sf_do_ecn_cwr
2834 *
2835 * Section: Appendix A: Explicit Congestion Notification
2836 *
2837 * CWR:
2838 *
2839 * RFC 2481 details a specific bit for a sender to send in the header of
2840 * its next outbound TCP segment to indicate to its peer that it has
2841 * reduced its congestion window. This is termed the CWR bit. For
2842 * SCTP the same indication is made by including the CWR chunk.
2843 * This chunk contains one data element, i.e. the TSN number that
2844 * was sent in the ECNE chunk. This element represents the lowest
2845 * TSN number in the datagram that was originally marked with the
2846 * CE bit.
2847 *
2848 * Verification Tag: 8.5 Verification Tag [Normal verification]
2849 * Inputs
2850 * (endpoint, asoc, chunk)
2851 *
2852 * Outputs
2853 * (asoc, reply_msg, msg_up, timers, counters)
2854 *
2855 * The return value is the disposition of the chunk.
2856 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002857sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2858 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 const struct sctp_association *asoc,
2860 const sctp_subtype_t type,
2861 void *arg,
2862 sctp_cmd_seq_t *commands)
2863{
2864 sctp_cwrhdr_t *cwr;
2865 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002866 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002869 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002872 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2876 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2877
Al Viro34bcca22006-11-20 17:27:15 -08002878 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002881 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 /* Stop sending ECNE. */
2883 sctp_add_cmd_sf(commands,
2884 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002885 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887 return SCTP_DISPOSITION_CONSUME;
2888}
2889
2890/*
2891 * sctp_sf_do_ecne
2892 *
2893 * Section: Appendix A: Explicit Congestion Notification
2894 *
2895 * ECN-Echo
2896 *
2897 * RFC 2481 details a specific bit for a receiver to send back in its
2898 * TCP acknowledgements to notify the sender of the Congestion
2899 * Experienced (CE) bit having arrived from the network. For SCTP this
2900 * same indication is made by including the ECNE chunk. This chunk
2901 * contains one data element, i.e. the lowest TSN associated with the IP
2902 * datagram marked with the CE bit.....
2903 *
2904 * Verification Tag: 8.5 Verification Tag [Normal verification]
2905 * Inputs
2906 * (endpoint, asoc, chunk)
2907 *
2908 * Outputs
2909 * (asoc, reply_msg, msg_up, timers, counters)
2910 *
2911 * The return value is the disposition of the chunk.
2912 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002913sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2914 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 const struct sctp_association *asoc,
2916 const sctp_subtype_t type,
2917 void *arg,
2918 sctp_cmd_seq_t *commands)
2919{
2920 sctp_ecnehdr_t *ecne;
2921 struct sctp_chunk *chunk = arg;
2922
2923 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002924 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002927 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 commands);
2929
2930 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2931 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2932
2933 /* If this is a newer ECNE than the last CWR packet we sent out */
2934 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2935 SCTP_U32(ntohl(ecne->lowest_tsn)));
2936
2937 return SCTP_DISPOSITION_CONSUME;
2938}
2939
2940/*
2941 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2942 *
2943 * The SCTP endpoint MUST always acknowledge the reception of each valid
2944 * DATA chunk.
2945 *
2946 * The guidelines on delayed acknowledgement algorithm specified in
2947 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2948 * acknowledgement SHOULD be generated for at least every second packet
2949 * (not every second DATA chunk) received, and SHOULD be generated within
2950 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2951 * situations it may be beneficial for an SCTP transmitter to be more
2952 * conservative than the algorithms detailed in this document allow.
2953 * However, an SCTP transmitter MUST NOT be more aggressive than the
2954 * following algorithms allow.
2955 *
2956 * A SCTP receiver MUST NOT generate more than one SACK for every
2957 * incoming packet, other than to update the offered window as the
2958 * receiving application consumes new data.
2959 *
2960 * Verification Tag: 8.5 Verification Tag [Normal verification]
2961 *
2962 * Inputs
2963 * (endpoint, asoc, chunk)
2964 *
2965 * Outputs
2966 * (asoc, reply_msg, msg_up, timers, counters)
2967 *
2968 * The return value is the disposition of the chunk.
2969 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002970sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2971 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 const struct sctp_association *asoc,
2973 const sctp_subtype_t type,
2974 void *arg,
2975 sctp_cmd_seq_t *commands)
2976{
2977 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002978 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 int error;
2980
2981 if (!sctp_vtag_verify(chunk, asoc)) {
2982 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2983 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002984 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002988 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 commands);
2990
wangweidongcb3f8372013-12-23 12:16:50 +08002991 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 switch (error) {
2993 case SCTP_IERROR_NO_ERROR:
2994 break;
2995 case SCTP_IERROR_HIGH_TSN:
2996 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002997 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 goto discard_noforce;
2999 case SCTP_IERROR_DUP_TSN:
3000 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003001 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 goto discard_force;
3003 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003004 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003005 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003006 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003007 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 default:
3009 BUG();
3010 }
3011
Wei Yongjun6dc76942009-11-23 15:53:53 -05003012 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3013 force = SCTP_FORCE();
3014
Neil Horman9f70f462013-12-10 06:48:15 -05003015 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3017 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3018 }
3019
3020 /* If this is the last chunk in a packet, we need to count it
3021 * toward sack generation. Note that we need to SACK every
3022 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3023 * THEM. We elect to NOT generate SACK's if the chunk fails
3024 * the verification tag test.
3025 *
3026 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3027 *
3028 * The SCTP endpoint MUST always acknowledge the reception of
3029 * each valid DATA chunk.
3030 *
3031 * The guidelines on delayed acknowledgement algorithm
3032 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3033 * Specifically, an acknowledgement SHOULD be generated for at
3034 * least every second packet (not every second DATA chunk)
3035 * received, and SHOULD be generated within 200 ms of the
3036 * arrival of any unacknowledged DATA chunk. In some
3037 * situations it may be beneficial for an SCTP transmitter to
3038 * be more conservative than the algorithms detailed in this
3039 * document allow. However, an SCTP transmitter MUST NOT be
3040 * more aggressive than the following algorithms allow.
3041 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003042 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003043 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return SCTP_DISPOSITION_CONSUME;
3046
3047discard_force:
3048 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3049 *
3050 * When a packet arrives with duplicate DATA chunk(s) and with
3051 * no new DATA chunk(s), the endpoint MUST immediately send a
3052 * SACK with no delay. If a packet arrives with duplicate
3053 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3054 * MAY immediately send a SACK. Normally receipt of duplicate
3055 * DATA chunks will occur when the original SACK chunk was lost
3056 * and the peer's RTO has expired. The duplicate TSN number(s)
3057 * SHOULD be reported in the SACK as duplicate.
3058 */
3059 /* In our case, we split the MAY SACK advice up whether or not
3060 * the last chunk is a duplicate.'
3061 */
3062 if (chunk->end_of_packet)
3063 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3064 return SCTP_DISPOSITION_DISCARD;
3065
3066discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003067 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003068 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073/*
3074 * sctp_sf_eat_data_fast_4_4
3075 *
3076 * Section: 4 (4)
3077 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3078 * DATA chunks without delay.
3079 *
3080 * Verification Tag: 8.5 Verification Tag [Normal verification]
3081 * Inputs
3082 * (endpoint, asoc, chunk)
3083 *
3084 * Outputs
3085 * (asoc, reply_msg, msg_up, timers, counters)
3086 *
3087 * The return value is the disposition of the chunk.
3088 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003089sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3090 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 const struct sctp_association *asoc,
3092 const sctp_subtype_t type,
3093 void *arg,
3094 sctp_cmd_seq_t *commands)
3095{
3096 struct sctp_chunk *chunk = arg;
3097 int error;
3098
3099 if (!sctp_vtag_verify(chunk, asoc)) {
3100 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3101 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003102 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
3104
3105 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003106 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 commands);
3108
wangweidongcb3f8372013-12-23 12:16:50 +08003109 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 switch (error) {
3111 case SCTP_IERROR_NO_ERROR:
3112 case SCTP_IERROR_HIGH_TSN:
3113 case SCTP_IERROR_DUP_TSN:
3114 case SCTP_IERROR_IGNORE_TSN:
3115 case SCTP_IERROR_BAD_STREAM:
3116 break;
3117 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003118 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003119 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003120 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003121 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 default:
3123 BUG();
3124 }
3125
3126 /* Go a head and force a SACK, since we are shutting down. */
3127
3128 /* Implementor's Guide.
3129 *
3130 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3131 * respond to each received packet containing one or more DATA chunk(s)
3132 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3133 */
3134 if (chunk->end_of_packet) {
3135 /* We must delay the chunk creation since the cumulative
3136 * TSN has not been updated yet.
3137 */
3138 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3139 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3140 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3141 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3142 }
3143
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return SCTP_DISPOSITION_CONSUME;
3145}
3146
3147/*
3148 * Section: 6.2 Processing a Received SACK
3149 * D) Any time a SACK arrives, the endpoint performs the following:
3150 *
3151 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3152 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3153 * increasing, a SACK whose Cumulative TSN Ack is less than the
3154 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3155 *
3156 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3157 * of bytes still outstanding after processing the Cumulative TSN Ack
3158 * and the Gap Ack Blocks.
3159 *
3160 * iii) If the SACK is missing a TSN that was previously
3161 * acknowledged via a Gap Ack Block (e.g., the data receiver
3162 * reneged on the data), then mark the corresponding DATA chunk
3163 * as available for retransmit: Mark it as missing for fast
3164 * retransmit as described in Section 7.2.4 and if no retransmit
3165 * timer is running for the destination address to which the DATA
3166 * chunk was originally transmitted, then T3-rtx is started for
3167 * that destination address.
3168 *
3169 * Verification Tag: 8.5 Verification Tag [Normal verification]
3170 *
3171 * Inputs
3172 * (endpoint, asoc, chunk)
3173 *
3174 * Outputs
3175 * (asoc, reply_msg, msg_up, timers, counters)
3176 *
3177 * The return value is the disposition of the chunk.
3178 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003179sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3180 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 const struct sctp_association *asoc,
3182 const sctp_subtype_t type,
3183 void *arg,
3184 sctp_cmd_seq_t *commands)
3185{
3186 struct sctp_chunk *chunk = arg;
3187 sctp_sackhdr_t *sackh;
3188 __u32 ctsn;
3189
3190 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003191 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 /* Make sure that the SACK chunk has a valid length. */
3194 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003195 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 commands);
3197
3198 /* Pull the SACK chunk from the data buffer */
3199 sackh = sctp_sm_pull_sack(chunk);
3200 /* Was this a bogus SACK? */
3201 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003202 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 chunk->subh.sack_hdr = sackh;
3204 ctsn = ntohl(sackh->cum_tsn_ack);
3205
3206 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3207 * Ack Point, then drop the SACK. Since Cumulative TSN
3208 * Ack is monotonically increasing, a SACK whose
3209 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3210 * Point indicates an out-of-order SACK.
3211 */
3212 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003213 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3214 asoc->ctsn_ack_point);
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 return SCTP_DISPOSITION_DISCARD;
3217 }
3218
Wei Yongjunaecedea2007-08-02 16:57:44 +08003219 /* If Cumulative TSN Ack beyond the max tsn currently
3220 * send, terminating the association and respond to the
3221 * sender with an ABORT.
3222 */
3223 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003224 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003227 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 /* Note: We do the rest of the work on the PROCESS_SACK
3230 * sideeffect.
3231 */
3232 return SCTP_DISPOSITION_CONSUME;
3233}
3234
3235/*
3236 * Generate an ABORT in response to a packet.
3237 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003238 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003240 * 8) The receiver should respond to the sender of the OOTB packet with
3241 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3242 * MUST fill in the Verification Tag field of the outbound packet
3243 * with the value found in the Verification Tag field of the OOTB
3244 * packet and set the T-bit in the Chunk Flags to indicate that the
3245 * Verification Tag is reflected. After sending this ABORT, the
3246 * receiver of the OOTB packet shall discard the OOTB packet and take
3247 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 *
3249 * Verification Tag:
3250 *
3251 * The return value is the disposition of the chunk.
3252*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003253static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3254 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 const struct sctp_association *asoc,
3256 const sctp_subtype_t type,
3257 void *arg,
3258 sctp_cmd_seq_t *commands)
3259{
3260 struct sctp_packet *packet = NULL;
3261 struct sctp_chunk *chunk = arg;
3262 struct sctp_chunk *abort;
3263
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003264 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
3266 if (packet) {
3267 /* Make an ABORT. The T bit will be set if the asoc
3268 * is NULL.
3269 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003270 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (!abort) {
3272 sctp_ootb_pkt_free(packet);
3273 return SCTP_DISPOSITION_NOMEM;
3274 }
3275
Jerome Forissier047a2422005-04-28 11:58:43 -07003276 /* Reflect vtag if T-Bit is set */
3277 if (sctp_test_T_bit(abort))
3278 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 /* Set the skb to the belonging sock for accounting. */
3281 abort->skb->sk = ep->base.sk;
3282
3283 sctp_packet_append_chunk(packet, abort);
3284
3285 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3286 SCTP_PACKET(packet));
3287
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003288 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003290 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return SCTP_DISPOSITION_CONSUME;
3292 }
3293
3294 return SCTP_DISPOSITION_NOMEM;
3295}
3296
3297/*
3298 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3299 * event as ULP notification for each cause included in the chunk.
3300 *
3301 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3302 *
3303 * The return value is the disposition of the chunk.
3304*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003305sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3306 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 const struct sctp_association *asoc,
3308 const sctp_subtype_t type,
3309 void *arg,
3310 sctp_cmd_seq_t *commands)
3311{
3312 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003313 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 /* Make sure that the ERROR chunk has a valid length. */
3319 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003320 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003322 sctp_walk_errors(err, chunk->chunk_hdr);
3323 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003324 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003325 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Wei Yongjun3df26782009-03-02 06:46:51 +00003327 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3328 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
3333/*
3334 * Process an inbound SHUTDOWN ACK.
3335 *
3336 * From Section 9.2:
3337 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3338 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3339 * peer, and remove all record of the association.
3340 *
3341 * The return value is the disposition.
3342 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003343sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3344 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 const struct sctp_association *asoc,
3346 const sctp_subtype_t type,
3347 void *arg,
3348 sctp_cmd_seq_t *commands)
3349{
3350 struct sctp_chunk *chunk = arg;
3351 struct sctp_chunk *reply;
3352 struct sctp_ulpevent *ev;
3353
3354 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003355 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3358 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003359 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 /* 10.2 H) SHUTDOWN COMPLETE notification
3362 *
3363 * When SCTP completes the shutdown procedures (section 9.2) this
3364 * notification is passed to the upper layer.
3365 */
3366 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003367 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 if (!ev)
3369 goto nomem;
3370
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003371 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3372 reply = sctp_make_shutdown_complete(asoc, chunk);
3373 if (!reply)
3374 goto nomem_chunk;
3375
3376 /* Do all the commands now (after allocation), so that we
3377 * have consistent state if memory allocation failes
3378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3380
3381 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3382 * stop the T2-shutdown timer,
3383 */
3384 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3385 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3386
3387 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3388 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3391 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003392 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3393 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3395
3396 /* ...and remove all record of the association. */
3397 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3398 return SCTP_DISPOSITION_DELETE_TCB;
3399
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003400nomem_chunk:
3401 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402nomem:
3403 return SCTP_DISPOSITION_NOMEM;
3404}
3405
3406/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003407 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3408 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3410 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3411 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3412 * packet must fill in the Verification Tag field of the outbound
3413 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003414 * set the T-bit in the Chunk Flags to indicate that the Verification
3415 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 *
3417 * 8) The receiver should respond to the sender of the OOTB packet with
3418 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3419 * MUST fill in the Verification Tag field of the outbound packet
3420 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003421 * packet and set the T-bit in the Chunk Flags to indicate that the
3422 * Verification Tag is reflected. After sending this ABORT, the
3423 * receiver of the OOTB packet shall discard the OOTB packet and take
3424 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003426sctp_disposition_t sctp_sf_ootb(struct net *net,
3427 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 const struct sctp_association *asoc,
3429 const sctp_subtype_t type,
3430 void *arg,
3431 sctp_cmd_seq_t *commands)
3432{
3433 struct sctp_chunk *chunk = arg;
3434 struct sk_buff *skb = chunk->skb;
3435 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003436 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 __u8 *ch_end;
3438 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003439 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003441 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3444 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003445 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003447 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003448 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003450 /* Report violation if chunk len overflows */
3451 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3452 if (ch_end > skb_tail_pointer(skb))
3453 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3454 commands);
3455
Vlad Yasevichece25df2007-09-07 16:30:54 -04003456 /* Now that we know we at least have a chunk header,
3457 * do things that are type appropriate.
3458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3460 ootb_shut_ack = 1;
3461
3462 /* RFC 2960, Section 3.3.7
3463 * Moreover, under any circumstances, an endpoint that
3464 * receives an ABORT MUST NOT respond to that ABORT by
3465 * sending an ABORT of its own.
3466 */
3467 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003468 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003469
Shan Wei85c5ed42011-04-19 21:30:01 +00003470 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3471 * or a COOKIE ACK the SCTP Packet should be silently
3472 * discarded.
3473 */
3474
3475 if (SCTP_CID_COOKIE_ACK == ch->type)
3476 ootb_cookie_ack = 1;
3477
3478 if (SCTP_CID_ERROR == ch->type) {
3479 sctp_walk_errors(err, ch) {
3480 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3481 ootb_cookie_ack = 1;
3482 break;
3483 }
3484 }
3485 }
3486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003488 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003491 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003492 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003493 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003495 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496}
3497
3498/*
3499 * Handle an "Out of the blue" SHUTDOWN ACK.
3500 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003501 * Section: 8.4 5, sctpimpguide 2.41.
3502 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003504 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3505 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3506 * packet must fill in the Verification Tag field of the outbound
3507 * packet with the Verification Tag received in the SHUTDOWN ACK and
3508 * set the T-bit in the Chunk Flags to indicate that the Verification
3509 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 *
3511 * Inputs
3512 * (endpoint, asoc, type, arg, commands)
3513 *
3514 * Outputs
3515 * (sctp_disposition_t)
3516 *
3517 * The return value is the disposition of the chunk.
3518 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003519static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3520 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 const struct sctp_association *asoc,
3522 const sctp_subtype_t type,
3523 void *arg,
3524 sctp_cmd_seq_t *commands)
3525{
3526 struct sctp_packet *packet = NULL;
3527 struct sctp_chunk *chunk = arg;
3528 struct sctp_chunk *shut;
3529
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003530 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 if (packet) {
3533 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003534 * The T bit will be set if the asoc is NULL.
3535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 shut = sctp_make_shutdown_complete(asoc, chunk);
3537 if (!shut) {
3538 sctp_ootb_pkt_free(packet);
3539 return SCTP_DISPOSITION_NOMEM;
3540 }
3541
Jerome Forissier047a2422005-04-28 11:58:43 -07003542 /* Reflect vtag if T-Bit is set */
3543 if (sctp_test_T_bit(shut))
3544 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 /* Set the skb to the belonging sock for accounting. */
3547 shut->skb->sk = ep->base.sk;
3548
3549 sctp_packet_append_chunk(packet, shut);
3550
3551 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3552 SCTP_PACKET(packet));
3553
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003554 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555
3556 /* If the chunk length is invalid, we don't want to process
3557 * the reset of the packet.
3558 */
3559 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003560 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003562 /* We need to discard the rest of the packet to prevent
3563 * potential bomming attacks from additional bundled chunks.
3564 * This is documented in SCTP Threats ID.
3565 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003566 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 }
3568
3569 return SCTP_DISPOSITION_NOMEM;
3570}
3571
3572/*
3573 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3574 *
3575 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3576 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3577 * procedures in section 8.4 SHOULD be followed, in other words it
3578 * should be treated as an Out Of The Blue packet.
3579 * [This means that we do NOT check the Verification Tag on these
3580 * chunks. --piggy ]
3581 *
3582 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003583sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3584 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 const struct sctp_association *asoc,
3586 const sctp_subtype_t type,
3587 void *arg,
3588 sctp_cmd_seq_t *commands)
3589{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003590 struct sctp_chunk *chunk = arg;
3591
3592 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3593 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003594 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003595 commands);
3596
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 /* Although we do have an association in this case, it corresponds
3598 * to a restarted association. So the packet is treated as an OOTB
3599 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3600 * called with a NULL association.
3601 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003602 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003603
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003604 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605}
3606
3607/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003608sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3609 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 const struct sctp_association *asoc,
3611 const sctp_subtype_t type, void *arg,
3612 sctp_cmd_seq_t *commands)
3613{
3614 struct sctp_chunk *chunk = arg;
3615 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003616 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 sctp_addiphdr_t *hdr;
3618 __u32 serial;
3619
3620 if (!sctp_vtag_verify(chunk, asoc)) {
3621 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3622 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003623 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003626 /* ADD-IP: Section 4.1.1
3627 * This chunk MUST be sent in an authenticated way by using
3628 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3629 * is received unauthenticated it MUST be silently discarded as
3630 * described in [I-D.ietf-tsvwg-sctp-auth].
3631 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003632 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003633 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3636 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003637 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 commands);
3639
3640 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3641 serial = ntohl(hdr->serial);
3642
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003643 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003644 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003645 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003646 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003647
Vlad Yasevicha08de642007-12-20 14:11:47 -08003648 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003650 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 */
3652 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003653 /* If this is the first instance of ASCONF in the packet,
3654 * we can clean our old ASCONF-ACKs.
3655 */
3656 if (!chunk->has_asconf)
3657 sctp_assoc_clean_asconf_ack_cache(asoc);
3658
3659 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3660 * expected, process the ASCONF as described below and after
3661 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3662 * the response packet and cache a copy of it (in the event it
3663 * later needs to be retransmitted).
3664 *
3665 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 */
3667 asconf_ack = sctp_process_asconf((struct sctp_association *)
3668 asoc, chunk);
3669 if (!asconf_ack)
3670 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003671 } else if (serial < asoc->peer.addip_serial + 1) {
3672 /* ADDIP 5.2 E2)
3673 * If the value found in the Sequence Number is less than the
3674 * ('Peer- Sequence-Number' + 1), simply skip to the next
3675 * ASCONF, and include in the outbound response packet
3676 * any previously cached ASCONF-ACK response that was
3677 * sent and saved that matches the Sequence Number of the
3678 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3679 * Chunk exists. This will occur when an older ASCONF
3680 * arrives out of order. In such a case, the receiver
3681 * should skip the ASCONF Chunk and not include ASCONF-ACK
3682 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003684 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3685 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003687
3688 /* Reset the transport so that we select the correct one
3689 * this time around. This is to make sure that we don't
3690 * accidentally use a stale transport that's been removed.
3691 */
3692 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003694 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003696 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return SCTP_DISPOSITION_DISCARD;
3698 }
3699
Vlad Yasevicha08de642007-12-20 14:11:47 -08003700 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3701 * containing the ASCONF-ACK Chunks MUST be the source address of
3702 * the SCTP packet that held the ASCONF Chunks.
3703 *
3704 * To do this properly, we'll set the destination address of the chunk
3705 * and at the transmit time, will try look up the transport to use.
3706 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003707 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003709 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003711 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003712 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003713 ((struct sctp_association *)asoc)->new_transport = NULL;
3714 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 return SCTP_DISPOSITION_CONSUME;
3717}
3718
3719/*
3720 * ADDIP Section 4.3 General rules for address manipulation
3721 * When building TLV parameters for the ASCONF Chunk that will add or
3722 * delete IP addresses the D0 to D13 rules should be applied:
3723 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003724sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3725 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003727 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 sctp_cmd_seq_t *commands)
3729{
3730 struct sctp_chunk *asconf_ack = arg;
3731 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3732 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003733 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 sctp_addiphdr_t *addip_hdr;
3735 __u32 sent_serial, rcvd_serial;
3736
3737 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3738 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3739 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003740 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 }
3742
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003743 /* ADD-IP, Section 4.1.2:
3744 * This chunk MUST be sent in an authenticated way by using
3745 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3746 * is received unauthenticated it MUST be silently discarded as
3747 * described in [I-D.ietf-tsvwg-sctp-auth].
3748 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003749 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003750 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 /* Make sure that the ADDIP chunk has a valid length. */
3753 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003754 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 commands);
3756
3757 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3758 rcvd_serial = ntohl(addip_hdr->serial);
3759
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003760 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003761 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003762 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003763 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003764
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (last_asconf) {
3766 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3767 sent_serial = ntohl(addip_hdr->serial);
3768 } else {
3769 sent_serial = asoc->addip_serial - 1;
3770 }
3771
3772 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3773 * equal to the next serial number to be used but no ASCONF chunk is
3774 * outstanding the endpoint MUST ABORT the association. Note that a
3775 * sequence number is greater than if it is no more than 2^^31-1
3776 * larger than the current sequence number (using serial arithmetic).
3777 */
3778 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3779 !(asoc->addip_last_asconf)) {
3780 abort = sctp_make_abort(asoc, asconf_ack,
3781 sizeof(sctp_errhdr_t));
3782 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003783 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3785 SCTP_CHUNK(abort));
3786 }
3787 /* We are going to ABORT, so we might as well stop
3788 * processing the rest of the chunks in the packet.
3789 */
3790 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3791 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003792 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003793 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003794 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003796 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003797 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3798 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return SCTP_DISPOSITION_ABORT;
3800 }
3801
3802 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3803 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3804 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3805
3806 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003807 asconf_ack)) {
3808 /* Successfully processed ASCONF_ACK. We can
3809 * release the next asconf if we have one.
3810 */
3811 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3812 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816 abort = sctp_make_abort(asoc, asconf_ack,
3817 sizeof(sctp_errhdr_t));
3818 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003819 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3821 SCTP_CHUNK(abort));
3822 }
3823 /* We are going to ABORT, so we might as well stop
3824 * processing the rest of the chunks in the packet.
3825 */
wangweidongcb3f8372013-12-23 12:16:50 +08003826 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003827 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003828 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003830 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003831 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3832 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return SCTP_DISPOSITION_ABORT;
3834 }
3835
3836 return SCTP_DISPOSITION_DISCARD;
3837}
3838
3839/*
3840 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3841 *
3842 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3843 * its cumulative TSN point to the value carried in the FORWARD TSN
3844 * chunk, and then MUST further advance its cumulative TSN point locally
3845 * if possible.
3846 * After the above processing, the data receiver MUST stop reporting any
3847 * missing TSNs earlier than or equal to the new cumulative TSN point.
3848 *
3849 * Verification Tag: 8.5 Verification Tag [Normal verification]
3850 *
3851 * The return value is the disposition of the chunk.
3852 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003853sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3854 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 const struct sctp_association *asoc,
3856 const sctp_subtype_t type,
3857 void *arg,
3858 sctp_cmd_seq_t *commands)
3859{
3860 struct sctp_chunk *chunk = arg;
3861 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003862 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 __u16 len;
3864 __u32 tsn;
3865
3866 if (!sctp_vtag_verify(chunk, asoc)) {
3867 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3868 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003869 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
3871
3872 /* Make sure that the FORWARD_TSN chunk has valid length. */
3873 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003874 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 commands);
3876
3877 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3878 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3879 len = ntohs(chunk->chunk_hdr->length);
3880 len -= sizeof(struct sctp_chunkhdr);
3881 skb_pull(chunk->skb, len);
3882
3883 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003884 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
3886 /* The TSN is too high--silently discard the chunk and count on it
3887 * getting retransmitted later.
3888 */
3889 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3890 goto discard_noforce;
3891
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003892 /* Silently discard the chunk if stream-id is not valid */
3893 sctp_walk_fwdtsn(skip, chunk) {
3894 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3895 goto discard_noforce;
3896 }
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3899 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003900 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003904 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3906 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3907 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003910 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 */
3912 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
3914 return SCTP_DISPOSITION_CONSUME;
3915
3916discard_noforce:
3917 return SCTP_DISPOSITION_DISCARD;
3918}
3919
3920sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003921 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 const struct sctp_endpoint *ep,
3923 const struct sctp_association *asoc,
3924 const sctp_subtype_t type,
3925 void *arg,
3926 sctp_cmd_seq_t *commands)
3927{
3928 struct sctp_chunk *chunk = arg;
3929 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003930 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 __u16 len;
3932 __u32 tsn;
3933
3934 if (!sctp_vtag_verify(chunk, asoc)) {
3935 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3936 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003937 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
3939
3940 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3941 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003942 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 commands);
3944
3945 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3946 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3947 len = ntohs(chunk->chunk_hdr->length);
3948 len -= sizeof(struct sctp_chunkhdr);
3949 skb_pull(chunk->skb, len);
3950
3951 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003952 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
3954 /* The TSN is too high--silently discard the chunk and count on it
3955 * getting retransmitted later.
3956 */
3957 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3958 goto gen_shutdown;
3959
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003960 /* Silently discard the chunk if stream-id is not valid */
3961 sctp_walk_fwdtsn(skip, chunk) {
3962 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3963 goto gen_shutdown;
3964 }
3965
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3967 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003968 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 /* Go a head and force a SACK, since we are shutting down. */
3972gen_shutdown:
3973 /* Implementor's Guide.
3974 *
3975 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3976 * respond to each received packet containing one or more DATA chunk(s)
3977 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3978 */
3979 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3980 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3981 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3982 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3983
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003984 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985}
3986
3987/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003988 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003989 *
3990 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3991 * Identifier field. If this algorithm was not specified by the
3992 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3993 * during association setup, the AUTH chunk and all chunks after it MUST
3994 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3995 * defined in Section 4.1.
3996 *
3997 * If an endpoint with no shared key receives a Shared Key Identifier
3998 * other than 0, it MUST silently discard all authenticated chunks. If
3999 * the endpoint has at least one endpoint pair shared key for the peer,
4000 * it MUST use the key specified by the Shared Key Identifier if a
4001 * key has been configured for that Shared Key Identifier. If no
4002 * endpoint pair shared key has been configured for that Shared Key
4003 * Identifier, all authenticated chunks MUST be silently discarded.
4004 *
4005 * Verification Tag: 8.5 Verification Tag [Normal verification]
4006 *
4007 * The return value is the disposition of the chunk.
4008 */
Xin Long0e67ad52018-05-05 14:59:47 +08004009static sctp_ierror_t sctp_sf_authenticate(
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004010 const struct sctp_association *asoc,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004011 struct sctp_chunk *chunk)
4012{
4013 struct sctp_authhdr *auth_hdr;
4014 struct sctp_hmac *hmac;
4015 unsigned int sig_len;
4016 __u16 key_id;
4017 __u8 *save_digest;
4018 __u8 *digest;
4019
4020 /* Pull in the auth header, so we can do some more verification */
4021 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4022 chunk->subh.auth_hdr = auth_hdr;
4023 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4024
Masanari Iida02582e92012-08-22 19:11:26 +09004025 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004026 * chunk.
4027 */
4028 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4029 return SCTP_IERROR_AUTH_BAD_HMAC;
4030
4031 /* Make sure that the provided shared key identifier has been
4032 * configured
4033 */
4034 key_id = ntohs(auth_hdr->shkey_id);
4035 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4036 return SCTP_IERROR_AUTH_BAD_KEYID;
4037
4038
4039 /* Make sure that the length of the signature matches what
4040 * we expect.
4041 */
4042 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4043 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4044 if (sig_len != hmac->hmac_len)
4045 return SCTP_IERROR_PROTO_VIOLATION;
4046
4047 /* Now that we've done validation checks, we can compute and
4048 * verify the hmac. The steps involved are:
4049 * 1. Save the digest from the chunk.
4050 * 2. Zero out the digest in the chunk.
4051 * 3. Compute the new digest
4052 * 4. Compare saved and new digests.
4053 */
4054 digest = auth_hdr->hmac;
4055 skb_pull(chunk->skb, sig_len);
4056
4057 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4058 if (!save_digest)
4059 goto nomem;
4060
4061 memset(digest, 0, sig_len);
4062
4063 sctp_auth_calculate_hmac(asoc, chunk->skb,
4064 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4065 GFP_ATOMIC);
4066
4067 /* Discard the packet if the digests do not match */
4068 if (memcmp(save_digest, digest, sig_len)) {
4069 kfree(save_digest);
4070 return SCTP_IERROR_BAD_SIG;
4071 }
4072
4073 kfree(save_digest);
4074 chunk->auth = 1;
4075
4076 return SCTP_IERROR_NO_ERROR;
4077nomem:
4078 return SCTP_IERROR_NOMEM;
4079}
4080
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004081sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4082 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004083 const struct sctp_association *asoc,
4084 const sctp_subtype_t type,
4085 void *arg,
4086 sctp_cmd_seq_t *commands)
4087{
4088 struct sctp_authhdr *auth_hdr;
4089 struct sctp_chunk *chunk = arg;
4090 struct sctp_chunk *err_chunk;
4091 sctp_ierror_t error;
4092
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004093 /* Make sure that the peer has AUTH capable */
4094 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004095 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004096
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004097 if (!sctp_vtag_verify(chunk, asoc)) {
4098 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4099 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004100 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004101 }
4102
4103 /* Make sure that the AUTH chunk has valid length. */
4104 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004105 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004106 commands);
4107
4108 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Xin Long0e67ad52018-05-05 14:59:47 +08004109 error = sctp_sf_authenticate(asoc, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004110 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004111 case SCTP_IERROR_AUTH_BAD_HMAC:
4112 /* Generate the ERROR chunk and discard the rest
4113 * of the packet
4114 */
4115 err_chunk = sctp_make_op_error(asoc, chunk,
4116 SCTP_ERROR_UNSUP_HMAC,
4117 &auth_hdr->hmac_id,
4118 sizeof(__u16), 0);
4119 if (err_chunk) {
4120 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4121 SCTP_CHUNK(err_chunk));
4122 }
4123 /* Fall Through */
4124 case SCTP_IERROR_AUTH_BAD_KEYID:
4125 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004126 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004127
4128 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004129 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004130 commands);
4131
4132 case SCTP_IERROR_NOMEM:
4133 return SCTP_DISPOSITION_NOMEM;
4134
4135 default: /* Prevent gcc warnings */
4136 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004137 }
4138
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004139 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4140 struct sctp_ulpevent *ev;
4141
4142 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4143 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4144
4145 if (!ev)
4146 return -ENOMEM;
4147
4148 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4149 SCTP_ULPEVENT(ev));
4150 }
4151
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004152 return SCTP_DISPOSITION_CONSUME;
4153}
4154
4155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 * Process an unknown chunk.
4157 *
4158 * Section: 3.2. Also, 2.1 in the implementor's guide.
4159 *
4160 * Chunk Types are encoded such that the highest-order two bits specify
4161 * the action that must be taken if the processing endpoint does not
4162 * recognize the Chunk Type.
4163 *
4164 * 00 - Stop processing this SCTP packet and discard it, do not process
4165 * any further chunks within it.
4166 *
4167 * 01 - Stop processing this SCTP packet and discard it, do not process
4168 * any further chunks within it, and report the unrecognized
4169 * chunk in an 'Unrecognized Chunk Type'.
4170 *
4171 * 10 - Skip this chunk and continue processing.
4172 *
4173 * 11 - Skip this chunk and continue processing, but report in an ERROR
4174 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4175 *
4176 * The return value is the disposition of the chunk.
4177 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004178sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4179 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 const struct sctp_association *asoc,
4181 const sctp_subtype_t type,
4182 void *arg,
4183 sctp_cmd_seq_t *commands)
4184{
4185 struct sctp_chunk *unk_chunk = arg;
4186 struct sctp_chunk *err_chunk;
4187 sctp_chunkhdr_t *hdr;
4188
Daniel Borkmannbb333812013-06-28 19:49:40 +02004189 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
4191 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004192 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 /* Make sure that the chunk has a valid length.
4195 * Since we don't know the chunk type, we use a general
4196 * chunkhdr structure to make a comparison.
4197 */
4198 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004199 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 commands);
4201
4202 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4203 case SCTP_CID_ACTION_DISCARD:
4204 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004205 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 /* Generate an ERROR chunk as response. */
4208 hdr = unk_chunk->chunk_hdr;
4209 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4210 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004211 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004212 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 if (err_chunk) {
4214 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4215 SCTP_CHUNK(err_chunk));
4216 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004217
4218 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004219 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 case SCTP_CID_ACTION_SKIP:
4222 /* Skip the chunk. */
4223 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 case SCTP_CID_ACTION_SKIP_ERR:
4225 /* Generate an ERROR chunk as response. */
4226 hdr = unk_chunk->chunk_hdr;
4227 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4228 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004229 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004230 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 if (err_chunk) {
4232 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4233 SCTP_CHUNK(err_chunk));
4234 }
4235 /* Skip the chunk. */
4236 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 default:
4238 break;
4239 }
4240
4241 return SCTP_DISPOSITION_DISCARD;
4242}
4243
4244/*
4245 * Discard the chunk.
4246 *
4247 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4248 * [Too numerous to mention...]
4249 * Verification Tag: No verification needed.
4250 * Inputs
4251 * (endpoint, asoc, chunk)
4252 *
4253 * Outputs
4254 * (asoc, reply_msg, msg_up, timers, counters)
4255 *
4256 * The return value is the disposition of the chunk.
4257 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004258sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4259 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 const struct sctp_association *asoc,
4261 const sctp_subtype_t type,
4262 void *arg,
4263 sctp_cmd_seq_t *commands)
4264{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004265 struct sctp_chunk *chunk = arg;
4266
4267 /* Make sure that the chunk has a valid length.
4268 * Since we don't know the chunk type, we use a general
4269 * chunkhdr structure to make a comparison.
4270 */
4271 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004272 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004273 commands);
4274
Daniel Borkmannbb333812013-06-28 19:49:40 +02004275 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return SCTP_DISPOSITION_DISCARD;
4278}
4279
4280/*
4281 * Discard the whole packet.
4282 *
4283 * Section: 8.4 2)
4284 *
4285 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4286 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 *
4288 * Verification Tag: No verification necessary
4289 *
4290 * Inputs
4291 * (endpoint, asoc, chunk)
4292 *
4293 * Outputs
4294 * (asoc, reply_msg, msg_up, timers, counters)
4295 *
4296 * The return value is the disposition of the chunk.
4297 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004298sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4299 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 const struct sctp_association *asoc,
4301 const sctp_subtype_t type,
4302 void *arg,
4303 sctp_cmd_seq_t *commands)
4304{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004305 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4307
4308 return SCTP_DISPOSITION_CONSUME;
4309}
4310
4311
4312/*
4313 * The other end is violating protocol.
4314 *
4315 * Section: Not specified
4316 * Verification Tag: Not specified
4317 * Inputs
4318 * (endpoint, asoc, chunk)
4319 *
4320 * Outputs
4321 * (asoc, reply_msg, msg_up, timers, counters)
4322 *
4323 * We simply tag the chunk as a violation. The state machine will log
4324 * the violation and continue.
4325 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004326sctp_disposition_t sctp_sf_violation(struct net *net,
4327 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 const struct sctp_association *asoc,
4329 const sctp_subtype_t type,
4330 void *arg,
4331 sctp_cmd_seq_t *commands)
4332{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004333 struct sctp_chunk *chunk = arg;
4334
4335 /* Make sure that the chunk has a valid length. */
4336 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004337 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004338 commands);
4339
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return SCTP_DISPOSITION_VIOLATION;
4341}
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004344 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004346static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004347 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004348 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004351 sctp_cmd_seq_t *commands,
4352 const __u8 *payload,
4353 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004355 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 struct sctp_chunk *chunk = arg;
4357 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004359 /* SCTP-AUTH, Section 6.3:
4360 * It should be noted that if the receiver wants to tear
4361 * down an association in an authenticated way only, the
4362 * handling of malformed packets should not result in
4363 * tearing down the association.
4364 *
4365 * This means that if we only want to abort associations
4366 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004367 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004368 * was malformed.
4369 */
4370 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4371 goto discard;
4372
Jesper Juhl9abed242007-11-11 23:57:49 +01004373 /* Make the abort chunk. */
4374 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4375 if (!abort)
4376 goto nomem;
4377
Vlad Yasevichece25df2007-09-07 16:30:54 -04004378 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004379 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4380 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4381 !asoc->peer.i.init_tag) {
4382 sctp_initack_chunk_t *initack;
4383
4384 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4385 if (!sctp_chunk_length_valid(chunk,
4386 sizeof(sctp_initack_chunk_t)))
4387 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4388 else {
4389 unsigned int inittag;
4390
4391 inittag = ntohl(initack->init_hdr.init_tag);
4392 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4393 SCTP_U32(inittag));
4394 }
4395 }
4396
Vlad Yasevichece25df2007-09-07 16:30:54 -04004397 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004398 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Vlad Yasevichece25df2007-09-07 16:30:54 -04004400 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4401 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4402 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4403 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4404 SCTP_ERROR(ECONNREFUSED));
4405 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4406 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4407 } else {
4408 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4409 SCTP_ERROR(ECONNABORTED));
4410 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4411 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004412 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004415 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004416
4417 if (!packet)
4418 goto nomem_pkt;
4419
4420 if (sctp_test_T_bit(abort))
4421 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4422
4423 abort->skb->sk = ep->base.sk;
4424
4425 sctp_packet_append_chunk(packet, abort);
4426
4427 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4428 SCTP_PACKET(packet));
4429
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004430 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 }
4432
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004433 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004434
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004435discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004436 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 return SCTP_DISPOSITION_ABORT;
4438
Vlad Yasevichece25df2007-09-07 16:30:54 -04004439nomem_pkt:
4440 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441nomem:
4442 return SCTP_DISPOSITION_NOMEM;
4443}
4444
Wei Yongjunaecedea2007-08-02 16:57:44 +08004445/*
4446 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004447 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004448 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004449 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004450 *
4451 * We inform the other end by sending an ABORT with a Protocol Violation
4452 * error code.
4453 *
4454 * Section: Not specified
4455 * Verification Tag: Nothing to do
4456 * Inputs
4457 * (endpoint, asoc, chunk)
4458 *
4459 * Outputs
4460 * (reply_msg, msg_up, counters)
4461 *
4462 * Generate an ABORT chunk and terminate the association.
4463 */
4464static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004465 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004466 const struct sctp_endpoint *ep,
4467 const struct sctp_association *asoc,
4468 const sctp_subtype_t type,
4469 void *arg,
4470 sctp_cmd_seq_t *commands)
4471{
wangweidongcb3f8372013-12-23 12:16:50 +08004472 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004473
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004474 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004475 sizeof(err_str));
4476}
4477
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004478/*
4479 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004480 * If the length is smaller than the minimum length of a given parameter,
4481 * or accumulated length in multi parameters exceeds the end of the chunk,
4482 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004483 */
4484static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004485 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004486 const struct sctp_endpoint *ep,
4487 const struct sctp_association *asoc,
4488 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004489 void *arg, void *ext,
4490 sctp_cmd_seq_t *commands)
4491{
4492 struct sctp_chunk *chunk = arg;
4493 struct sctp_paramhdr *param = ext;
4494 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004495
Wei Yongjunba016672008-09-30 05:32:24 -07004496 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4497 goto discard;
4498
4499 /* Make the abort chunk. */
4500 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4501 if (!abort)
4502 goto nomem;
4503
4504 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004505 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004506
4507 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4508 SCTP_ERROR(ECONNABORTED));
4509 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4510 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004511 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4512 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004513
4514discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004515 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004516 return SCTP_DISPOSITION_ABORT;
4517nomem:
4518 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004519}
4520
Wei Yongjunaecedea2007-08-02 16:57:44 +08004521/* Handle a protocol violation when the peer trying to advance the
4522 * cumulative tsn ack to a point beyond the max tsn currently sent.
4523 *
4524 * We inform the other end by sending an ABORT with a Protocol Violation
4525 * error code.
4526 */
4527static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004528 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004529 const struct sctp_endpoint *ep,
4530 const struct sctp_association *asoc,
4531 const sctp_subtype_t type,
4532 void *arg,
4533 sctp_cmd_seq_t *commands)
4534{
wangweidongcb3f8372013-12-23 12:16:50 +08004535 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004536
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004537 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004538 sizeof(err_str));
4539}
4540
Vlad Yasevichece25df2007-09-07 16:30:54 -04004541/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004542 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004543 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004544 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004545 * on the path and we may not want to continue this communication.
4546 */
4547static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004548 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004549 const struct sctp_endpoint *ep,
4550 const struct sctp_association *asoc,
4551 const sctp_subtype_t type,
4552 void *arg,
4553 sctp_cmd_seq_t *commands)
4554{
wangweidongcb3f8372013-12-23 12:16:50 +08004555 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004556
4557 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004558 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004559
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004560 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004561 sizeof(err_str));
4562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563/***************************************************************************
4564 * These are the state functions for handling primitive (Section 10) events.
4565 ***************************************************************************/
4566/*
4567 * sctp_sf_do_prm_asoc
4568 *
4569 * Section: 10.1 ULP-to-SCTP
4570 * B) Associate
4571 *
4572 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4573 * outbound stream count)
4574 * -> association id [,destination transport addr list] [,outbound stream
4575 * count]
4576 *
4577 * This primitive allows the upper layer to initiate an association to a
4578 * specific peer endpoint.
4579 *
4580 * The peer endpoint shall be specified by one of the transport addresses
4581 * which defines the endpoint (see Section 1.4). If the local SCTP
4582 * instance has not been initialized, the ASSOCIATE is considered an
4583 * error.
4584 * [This is not relevant for the kernel implementation since we do all
4585 * initialization at boot time. It we hadn't initialized we wouldn't
4586 * get anywhere near this code.]
4587 *
4588 * An association id, which is a local handle to the SCTP association,
4589 * will be returned on successful establishment of the association. If
4590 * SCTP is not able to open an SCTP association with the peer endpoint,
4591 * an error is returned.
4592 * [In the kernel implementation, the struct sctp_association needs to
4593 * be created BEFORE causing this primitive to run.]
4594 *
4595 * Other association parameters may be returned, including the
4596 * complete destination transport addresses of the peer as well as the
4597 * outbound stream count of the local endpoint. One of the transport
4598 * address from the returned destination addresses will be selected by
4599 * the local endpoint as default primary path for sending SCTP packets
4600 * to this peer. The returned "destination transport addr list" can
4601 * be used by the ULP to change the default primary path or to force
4602 * sending a packet to a specific transport address. [All of this
4603 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4604 * function.]
4605 *
4606 * Mandatory attributes:
4607 *
4608 * o local SCTP instance name - obtained from the INITIALIZE operation.
4609 * [This is the argument asoc.]
4610 * o destination transport addr - specified as one of the transport
4611 * addresses of the peer endpoint with which the association is to be
4612 * established.
4613 * [This is asoc->peer.active_path.]
4614 * o outbound stream count - the number of outbound streams the ULP
4615 * would like to open towards this peer endpoint.
4616 * [BUG: This is not currently implemented.]
4617 * Optional attributes:
4618 *
4619 * None.
4620 *
4621 * The return value is a disposition.
4622 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004623sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4624 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 const struct sctp_association *asoc,
4626 const sctp_subtype_t type,
4627 void *arg,
4628 sctp_cmd_seq_t *commands)
4629{
4630 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004631 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
4633 /* The comment below says that we enter COOKIE-WAIT AFTER
4634 * sending the INIT, but that doesn't actually work in our
4635 * implementation...
4636 */
4637 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4638 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4639
4640 /* RFC 2960 5.1 Normal Establishment of an Association
4641 *
4642 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4643 * must provide its Verification Tag (Tag_A) in the Initiate
4644 * Tag field. Tag_A SHOULD be a random number in the range of
4645 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4646 */
4647
4648 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4649 if (!repl)
4650 goto nomem;
4651
Daniel Borkmann405426f2013-06-14 18:24:05 +02004652 /* Choose transport for INIT. */
4653 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4654 SCTP_CHUNK(repl));
4655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 /* Cast away the const modifier, as we want to just
4657 * rerun it through as a sideffect.
4658 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004659 my_asoc = (struct sctp_association *)asoc;
4660 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
4662 /* After sending the INIT, "A" starts the T1-init timer and
4663 * enters the COOKIE-WAIT state.
4664 */
4665 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4666 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4667 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4668 return SCTP_DISPOSITION_CONSUME;
4669
4670nomem:
4671 return SCTP_DISPOSITION_NOMEM;
4672}
4673
4674/*
4675 * Process the SEND primitive.
4676 *
4677 * Section: 10.1 ULP-to-SCTP
4678 * E) Send
4679 *
4680 * Format: SEND(association id, buffer address, byte count [,context]
4681 * [,stream id] [,life time] [,destination transport address]
4682 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4683 * -> result
4684 *
4685 * This is the main method to send user data via SCTP.
4686 *
4687 * Mandatory attributes:
4688 *
4689 * o association id - local handle to the SCTP association
4690 *
4691 * o buffer address - the location where the user message to be
4692 * transmitted is stored;
4693 *
4694 * o byte count - The size of the user data in number of bytes;
4695 *
4696 * Optional attributes:
4697 *
4698 * o context - an optional 32 bit integer that will be carried in the
4699 * sending failure notification to the ULP if the transportation of
4700 * this User Message fails.
4701 *
4702 * o stream id - to indicate which stream to send the data on. If not
4703 * specified, stream 0 will be used.
4704 *
4705 * o life time - specifies the life time of the user data. The user data
4706 * will not be sent by SCTP after the life time expires. This
4707 * parameter can be used to avoid efforts to transmit stale
4708 * user messages. SCTP notifies the ULP if the data cannot be
4709 * initiated to transport (i.e. sent to the destination via SCTP's
4710 * send primitive) within the life time variable. However, the
4711 * user data will be transmitted if SCTP has attempted to transmit a
4712 * chunk before the life time expired.
4713 *
4714 * o destination transport address - specified as one of the destination
4715 * transport addresses of the peer endpoint to which this packet
4716 * should be sent. Whenever possible, SCTP should use this destination
4717 * transport address for sending the packets, instead of the current
4718 * primary path.
4719 *
4720 * o unorder flag - this flag, if present, indicates that the user
4721 * would like the data delivered in an unordered fashion to the peer
4722 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4723 * message).
4724 *
4725 * o no-bundle flag - instructs SCTP not to bundle this user data with
4726 * other outbound DATA chunks. SCTP MAY still bundle even when
4727 * this flag is present, when faced with network congestion.
4728 *
4729 * o payload protocol-id - A 32 bit unsigned integer that is to be
4730 * passed to the peer indicating the type of payload protocol data
4731 * being transmitted. This value is passed as opaque data by SCTP.
4732 *
4733 * The return value is the disposition.
4734 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004735sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4736 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 const struct sctp_association *asoc,
4738 const sctp_subtype_t type,
4739 void *arg,
4740 sctp_cmd_seq_t *commands)
4741{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004742 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004744 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 return SCTP_DISPOSITION_CONSUME;
4746}
4747
4748/*
4749 * Process the SHUTDOWN primitive.
4750 *
4751 * Section: 10.1:
4752 * C) Shutdown
4753 *
4754 * Format: SHUTDOWN(association id)
4755 * -> result
4756 *
4757 * Gracefully closes an association. Any locally queued user data
4758 * will be delivered to the peer. The association will be terminated only
4759 * after the peer acknowledges all the SCTP packets sent. A success code
4760 * will be returned on successful termination of the association. If
4761 * attempting to terminate the association results in a failure, an error
4762 * code shall be returned.
4763 *
4764 * Mandatory attributes:
4765 *
4766 * o association id - local handle to the SCTP association
4767 *
4768 * Optional attributes:
4769 *
4770 * None.
4771 *
4772 * The return value is the disposition.
4773 */
4774sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004775 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 const struct sctp_endpoint *ep,
4777 const struct sctp_association *asoc,
4778 const sctp_subtype_t type,
4779 void *arg,
4780 sctp_cmd_seq_t *commands)
4781{
4782 int disposition;
4783
4784 /* From 9.2 Shutdown of an Association
4785 * Upon receipt of the SHUTDOWN primitive from its upper
4786 * layer, the endpoint enters SHUTDOWN-PENDING state and
4787 * remains there until all outstanding data has been
4788 * acknowledged by its peer. The endpoint accepts no new data
4789 * from its upper layer, but retransmits data to the far end
4790 * if necessary to fill gaps.
4791 */
4792 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4793 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 disposition = SCTP_DISPOSITION_CONSUME;
4796 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004797 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 arg, commands);
4799 }
4800 return disposition;
4801}
4802
4803/*
4804 * Process the ABORT primitive.
4805 *
4806 * Section: 10.1:
4807 * C) Abort
4808 *
4809 * Format: Abort(association id [, cause code])
4810 * -> result
4811 *
4812 * Ungracefully closes an association. Any locally queued user data
4813 * will be discarded and an ABORT chunk is sent to the peer. A success code
4814 * will be returned on successful abortion of the association. If
4815 * attempting to abort the association results in a failure, an error
4816 * code shall be returned.
4817 *
4818 * Mandatory attributes:
4819 *
4820 * o association id - local handle to the SCTP association
4821 *
4822 * Optional attributes:
4823 *
4824 * o cause code - reason of the abort to be passed to the peer
4825 *
4826 * None.
4827 *
4828 * The return value is the disposition.
4829 */
4830sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004831 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 const struct sctp_endpoint *ep,
4833 const struct sctp_association *asoc,
4834 const sctp_subtype_t type,
4835 void *arg,
4836 sctp_cmd_seq_t *commands)
4837{
4838 /* From 9.1 Abort of an Association
4839 * Upon receipt of the ABORT primitive from its upper
4840 * layer, the endpoint enters CLOSED state and
4841 * discard all outstanding data has been
4842 * acknowledged by its peer. The endpoint accepts no new data
4843 * from its upper layer, but retransmits data to the far end
4844 * if necessary to fill gaps.
4845 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004846 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Xin Long068d8bd2015-12-29 17:49:25 +08004848 if (abort)
4849 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
4851 /* Even if we can't send the ABORT due to low memory delete the
4852 * TCB. This is a departure from our typical NOMEM handling.
4853 */
4854
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004855 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4856 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 /* Delete the established association. */
4858 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004859 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004861 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4862 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004864 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
4867/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004868sctp_disposition_t sctp_sf_error_closed(struct net *net,
4869 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 const struct sctp_association *asoc,
4871 const sctp_subtype_t type,
4872 void *arg,
4873 sctp_cmd_seq_t *commands)
4874{
4875 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4876 return SCTP_DISPOSITION_CONSUME;
4877}
4878
4879/* We tried an illegal operation on an association which is shutting
4880 * down.
4881 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004882sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4883 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 const struct sctp_association *asoc,
4885 const sctp_subtype_t type,
4886 void *arg,
4887 sctp_cmd_seq_t *commands)
4888{
4889 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4890 SCTP_ERROR(-ESHUTDOWN));
4891 return SCTP_DISPOSITION_CONSUME;
4892}
4893
4894/*
4895 * sctp_cookie_wait_prm_shutdown
4896 *
4897 * Section: 4 Note: 2
4898 * Verification Tag:
4899 * Inputs
4900 * (endpoint, asoc)
4901 *
4902 * The RFC does not explicitly address this issue, but is the route through the
4903 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4904 *
4905 * Outputs
4906 * (timers)
4907 */
4908sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004909 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 const struct sctp_endpoint *ep,
4911 const struct sctp_association *asoc,
4912 const sctp_subtype_t type,
4913 void *arg,
4914 sctp_cmd_seq_t *commands)
4915{
4916 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4917 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4918
4919 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4920 SCTP_STATE(SCTP_STATE_CLOSED));
4921
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004922 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4925
4926 return SCTP_DISPOSITION_DELETE_TCB;
4927}
4928
4929/*
4930 * sctp_cookie_echoed_prm_shutdown
4931 *
4932 * Section: 4 Note: 2
4933 * Verification Tag:
4934 * Inputs
4935 * (endpoint, asoc)
4936 *
4937 * The RFC does not explcitly address this issue, but is the route through the
4938 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4939 *
4940 * Outputs
4941 * (timers)
4942 */
4943sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004944 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 const struct sctp_endpoint *ep,
4946 const struct sctp_association *asoc,
4947 const sctp_subtype_t type,
4948 void *arg, sctp_cmd_seq_t *commands)
4949{
4950 /* There is a single T1 timer, so we should be able to use
4951 * common function with the COOKIE-WAIT state.
4952 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004953 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954}
4955
4956/*
4957 * sctp_sf_cookie_wait_prm_abort
4958 *
4959 * Section: 4 Note: 2
4960 * Verification Tag:
4961 * Inputs
4962 * (endpoint, asoc)
4963 *
4964 * The RFC does not explicitly address this issue, but is the route through the
4965 * state table when someone issues an abort while in COOKIE_WAIT state.
4966 *
4967 * Outputs
4968 * (timers)
4969 */
4970sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004971 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972 const struct sctp_endpoint *ep,
4973 const struct sctp_association *asoc,
4974 const sctp_subtype_t type,
4975 void *arg,
4976 sctp_cmd_seq_t *commands)
4977{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004978 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 /* Stop T1-init timer */
4981 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4982 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983
Xin Long068d8bd2015-12-29 17:49:25 +08004984 if (abort)
4985 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
4987 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4988 SCTP_STATE(SCTP_STATE_CLOSED));
4989
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004990 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 /* Even if we can't send the ABORT due to low memory delete the
4993 * TCB. This is a departure from our typical NOMEM handling.
4994 */
4995
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004996 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4997 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 /* Delete the established association. */
4999 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005000 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02005002 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003}
5004
5005/*
5006 * sctp_sf_cookie_echoed_prm_abort
5007 *
5008 * Section: 4 Note: 3
5009 * Verification Tag:
5010 * Inputs
5011 * (endpoint, asoc)
5012 *
5013 * The RFC does not explcitly address this issue, but is the route through the
5014 * state table when someone issues an abort while in COOKIE_ECHOED state.
5015 *
5016 * Outputs
5017 * (timers)
5018 */
5019sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005020 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 const struct sctp_endpoint *ep,
5022 const struct sctp_association *asoc,
5023 const sctp_subtype_t type,
5024 void *arg,
5025 sctp_cmd_seq_t *commands)
5026{
5027 /* There is a single T1 timer, so we should be able to use
5028 * common function with the COOKIE-WAIT state.
5029 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005030 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031}
5032
5033/*
5034 * sctp_sf_shutdown_pending_prm_abort
5035 *
5036 * Inputs
5037 * (endpoint, asoc)
5038 *
5039 * The RFC does not explicitly address this issue, but is the route through the
5040 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5041 *
5042 * Outputs
5043 * (timers)
5044 */
5045sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005046 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 const struct sctp_endpoint *ep,
5048 const struct sctp_association *asoc,
5049 const sctp_subtype_t type,
5050 void *arg,
5051 sctp_cmd_seq_t *commands)
5052{
5053 /* Stop the T5-shutdown guard timer. */
5054 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5055 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5056
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005057 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058}
5059
5060/*
5061 * sctp_sf_shutdown_sent_prm_abort
5062 *
5063 * Inputs
5064 * (endpoint, asoc)
5065 *
5066 * The RFC does not explicitly address this issue, but is the route through the
5067 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5068 *
5069 * Outputs
5070 * (timers)
5071 */
5072sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005073 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 const struct sctp_endpoint *ep,
5075 const struct sctp_association *asoc,
5076 const sctp_subtype_t type,
5077 void *arg,
5078 sctp_cmd_seq_t *commands)
5079{
5080 /* Stop the T2-shutdown timer. */
5081 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5082 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5083
5084 /* Stop the T5-shutdown guard timer. */
5085 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5086 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5087
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005088 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
5090
5091/*
5092 * sctp_sf_cookie_echoed_prm_abort
5093 *
5094 * Inputs
5095 * (endpoint, asoc)
5096 *
5097 * The RFC does not explcitly address this issue, but is the route through the
5098 * state table when someone issues an abort while in COOKIE_ECHOED state.
5099 *
5100 * Outputs
5101 * (timers)
5102 */
5103sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005104 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 const struct sctp_endpoint *ep,
5106 const struct sctp_association *asoc,
5107 const sctp_subtype_t type,
5108 void *arg,
5109 sctp_cmd_seq_t *commands)
5110{
5111 /* The same T2 timer, so we should be able to use
5112 * common function with the SHUTDOWN-SENT state.
5113 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005114 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115}
5116
5117/*
5118 * Process the REQUESTHEARTBEAT primitive
5119 *
5120 * 10.1 ULP-to-SCTP
5121 * J) Request Heartbeat
5122 *
5123 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5124 *
5125 * -> result
5126 *
5127 * Instructs the local endpoint to perform a HeartBeat on the specified
5128 * destination transport address of the given association. The returned
5129 * result should indicate whether the transmission of the HEARTBEAT
5130 * chunk to the destination address is successful.
5131 *
5132 * Mandatory attributes:
5133 *
5134 * o association id - local handle to the SCTP association
5135 *
5136 * o destination transport address - the transport address of the
5137 * association on which a heartbeat should be issued.
5138 */
5139sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005140 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 const struct sctp_endpoint *ep,
5142 const struct sctp_association *asoc,
5143 const sctp_subtype_t type,
5144 void *arg,
5145 sctp_cmd_seq_t *commands)
5146{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005147 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5148 (struct sctp_transport *)arg, commands))
5149 return SCTP_DISPOSITION_NOMEM;
5150
5151 /*
5152 * RFC 2960 (bis), section 8.3
5153 *
5154 * D) Request an on-demand HEARTBEAT on a specific destination
5155 * transport address of a given association.
5156 *
5157 * The endpoint should increment the respective error counter of
5158 * the destination transport address each time a HEARTBEAT is sent
5159 * to that address and not acknowledged within one RTO.
5160 *
5161 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005162 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005163 SCTP_TRANSPORT(arg));
5164 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165}
5166
5167/*
5168 * ADDIP Section 4.1 ASCONF Chunk Procedures
5169 * When an endpoint has an ASCONF signaled change to be sent to the
5170 * remote endpoint it should do A1 to A9
5171 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005172sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5173 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 const struct sctp_association *asoc,
5175 const sctp_subtype_t type,
5176 void *arg,
5177 sctp_cmd_seq_t *commands)
5178{
5179 struct sctp_chunk *chunk = arg;
5180
5181 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5182 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5183 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5184 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5185 return SCTP_DISPOSITION_CONSUME;
5186}
5187
5188/*
5189 * Ignore the primitive event
5190 *
5191 * The return value is the disposition of the primitive.
5192 */
5193sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005194 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 const struct sctp_endpoint *ep,
5196 const struct sctp_association *asoc,
5197 const sctp_subtype_t type,
5198 void *arg,
5199 sctp_cmd_seq_t *commands)
5200{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005201 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5202 type.primitive);
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 return SCTP_DISPOSITION_DISCARD;
5205}
5206
5207/***************************************************************************
5208 * These are the state functions for the OTHER events.
5209 ***************************************************************************/
5210
5211/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005212 * When the SCTP stack has no more user data to send or retransmit, this
5213 * notification is given to the user. Also, at the time when a user app
5214 * subscribes to this event, if there is no data to be sent or
5215 * retransmit, the stack will immediately send up this notification.
5216 */
5217sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005218 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005219 const struct sctp_endpoint *ep,
5220 const struct sctp_association *asoc,
5221 const sctp_subtype_t type,
5222 void *arg,
5223 sctp_cmd_seq_t *commands)
5224{
5225 struct sctp_ulpevent *event;
5226
5227 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5228 if (!event)
5229 return SCTP_DISPOSITION_NOMEM;
5230
5231 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5232
5233 return SCTP_DISPOSITION_CONSUME;
5234}
5235
5236/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 * Start the shutdown negotiation.
5238 *
5239 * From Section 9.2:
5240 * Once all its outstanding data has been acknowledged, the endpoint
5241 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5242 * TSN Ack field the last sequential TSN it has received from the peer.
5243 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5244 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5245 * with the updated last sequential TSN received from its peer.
5246 *
5247 * The return value is the disposition.
5248 */
5249sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005250 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 const struct sctp_endpoint *ep,
5252 const struct sctp_association *asoc,
5253 const sctp_subtype_t type,
5254 void *arg,
5255 sctp_cmd_seq_t *commands)
5256{
5257 struct sctp_chunk *reply;
5258
5259 /* Once all its outstanding data has been acknowledged, the
5260 * endpoint shall send a SHUTDOWN chunk to its peer including
5261 * in the Cumulative TSN Ack field the last sequential TSN it
5262 * has received from the peer.
5263 */
5264 reply = sctp_make_shutdown(asoc, NULL);
5265 if (!reply)
5266 goto nomem;
5267
5268 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5269 * T2-shutdown timer.
5270 */
5271 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5272
5273 /* It shall then start the T2-shutdown timer */
5274 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5275 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5276
Wei Yongjun536428a2008-09-05 08:55:26 +08005277 /* RFC 4960 Section 9.2
5278 * The sender of the SHUTDOWN MAY also start an overall guard timer
5279 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5280 */
Thomas Graff8d96052011-07-07 00:28:35 +00005281 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005282 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5283
Neil Horman9f70f462013-12-10 06:48:15 -05005284 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5286 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5287
5288 /* and enter the SHUTDOWN-SENT state. */
5289 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5290 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5291
5292 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5293 *
5294 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005295 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 */
5297 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5298
5299 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5300
5301 return SCTP_DISPOSITION_CONSUME;
5302
5303nomem:
5304 return SCTP_DISPOSITION_NOMEM;
5305}
5306
5307/*
5308 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5309 *
5310 * From Section 9.2:
5311 *
5312 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5313 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5314 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5315 * endpoint must re-send the SHUTDOWN ACK.
5316 *
5317 * The return value is the disposition.
5318 */
5319sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005320 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 const struct sctp_endpoint *ep,
5322 const struct sctp_association *asoc,
5323 const sctp_subtype_t type,
5324 void *arg,
5325 sctp_cmd_seq_t *commands)
5326{
5327 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5328 struct sctp_chunk *reply;
5329
5330 /* There are 2 ways of getting here:
5331 * 1) called in response to a SHUTDOWN chunk
5332 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5333 *
5334 * For the case (2), the arg parameter is set to NULL. We need
5335 * to check that we have a chunk before accessing it's fields.
5336 */
5337 if (chunk) {
5338 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005339 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
5341 /* Make sure that the SHUTDOWN chunk has a valid length. */
5342 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005343 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 commands);
5345 }
5346
5347 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5348 * shall send a SHUTDOWN ACK ...
5349 */
5350 reply = sctp_make_shutdown_ack(asoc, chunk);
5351 if (!reply)
5352 goto nomem;
5353
5354 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5355 * the T2-shutdown timer.
5356 */
5357 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5358
5359 /* and start/restart a T2-shutdown timer of its own, */
5360 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5361 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5362
Neil Horman9f70f462013-12-10 06:48:15 -05005363 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5365 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5366
5367 /* Enter the SHUTDOWN-ACK-SENT state. */
5368 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5369 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5370
5371 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5372 *
5373 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005374 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 */
5376 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5377
5378 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5379
5380 return SCTP_DISPOSITION_CONSUME;
5381
5382nomem:
5383 return SCTP_DISPOSITION_NOMEM;
5384}
5385
5386/*
5387 * Ignore the event defined as other
5388 *
5389 * The return value is the disposition of the event.
5390 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005391sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5392 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 const struct sctp_association *asoc,
5394 const sctp_subtype_t type,
5395 void *arg,
5396 sctp_cmd_seq_t *commands)
5397{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005398 pr_debug("%s: the event other type:%d is ignored\n",
5399 __func__, type.other);
5400
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 return SCTP_DISPOSITION_DISCARD;
5402}
5403
5404/************************************************************
5405 * These are the state functions for handling timeout events.
5406 ************************************************************/
5407
5408/*
5409 * RTX Timeout
5410 *
5411 * Section: 6.3.3 Handle T3-rtx Expiration
5412 *
5413 * Whenever the retransmission timer T3-rtx expires for a destination
5414 * address, do the following:
5415 * [See below]
5416 *
5417 * The return value is the disposition of the chunk.
5418 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005419sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5420 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 const struct sctp_association *asoc,
5422 const sctp_subtype_t type,
5423 void *arg,
5424 sctp_cmd_seq_t *commands)
5425{
5426 struct sctp_transport *transport = arg;
5427
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005428 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005431 if (asoc->peer.zero_window_announced &&
5432 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005433 /*
5434 * We are here likely because the receiver had its rwnd
5435 * closed for a while and we have not been able to
5436 * transmit the locally queued data within the maximum
5437 * retransmission attempts limit. Start the T5
5438 * shutdown guard timer to give the receiver one last
5439 * chance and some additional time to recover before
5440 * aborting.
5441 */
5442 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5443 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5444 } else {
5445 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5446 SCTP_ERROR(ETIMEDOUT));
5447 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5448 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5449 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005450 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5451 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005452 return SCTP_DISPOSITION_DELETE_TCB;
5453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 }
5455
5456 /* E1) For the destination address for which the timer
5457 * expires, adjust its ssthresh with rules defined in Section
5458 * 7.2.3 and set the cwnd <- MTU.
5459 */
5460
5461 /* E2) For the destination address for which the timer
5462 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5463 * maximum value discussed in rule C7 above (RTO.max) may be
5464 * used to provide an upper bound to this doubling operation.
5465 */
5466
5467 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5468 * outstanding DATA chunks for the address for which the
5469 * T3-rtx has expired will fit into a single packet, subject
5470 * to the MTU constraint for the path corresponding to the
5471 * destination transport address to which the retransmission
5472 * is being sent (this may be different from the address for
5473 * which the timer expires [see Section 6.4]). Call this
5474 * value K. Bundle and retransmit those K DATA chunks in a
5475 * single packet to the destination endpoint.
5476 *
5477 * Note: Any DATA chunks that were sent to the address for
5478 * which the T3-rtx timer expired but did not fit in one MTU
5479 * (rule E3 above), should be marked for retransmission and
5480 * sent as soon as cwnd allows (normally when a SACK arrives).
5481 */
5482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 /* Do some failure management (Section 8.2). */
5484 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5485
Vlad Yasevich1845a572007-02-21 02:06:19 -08005486 /* NB: Rules E4 and F1 are implicit in R1. */
5487 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 return SCTP_DISPOSITION_CONSUME;
5490}
5491
5492/*
5493 * Generate delayed SACK on timeout
5494 *
5495 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5496 *
5497 * The guidelines on delayed acknowledgement algorithm specified in
5498 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5499 * acknowledgement SHOULD be generated for at least every second packet
5500 * (not every second DATA chunk) received, and SHOULD be generated
5501 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5502 * some situations it may be beneficial for an SCTP transmitter to be
5503 * more conservative than the algorithms detailed in this document
5504 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5505 * the following algorithms allow.
5506 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005507sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5508 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 const struct sctp_association *asoc,
5510 const sctp_subtype_t type,
5511 void *arg,
5512 sctp_cmd_seq_t *commands)
5513{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005514 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5516 return SCTP_DISPOSITION_CONSUME;
5517}
5518
5519/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005520 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 *
5522 * Section: 4 Note: 2
5523 * Verification Tag:
5524 * Inputs
5525 * (endpoint, asoc)
5526 *
5527 * RFC 2960 Section 4 Notes
5528 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5529 * and re-start the T1-init timer without changing state. This MUST
5530 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5531 * endpoint MUST abort the initialization process and report the
5532 * error to SCTP user.
5533 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005534 * Outputs
5535 * (timers, events)
5536 *
5537 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005538sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5539 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005540 const struct sctp_association *asoc,
5541 const sctp_subtype_t type,
5542 void *arg,
5543 sctp_cmd_seq_t *commands)
5544{
5545 struct sctp_chunk *repl = NULL;
5546 struct sctp_bind_addr *bp;
5547 int attempts = asoc->init_err_counter + 1;
5548
Daniel Borkmannbb333812013-06-28 19:49:40 +02005549 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5550
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005551 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005552
Vlad Yasevich81845c22006-01-30 15:59:54 -08005553 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005554 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5555 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5556 if (!repl)
5557 return SCTP_DISPOSITION_NOMEM;
5558
5559 /* Choose transport for INIT. */
5560 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5561 SCTP_CHUNK(repl));
5562
5563 /* Issue a sideeffect to do the needed accounting. */
5564 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5565 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5566
5567 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5568 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005569 pr_debug("%s: giving up on INIT, attempts:%d "
5570 "max_init_attempts:%d\n", __func__, attempts,
5571 asoc->max_init_attempts);
5572
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005573 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5574 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005575 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005576 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005577 return SCTP_DISPOSITION_DELETE_TCB;
5578 }
5579
5580 return SCTP_DISPOSITION_CONSUME;
5581}
5582
5583/*
5584 * sctp_sf_t1_cookie_timer_expire
5585 *
5586 * Section: 4 Note: 2
5587 * Verification Tag:
5588 * Inputs
5589 * (endpoint, asoc)
5590 *
5591 * RFC 2960 Section 4 Notes
5592 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 * COOKIE ECHO and re-start the T1-cookie timer without changing
5594 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5595 * After that, the endpoint MUST abort the initialization process and
5596 * report the error to SCTP user.
5597 *
5598 * Outputs
5599 * (timers, events)
5600 *
5601 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005602sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5603 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 const struct sctp_association *asoc,
5605 const sctp_subtype_t type,
5606 void *arg,
5607 sctp_cmd_seq_t *commands)
5608{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005609 struct sctp_chunk *repl = NULL;
5610 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
Daniel Borkmannbb333812013-06-28 19:49:40 +02005612 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5613
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005614 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
Vlad Yasevich81845c22006-01-30 15:59:54 -08005616 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005617 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005619 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005621 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5622 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005624 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5625 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5626
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5628 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005629 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5630 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005632 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 return SCTP_DISPOSITION_DELETE_TCB;
5634 }
5635
5636 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637}
5638
5639/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5640 * with the updated last sequential TSN received from its peer.
5641 *
5642 * An endpoint should limit the number of retransmissions of the
5643 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5644 * If this threshold is exceeded the endpoint should destroy the TCB and
5645 * MUST report the peer endpoint unreachable to the upper layer (and
5646 * thus the association enters the CLOSED state). The reception of any
5647 * packet from its peer (i.e. as the peer sends all of its queued DATA
5648 * chunks) should clear the endpoint's retransmission count and restart
5649 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5650 * all of its queued DATA chunks that have not yet been sent.
5651 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005652sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5653 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 const struct sctp_association *asoc,
5655 const sctp_subtype_t type,
5656 void *arg,
5657 sctp_cmd_seq_t *commands)
5658{
5659 struct sctp_chunk *reply = NULL;
5660
Daniel Borkmannbb333812013-06-28 19:49:40 +02005661 pr_debug("%s: timer T2 expired\n", __func__);
5662
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005663 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005664
Neil Horman58fbbed2008-02-29 11:40:56 -08005665 ((struct sctp_association *)asoc)->shutdown_retries++;
5666
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005668 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5669 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5671 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005672 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005673 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5674 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 return SCTP_DISPOSITION_DELETE_TCB;
5676 }
5677
5678 switch (asoc->state) {
5679 case SCTP_STATE_SHUTDOWN_SENT:
5680 reply = sctp_make_shutdown(asoc, NULL);
5681 break;
5682
5683 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5684 reply = sctp_make_shutdown_ack(asoc, NULL);
5685 break;
5686
5687 default:
5688 BUG();
5689 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
5692 if (!reply)
5693 goto nomem;
5694
Wei Yongjun6345b192009-04-26 23:13:35 +08005695 /* Do some failure management (Section 8.2).
5696 * If we remove the transport an SHUTDOWN was last sent to, don't
5697 * do failure management.
5698 */
5699 if (asoc->shutdown_last_sent_to)
5700 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5701 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
5703 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5704 * the T2-shutdown timer.
5705 */
5706 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5707
5708 /* Restart the T2-shutdown timer. */
5709 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5710 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5711 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5712 return SCTP_DISPOSITION_CONSUME;
5713
5714nomem:
5715 return SCTP_DISPOSITION_NOMEM;
5716}
5717
5718/*
5719 * ADDIP Section 4.1 ASCONF CHunk Procedures
5720 * If the T4 RTO timer expires the endpoint should do B1 to B5
5721 */
5722sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005723 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 const struct sctp_endpoint *ep,
5725 const struct sctp_association *asoc,
5726 const sctp_subtype_t type,
5727 void *arg,
5728 sctp_cmd_seq_t *commands)
5729{
5730 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5731 struct sctp_transport *transport = chunk->transport;
5732
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005733 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005734
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5736 * detection on the appropriate destination address as defined in
5737 * RFC2960 [5] section 8.1 and 8.2.
5738 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005739 if (transport)
5740 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5741 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742
5743 /* Reconfig T4 timer and transport. */
5744 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5745
5746 /* ADDIP 4.1 B2) Increment the association error counters and perform
5747 * endpoint failure detection on the association as defined in
5748 * RFC2960 [5] section 8.1 and 8.2.
5749 * association error counter is incremented in SCTP_CMD_STRIKE.
5750 */
5751 if (asoc->overall_error_count >= asoc->max_retrans) {
5752 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5753 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005754 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5755 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 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_ABORT;
5761 }
5762
5763 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5764 * the ASCONF chunk was sent by doubling the RTO timer value.
5765 * This is done in SCTP_CMD_STRIKE.
5766 */
5767
5768 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5769 * choose an alternate destination address (please refer to RFC2960
5770 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005771 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 * ASCONF sent.
5773 */
5774 sctp_chunk_hold(asoc->addip_last_asconf);
5775 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5776 SCTP_CHUNK(asoc->addip_last_asconf));
5777
5778 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5779 * destination is selected, then the RTO used will be that of the new
5780 * destination address.
5781 */
5782 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5783 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5784
5785 return SCTP_DISPOSITION_CONSUME;
5786}
5787
5788/* sctpimpguide-05 Section 2.12.2
5789 * The sender of the SHUTDOWN MAY also start an overall guard timer
5790 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5791 * At the expiration of this timer the sender SHOULD abort the association
5792 * by sending an ABORT chunk.
5793 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005794sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5795 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 const struct sctp_association *asoc,
5797 const sctp_subtype_t type,
5798 void *arg,
5799 sctp_cmd_seq_t *commands)
5800{
5801 struct sctp_chunk *reply = NULL;
5802
Daniel Borkmannbb333812013-06-28 19:49:40 +02005803 pr_debug("%s: timer T5 expired\n", __func__);
5804
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005805 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
5807 reply = sctp_make_abort(asoc, NULL, 0);
5808 if (!reply)
5809 goto nomem;
5810
5811 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005812 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5813 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005815 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005817 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5818 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 return SCTP_DISPOSITION_DELETE_TCB;
5821nomem:
5822 return SCTP_DISPOSITION_NOMEM;
5823}
5824
5825/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5826 * the association is automatically closed by starting the shutdown process.
5827 * The work that needs to be done is same as when SHUTDOWN is initiated by
5828 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5829 */
5830sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005831 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 const struct sctp_endpoint *ep,
5833 const struct sctp_association *asoc,
5834 const sctp_subtype_t type,
5835 void *arg,
5836 sctp_cmd_seq_t *commands)
5837{
5838 int disposition;
5839
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005840 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005841
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 /* From 9.2 Shutdown of an Association
5843 * Upon receipt of the SHUTDOWN primitive from its upper
5844 * layer, the endpoint enters SHUTDOWN-PENDING state and
5845 * remains there until all outstanding data has been
5846 * acknowledged by its peer. The endpoint accepts no new data
5847 * from its upper layer, but retransmits data to the far end
5848 * if necessary to fill gaps.
5849 */
5850 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5851 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5852
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 disposition = SCTP_DISPOSITION_CONSUME;
5854 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005855 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 arg, commands);
5857 }
5858 return disposition;
5859}
5860
5861/*****************************************************************************
5862 * These are sa state functions which could apply to all types of events.
5863 ****************************************************************************/
5864
5865/*
5866 * This table entry is not implemented.
5867 *
5868 * Inputs
5869 * (endpoint, asoc, chunk)
5870 *
5871 * The return value is the disposition of the chunk.
5872 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005873sctp_disposition_t sctp_sf_not_impl(struct net *net,
5874 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 const struct sctp_association *asoc,
5876 const sctp_subtype_t type,
5877 void *arg,
5878 sctp_cmd_seq_t *commands)
5879{
5880 return SCTP_DISPOSITION_NOT_IMPL;
5881}
5882
5883/*
5884 * This table entry represents a bug.
5885 *
5886 * Inputs
5887 * (endpoint, asoc, chunk)
5888 *
5889 * The return value is the disposition of the chunk.
5890 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005891sctp_disposition_t sctp_sf_bug(struct net *net,
5892 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 const struct sctp_association *asoc,
5894 const sctp_subtype_t type,
5895 void *arg,
5896 sctp_cmd_seq_t *commands)
5897{
5898 return SCTP_DISPOSITION_BUG;
5899}
5900
5901/*
5902 * This table entry represents the firing of a timer in the wrong state.
5903 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5904 * when the association is in the wrong state. This event should
5905 * be ignored, so as to prevent any rearming of the timer.
5906 *
5907 * Inputs
5908 * (endpoint, asoc, chunk)
5909 *
5910 * The return value is the disposition of the chunk.
5911 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005912sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5913 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 const struct sctp_association *asoc,
5915 const sctp_subtype_t type,
5916 void *arg,
5917 sctp_cmd_seq_t *commands)
5918{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005919 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 return SCTP_DISPOSITION_CONSUME;
5922}
5923
5924/********************************************************************
5925 * 2nd Level Abstractions
5926 ********************************************************************/
5927
5928/* Pull the SACK chunk based on the SACK header. */
5929static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5930{
5931 struct sctp_sackhdr *sack;
5932 unsigned int len;
5933 __u16 num_blocks;
5934 __u16 num_dup_tsns;
5935
5936 /* Protect ourselves from reading too far into
5937 * the skb from a bogus sender.
5938 */
5939 sack = (struct sctp_sackhdr *) chunk->skb->data;
5940
5941 num_blocks = ntohs(sack->num_gap_ack_blocks);
5942 num_dup_tsns = ntohs(sack->num_dup_tsns);
5943 len = sizeof(struct sctp_sackhdr);
5944 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5945 if (len > chunk->skb->len)
5946 return NULL;
5947
5948 skb_pull(chunk->skb, len);
5949
5950 return sack;
5951}
5952
5953/* Create an ABORT packet to be sent as a response, with the specified
5954 * error causes.
5955 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005956static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5957 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 const struct sctp_association *asoc,
5959 struct sctp_chunk *chunk,
5960 const void *payload,
5961 size_t paylen)
5962{
5963 struct sctp_packet *packet;
5964 struct sctp_chunk *abort;
5965
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005966 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
5968 if (packet) {
5969 /* Make an ABORT.
5970 * The T bit will be set if the asoc is NULL.
5971 */
5972 abort = sctp_make_abort(asoc, chunk, paylen);
5973 if (!abort) {
5974 sctp_ootb_pkt_free(packet);
5975 return NULL;
5976 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005977
5978 /* Reflect vtag if T-Bit is set */
5979 if (sctp_test_T_bit(abort))
5980 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5981
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 /* Add specified error causes, i.e., payload, to the
5983 * end of the chunk.
5984 */
5985 sctp_addto_chunk(abort, paylen, payload);
5986
5987 /* Set the skb to the belonging sock for accounting. */
5988 abort->skb->sk = ep->base.sk;
5989
5990 sctp_packet_append_chunk(packet, abort);
5991
5992 }
5993
5994 return packet;
5995}
5996
5997/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005998static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5999 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 const struct sctp_chunk *chunk)
6001{
6002 struct sctp_packet *packet;
6003 struct sctp_transport *transport;
6004 __u16 sport;
6005 __u16 dport;
6006 __u32 vtag;
6007
6008 /* Get the source and destination port from the inbound packet. */
6009 sport = ntohs(chunk->sctp_hdr->dest);
6010 dport = ntohs(chunk->sctp_hdr->source);
6011
6012 /* The V-tag is going to be the same as the inbound packet if no
6013 * association exists, otherwise, use the peer's vtag.
6014 */
6015 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006016 /* Special case the INIT-ACK as there is no peer's vtag
6017 * yet.
6018 */
wangweidongcb3f8372013-12-23 12:16:50 +08006019 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006020 case SCTP_CID_INIT_ACK:
6021 {
6022 sctp_initack_chunk_t *initack;
6023
6024 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6025 vtag = ntohl(initack->init_hdr.init_tag);
6026 break;
6027 }
6028 default:
6029 vtag = asoc->peer.i.init_tag;
6030 break;
6031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 } else {
6033 /* Special case the INIT and stale COOKIE_ECHO as there is no
6034 * vtag yet.
6035 */
wangweidongcb3f8372013-12-23 12:16:50 +08006036 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 case SCTP_CID_INIT:
6038 {
6039 sctp_init_chunk_t *init;
6040
6041 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6042 vtag = ntohl(init->init_hdr.init_tag);
6043 break;
6044 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006045 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 vtag = ntohl(chunk->sctp_hdr->vtag);
6047 break;
6048 }
6049 }
6050
6051 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006052 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 if (!transport)
6054 goto nomem;
6055
6056 /* Cache a route for the transport with the chunk's destination as
6057 * the source address.
6058 */
Al Viro16b0a032006-11-20 17:13:38 -08006059 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006060 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
6062 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6063 packet = sctp_packet_config(packet, vtag, 0);
6064
6065 return packet;
6066
6067nomem:
6068 return NULL;
6069}
6070
6071/* Free the packet allocated earlier for responding in the OOTB condition. */
6072void sctp_ootb_pkt_free(struct sctp_packet *packet)
6073{
6074 sctp_transport_free(packet->transport);
6075}
6076
6077/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006078static void sctp_send_stale_cookie_err(struct net *net,
6079 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 const struct sctp_association *asoc,
6081 const struct sctp_chunk *chunk,
6082 sctp_cmd_seq_t *commands,
6083 struct sctp_chunk *err_chunk)
6084{
6085 struct sctp_packet *packet;
6086
6087 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006088 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 if (packet) {
6090 struct sctp_signed_cookie *cookie;
6091
6092 /* Override the OOTB vtag from the cookie. */
6093 cookie = chunk->subh.cookie_hdr;
6094 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 /* Set the skb to the belonging sock for accounting. */
6097 err_chunk->skb->sk = ep->base.sk;
6098 sctp_packet_append_chunk(packet, err_chunk);
6099 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6100 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006101 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 } else
6103 sctp_chunk_free (err_chunk);
6104 }
6105}
6106
6107
6108/* Process a data chunk */
6109static int sctp_eat_data(const struct sctp_association *asoc,
6110 struct sctp_chunk *chunk,
6111 sctp_cmd_seq_t *commands)
6112{
6113 sctp_datahdr_t *data_hdr;
6114 struct sctp_chunk *err;
6115 size_t datalen;
6116 sctp_verb_t deliver;
6117 int tmp;
6118 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006119 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006120 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006121 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006122 u16 ssn;
6123 u16 sid;
6124 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125
6126 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6127 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6128
6129 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006130 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
6132 /* ASSERT: Now skb->data is really the user data. */
6133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 /* Process ECN based congestion.
6135 *
6136 * Since the chunk structure is reused for all chunks within
6137 * a packet, we use ecn_ce_done to track if we've already
6138 * done CE processing for this packet.
6139 *
6140 * We need to do ECN processing even if we plan to discard the
6141 * chunk later.
6142 */
6143
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006144 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006145 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 chunk->ecn_ce_done = 1;
6147
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006148 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 /* Do real work as sideffect. */
6150 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6151 SCTP_U32(tsn));
6152 }
6153 }
6154
6155 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6156 if (tmp < 0) {
6157 /* The TSN is too high--silently discard the chunk and
6158 * count on it getting retransmitted later.
6159 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006160 if (chunk->asoc)
6161 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162 return SCTP_IERROR_HIGH_TSN;
6163 } else if (tmp > 0) {
6164 /* This is a duplicate. Record it. */
6165 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6166 return SCTP_IERROR_DUP_TSN;
6167 }
6168
6169 /* This is a new TSN. */
6170
6171 /* Discard if there is no room in the receive window.
6172 * Actually, allow a little bit of overflow (up to a MTU).
6173 */
6174 datalen = ntohs(chunk->chunk_hdr->length);
6175 datalen -= sizeof(sctp_data_chunk_t);
6176
6177 deliver = SCTP_CMD_CHUNK_ULP;
6178
6179 /* Think about partial delivery. */
6180 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6181
6182 /* Even if we don't accept this chunk there is
6183 * memory pressure.
6184 */
6185 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6186 }
6187
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006188 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 * seems a bit troublesome in that frag_point varies based on
6190 * PMTU. In cases, such as loopback, this might be a rather
6191 * large spill over.
6192 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006193 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006194 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195
6196 /* If this is the next TSN, consider reneging to make
6197 * room. Note: Playing nice with a confused sender. A
6198 * malicious sender can still eat up all our buffer
6199 * space and in the future we may want to detect and
6200 * do more drastic reneging.
6201 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006202 if (sctp_tsnmap_has_gap(map) &&
6203 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006204 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 deliver = SCTP_CMD_RENEGE;
6206 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006207 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6208 __func__, tsn, datalen, asoc->rwnd);
6209
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 return SCTP_IERROR_IGNORE_TSN;
6211 }
6212 }
6213
6214 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006215 * Also try to renege to limit our memory usage in the event that
6216 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006217 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006218 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6219 * memory usage too much
6220 */
6221 if (*sk->sk_prot_creator->memory_pressure) {
6222 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006223 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006224 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6225 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006226 deliver = SCTP_CMD_RENEGE;
6227 }
6228 }
6229
6230 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 * Section 3.3.10.9 No User Data (9)
6232 *
6233 * Cause of error
6234 * ---------------
6235 * No User Data: This error cause is returned to the originator of a
6236 * DATA chunk if a received DATA chunk has no user data.
6237 */
6238 if (unlikely(0 == datalen)) {
6239 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6240 if (err) {
6241 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6242 SCTP_CHUNK(err));
6243 }
6244 /* We are going to ABORT, so we might as well stop
6245 * processing the rest of the chunks in the packet.
6246 */
wangweidongcb3f8372013-12-23 12:16:50 +08006247 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006248 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6249 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006251 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006252 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6253 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 return SCTP_IERROR_NO_DATA;
6255 }
6256
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006257 chunk->data_accepted = 1;
6258
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6260 * if we renege and the chunk arrives again.
6261 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006262 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006263 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006264 if (chunk->asoc)
6265 chunk->asoc->stats.iuodchunks++;
6266 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006267 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006268 if (chunk->asoc)
6269 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006270 ordered = 1;
6271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
6273 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6274 *
6275 * If an endpoint receive a DATA chunk with an invalid stream
6276 * identifier, it shall acknowledge the reception of the DATA chunk
6277 * following the normal procedure, immediately send an ERROR chunk
6278 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6279 * and discard the DATA chunk.
6280 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006281 sid = ntohs(data_hdr->stream);
6282 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006283 /* Mark tsn as received even though we drop it */
6284 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6285
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6287 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006288 sizeof(data_hdr->stream),
6289 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 if (err)
6291 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6292 SCTP_CHUNK(err));
6293 return SCTP_IERROR_BAD_STREAM;
6294 }
6295
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006296 /* Check to see if the SSN is possible for this TSN.
6297 * The biggest gap we can record is 4K wide. Since SSNs wrap
6298 * at an unsigned short, there is no way that an SSN can
6299 * wrap and for a valid TSN. We can simply check if the current
6300 * SSN is smaller then the next expected one. If it is, it wrapped
6301 * and is invalid.
6302 */
6303 ssn = ntohs(data_hdr->ssn);
6304 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6305 return SCTP_IERROR_PROTO_VIOLATION;
6306 }
6307
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308 /* Send the data up to the user. Note: Schedule the
6309 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6310 * chunk needs the updated rwnd.
6311 */
6312 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6313
6314 return SCTP_IERROR_NO_ERROR;
6315}