blob: bfd0686797104a6f79a12ba06a64887ad6e8957d [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
180/**********************************************************
181 * These are the state functions for handling chunk events.
182 **********************************************************/
183
184/*
185 * Process the final SHUTDOWN COMPLETE.
186 *
187 * Section: 4 (C) (diagram), 9.2
188 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
189 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
190 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
191 * should stop the T2-shutdown timer and remove all knowledge of the
192 * association (and thus the association enters the CLOSED state).
193 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700194 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * C) Rules for packet carrying SHUTDOWN COMPLETE:
196 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700197 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
198 * if the Verification Tag field of the packet matches its own tag and
199 * the T bit is not set
200 * OR
201 * it is set to its peer's tag and the T bit is set in the Chunk
202 * Flags.
203 * Otherwise, the receiver MUST silently discard the packet
204 * and take no further action. An endpoint MUST ignore the
205 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * Inputs
208 * (endpoint, asoc, chunk)
209 *
210 * Outputs
211 * (asoc, reply_msg, msg_up, timers, counters)
212 *
213 * The return value is the disposition of the chunk.
214 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000215sctp_disposition_t sctp_sf_do_4_C(struct net *net,
216 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 const struct sctp_association *asoc,
218 const sctp_subtype_t type,
219 void *arg,
220 sctp_cmd_seq_t *commands)
221{
222 struct sctp_chunk *chunk = arg;
223 struct sctp_ulpevent *ev;
224
Vlad Yasevichece25df2007-09-07 16:30:54 -0400225 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000226 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 /* RFC 2960 6.10 Bundling
229 *
230 * An endpoint MUST NOT bundle INIT, INIT ACK or
231 * SHUTDOWN COMPLETE with any other chunks.
232 */
233 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000234 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Vlad Yasevichece25df2007-09-07 16:30:54 -0400236 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
237 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000238 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400239 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /* RFC 2960 10.2 SCTP-to-ULP
242 *
243 * H) SHUTDOWN COMPLETE notification
244 *
245 * When SCTP completes the shutdown procedures (section 9.2) this
246 * notification is passed to the upper layer.
247 */
248 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700249 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700250 if (ev)
251 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900252 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
255 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
256 * not the chunk should be discarded. If the endpoint is in
257 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
258 * T2-shutdown timer and remove all knowledge of the
259 * association (and thus the association enters the CLOSED
260 * state).
261 */
262 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
263 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
264
265 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
266 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
267
268 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
269 SCTP_STATE(SCTP_STATE_CLOSED));
270
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000271 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
272 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
275
276 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279/*
280 * Respond to a normal INIT chunk.
281 * We are the side that is being asked for an association.
282 *
283 * Section: 5.1 Normal Establishment of an Association, B
284 * B) "Z" shall respond immediately with an INIT ACK chunk. The
285 * destination IP address of the INIT ACK MUST be set to the source
286 * IP address of the INIT to which this INIT ACK is responding. In
287 * the response, besides filling in other parameters, "Z" must set the
288 * Verification Tag field to Tag_A, and also provide its own
289 * Verification Tag (Tag_Z) in the Initiate Tag field.
290 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900291 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 *
293 * Inputs
294 * (endpoint, asoc, chunk)
295 *
296 * Outputs
297 * (asoc, reply_msg, msg_up, timers, counters)
298 *
299 * The return value is the disposition of the chunk.
300 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000301sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
302 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 const struct sctp_association *asoc,
304 const sctp_subtype_t type,
305 void *arg,
306 sctp_cmd_seq_t *commands)
307{
308 struct sctp_chunk *chunk = arg;
309 struct sctp_chunk *repl;
310 struct sctp_association *new_asoc;
311 struct sctp_chunk *err_chunk;
312 struct sctp_packet *packet;
313 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int len;
315
316 /* 6.10 Bundling
317 * An endpoint MUST NOT bundle INIT, INIT ACK or
318 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900319 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * IG Section 2.11.2
321 * Furthermore, we require that the receiver of an INIT chunk MUST
322 * enforce these rules by silently discarding an arriving packet
323 * with an INIT chunk that is bundled with other chunks.
324 */
325 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000326 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* If the packet is an OOTB packet which is temporarily on the
329 * control endpoint, respond with an ABORT.
330 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000331 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000332 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000333 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900337 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000340 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* Make sure that the INIT chunk has a valid length.
343 * Normally, this would cause an ABORT with a Protocol Violation
344 * error, but since we don't have an association, we'll
345 * just discard the packet.
346 */
347 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000348 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Vlad Yasevichbec96402009-07-30 18:08:28 -0400350 /* If the INIT is coming toward a closing socket, we'll send back
351 * and ABORT. Essentially, this catches the race of INIT being
352 * backloged to the socket at the same time as the user isses close().
353 * Since the socket and all its associations are going away, we
354 * can treat this OOTB
355 */
356 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000357 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 /* Verify the INIT chunk before processing it. */
360 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200361 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
363 &err_chunk)) {
364 /* This chunk contains fatal error. It is to be discarded.
365 * Send an ABORT, with causes if there is any.
366 */
367 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000368 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 (__u8 *)(err_chunk->chunk_hdr) +
370 sizeof(sctp_chunkhdr_t),
371 ntohs(err_chunk->chunk_hdr->length) -
372 sizeof(sctp_chunkhdr_t));
373
374 sctp_chunk_free(err_chunk);
375
376 if (packet) {
377 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
378 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000379 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return SCTP_DISPOSITION_CONSUME;
381 } else {
382 return SCTP_DISPOSITION_NOMEM;
383 }
384 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000385 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 commands);
387 }
388 }
389
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900390 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
392
393 /* Tag the variable length parameters. */
394 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
395
396 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
397 if (!new_asoc)
398 goto nomem;
399
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000400 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
401 sctp_scope(sctp_source(chunk)),
402 GFP_ATOMIC) < 0)
403 goto nomem_init;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000406 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 (sctp_init_chunk_t *)chunk->chunk_hdr,
408 GFP_ATOMIC))
409 goto nomem_init;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
412
413 /* If there are errors need to be reported for unknown parameters,
414 * make sure to reserve enough room in the INIT ACK for them.
415 */
416 len = 0;
417 if (err_chunk)
418 len = ntohs(err_chunk->chunk_hdr->length) -
419 sizeof(sctp_chunkhdr_t);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
422 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700423 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* If there are errors need to be reported for unknown parameters,
426 * include them in the outgoing INIT ACK as "Unrecognized parameter"
427 * parameter.
428 */
429 if (err_chunk) {
430 /* Get the "Unrecognized parameter" parameter(s) out of the
431 * ERROR chunk generated by sctp_verify_init(). Since the
432 * error cause code for "unknown parameter" and the
433 * "Unrecognized parameter" type is the same, we can
434 * construct the parameters in INIT ACK by copying the
435 * ERROR causes over.
436 */
437 unk_param = (sctp_unrecognized_param_t *)
438 ((__u8 *)(err_chunk->chunk_hdr) +
439 sizeof(sctp_chunkhdr_t));
440 /* Replace the cause code with the "Unrecognized parameter"
441 * parameter type.
442 */
443 sctp_addto_chunk(repl, len, unk_param);
444 sctp_chunk_free(err_chunk);
445 }
446
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700447 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
450
451 /*
452 * Note: After sending out INIT ACK with the State Cookie parameter,
453 * "Z" MUST NOT allocate any resources, nor keep any states for the
454 * new association. Otherwise, "Z" will be vulnerable to resource
455 * attacks.
456 */
457 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
458
459 return SCTP_DISPOSITION_DELETE_TCB;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461nomem_init:
462 sctp_association_free(new_asoc);
463nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700464 if (err_chunk)
465 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return SCTP_DISPOSITION_NOMEM;
467}
468
469/*
470 * Respond to a normal INIT ACK chunk.
471 * We are the side that is initiating the association.
472 *
473 * Section: 5.1 Normal Establishment of an Association, C
474 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
475 * timer and leave COOKIE-WAIT state. "A" shall then send the State
476 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
477 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
478 *
479 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
480 * DATA chunks, but it MUST be the first chunk in the packet and
481 * until the COOKIE ACK is returned the sender MUST NOT send any
482 * other packets to the peer.
483 *
484 * Verification Tag: 3.3.3
485 * If the value of the Initiate Tag in a received INIT ACK chunk is
486 * found to be 0, the receiver MUST treat it as an error and close the
487 * association by transmitting an ABORT.
488 *
489 * Inputs
490 * (endpoint, asoc, chunk)
491 *
492 * Outputs
493 * (asoc, reply_msg, msg_up, timers, counters)
494 *
495 * The return value is the disposition of the chunk.
496 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000497sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
498 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 const struct sctp_association *asoc,
500 const sctp_subtype_t type,
501 void *arg,
502 sctp_cmd_seq_t *commands)
503{
504 struct sctp_chunk *chunk = arg;
505 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 struct sctp_chunk *err_chunk;
507 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000510 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* 6.10 Bundling
513 * An endpoint MUST NOT bundle INIT, INIT ACK or
514 * SHUTDOWN COMPLETE with any other chunks.
515 */
516 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000517 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Vlad Yasevichece25df2007-09-07 16:30:54 -0400519 /* Make sure that the INIT-ACK chunk has a valid length */
520 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000521 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400522 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Grab the INIT header. */
524 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Verify the INIT chunk before processing it. */
527 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +0200528 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
530 &err_chunk)) {
531
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800532 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800535 * Send an ABORT, with causes. If there are no causes,
536 * then there wasn't enough memory. Just terminate
537 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
539 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000540 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 (__u8 *)(err_chunk->chunk_hdr) +
542 sizeof(sctp_chunkhdr_t),
543 ntohs(err_chunk->chunk_hdr->length) -
544 sizeof(sctp_chunkhdr_t));
545
546 sctp_chunk_free(err_chunk);
547
548 if (packet) {
549 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
550 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000551 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700552 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700555
556 /* SCTP-AUTH, Section 6.3:
557 * It should be noted that if the receiver wants to tear
558 * down an association in an authenticated way only, the
559 * handling of malformed packets should not result in
560 * tearing down the association.
561 *
562 * This means that if we only want to abort associations
563 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300564 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700565 * was malformed.
566 */
567 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000568 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700569
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000570 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
571 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700572 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
575 /* Tag the variable length parameters. Note that we never
576 * convert the parameters in an INIT chunk.
577 */
578 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
579
580 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
581
582 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
583 SCTP_PEER_INIT(initchunk));
584
Frank Filz3f7a87d2005-06-20 13:14:57 -0700585 /* Reset init error count upon receipt of INIT-ACK. */
586 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* 5.1 C) "A" shall stop the T1-init timer and leave
589 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
590 * timer, and enter the COOKIE-ECHOED state.
591 */
592 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
593 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
594 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
595 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
596 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
597 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
598
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700599 /* SCTP-AUTH: genereate the assocition shared keys so that
600 * we can potentially signe the COOKIE-ECHO.
601 */
602 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* 5.1 C) "A" shall then send the State Cookie received in the
605 * INIT ACK chunk in a COOKIE ECHO chunk, ...
606 */
607 /* If there is any errors to report, send the ERROR chunk generated
608 * for unknown parameters as well.
609 */
610 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
611 SCTP_CHUNK(err_chunk));
612
613 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Xin Long0e67ad52018-05-05 14:59:47 +0800616static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
617 const struct sctp_association *asoc)
618{
619 struct sctp_chunk auth;
620
621 if (!chunk->auth_chunk)
622 return true;
623
624 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
625 * is supposed to be authenticated and we have to do delayed
626 * authentication. We've just recreated the association using
627 * the information in the cookie and now it's much easier to
628 * do the authentication.
629 */
630
631 /* Make sure that we and the peer are AUTH capable */
632 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
633 return false;
634
635 /* set-up our fake chunk so that we can process it */
636 auth.skb = chunk->auth_chunk;
637 auth.asoc = chunk->asoc;
638 auth.sctp_hdr = chunk->sctp_hdr;
639 auth.chunk_hdr = (struct sctp_chunkhdr *)
640 skb_push(chunk->auth_chunk,
641 sizeof(struct sctp_chunkhdr));
642 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
643 auth.transport = chunk->transport;
644
645 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * Respond to a normal COOKIE ECHO chunk.
650 * We are the side that is being asked for an association.
651 *
652 * Section: 5.1 Normal Establishment of an Association, D
653 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
654 * with a COOKIE ACK chunk after building a TCB and moving to
655 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
656 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
657 * chunk MUST be the first chunk in the packet.
658 *
659 * IMPLEMENTATION NOTE: An implementation may choose to send the
660 * Communication Up notification to the SCTP user upon reception
661 * of a valid COOKIE ECHO chunk.
662 *
663 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
664 * D) Rules for packet carrying a COOKIE ECHO
665 *
666 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
667 * Initial Tag received in the INIT ACK.
668 *
669 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
670 *
671 * Inputs
672 * (endpoint, asoc, chunk)
673 *
674 * Outputs
675 * (asoc, reply_msg, msg_up, timers, counters)
676 *
677 * The return value is the disposition of the chunk.
678 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000679sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
680 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 const struct sctp_association *asoc,
682 const sctp_subtype_t type, void *arg,
683 sctp_cmd_seq_t *commands)
684{
685 struct sctp_chunk *chunk = arg;
686 struct sctp_association *new_asoc;
687 sctp_init_chunk_t *peer_init;
688 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700689 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int error = 0;
691 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900692 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* If the packet is an OOTB packet which is temporarily on the
695 * control endpoint, respond with an ABORT.
696 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000697 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000698 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000699 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* Make sure that the COOKIE_ECHO chunk has a valid length.
703 * In this case, we check that we have enough for at least a
704 * chunk header. More detailed verification is done
705 * in sctp_unpack_cookie().
706 */
707 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000708 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Vlad Yasevich609ee462007-08-31 03:10:59 +0900710 /* If the endpoint is not listening or if the number of associations
711 * on the TCP-style socket exceed the max backlog, respond with an
712 * ABORT.
713 */
714 sk = ep->base.sk;
715 if (!sctp_sstate(sk, LISTENING) ||
716 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000717 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 /* "Decode" the chunk. We have no optional parameters so we
720 * are in good shape.
721 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900722 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700724 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
725 sizeof(sctp_chunkhdr_t)))
726 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
729 * "Z" will reply with a COOKIE ACK chunk after building a TCB
730 * and moving to the ESTABLISHED state.
731 */
732 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
733 &err_chk_p);
734
735 /* FIXME:
736 * If the re-build failed, what is the proper error path
737 * from here?
738 *
739 * [We should abort the association. --piggy]
740 */
741 if (!new_asoc) {
742 /* FIXME: Several errors are possible. A bad cookie should
743 * be silently discarded, but think about logging it too.
744 */
745 switch (error) {
746 case -SCTP_IERROR_NOMEM:
747 goto nomem;
748
749 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000750 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000752 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 case -SCTP_IERROR_BAD_SIG:
755 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000756 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700761 /* Delay state machine commands until later.
762 *
763 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * the sctp_unpack_cookie() already.
765 */
766 /* This is a brand-new association, so these are not yet side
767 * effects--it is safe to run them here.
768 */
769 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
770
Wei Yongjunde6becd2011-04-19 21:30:51 +0000771 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800772 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 peer_init, GFP_ATOMIC))
774 goto nomem_init;
775
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700776 /* SCTP-AUTH: Now that we've populate required fields in
777 * sctp_process_init, set up the assocaition shared keys as
778 * necessary so that we can potentially authenticate the ACK
779 */
780 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
781 if (error)
782 goto nomem_init;
783
Xin Long0e67ad52018-05-05 14:59:47 +0800784 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
785 sctp_association_free(new_asoc);
786 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700787 }
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 repl = sctp_make_cookie_ack(new_asoc, chunk);
790 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700791 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* RFC 2960 5.1 Normal Establishment of an Association
794 *
795 * D) IMPLEMENTATION NOTE: An implementation may choose to
796 * send the Communication Up notification to the SCTP user
797 * upon reception of a valid COOKIE ECHO chunk.
798 */
799 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
800 new_asoc->c.sinit_num_ostreams,
801 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700802 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!ev)
804 goto nomem_ev;
805
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900806 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800807 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800809 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800811 if (new_asoc->peer.adaptation_ind) {
812 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700814 if (!ai_ev)
815 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700818 /* Add all the state machine commands now since we've created
819 * everything. This way we don't introduce memory corruptions
820 * during side-effect processing and correclty count established
821 * associations.
822 */
823 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
824 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
825 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000826 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
827 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700828 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
829
Neil Horman9f70f462013-12-10 06:48:15 -0500830 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700831 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
832 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
833
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700834 /* This will send the COOKIE ACK */
835 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
836
837 /* Queue the ASSOC_CHANGE event */
838 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
839
840 /* Send up the Adaptation Layer Indication event */
841 if (ai_ev)
842 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
843 SCTP_ULPEVENT(ai_ev));
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return SCTP_DISPOSITION_CONSUME;
846
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700847nomem_aiev:
848 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849nomem_ev:
850 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851nomem_init:
852 sctp_association_free(new_asoc);
853nomem:
854 return SCTP_DISPOSITION_NOMEM;
855}
856
857/*
858 * Respond to a normal COOKIE ACK chunk.
Marcelo Ricardo Leitnerb52effd2015-07-17 13:50:21 -0300859 * We are the side that is asking for an association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 *
861 * RFC 2960 5.1 Normal Establishment of an Association
862 *
863 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
864 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
865 * timer. It may also notify its ULP about the successful
866 * establishment of the association with a Communication Up
867 * notification (see Section 10).
868 *
869 * Verification Tag:
870 * Inputs
871 * (endpoint, asoc, chunk)
872 *
873 * Outputs
874 * (asoc, reply_msg, msg_up, timers, counters)
875 *
876 * The return value is the disposition of the chunk.
877 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000878sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
879 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 const struct sctp_association *asoc,
881 const sctp_subtype_t type, void *arg,
882 sctp_cmd_seq_t *commands)
883{
884 struct sctp_chunk *chunk = arg;
885 struct sctp_ulpevent *ev;
886
887 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000888 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* Verify that the chunk length for the COOKIE-ACK is OK.
891 * If we don't do this, any bundled chunks may be junked.
892 */
893 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000894 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 commands);
896
897 /* Reset init error count upon receipt of COOKIE-ACK,
898 * to avoid problems with the managemement of this
899 * counter in stale cookie situations when a transition back
900 * from the COOKIE-ECHOED state to the COOKIE-WAIT
901 * state is performed.
902 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700903 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* RFC 2960 5.1 Normal Establishment of an Association
906 *
907 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
908 * from the COOKIE-ECHOED state to the ESTABLISHED state,
909 * stopping the T1-cookie timer.
910 */
911 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
912 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
913 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
914 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000915 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
916 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500918 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
920 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* It may also notify its ULP about the successful
923 * establishment of the association with a Communication Up
924 * notification (see Section 10).
925 */
926 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
927 0, asoc->c.sinit_num_ostreams,
928 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700929 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (!ev)
932 goto nomem;
933
934 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
935
936 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800937 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800939 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800941 if (asoc->peer.adaptation_ind) {
942 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!ev)
944 goto nomem;
945
946 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
947 SCTP_ULPEVENT(ev));
948 }
949
950 return SCTP_DISPOSITION_CONSUME;
951nomem:
952 return SCTP_DISPOSITION_NOMEM;
953}
954
955/* Generate and sendout a heartbeat packet. */
956static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
957 const struct sctp_association *asoc,
958 const sctp_subtype_t type,
959 void *arg,
960 sctp_cmd_seq_t *commands)
961{
962 struct sctp_transport *transport = (struct sctp_transport *) arg;
963 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000966 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!reply)
968 return SCTP_DISPOSITION_NOMEM;
969
970 /* Set rto_pending indicating that an RTT measurement
971 * is started with this heartbeat chunk.
972 */
973 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
974 SCTP_TRANSPORT(transport));
975
976 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
977 return SCTP_DISPOSITION_CONSUME;
978}
979
980/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000981sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
982 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 const struct sctp_association *asoc,
984 const sctp_subtype_t type,
985 void *arg,
986 sctp_cmd_seq_t *commands)
987{
988 struct sctp_transport *transport = (struct sctp_transport *) arg;
989
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400990 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700991 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
992 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
994 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800995 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000996 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
997 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 return SCTP_DISPOSITION_DELETE_TCB;
999 }
1000
1001 /* Section 3.3.5.
1002 * The Sender-specific Heartbeat Info field should normally include
1003 * information about the sender's current time when this HEARTBEAT
1004 * chunk is sent and the destination transport address to which this
1005 * HEARTBEAT is sent (see Section 8.3).
1006 */
1007
Frank Filz52ccb8e2005-12-22 11:36:46 -08001008 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (SCTP_DISPOSITION_NOMEM ==
1010 sctp_sf_heartbeat(ep, asoc, type, arg,
1011 commands))
1012 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* Set transport error counter and association error counter
1015 * when sending heartbeat.
1016 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001017 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 SCTP_TRANSPORT(transport));
1019 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001020 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1021 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1023 SCTP_TRANSPORT(transport));
1024
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001025 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028/*
1029 * Process an heartbeat request.
1030 *
1031 * Section: 8.3 Path Heartbeat
1032 * The receiver of the HEARTBEAT should immediately respond with a
1033 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1034 * from the received HEARTBEAT chunk.
1035 *
1036 * Verification Tag: 8.5 Verification Tag [Normal verification]
1037 * When receiving an SCTP packet, the endpoint MUST ensure that the
1038 * value in the Verification Tag field of the received SCTP packet
1039 * matches its own Tag. If the received Verification Tag value does not
1040 * match the receiver's own tag value, the receiver shall silently
1041 * discard the packet and shall not process it any further except for
1042 * those cases listed in Section 8.5.1 below.
1043 *
1044 * Inputs
1045 * (endpoint, asoc, chunk)
1046 *
1047 * Outputs
1048 * (asoc, reply_msg, msg_up, timers, counters)
1049 *
1050 * The return value is the disposition of the chunk.
1051 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001052sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1053 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 const struct sctp_association *asoc,
1055 const sctp_subtype_t type,
1056 void *arg,
1057 sctp_cmd_seq_t *commands)
1058{
Thomas Graf06a31e22012-11-30 02:16:27 +00001059 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct sctp_chunk *chunk = arg;
1061 struct sctp_chunk *reply;
1062 size_t paylen = 0;
1063
1064 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001065 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /* Make sure that the HEARTBEAT chunk has a valid length. */
1068 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001069 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 commands);
1071
1072 /* 8.3 The receiver of the HEARTBEAT should immediately
1073 * respond with a HEARTBEAT ACK that contains the Heartbeat
1074 * Information field copied from the received HEARTBEAT chunk.
1075 */
1076 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001077 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001079
1080 if (ntohs(param_hdr->length) > paylen)
1081 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1082 param_hdr, commands);
1083
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001084 if (!pskb_pull(chunk->skb, paylen))
1085 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Thomas Graf06a31e22012-11-30 02:16:27 +00001087 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (!reply)
1089 goto nomem;
1090
1091 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1092 return SCTP_DISPOSITION_CONSUME;
1093
1094nomem:
1095 return SCTP_DISPOSITION_NOMEM;
1096}
1097
1098/*
1099 * Process the returning HEARTBEAT ACK.
1100 *
1101 * Section: 8.3 Path Heartbeat
1102 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1103 * should clear the error counter of the destination transport
1104 * address to which the HEARTBEAT was sent, and mark the destination
1105 * transport address as active if it is not so marked. The endpoint may
1106 * optionally report to the upper layer when an inactive destination
1107 * address is marked as active due to the reception of the latest
1108 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1109 * clear the association overall error count as well (as defined
1110 * in section 8.1).
1111 *
1112 * The receiver of the HEARTBEAT ACK should also perform an RTT
1113 * measurement for that destination transport address using the time
1114 * value carried in the HEARTBEAT ACK chunk.
1115 *
1116 * Verification Tag: 8.5 Verification Tag [Normal verification]
1117 *
1118 * Inputs
1119 * (endpoint, asoc, chunk)
1120 *
1121 * Outputs
1122 * (asoc, reply_msg, msg_up, timers, counters)
1123 *
1124 * The return value is the disposition of the chunk.
1125 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001126sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1127 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 const struct sctp_association *asoc,
1129 const sctp_subtype_t type,
1130 void *arg,
1131 sctp_cmd_seq_t *commands)
1132{
1133 struct sctp_chunk *chunk = arg;
1134 union sctp_addr from_addr;
1135 struct sctp_transport *link;
1136 sctp_sender_hb_info_t *hbinfo;
1137 unsigned long max_interval;
1138
1139 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001140 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001143 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1144 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001145 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 commands);
1147
1148 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001149 /* Make sure that the length of the parameter is what we expect */
1150 if (ntohs(hbinfo->param_hdr.length) !=
1151 sizeof(sctp_sender_hb_info_t)) {
1152 return SCTP_DISPOSITION_DISCARD;
1153 }
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001156 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001159 if (unlikely(!link)) {
1160 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001161 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1162 __func__,
1163 asoc,
1164 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001165 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001166 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1167 __func__,
1168 asoc,
1169 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 return SCTP_DISPOSITION_DISCARD;
1172 }
1173
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001174 /* Validate the 64-bit random nonce. */
1175 if (hbinfo->hb_nonce != link->hb_nonce)
1176 return SCTP_DISPOSITION_DISCARD;
1177
Frank Filz52ccb8e2005-12-22 11:36:46 -08001178 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180 /* Check if the timestamp looks valid. */
1181 if (time_after(hbinfo->sent_at, jiffies) ||
1182 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001183 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1184 "for transport:%p\n", __func__, link);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return SCTP_DISPOSITION_DISCARD;
1187 }
1188
1189 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1190 * the HEARTBEAT should clear the error counter of the
1191 * destination transport address to which the HEARTBEAT was
1192 * sent and mark the destination transport address as active if
1193 * it is not so marked.
1194 */
1195 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1196
1197 return SCTP_DISPOSITION_CONSUME;
1198}
1199
1200/* Helper function to send out an abort for the restart
1201 * condition.
1202 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001203static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 struct sctp_chunk *init,
1205 sctp_cmd_seq_t *commands)
1206{
1207 int len;
1208 struct sctp_packet *pkt;
1209 union sctp_addr_param *addrparm;
1210 struct sctp_errhdr *errhdr;
1211 struct sctp_endpoint *ep;
1212 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1213 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1214
1215 /* Build the error on the stack. We are way to malloc crazy
1216 * throughout the code today.
1217 */
1218 errhdr = (struct sctp_errhdr *)buffer;
1219 addrparm = (union sctp_addr_param *)errhdr->variable;
1220
1221 /* Copy into a parm format. */
1222 len = af->to_addr_param(ssa, addrparm);
1223 len += sizeof(sctp_errhdr_t);
1224
1225 errhdr->cause = SCTP_ERROR_RESTART;
1226 errhdr->length = htons(len);
1227
1228 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001229 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 /* Association is NULL since this may be a restart attack and we
1232 * want to send back the attacker's vtag.
1233 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001234 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (!pkt)
1237 goto out;
1238 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1239
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001240 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 /* Discard the rest of the inbound packet. */
1243 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1244
1245out:
1246 /* Even if there is no memory, treat as a failure so
1247 * the packet will get dropped.
1248 */
1249 return 0;
1250}
1251
Joe Perches123031c2010-09-08 11:04:21 +00001252static bool list_has_sctp_addr(const struct list_head *list,
1253 union sctp_addr *ipaddr)
1254{
1255 struct sctp_transport *addr;
1256
1257 list_for_each_entry(addr, list, transports) {
1258 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1259 return true;
1260 }
1261
1262 return false;
1263}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/* A restart is occurring, check to make sure no new addresses
1265 * are being added as we may be under a takeover attack.
1266 */
1267static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1268 const struct sctp_association *asoc,
1269 struct sctp_chunk *init,
1270 sctp_cmd_seq_t *commands)
1271{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001272 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001273 struct sctp_transport *new_addr;
1274 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Joe Perches123031c2010-09-08 11:04:21 +00001276 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 * ...
1278 * Before responding the endpoint MUST check to see if the
1279 * unexpected INIT adds new addresses to the association. If new
1280 * addresses are added to the association, the endpoint MUST respond
1281 * with an ABORT..
1282 */
1283
1284 /* Search through all current addresses and make sure
1285 * we aren't adding any new ones.
1286 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001287 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001288 transports) {
1289 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1290 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001291 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001292 commands);
1293 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 break;
Joe Perches123031c2010-09-08 11:04:21 +00001295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
1298 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001299 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302/* Populate the verification/tie tags based on overlapping INIT
1303 * scenario.
1304 *
1305 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1306 */
1307static void sctp_tietags_populate(struct sctp_association *new_asoc,
1308 const struct sctp_association *asoc)
1309{
1310 switch (asoc->state) {
1311
1312 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1313
1314 case SCTP_STATE_COOKIE_WAIT:
1315 new_asoc->c.my_vtag = asoc->c.my_vtag;
1316 new_asoc->c.my_ttag = asoc->c.my_vtag;
1317 new_asoc->c.peer_ttag = 0;
1318 break;
1319
1320 case SCTP_STATE_COOKIE_ECHOED:
1321 new_asoc->c.my_vtag = asoc->c.my_vtag;
1322 new_asoc->c.my_ttag = asoc->c.my_vtag;
1323 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1324 break;
1325
1326 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1327 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1328 */
1329 default:
1330 new_asoc->c.my_ttag = asoc->c.my_vtag;
1331 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1332 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /* Other parameters for the endpoint SHOULD be copied from the
1336 * existing parameters of the association (e.g. number of
1337 * outbound streams) into the INIT ACK and cookie.
1338 */
1339 new_asoc->rwnd = asoc->rwnd;
1340 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1341 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1342 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1343}
1344
1345/*
1346 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1347 * handling action.
1348 *
1349 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1350 *
1351 * Returns value representing action to be taken. These action values
1352 * correspond to Action/Description values in RFC 2960, Table 2.
1353 */
1354static char sctp_tietags_compare(struct sctp_association *new_asoc,
1355 const struct sctp_association *asoc)
1356{
1357 /* In this case, the peer may have restarted. */
1358 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1359 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1360 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1361 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1362 return 'A';
1363
1364 /* Collision case B. */
1365 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1366 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1367 (0 == asoc->c.peer_vtag))) {
1368 return 'B';
1369 }
1370
1371 /* Collision case D. */
1372 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1373 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1374 return 'D';
1375
1376 /* Collision case C. */
1377 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1378 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1379 (0 == new_asoc->c.my_ttag) &&
1380 (0 == new_asoc->c.peer_ttag))
1381 return 'C';
1382
1383 /* No match to any of the special cases; discard this packet. */
1384 return 'E';
1385}
1386
1387/* Common helper routine for both duplicate and simulataneous INIT
1388 * chunk handling.
1389 */
1390static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001391 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 const struct sctp_endpoint *ep,
1393 const struct sctp_association *asoc,
1394 const sctp_subtype_t type,
1395 void *arg, sctp_cmd_seq_t *commands)
1396{
1397 sctp_disposition_t retval;
1398 struct sctp_chunk *chunk = arg;
1399 struct sctp_chunk *repl;
1400 struct sctp_association *new_asoc;
1401 struct sctp_chunk *err_chunk;
1402 struct sctp_packet *packet;
1403 sctp_unrecognized_param_t *unk_param;
1404 int len;
1405
1406 /* 6.10 Bundling
1407 * An endpoint MUST NOT bundle INIT, INIT ACK or
1408 * SHUTDOWN COMPLETE with any other chunks.
1409 *
1410 * IG Section 2.11.2
1411 * Furthermore, we require that the receiver of an INIT chunk MUST
1412 * enforce these rules by silently discarding an arriving packet
1413 * with an INIT chunk that is bundled with other chunks.
1414 */
1415 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001416 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001419 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 */
1421 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001422 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 /* Make sure that the INIT chunk has a valid length.
1425 * In this case, we generate a protocol violation since we have
1426 * an association established.
1427 */
1428 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001429 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 commands);
1431 /* Grab the INIT header. */
1432 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1433
1434 /* Tag the variable length parameters. */
1435 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1436
1437 /* Verify the INIT chunk before processing it. */
1438 err_chunk = NULL;
Vlad Yasevichb14878c2014-04-17 17:26:50 +02001439 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1441 &err_chunk)) {
1442 /* This chunk contains fatal error. It is to be discarded.
1443 * Send an ABORT, with causes if there is any.
1444 */
1445 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001446 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 (__u8 *)(err_chunk->chunk_hdr) +
1448 sizeof(sctp_chunkhdr_t),
1449 ntohs(err_chunk->chunk_hdr->length) -
1450 sizeof(sctp_chunkhdr_t));
1451
1452 if (packet) {
1453 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1454 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001455 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 retval = SCTP_DISPOSITION_CONSUME;
1457 } else {
1458 retval = SCTP_DISPOSITION_NOMEM;
1459 }
1460 goto cleanup;
1461 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001462 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 commands);
1464 }
1465 }
1466
1467 /*
1468 * Other parameters for the endpoint SHOULD be copied from the
1469 * existing parameters of the association (e.g. number of
1470 * outbound streams) into the INIT ACK and cookie.
1471 * FIXME: We are copying parameters from the endpoint not the
1472 * association.
1473 */
1474 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1475 if (!new_asoc)
1476 goto nomem;
1477
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001478 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1479 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1480 goto nomem;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /* In the outbound INIT ACK the endpoint MUST copy its current
1483 * Verification Tag and Peers Verification tag into a reserved
1484 * place (local tie-tag and per tie-tag) within the state cookie.
1485 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001486 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001488 GFP_ATOMIC))
1489 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /* Make sure no new addresses are being added during the
1492 * restart. Do not do this check for COOKIE-WAIT state,
1493 * since there are no peer addresses to check against.
1494 * Upon return an ABORT will have been sent if needed.
1495 */
1496 if (!sctp_state(asoc, COOKIE_WAIT)) {
1497 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1498 commands)) {
1499 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001500 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502 }
1503
1504 sctp_tietags_populate(new_asoc, asoc);
1505
1506 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1507
1508 /* If there are errors need to be reported for unknown parameters,
1509 * make sure to reserve enough room in the INIT ACK for them.
1510 */
1511 len = 0;
1512 if (err_chunk) {
1513 len = ntohs(err_chunk->chunk_hdr->length) -
1514 sizeof(sctp_chunkhdr_t);
1515 }
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1518 if (!repl)
1519 goto nomem;
1520
1521 /* If there are errors need to be reported for unknown parameters,
1522 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1523 * parameter.
1524 */
1525 if (err_chunk) {
1526 /* Get the "Unrecognized parameter" parameter(s) out of the
1527 * ERROR chunk generated by sctp_verify_init(). Since the
1528 * error cause code for "unknown parameter" and the
1529 * "Unrecognized parameter" type is the same, we can
1530 * construct the parameters in INIT ACK by copying the
1531 * ERROR causes over.
1532 */
1533 unk_param = (sctp_unrecognized_param_t *)
1534 ((__u8 *)(err_chunk->chunk_hdr) +
1535 sizeof(sctp_chunkhdr_t));
1536 /* Replace the cause code with the "Unrecognized parameter"
1537 * parameter type.
1538 */
1539 sctp_addto_chunk(repl, len, unk_param);
1540 }
1541
1542 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1543 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1544
1545 /*
1546 * Note: After sending out INIT ACK with the State Cookie parameter,
1547 * "Z" MUST NOT allocate any resources for this new association.
1548 * Otherwise, "Z" will be vulnerable to resource attacks.
1549 */
1550 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1551 retval = SCTP_DISPOSITION_CONSUME;
1552
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001553 return retval;
1554
1555nomem:
1556 retval = SCTP_DISPOSITION_NOMEM;
1557nomem_retval:
1558 if (new_asoc)
1559 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560cleanup:
1561 if (err_chunk)
1562 sctp_chunk_free(err_chunk);
1563 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001567 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 * This means we started an INIT and then we got an INIT request from
1569 * our peer.
1570 *
1571 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1572 * This usually indicates an initialization collision, i.e., each
1573 * endpoint is attempting, at about the same time, to establish an
1574 * association with the other endpoint.
1575 *
1576 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1577 * endpoint MUST respond with an INIT ACK using the same parameters it
1578 * sent in its original INIT chunk (including its Verification Tag,
1579 * unchanged). These original parameters are combined with those from the
1580 * newly received INIT chunk. The endpoint shall also generate a State
1581 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1582 * INIT to calculate the State Cookie.
1583 *
1584 * After that, the endpoint MUST NOT change its state, the T1-init
1585 * timer shall be left running and the corresponding TCB MUST NOT be
1586 * destroyed. The normal procedures for handling State Cookies when
1587 * a TCB exists will resolve the duplicate INITs to a single association.
1588 *
1589 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1590 * its Tie-Tags with the Tag information of itself and its peer (see
1591 * section 5.2.2 for a description of the Tie-Tags).
1592 *
1593 * Verification Tag: Not explicit, but an INIT can not have a valid
1594 * verification tag, so we skip the check.
1595 *
1596 * Inputs
1597 * (endpoint, asoc, chunk)
1598 *
1599 * Outputs
1600 * (asoc, reply_msg, msg_up, timers, counters)
1601 *
1602 * The return value is the disposition of the chunk.
1603 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001604sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1605 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 const struct sctp_association *asoc,
1607 const sctp_subtype_t type,
1608 void *arg,
1609 sctp_cmd_seq_t *commands)
1610{
1611 /* Call helper to do the real work for both simulataneous and
1612 * duplicate INIT chunk handling.
1613 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001614 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615}
1616
1617/*
1618 * Handle duplicated INIT messages. These are usually delayed
1619 * restransmissions.
1620 *
1621 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1622 * COOKIE-ECHOED and COOKIE-WAIT
1623 *
1624 * Unless otherwise stated, upon reception of an unexpected INIT for
1625 * this association, the endpoint shall generate an INIT ACK with a
1626 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1627 * current Verification Tag and peer's Verification Tag into a reserved
1628 * place within the state cookie. We shall refer to these locations as
1629 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1630 * containing this INIT ACK MUST carry a Verification Tag value equal to
1631 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1632 * MUST contain a new Initiation Tag (randomly generated see Section
1633 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1634 * existing parameters of the association (e.g. number of outbound
1635 * streams) into the INIT ACK and cookie.
1636 *
1637 * After sending out the INIT ACK, the endpoint shall take no further
1638 * actions, i.e., the existing association, including its current state,
1639 * and the corresponding TCB MUST NOT be changed.
1640 *
1641 * Note: Only when a TCB exists and the association is not in a COOKIE-
1642 * WAIT state are the Tie-Tags populated. For a normal association INIT
1643 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1644 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1645 * State Cookie are populated as specified in section 5.2.1.
1646 *
1647 * Verification Tag: Not specified, but an INIT has no way of knowing
1648 * what the verification tag could be, so we ignore it.
1649 *
1650 * Inputs
1651 * (endpoint, asoc, chunk)
1652 *
1653 * Outputs
1654 * (asoc, reply_msg, msg_up, timers, counters)
1655 *
1656 * The return value is the disposition of the chunk.
1657 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001658sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1659 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 const struct sctp_association *asoc,
1661 const sctp_subtype_t type,
1662 void *arg,
1663 sctp_cmd_seq_t *commands)
1664{
1665 /* Call helper to do the real work for both simulataneous and
1666 * duplicate INIT chunk handling.
1667 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001668 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
1671
Vlad Yasevich610ab732007-01-15 19:18:30 -08001672/*
1673 * Unexpected INIT-ACK handler.
1674 *
1675 * Section 5.2.3
1676 * If an INIT ACK received by an endpoint in any state other than the
1677 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1678 * An unexpected INIT ACK usually indicates the processing of an old or
1679 * duplicated INIT chunk.
1680*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001681sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1682 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001683 const struct sctp_association *asoc,
1684 const sctp_subtype_t type,
1685 void *arg, sctp_cmd_seq_t *commands)
1686{
1687 /* Per the above section, we'll discard the chunk if we have an
1688 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1689 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001690 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001691 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001692 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001693 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1697 *
1698 * Section 5.2.4
1699 * A) In this case, the peer may have restarted.
1700 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001701static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1702 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 const struct sctp_association *asoc,
1704 struct sctp_chunk *chunk,
1705 sctp_cmd_seq_t *commands,
1706 struct sctp_association *new_asoc)
1707{
1708 sctp_init_chunk_t *peer_init;
1709 struct sctp_ulpevent *ev;
1710 struct sctp_chunk *repl;
1711 struct sctp_chunk *err;
1712 sctp_disposition_t disposition;
1713
1714 /* new_asoc is a brand-new association, so these are not yet
1715 * side effects--it is safe to run them here.
1716 */
1717 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1718
Wei Yongjunde6becd2011-04-19 21:30:51 +00001719 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 GFP_ATOMIC))
1721 goto nomem;
1722
Xin Long0e67ad52018-05-05 14:59:47 +08001723 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1724 return SCTP_DISPOSITION_DISCARD;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* Make sure no new addresses are being added during the
1727 * restart. Though this is a pretty complicated attack
1728 * since you'd have to get inside the cookie.
1729 */
Xin Long0e67ad52018-05-05 14:59:47 +08001730 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1734 * the peer has restarted (Action A), it MUST NOT setup a new
1735 * association but instead resend the SHUTDOWN ACK and send an ERROR
1736 * chunk with a "Cookie Received while Shutting Down" error cause to
1737 * its peer.
1738 */
1739 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001740 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1742 chunk, commands);
1743 if (SCTP_DISPOSITION_NOMEM == disposition)
1744 goto nomem;
1745
1746 err = sctp_make_op_error(asoc, chunk,
1747 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001748 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 if (err)
1750 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1751 SCTP_CHUNK(err));
1752
1753 return SCTP_DISPOSITION_CONSUME;
1754 }
1755
Wei Yongjuna000c012011-05-29 23:23:36 +00001756 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1757 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001759 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1760 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1761 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1763
Wei Yongjuna000c012011-05-29 23:23:36 +00001764 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1765 * and ASCONF-ACK cache.
1766 */
1767 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1768 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1769 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 repl = sctp_make_cookie_ack(new_asoc, chunk);
1772 if (!repl)
1773 goto nomem;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 /* Report association restart to upper layer. */
1776 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1777 new_asoc->c.sinit_num_ostreams,
1778 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001779 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (!ev)
1781 goto nomem_ev;
1782
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001783 /* Update the content of current association. */
1784 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevichbdf6fa52014-10-03 18:16:20 -04001786 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1787 (sctp_sstate(asoc->base.sk, CLOSING) ||
1788 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1789 /* if were currently in SHUTDOWN_PENDING, but the socket
1790 * has been closed by user, don't transition to ESTABLISHED.
1791 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1792 */
1793 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1794 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1795 SCTP_ST_CHUNK(0), NULL,
1796 commands);
1797 } else {
1798 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1799 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1800 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return SCTP_DISPOSITION_CONSUME;
1803
1804nomem_ev:
1805 sctp_chunk_free(repl);
1806nomem:
1807 return SCTP_DISPOSITION_NOMEM;
1808}
1809
1810/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1811 *
1812 * Section 5.2.4
1813 * B) In this case, both sides may be attempting to start an association
1814 * at about the same time but the peer endpoint started its INIT
1815 * after responding to the local endpoint's INIT
1816 */
1817/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001818static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1819 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 const struct sctp_association *asoc,
1821 struct sctp_chunk *chunk,
1822 sctp_cmd_seq_t *commands,
1823 struct sctp_association *new_asoc)
1824{
1825 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct sctp_chunk *repl;
1827
1828 /* new_asoc is a brand-new association, so these are not yet
1829 * side effects--it is safe to run them here.
1830 */
1831 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001832 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 GFP_ATOMIC))
1834 goto nomem;
1835
Xin Long0e67ad52018-05-05 14:59:47 +08001836 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1837 return SCTP_DISPOSITION_DISCARD;
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* Update the content of current association. */
1840 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1841 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1842 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001843 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1845
1846 repl = sctp_make_cookie_ack(new_asoc, chunk);
1847 if (!repl)
1848 goto nomem;
1849
1850 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* RFC 2960 5.1 Normal Establishment of an Association
1853 *
1854 * D) IMPLEMENTATION NOTE: An implementation may choose to
1855 * send the Communication Up notification to the SCTP user
1856 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001857 *
1858 * Sadly, this needs to be implemented as a side-effect, because
1859 * we are not guaranteed to have set the association id of the real
1860 * association and so these notifications need to be delayed until
1861 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Vlad Yasevich07d93962007-05-04 13:55:27 -07001864 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001867 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001869 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001870 *
1871 * This also needs to be done as a side effect for the same reason as
1872 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001874 if (asoc->peer.adaptation_ind)
1875 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 return SCTP_DISPOSITION_CONSUME;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879nomem:
1880 return SCTP_DISPOSITION_NOMEM;
1881}
1882
1883/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1884 *
1885 * Section 5.2.4
1886 * C) In this case, the local endpoint's cookie has arrived late.
1887 * Before it arrived, the local endpoint sent an INIT and received an
1888 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1889 * but a new tag of its own.
1890 */
1891/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001892static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1893 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 const struct sctp_association *asoc,
1895 struct sctp_chunk *chunk,
1896 sctp_cmd_seq_t *commands,
1897 struct sctp_association *new_asoc)
1898{
1899 /* The cookie should be silently discarded.
1900 * The endpoint SHOULD NOT change states and should leave
1901 * any timers running.
1902 */
1903 return SCTP_DISPOSITION_DISCARD;
1904}
1905
1906/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1907 *
1908 * Section 5.2.4
1909 *
1910 * D) When both local and remote tags match the endpoint should always
1911 * enter the ESTABLISHED state, if it has not already done so.
1912 */
1913/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001914static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1915 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 const struct sctp_association *asoc,
1917 struct sctp_chunk *chunk,
1918 sctp_cmd_seq_t *commands,
1919 struct sctp_association *new_asoc)
1920{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001921 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 struct sctp_chunk *repl;
1923
1924 /* Clarification from Implementor's Guide:
1925 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001926 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1927 * It should stop any cookie timer that may be running and send
1928 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 */
1930
Xin Long0e67ad52018-05-05 14:59:47 +08001931 if (!sctp_auth_chunk_verify(net, chunk, asoc))
1932 return SCTP_DISPOSITION_DISCARD;
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /* Don't accidentally move back into established state. */
1935 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1936 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1937 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1938 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1939 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001940 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1942 SCTP_NULL());
1943
1944 /* RFC 2960 5.1 Normal Establishment of an Association
1945 *
1946 * D) IMPLEMENTATION NOTE: An implementation may choose
1947 * to send the Communication Up notification to the
1948 * SCTP user upon reception of a valid COOKIE
1949 * ECHO chunk.
1950 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001951 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001953 asoc->c.sinit_num_ostreams,
1954 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001955 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (!ev)
1957 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001960 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001962 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001964 if (asoc->peer.adaptation_ind) {
1965 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001967 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 goto nomem;
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Xin Longc832ac42018-05-02 13:39:46 +08001973 repl = sctp_make_cookie_ack(asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!repl)
1975 goto nomem;
1976
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001977 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1978
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001979 if (ev)
1980 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1981 SCTP_ULPEVENT(ev));
1982 if (ai_ev)
1983 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1984 SCTP_ULPEVENT(ai_ev));
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 return SCTP_DISPOSITION_CONSUME;
1987
1988nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001989 if (ai_ev)
1990 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (ev)
1992 sctp_ulpevent_free(ev);
1993 return SCTP_DISPOSITION_NOMEM;
1994}
1995
1996/*
1997 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1998 * chunk was retransmitted and then delayed in the network.
1999 *
2000 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2001 *
2002 * Verification Tag: None. Do cookie validation.
2003 *
2004 * Inputs
2005 * (endpoint, asoc, chunk)
2006 *
2007 * Outputs
2008 * (asoc, reply_msg, msg_up, timers, counters)
2009 *
2010 * The return value is the disposition of the chunk.
2011 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002012sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2013 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 const struct sctp_association *asoc,
2015 const sctp_subtype_t type,
2016 void *arg,
2017 sctp_cmd_seq_t *commands)
2018{
2019 sctp_disposition_t retval;
2020 struct sctp_chunk *chunk = arg;
2021 struct sctp_association *new_asoc;
2022 int error = 0;
2023 char action;
2024 struct sctp_chunk *err_chk_p;
2025
2026 /* Make sure that the chunk has a valid length from the protocol
2027 * perspective. In this case check to make sure we have at least
2028 * enough for the chunk header. Cookie length verification is
2029 * done later.
2030 */
2031 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002032 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 commands);
2034
2035 /* "Decode" the chunk. We have no optional parameters so we
2036 * are in good shape.
2037 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002038 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002039 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2040 sizeof(sctp_chunkhdr_t)))
2041 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2044 * of a duplicate COOKIE ECHO match the Verification Tags of the
2045 * current association, consider the State Cookie valid even if
2046 * the lifespan is exceeded.
2047 */
2048 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2049 &err_chk_p);
2050
2051 /* FIXME:
2052 * If the re-build failed, what is the proper error path
2053 * from here?
2054 *
2055 * [We should abort the association. --piggy]
2056 */
2057 if (!new_asoc) {
2058 /* FIXME: Several errors are possible. A bad cookie should
2059 * be silently discarded, but think about logging it too.
2060 */
2061 switch (error) {
2062 case -SCTP_IERROR_NOMEM:
2063 goto nomem;
2064
2065 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002066 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002068 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 case -SCTP_IERROR_BAD_SIG:
2070 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002071 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
2074
2075 /* Compare the tie_tag in cookie with the verification tag of
2076 * current association.
2077 */
2078 action = sctp_tietags_compare(new_asoc, asoc);
2079
2080 switch (action) {
2081 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002082 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 new_asoc);
2084 break;
2085
2086 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002087 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 new_asoc);
2089 break;
2090
2091 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002092 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 new_asoc);
2094 break;
2095
2096 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002097 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 new_asoc);
2099 break;
2100
2101 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002102 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002107 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2109
Max Matveevd5ccd492011-08-29 21:02:24 +00002110 /* Restore association pointer to provide SCTP command interpeter
2111 * with a valid context in case it needs to manipulate
2112 * the queues */
2113 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2114 SCTP_ASOC((struct sctp_association *)asoc));
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return retval;
2117
2118nomem:
2119 return SCTP_DISPOSITION_NOMEM;
2120}
2121
2122/*
2123 * Process an ABORT. (SHUTDOWN-PENDING state)
2124 *
2125 * See sctp_sf_do_9_1_abort().
2126 */
2127sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002128 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 const struct sctp_endpoint *ep,
2130 const struct sctp_association *asoc,
2131 const sctp_subtype_t type,
2132 void *arg,
2133 sctp_cmd_seq_t *commands)
2134{
2135 struct sctp_chunk *chunk = arg;
2136
2137 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002138 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 /* Make sure that the ABORT chunk has a valid length.
2141 * Since this is an ABORT chunk, we have to discard it
2142 * because of the following text:
2143 * RFC 2960, Section 3.3.7
2144 * If an endpoint receives an ABORT with a format error or for an
2145 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002146 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 * as we do not know its true length. So, to be safe, discard the
2148 * packet.
2149 */
2150 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002151 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Vlad Yasevich75205f42007-12-20 14:12:59 -08002153 /* ADD-IP: Special case for ABORT chunks
2154 * F4) One special consideration is that ABORT Chunks arriving
2155 * destined to the IP address being deleted MUST be
2156 * ignored (see Section 5.3.1 for further details).
2157 */
2158 if (SCTP_ADDR_DEL ==
2159 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002160 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002161
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002162 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
2165/*
2166 * Process an ABORT. (SHUTDOWN-SENT state)
2167 *
2168 * See sctp_sf_do_9_1_abort().
2169 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002170sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2171 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 const struct sctp_association *asoc,
2173 const sctp_subtype_t type,
2174 void *arg,
2175 sctp_cmd_seq_t *commands)
2176{
2177 struct sctp_chunk *chunk = arg;
2178
2179 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002180 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* Make sure that the ABORT chunk has a valid length.
2183 * Since this is an ABORT chunk, we have to discard it
2184 * because of the following text:
2185 * RFC 2960, Section 3.3.7
2186 * If an endpoint receives an ABORT with a format error or for an
2187 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002188 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 * as we do not know its true length. So, to be safe, discard the
2190 * packet.
2191 */
2192 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002193 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Vlad Yasevich75205f42007-12-20 14:12:59 -08002195 /* ADD-IP: Special case for ABORT chunks
2196 * F4) One special consideration is that ABORT Chunks arriving
2197 * destined to the IP address being deleted MUST be
2198 * ignored (see Section 5.3.1 for further details).
2199 */
2200 if (SCTP_ADDR_DEL ==
2201 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002202 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* Stop the T2-shutdown timer. */
2205 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2206 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2207
2208 /* Stop the T5-shutdown guard timer. */
2209 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2210 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2211
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002212 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215/*
2216 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2217 *
2218 * See sctp_sf_do_9_1_abort().
2219 */
2220sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002221 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 const struct sctp_endpoint *ep,
2223 const struct sctp_association *asoc,
2224 const sctp_subtype_t type,
2225 void *arg,
2226 sctp_cmd_seq_t *commands)
2227{
2228 /* The same T2 timer, so we should be able to use
2229 * common function with the SHUTDOWN-SENT state.
2230 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002231 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234/*
2235 * Handle an Error received in COOKIE_ECHOED state.
2236 *
2237 * Only handle the error type of stale COOKIE Error, the other errors will
2238 * be ignored.
2239 *
2240 * Inputs
2241 * (endpoint, asoc, chunk)
2242 *
2243 * Outputs
2244 * (asoc, reply_msg, msg_up, timers, counters)
2245 *
2246 * The return value is the disposition of the chunk.
2247 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002248sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2249 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 const struct sctp_association *asoc,
2251 const sctp_subtype_t type,
2252 void *arg,
2253 sctp_cmd_seq_t *commands)
2254{
2255 struct sctp_chunk *chunk = arg;
2256 sctp_errhdr_t *err;
2257
2258 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002259 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 /* Make sure that the ERROR chunk has a valid length.
2262 * The parameter walking depends on this as well.
2263 */
2264 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002265 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 commands);
2267
2268 /* Process the error here */
2269 /* FUTURE FIXME: When PR-SCTP related and other optional
2270 * parms are emitted, this will have to change to handle multiple
2271 * errors.
2272 */
2273 sctp_walk_errors(err, chunk->chunk_hdr) {
2274 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002275 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 arg, commands);
2277 }
2278
2279 /* It is possible to have malformed error causes, and that
2280 * will cause us to end the walk early. However, since
2281 * we are discarding the packet, there should be no adverse
2282 * affects.
2283 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002284 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
2287/*
2288 * Handle a Stale COOKIE Error
2289 *
2290 * Section: 5.2.6 Handle Stale COOKIE Error
2291 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2292 * one of the following three alternatives.
2293 * ...
2294 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2295 * Preservative parameter requesting an extension to the lifetime of
2296 * the State Cookie. When calculating the time extension, an
2297 * implementation SHOULD use the RTT information measured based on the
2298 * previous COOKIE ECHO / ERROR exchange, and should add no more
2299 * than 1 second beyond the measured RTT, due to long State Cookie
2300 * lifetimes making the endpoint more subject to a replay attack.
2301 *
2302 * Verification Tag: Not explicit, but safe to ignore.
2303 *
2304 * Inputs
2305 * (endpoint, asoc, chunk)
2306 *
2307 * Outputs
2308 * (asoc, reply_msg, msg_up, timers, counters)
2309 *
2310 * The return value is the disposition of the chunk.
2311 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002312static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2313 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 const struct sctp_association *asoc,
2315 const sctp_subtype_t type,
2316 void *arg,
2317 sctp_cmd_seq_t *commands)
2318{
2319 struct sctp_chunk *chunk = arg;
Arnd Bergmann3ef0a252015-09-30 13:26:40 +02002320 u32 stale;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 sctp_cookie_preserve_param_t bht;
2322 sctp_errhdr_t *err;
2323 struct sctp_chunk *reply;
2324 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002325 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Vlad Yasevich81845c22006-01-30 15:59:54 -08002327 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002328 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2329 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002331 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 return SCTP_DISPOSITION_DELETE_TCB;
2333 }
2334
2335 err = (sctp_errhdr_t *)(chunk->skb->data);
2336
2337 /* When calculating the time extension, an implementation
2338 * SHOULD use the RTT information measured based on the
2339 * previous COOKIE ECHO / ERROR exchange, and should add no
2340 * more than 1 second beyond the measured RTT, due to long
2341 * State Cookie lifetimes making the endpoint more subject to
2342 * a replay attack.
2343 * Measure of Staleness's unit is usec. (1/1000000 sec)
2344 * Suggested Cookie Life-span Increment's unit is msec.
2345 * (1/1000 sec)
2346 * In general, if you use the suggested cookie life, the value
2347 * found in the field of measure of staleness should be doubled
2348 * to give ample time to retransmit the new cookie and thus
2349 * yield a higher probability of success on the reattempt.
2350 */
Al Viro34bcca22006-11-20 17:27:15 -08002351 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 stale = (stale * 2) / 1000;
2353
2354 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2355 bht.param_hdr.length = htons(sizeof(bht));
2356 bht.lifespan_increment = htonl(stale);
2357
2358 /* Build that new INIT chunk. */
2359 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2360 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2361 if (!reply)
2362 goto nomem;
2363
2364 sctp_addto_chunk(reply, sizeof(bht), &bht);
2365
2366 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2367 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2368
2369 /* Stop pending T3-rtx and heartbeat timers */
2370 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2371 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2372
2373 /* Delete non-primary peer ip addresses since we are transitioning
2374 * back to the COOKIE-WAIT state
2375 */
2376 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2377
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002378 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2379 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002381 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 SCTP_TRANSPORT(asoc->peer.primary_path));
2383
2384 /* Cast away the const modifier, as we want to just
2385 * rerun it through as a sideffect.
2386 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002387 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2390 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2391 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2392 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2393 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2394 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2395
2396 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2397
2398 return SCTP_DISPOSITION_CONSUME;
2399
2400nomem:
2401 return SCTP_DISPOSITION_NOMEM;
2402}
2403
2404/*
2405 * Process an ABORT.
2406 *
2407 * Section: 9.1
2408 * After checking the Verification Tag, the receiving endpoint shall
2409 * remove the association from its record, and shall report the
2410 * termination to its upper layer.
2411 *
2412 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2413 * B) Rules for packet carrying ABORT:
2414 *
2415 * - The endpoint shall always fill in the Verification Tag field of the
2416 * outbound packet with the destination endpoint's tag value if it
2417 * is known.
2418 *
2419 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2420 * MUST follow the procedure described in Section 8.4.
2421 *
2422 * - The receiver MUST accept the packet if the Verification Tag
2423 * matches either its own tag, OR the tag of its peer. Otherwise, the
2424 * receiver MUST silently discard the packet and take no further
2425 * action.
2426 *
2427 * Inputs
2428 * (endpoint, asoc, chunk)
2429 *
2430 * Outputs
2431 * (asoc, reply_msg, msg_up, timers, counters)
2432 *
2433 * The return value is the disposition of the chunk.
2434 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002435sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2436 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 const struct sctp_association *asoc,
2438 const sctp_subtype_t type,
2439 void *arg,
2440 sctp_cmd_seq_t *commands)
2441{
2442 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002445 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 /* Make sure that the ABORT chunk has a valid length.
2448 * Since this is an ABORT chunk, we have to discard it
2449 * because of the following text:
2450 * RFC 2960, Section 3.3.7
2451 * If an endpoint receives an ABORT with a format error or for an
2452 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002453 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 * as we do not know its true length. So, to be safe, discard the
2455 * packet.
2456 */
2457 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002458 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459
Vlad Yasevich75205f42007-12-20 14:12:59 -08002460 /* ADD-IP: Special case for ABORT chunks
2461 * F4) One special consideration is that ABORT Chunks arriving
2462 * destined to the IP address being deleted MUST be
2463 * ignored (see Section 5.3.1 for further details).
2464 */
2465 if (SCTP_ADDR_DEL ==
2466 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002467 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002468
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002469 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002470}
2471
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002472static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2473 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002474 const struct sctp_association *asoc,
2475 const sctp_subtype_t type,
2476 void *arg,
2477 sctp_cmd_seq_t *commands)
2478{
2479 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002480 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002481 __be16 error = SCTP_ERROR_NO_ERROR;
2482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /* See if we have an error cause code in the chunk. */
2484 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002485 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2486
2487 sctp_errhdr_t *err;
2488 sctp_walk_errors(err, chunk->chunk_hdr);
2489 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002490 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002495 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002496 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002497 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002498 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2499 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 return SCTP_DISPOSITION_ABORT;
2502}
2503
2504/*
2505 * Process an ABORT. (COOKIE-WAIT state)
2506 *
2507 * See sctp_sf_do_9_1_abort() above.
2508 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002509sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2510 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 const struct sctp_association *asoc,
2512 const sctp_subtype_t type,
2513 void *arg,
2514 sctp_cmd_seq_t *commands)
2515{
2516 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002517 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002518 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002521 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 /* Make sure that the ABORT chunk has a valid length.
2524 * Since this is an ABORT chunk, we have to discard it
2525 * because of the following text:
2526 * RFC 2960, Section 3.3.7
2527 * If an endpoint receives an ABORT with a format error or for an
2528 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002529 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 * as we do not know its true length. So, to be safe, discard the
2531 * packet.
2532 */
2533 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002534 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 /* See if we have an error cause code in the chunk. */
2537 len = ntohs(chunk->chunk_hdr->length);
2538 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2539 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2540
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002541 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002542 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543}
2544
2545/*
2546 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2547 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002548sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2549 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 const struct sctp_association *asoc,
2551 const sctp_subtype_t type,
2552 void *arg,
2553 sctp_cmd_seq_t *commands)
2554{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002555 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002556 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002557 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560/*
2561 * Process an ABORT. (COOKIE-ECHOED state)
2562 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002563sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2564 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 const struct sctp_association *asoc,
2566 const sctp_subtype_t type,
2567 void *arg,
2568 sctp_cmd_seq_t *commands)
2569{
2570 /* There is a single T1 timer, so we should be able to use
2571 * common function with the COOKIE-WAIT state.
2572 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002573 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576/*
2577 * Stop T1 timer and abort association with "INIT failed".
2578 *
2579 * This is common code called by several sctp_sf_*_abort() functions above.
2580 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002581static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2582 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002583 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002584 const struct sctp_association *asoc,
2585 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002587 pr_debug("%s: ABORT received (INIT)\n", __func__);
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2590 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002591 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2593 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002594 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 /* CMD_INIT_FAILED will DELETE_TCB. */
2596 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002597 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002598
Frank Filz3f7a87d2005-06-20 13:14:57 -07002599 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600}
2601
2602/*
2603 * sctp_sf_do_9_2_shut
2604 *
2605 * Section: 9.2
2606 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2607 * - enter the SHUTDOWN-RECEIVED state,
2608 *
2609 * - stop accepting new data from its SCTP user
2610 *
2611 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2612 * that all its outstanding DATA chunks have been received by the
2613 * SHUTDOWN sender.
2614 *
2615 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2616 * send a SHUTDOWN in response to a ULP request. And should discard
2617 * subsequent SHUTDOWN chunks.
2618 *
2619 * If there are still outstanding DATA chunks left, the SHUTDOWN
2620 * receiver shall continue to follow normal data transmission
2621 * procedures defined in Section 6 until all outstanding DATA chunks
2622 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2623 * new data from its SCTP user.
2624 *
2625 * Verification Tag: 8.5 Verification Tag [Normal verification]
2626 *
2627 * Inputs
2628 * (endpoint, asoc, chunk)
2629 *
2630 * Outputs
2631 * (asoc, reply_msg, msg_up, timers, counters)
2632 *
2633 * The return value is the disposition of the chunk.
2634 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002635sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2636 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 const struct sctp_association *asoc,
2638 const sctp_subtype_t type,
2639 void *arg,
2640 sctp_cmd_seq_t *commands)
2641{
2642 struct sctp_chunk *chunk = arg;
2643 sctp_shutdownhdr_t *sdh;
2644 sctp_disposition_t disposition;
2645 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002646 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002649 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 /* Make sure that the SHUTDOWN chunk has a valid length. */
2652 if (!sctp_chunk_length_valid(chunk,
2653 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002654 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 commands);
2656
2657 /* Convert the elaborate header. */
2658 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2659 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2660 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002661 ctsn = ntohl(sdh->cum_tsn_ack);
2662
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002663 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002664 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2665 asoc->ctsn_ack_point);
2666
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002667 return SCTP_DISPOSITION_DISCARD;
2668 }
2669
Wei Yongjundf10eec2008-10-23 01:00:21 -07002670 /* If Cumulative TSN Ack beyond the max tsn currently
2671 * send, terminating the association and respond to the
2672 * sender with an ABORT.
2673 */
2674 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002675 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002677 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2678 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2679 * inform the application that it should cease sending data.
2680 */
2681 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2682 if (!ev) {
2683 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002684 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002685 }
2686 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2689 * - enter the SHUTDOWN-RECEIVED state,
2690 * - stop accepting new data from its SCTP user
2691 *
2692 * [This is implicit in the new state.]
2693 */
2694 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2695 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2696 disposition = SCTP_DISPOSITION_CONSUME;
2697
2698 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002699 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 arg, commands);
2701 }
2702
2703 if (SCTP_DISPOSITION_NOMEM == disposition)
2704 goto out;
2705
2706 /* - verify, by checking the Cumulative TSN Ack field of the
2707 * chunk, that all its outstanding DATA chunks have been
2708 * received by the SHUTDOWN sender.
2709 */
2710 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002711 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713out:
2714 return disposition;
2715}
2716
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002717/*
2718 * sctp_sf_do_9_2_shut_ctsn
2719 *
2720 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2721 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2722 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2723 * MUST be processed.
2724 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002725sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2726 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002727 const struct sctp_association *asoc,
2728 const sctp_subtype_t type,
2729 void *arg,
2730 sctp_cmd_seq_t *commands)
2731{
2732 struct sctp_chunk *chunk = arg;
2733 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002734 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002735
2736 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002737 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002738
2739 /* Make sure that the SHUTDOWN chunk has a valid length. */
2740 if (!sctp_chunk_length_valid(chunk,
2741 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002742 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002743 commands);
2744
2745 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002746 ctsn = ntohl(sdh->cum_tsn_ack);
2747
2748 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002749 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2750 asoc->ctsn_ack_point);
2751
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002752 return SCTP_DISPOSITION_DISCARD;
2753 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002754
2755 /* If Cumulative TSN Ack beyond the max tsn currently
2756 * send, terminating the association and respond to the
2757 * sender with an ABORT.
2758 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002759 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002760 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002761
2762 /* verify, by checking the Cumulative TSN Ack field of the
2763 * chunk, that all its outstanding DATA chunks have been
2764 * received by the SHUTDOWN sender.
2765 */
2766 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2767 SCTP_BE32(sdh->cum_tsn_ack));
2768
2769 return SCTP_DISPOSITION_CONSUME;
2770}
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772/* RFC 2960 9.2
2773 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2774 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2775 * transport addresses (either in the IP addresses or in the INIT chunk)
2776 * that belong to this association, it should discard the INIT chunk and
2777 * retransmit the SHUTDOWN ACK chunk.
2778 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002779sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2780 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 const struct sctp_association *asoc,
2782 const sctp_subtype_t type,
2783 void *arg,
2784 sctp_cmd_seq_t *commands)
2785{
2786 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2787 struct sctp_chunk *reply;
2788
Vlad Yasevichece25df2007-09-07 16:30:54 -04002789 /* Make sure that the chunk has a valid length */
2790 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002791 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002792 commands);
2793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 /* Since we are not going to really process this INIT, there
2795 * is no point in verifying chunk boundries. Just generate
2796 * the SHUTDOWN ACK.
2797 */
2798 reply = sctp_make_shutdown_ack(asoc, chunk);
2799 if (NULL == reply)
2800 goto nomem;
2801
2802 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2803 * the T2-SHUTDOWN timer.
2804 */
2805 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2806
2807 /* and restart the T2-shutdown timer. */
2808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2809 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2810
2811 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2812
2813 return SCTP_DISPOSITION_CONSUME;
2814nomem:
2815 return SCTP_DISPOSITION_NOMEM;
2816}
2817
2818/*
2819 * sctp_sf_do_ecn_cwr
2820 *
2821 * Section: Appendix A: Explicit Congestion Notification
2822 *
2823 * CWR:
2824 *
2825 * RFC 2481 details a specific bit for a sender to send in the header of
2826 * its next outbound TCP segment to indicate to its peer that it has
2827 * reduced its congestion window. This is termed the CWR bit. For
2828 * SCTP the same indication is made by including the CWR chunk.
2829 * This chunk contains one data element, i.e. the TSN number that
2830 * was sent in the ECNE chunk. This element represents the lowest
2831 * TSN number in the datagram that was originally marked with the
2832 * CE bit.
2833 *
2834 * Verification Tag: 8.5 Verification Tag [Normal verification]
2835 * Inputs
2836 * (endpoint, asoc, chunk)
2837 *
2838 * Outputs
2839 * (asoc, reply_msg, msg_up, timers, counters)
2840 *
2841 * The return value is the disposition of the chunk.
2842 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002843sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2844 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 const struct sctp_association *asoc,
2846 const sctp_subtype_t type,
2847 void *arg,
2848 sctp_cmd_seq_t *commands)
2849{
2850 sctp_cwrhdr_t *cwr;
2851 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002852 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002855 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
2857 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002858 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002860
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2862 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2863
Al Viro34bcca22006-11-20 17:27:15 -08002864 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002867 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 /* Stop sending ECNE. */
2869 sctp_add_cmd_sf(commands,
2870 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002871 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873 return SCTP_DISPOSITION_CONSUME;
2874}
2875
2876/*
2877 * sctp_sf_do_ecne
2878 *
2879 * Section: Appendix A: Explicit Congestion Notification
2880 *
2881 * ECN-Echo
2882 *
2883 * RFC 2481 details a specific bit for a receiver to send back in its
2884 * TCP acknowledgements to notify the sender of the Congestion
2885 * Experienced (CE) bit having arrived from the network. For SCTP this
2886 * same indication is made by including the ECNE chunk. This chunk
2887 * contains one data element, i.e. the lowest TSN associated with the IP
2888 * datagram marked with the CE bit.....
2889 *
2890 * Verification Tag: 8.5 Verification Tag [Normal verification]
2891 * Inputs
2892 * (endpoint, asoc, chunk)
2893 *
2894 * Outputs
2895 * (asoc, reply_msg, msg_up, timers, counters)
2896 *
2897 * The return value is the disposition of the chunk.
2898 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002899sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2900 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 const struct sctp_association *asoc,
2902 const sctp_subtype_t type,
2903 void *arg,
2904 sctp_cmd_seq_t *commands)
2905{
2906 sctp_ecnehdr_t *ecne;
2907 struct sctp_chunk *chunk = arg;
2908
2909 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002910 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002913 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 commands);
2915
2916 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2917 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2918
2919 /* If this is a newer ECNE than the last CWR packet we sent out */
2920 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2921 SCTP_U32(ntohl(ecne->lowest_tsn)));
2922
2923 return SCTP_DISPOSITION_CONSUME;
2924}
2925
2926/*
2927 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2928 *
2929 * The SCTP endpoint MUST always acknowledge the reception of each valid
2930 * DATA chunk.
2931 *
2932 * The guidelines on delayed acknowledgement algorithm specified in
2933 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2934 * acknowledgement SHOULD be generated for at least every second packet
2935 * (not every second DATA chunk) received, and SHOULD be generated within
2936 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2937 * situations it may be beneficial for an SCTP transmitter to be more
2938 * conservative than the algorithms detailed in this document allow.
2939 * However, an SCTP transmitter MUST NOT be more aggressive than the
2940 * following algorithms allow.
2941 *
2942 * A SCTP receiver MUST NOT generate more than one SACK for every
2943 * incoming packet, other than to update the offered window as the
2944 * receiving application consumes new data.
2945 *
2946 * Verification Tag: 8.5 Verification Tag [Normal verification]
2947 *
2948 * Inputs
2949 * (endpoint, asoc, chunk)
2950 *
2951 * Outputs
2952 * (asoc, reply_msg, msg_up, timers, counters)
2953 *
2954 * The return value is the disposition of the chunk.
2955 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002956sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2957 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 const struct sctp_association *asoc,
2959 const sctp_subtype_t type,
2960 void *arg,
2961 sctp_cmd_seq_t *commands)
2962{
2963 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002964 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 int error;
2966
2967 if (!sctp_vtag_verify(chunk, asoc)) {
2968 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2969 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002970 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002974 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 commands);
2976
wangweidongcb3f8372013-12-23 12:16:50 +08002977 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 switch (error) {
2979 case SCTP_IERROR_NO_ERROR:
2980 break;
2981 case SCTP_IERROR_HIGH_TSN:
2982 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002983 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 goto discard_noforce;
2985 case SCTP_IERROR_DUP_TSN:
2986 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002987 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 goto discard_force;
2989 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02002990 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002991 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002992 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002993 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 default:
2995 BUG();
2996 }
2997
Wei Yongjun6dc76942009-11-23 15:53:53 -05002998 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2999 force = SCTP_FORCE();
3000
Neil Horman9f70f462013-12-10 06:48:15 -05003001 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3003 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3004 }
3005
3006 /* If this is the last chunk in a packet, we need to count it
3007 * toward sack generation. Note that we need to SACK every
3008 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3009 * THEM. We elect to NOT generate SACK's if the chunk fails
3010 * the verification tag test.
3011 *
3012 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3013 *
3014 * The SCTP endpoint MUST always acknowledge the reception of
3015 * each valid DATA chunk.
3016 *
3017 * The guidelines on delayed acknowledgement algorithm
3018 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3019 * Specifically, an acknowledgement SHOULD be generated for at
3020 * least every second packet (not every second DATA chunk)
3021 * received, and SHOULD be generated within 200 ms of the
3022 * arrival of any unacknowledged DATA chunk. In some
3023 * situations it may be beneficial for an SCTP transmitter to
3024 * be more conservative than the algorithms detailed in this
3025 * document allow. However, an SCTP transmitter MUST NOT be
3026 * more aggressive than the following algorithms allow.
3027 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003028 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003029 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return SCTP_DISPOSITION_CONSUME;
3032
3033discard_force:
3034 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3035 *
3036 * When a packet arrives with duplicate DATA chunk(s) and with
3037 * no new DATA chunk(s), the endpoint MUST immediately send a
3038 * SACK with no delay. If a packet arrives with duplicate
3039 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3040 * MAY immediately send a SACK. Normally receipt of duplicate
3041 * DATA chunks will occur when the original SACK chunk was lost
3042 * and the peer's RTO has expired. The duplicate TSN number(s)
3043 * SHOULD be reported in the SACK as duplicate.
3044 */
3045 /* In our case, we split the MAY SACK advice up whether or not
3046 * the last chunk is a duplicate.'
3047 */
3048 if (chunk->end_of_packet)
3049 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3050 return SCTP_DISPOSITION_DISCARD;
3051
3052discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003053 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003054 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057}
3058
3059/*
3060 * sctp_sf_eat_data_fast_4_4
3061 *
3062 * Section: 4 (4)
3063 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3064 * DATA chunks without delay.
3065 *
3066 * Verification Tag: 8.5 Verification Tag [Normal verification]
3067 * Inputs
3068 * (endpoint, asoc, chunk)
3069 *
3070 * Outputs
3071 * (asoc, reply_msg, msg_up, timers, counters)
3072 *
3073 * The return value is the disposition of the chunk.
3074 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003075sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3076 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 const struct sctp_association *asoc,
3078 const sctp_subtype_t type,
3079 void *arg,
3080 sctp_cmd_seq_t *commands)
3081{
3082 struct sctp_chunk *chunk = arg;
3083 int error;
3084
3085 if (!sctp_vtag_verify(chunk, asoc)) {
3086 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3087 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003088 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
3090
3091 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003092 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 commands);
3094
wangweidongcb3f8372013-12-23 12:16:50 +08003095 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 switch (error) {
3097 case SCTP_IERROR_NO_ERROR:
3098 case SCTP_IERROR_HIGH_TSN:
3099 case SCTP_IERROR_DUP_TSN:
3100 case SCTP_IERROR_IGNORE_TSN:
3101 case SCTP_IERROR_BAD_STREAM:
3102 break;
3103 case SCTP_IERROR_NO_DATA:
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02003104 return SCTP_DISPOSITION_ABORT;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003105 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003106 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003107 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 default:
3109 BUG();
3110 }
3111
3112 /* Go a head and force a SACK, since we are shutting down. */
3113
3114 /* Implementor's Guide.
3115 *
3116 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3117 * respond to each received packet containing one or more DATA chunk(s)
3118 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3119 */
3120 if (chunk->end_of_packet) {
3121 /* We must delay the chunk creation since the cumulative
3122 * TSN has not been updated yet.
3123 */
3124 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3125 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3126 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3127 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3128 }
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 return SCTP_DISPOSITION_CONSUME;
3131}
3132
3133/*
3134 * Section: 6.2 Processing a Received SACK
3135 * D) Any time a SACK arrives, the endpoint performs the following:
3136 *
3137 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3138 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3139 * increasing, a SACK whose Cumulative TSN Ack is less than the
3140 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3141 *
3142 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3143 * of bytes still outstanding after processing the Cumulative TSN Ack
3144 * and the Gap Ack Blocks.
3145 *
3146 * iii) If the SACK is missing a TSN that was previously
3147 * acknowledged via a Gap Ack Block (e.g., the data receiver
3148 * reneged on the data), then mark the corresponding DATA chunk
3149 * as available for retransmit: Mark it as missing for fast
3150 * retransmit as described in Section 7.2.4 and if no retransmit
3151 * timer is running for the destination address to which the DATA
3152 * chunk was originally transmitted, then T3-rtx is started for
3153 * that destination address.
3154 *
3155 * Verification Tag: 8.5 Verification Tag [Normal verification]
3156 *
3157 * Inputs
3158 * (endpoint, asoc, chunk)
3159 *
3160 * Outputs
3161 * (asoc, reply_msg, msg_up, timers, counters)
3162 *
3163 * The return value is the disposition of the chunk.
3164 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003165sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3166 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 const struct sctp_association *asoc,
3168 const sctp_subtype_t type,
3169 void *arg,
3170 sctp_cmd_seq_t *commands)
3171{
3172 struct sctp_chunk *chunk = arg;
3173 sctp_sackhdr_t *sackh;
3174 __u32 ctsn;
3175
3176 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003177 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178
3179 /* Make sure that the SACK chunk has a valid length. */
3180 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003181 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 commands);
3183
3184 /* Pull the SACK chunk from the data buffer */
3185 sackh = sctp_sm_pull_sack(chunk);
3186 /* Was this a bogus SACK? */
3187 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003188 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 chunk->subh.sack_hdr = sackh;
3190 ctsn = ntohl(sackh->cum_tsn_ack);
3191
3192 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3193 * Ack Point, then drop the SACK. Since Cumulative TSN
3194 * Ack is monotonically increasing, a SACK whose
3195 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3196 * Point indicates an out-of-order SACK.
3197 */
3198 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003199 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3200 asoc->ctsn_ack_point);
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 return SCTP_DISPOSITION_DISCARD;
3203 }
3204
Wei Yongjunaecedea2007-08-02 16:57:44 +08003205 /* If Cumulative TSN Ack beyond the max tsn currently
3206 * send, terminating the association and respond to the
3207 * sender with an ABORT.
3208 */
3209 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003210 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003213 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 /* Note: We do the rest of the work on the PROCESS_SACK
3216 * sideeffect.
3217 */
3218 return SCTP_DISPOSITION_CONSUME;
3219}
3220
3221/*
3222 * Generate an ABORT in response to a packet.
3223 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003224 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003226 * 8) The receiver should respond to the sender of the OOTB packet with
3227 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3228 * MUST fill in the Verification Tag field of the outbound packet
3229 * with the value found in the Verification Tag field of the OOTB
3230 * packet and set the T-bit in the Chunk Flags to indicate that the
3231 * Verification Tag is reflected. After sending this ABORT, the
3232 * receiver of the OOTB packet shall discard the OOTB packet and take
3233 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 *
3235 * Verification Tag:
3236 *
3237 * The return value is the disposition of the chunk.
3238*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003239static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3240 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 const struct sctp_association *asoc,
3242 const sctp_subtype_t type,
3243 void *arg,
3244 sctp_cmd_seq_t *commands)
3245{
3246 struct sctp_packet *packet = NULL;
3247 struct sctp_chunk *chunk = arg;
3248 struct sctp_chunk *abort;
3249
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003250 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251
3252 if (packet) {
3253 /* Make an ABORT. The T bit will be set if the asoc
3254 * is NULL.
3255 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003256 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (!abort) {
3258 sctp_ootb_pkt_free(packet);
3259 return SCTP_DISPOSITION_NOMEM;
3260 }
3261
Jerome Forissier047a2422005-04-28 11:58:43 -07003262 /* Reflect vtag if T-Bit is set */
3263 if (sctp_test_T_bit(abort))
3264 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 /* Set the skb to the belonging sock for accounting. */
3267 abort->skb->sk = ep->base.sk;
3268
3269 sctp_packet_append_chunk(packet, abort);
3270
3271 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3272 SCTP_PACKET(packet));
3273
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003274 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003276 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 return SCTP_DISPOSITION_CONSUME;
3278 }
3279
3280 return SCTP_DISPOSITION_NOMEM;
3281}
3282
3283/*
3284 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3285 * event as ULP notification for each cause included in the chunk.
3286 *
3287 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3288 *
3289 * The return value is the disposition of the chunk.
3290*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003291sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3292 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 const struct sctp_association *asoc,
3294 const sctp_subtype_t type,
3295 void *arg,
3296 sctp_cmd_seq_t *commands)
3297{
3298 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003299 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003302 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
3304 /* Make sure that the ERROR chunk has a valid length. */
3305 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003306 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003308 sctp_walk_errors(err, chunk->chunk_hdr);
3309 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003310 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003311 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
Wei Yongjun3df26782009-03-02 06:46:51 +00003313 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3314 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317}
3318
3319/*
3320 * Process an inbound SHUTDOWN ACK.
3321 *
3322 * From Section 9.2:
3323 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3324 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3325 * peer, and remove all record of the association.
3326 *
3327 * The return value is the disposition.
3328 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003329sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3330 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 const struct sctp_association *asoc,
3332 const sctp_subtype_t type,
3333 void *arg,
3334 sctp_cmd_seq_t *commands)
3335{
3336 struct sctp_chunk *chunk = arg;
3337 struct sctp_chunk *reply;
3338 struct sctp_ulpevent *ev;
3339
3340 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003341 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3344 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003345 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 /* 10.2 H) SHUTDOWN COMPLETE notification
3348 *
3349 * When SCTP completes the shutdown procedures (section 9.2) this
3350 * notification is passed to the upper layer.
3351 */
3352 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003353 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (!ev)
3355 goto nomem;
3356
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003357 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3358 reply = sctp_make_shutdown_complete(asoc, chunk);
3359 if (!reply)
3360 goto nomem_chunk;
3361
3362 /* Do all the commands now (after allocation), so that we
3363 * have consistent state if memory allocation failes
3364 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3366
3367 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3368 * stop the T2-shutdown timer,
3369 */
3370 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3371 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3372
3373 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3374 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3375
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3377 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003378 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3379 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3381
3382 /* ...and remove all record of the association. */
3383 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3384 return SCTP_DISPOSITION_DELETE_TCB;
3385
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003386nomem_chunk:
3387 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388nomem:
3389 return SCTP_DISPOSITION_NOMEM;
3390}
3391
3392/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003393 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3394 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3396 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3397 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3398 * packet must fill in the Verification Tag field of the outbound
3399 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003400 * set the T-bit in the Chunk Flags to indicate that the Verification
3401 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 *
3403 * 8) The receiver should respond to the sender of the OOTB packet with
3404 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3405 * MUST fill in the Verification Tag field of the outbound packet
3406 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003407 * packet and set the T-bit in the Chunk Flags to indicate that the
3408 * Verification Tag is reflected. After sending this ABORT, the
3409 * receiver of the OOTB packet shall discard the OOTB packet and take
3410 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003412sctp_disposition_t sctp_sf_ootb(struct net *net,
3413 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 const struct sctp_association *asoc,
3415 const sctp_subtype_t type,
3416 void *arg,
3417 sctp_cmd_seq_t *commands)
3418{
3419 struct sctp_chunk *chunk = arg;
3420 struct sk_buff *skb = chunk->skb;
3421 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003422 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 __u8 *ch_end;
3424 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003425 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003427 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3430 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003431 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003433 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003434 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Marcelo Ricardo Leitnerbf911e92016-10-25 14:27:39 -02003436 /* Report violation if chunk len overflows */
3437 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3438 if (ch_end > skb_tail_pointer(skb))
3439 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3440 commands);
3441
Vlad Yasevichece25df2007-09-07 16:30:54 -04003442 /* Now that we know we at least have a chunk header,
3443 * do things that are type appropriate.
3444 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3446 ootb_shut_ack = 1;
3447
3448 /* RFC 2960, Section 3.3.7
3449 * Moreover, under any circumstances, an endpoint that
3450 * receives an ABORT MUST NOT respond to that ABORT by
3451 * sending an ABORT of its own.
3452 */
3453 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003454 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003455
Shan Wei85c5ed42011-04-19 21:30:01 +00003456 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3457 * or a COOKIE ACK the SCTP Packet should be silently
3458 * discarded.
3459 */
3460
3461 if (SCTP_CID_COOKIE_ACK == ch->type)
3462 ootb_cookie_ack = 1;
3463
3464 if (SCTP_CID_ERROR == ch->type) {
3465 sctp_walk_errors(err, ch) {
3466 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3467 ootb_cookie_ack = 1;
3468 break;
3469 }
3470 }
3471 }
3472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003474 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
3476 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003477 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003478 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003479 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003481 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
3484/*
3485 * Handle an "Out of the blue" SHUTDOWN ACK.
3486 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003487 * Section: 8.4 5, sctpimpguide 2.41.
3488 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003490 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3491 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3492 * packet must fill in the Verification Tag field of the outbound
3493 * packet with the Verification Tag received in the SHUTDOWN ACK and
3494 * set the T-bit in the Chunk Flags to indicate that the Verification
3495 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 *
3497 * Inputs
3498 * (endpoint, asoc, type, arg, commands)
3499 *
3500 * Outputs
3501 * (sctp_disposition_t)
3502 *
3503 * The return value is the disposition of the chunk.
3504 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003505static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3506 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 const struct sctp_association *asoc,
3508 const sctp_subtype_t type,
3509 void *arg,
3510 sctp_cmd_seq_t *commands)
3511{
3512 struct sctp_packet *packet = NULL;
3513 struct sctp_chunk *chunk = arg;
3514 struct sctp_chunk *shut;
3515
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003516 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 if (packet) {
3519 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003520 * The T bit will be set if the asoc is NULL.
3521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 shut = sctp_make_shutdown_complete(asoc, chunk);
3523 if (!shut) {
3524 sctp_ootb_pkt_free(packet);
3525 return SCTP_DISPOSITION_NOMEM;
3526 }
3527
Jerome Forissier047a2422005-04-28 11:58:43 -07003528 /* Reflect vtag if T-Bit is set */
3529 if (sctp_test_T_bit(shut))
3530 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3531
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 /* Set the skb to the belonging sock for accounting. */
3533 shut->skb->sk = ep->base.sk;
3534
3535 sctp_packet_append_chunk(packet, shut);
3536
3537 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3538 SCTP_PACKET(packet));
3539
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003540 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
3542 /* If the chunk length is invalid, we don't want to process
3543 * the reset of the packet.
3544 */
3545 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003546 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003548 /* We need to discard the rest of the packet to prevent
3549 * potential bomming attacks from additional bundled chunks.
3550 * This is documented in SCTP Threats ID.
3551 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003552 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 }
3554
3555 return SCTP_DISPOSITION_NOMEM;
3556}
3557
3558/*
3559 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3560 *
3561 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3562 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3563 * procedures in section 8.4 SHOULD be followed, in other words it
3564 * should be treated as an Out Of The Blue packet.
3565 * [This means that we do NOT check the Verification Tag on these
3566 * chunks. --piggy ]
3567 *
3568 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003569sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3570 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 const struct sctp_association *asoc,
3572 const sctp_subtype_t type,
3573 void *arg,
3574 sctp_cmd_seq_t *commands)
3575{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003576 struct sctp_chunk *chunk = arg;
3577
3578 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3579 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003580 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003581 commands);
3582
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 /* Although we do have an association in this case, it corresponds
3584 * to a restarted association. So the packet is treated as an OOTB
3585 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3586 * called with a NULL association.
3587 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003588 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003589
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003590 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591}
3592
3593/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003594sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3595 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 const struct sctp_association *asoc,
3597 const sctp_subtype_t type, void *arg,
3598 sctp_cmd_seq_t *commands)
3599{
3600 struct sctp_chunk *chunk = arg;
3601 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003602 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 sctp_addiphdr_t *hdr;
3604 __u32 serial;
3605
3606 if (!sctp_vtag_verify(chunk, asoc)) {
3607 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3608 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003609 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 }
3611
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003612 /* ADD-IP: Section 4.1.1
3613 * This chunk MUST be sent in an authenticated way by using
3614 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3615 * is received unauthenticated it MUST be silently discarded as
3616 * described in [I-D.ietf-tsvwg-sctp-auth].
3617 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003618 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003619 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3622 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003623 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 commands);
3625
3626 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3627 serial = ntohl(hdr->serial);
3628
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003629 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003630 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003631 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003632 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003633
Vlad Yasevicha08de642007-12-20 14:11:47 -08003634 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003636 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 */
3638 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003639 /* If this is the first instance of ASCONF in the packet,
3640 * we can clean our old ASCONF-ACKs.
3641 */
3642 if (!chunk->has_asconf)
3643 sctp_assoc_clean_asconf_ack_cache(asoc);
3644
3645 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3646 * expected, process the ASCONF as described below and after
3647 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3648 * the response packet and cache a copy of it (in the event it
3649 * later needs to be retransmitted).
3650 *
3651 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 */
3653 asconf_ack = sctp_process_asconf((struct sctp_association *)
3654 asoc, chunk);
3655 if (!asconf_ack)
3656 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003657 } else if (serial < asoc->peer.addip_serial + 1) {
3658 /* ADDIP 5.2 E2)
3659 * If the value found in the Sequence Number is less than the
3660 * ('Peer- Sequence-Number' + 1), simply skip to the next
3661 * ASCONF, and include in the outbound response packet
3662 * any previously cached ASCONF-ACK response that was
3663 * sent and saved that matches the Sequence Number of the
3664 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3665 * Chunk exists. This will occur when an older ASCONF
3666 * arrives out of order. In such a case, the receiver
3667 * should skip the ASCONF Chunk and not include ASCONF-ACK
3668 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003670 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3671 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003673
3674 /* Reset the transport so that we select the correct one
3675 * this time around. This is to make sure that we don't
3676 * accidentally use a stale transport that's been removed.
3677 */
3678 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003680 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003682 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return SCTP_DISPOSITION_DISCARD;
3684 }
3685
Vlad Yasevicha08de642007-12-20 14:11:47 -08003686 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3687 * containing the ASCONF-ACK Chunks MUST be the source address of
3688 * the SCTP packet that held the ASCONF Chunks.
3689 *
3690 * To do this properly, we'll set the destination address of the chunk
3691 * and at the transmit time, will try look up the transport to use.
3692 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003693 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003695 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003697 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003698 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003699 ((struct sctp_association *)asoc)->new_transport = NULL;
3700 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003701
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return SCTP_DISPOSITION_CONSUME;
3703}
3704
3705/*
3706 * ADDIP Section 4.3 General rules for address manipulation
3707 * When building TLV parameters for the ASCONF Chunk that will add or
3708 * delete IP addresses the D0 to D13 rules should be applied:
3709 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003710sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3711 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003713 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 sctp_cmd_seq_t *commands)
3715{
3716 struct sctp_chunk *asconf_ack = arg;
3717 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3718 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003719 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 sctp_addiphdr_t *addip_hdr;
3721 __u32 sent_serial, rcvd_serial;
3722
3723 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3724 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3725 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003726 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 }
3728
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003729 /* ADD-IP, Section 4.1.2:
3730 * This chunk MUST be sent in an authenticated way by using
3731 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3732 * is received unauthenticated it MUST be silently discarded as
3733 * described in [I-D.ietf-tsvwg-sctp-auth].
3734 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003735 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003736 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 /* Make sure that the ADDIP chunk has a valid length. */
3739 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003740 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 commands);
3742
3743 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3744 rcvd_serial = ntohl(addip_hdr->serial);
3745
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003746 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmann9de79222014-10-09 22:55:31 +02003747 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003748 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003749 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 if (last_asconf) {
3752 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3753 sent_serial = ntohl(addip_hdr->serial);
3754 } else {
3755 sent_serial = asoc->addip_serial - 1;
3756 }
3757
3758 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3759 * equal to the next serial number to be used but no ASCONF chunk is
3760 * outstanding the endpoint MUST ABORT the association. Note that a
3761 * sequence number is greater than if it is no more than 2^^31-1
3762 * larger than the current sequence number (using serial arithmetic).
3763 */
3764 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3765 !(asoc->addip_last_asconf)) {
3766 abort = sctp_make_abort(asoc, asconf_ack,
3767 sizeof(sctp_errhdr_t));
3768 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003769 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3771 SCTP_CHUNK(abort));
3772 }
3773 /* We are going to ABORT, so we might as well stop
3774 * processing the rest of the chunks in the packet.
3775 */
3776 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3777 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003778 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003779 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003780 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003782 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003783 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3784 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 return SCTP_DISPOSITION_ABORT;
3786 }
3787
3788 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3789 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3790 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3791
3792 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003793 asconf_ack)) {
3794 /* Successfully processed ASCONF_ACK. We can
3795 * release the next asconf if we have one.
3796 */
3797 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3798 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801
3802 abort = sctp_make_abort(asoc, asconf_ack,
3803 sizeof(sctp_errhdr_t));
3804 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003805 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3807 SCTP_CHUNK(abort));
3808 }
3809 /* We are going to ABORT, so we might as well stop
3810 * processing the rest of the chunks in the packet.
3811 */
wangweidongcb3f8372013-12-23 12:16:50 +08003812 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003813 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003814 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003816 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003817 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3818 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 return SCTP_DISPOSITION_ABORT;
3820 }
3821
3822 return SCTP_DISPOSITION_DISCARD;
3823}
3824
3825/*
3826 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3827 *
3828 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3829 * its cumulative TSN point to the value carried in the FORWARD TSN
3830 * chunk, and then MUST further advance its cumulative TSN point locally
3831 * if possible.
3832 * After the above processing, the data receiver MUST stop reporting any
3833 * missing TSNs earlier than or equal to the new cumulative TSN point.
3834 *
3835 * Verification Tag: 8.5 Verification Tag [Normal verification]
3836 *
3837 * The return value is the disposition of the chunk.
3838 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003839sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3840 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 const struct sctp_association *asoc,
3842 const sctp_subtype_t type,
3843 void *arg,
3844 sctp_cmd_seq_t *commands)
3845{
3846 struct sctp_chunk *chunk = arg;
3847 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003848 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 __u16 len;
3850 __u32 tsn;
3851
3852 if (!sctp_vtag_verify(chunk, asoc)) {
3853 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3854 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003855 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
3857
3858 /* Make sure that the FORWARD_TSN chunk has valid length. */
3859 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003860 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 commands);
3862
3863 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3864 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3865 len = ntohs(chunk->chunk_hdr->length);
3866 len -= sizeof(struct sctp_chunkhdr);
3867 skb_pull(chunk->skb, len);
3868
3869 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003870 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 /* The TSN is too high--silently discard the chunk and count on it
3873 * getting retransmitted later.
3874 */
3875 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3876 goto discard_noforce;
3877
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003878 /* Silently discard the chunk if stream-id is not valid */
3879 sctp_walk_fwdtsn(skip, chunk) {
3880 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3881 goto discard_noforce;
3882 }
3883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3885 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003886 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003890 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3892 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3893 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003896 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 */
3898 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
3900 return SCTP_DISPOSITION_CONSUME;
3901
3902discard_noforce:
3903 return SCTP_DISPOSITION_DISCARD;
3904}
3905
3906sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003907 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 const struct sctp_endpoint *ep,
3909 const struct sctp_association *asoc,
3910 const sctp_subtype_t type,
3911 void *arg,
3912 sctp_cmd_seq_t *commands)
3913{
3914 struct sctp_chunk *chunk = arg;
3915 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003916 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 __u16 len;
3918 __u32 tsn;
3919
3920 if (!sctp_vtag_verify(chunk, asoc)) {
3921 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3922 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003923 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 }
3925
3926 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3927 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003928 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 commands);
3930
3931 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3932 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3933 len = ntohs(chunk->chunk_hdr->length);
3934 len -= sizeof(struct sctp_chunkhdr);
3935 skb_pull(chunk->skb, len);
3936
3937 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003938 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940 /* The TSN is too high--silently discard the chunk and count on it
3941 * getting retransmitted later.
3942 */
3943 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3944 goto gen_shutdown;
3945
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003946 /* Silently discard the chunk if stream-id is not valid */
3947 sctp_walk_fwdtsn(skip, chunk) {
3948 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3949 goto gen_shutdown;
3950 }
3951
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3953 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003954 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003956
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 /* Go a head and force a SACK, since we are shutting down. */
3958gen_shutdown:
3959 /* Implementor's Guide.
3960 *
3961 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3962 * respond to each received packet containing one or more DATA chunk(s)
3963 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3964 */
3965 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3966 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3967 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3968 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3969
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003970 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
3972
3973/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003974 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003975 *
3976 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3977 * Identifier field. If this algorithm was not specified by the
3978 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3979 * during association setup, the AUTH chunk and all chunks after it MUST
3980 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3981 * defined in Section 4.1.
3982 *
3983 * If an endpoint with no shared key receives a Shared Key Identifier
3984 * other than 0, it MUST silently discard all authenticated chunks. If
3985 * the endpoint has at least one endpoint pair shared key for the peer,
3986 * it MUST use the key specified by the Shared Key Identifier if a
3987 * key has been configured for that Shared Key Identifier. If no
3988 * endpoint pair shared key has been configured for that Shared Key
3989 * Identifier, all authenticated chunks MUST be silently discarded.
3990 *
3991 * Verification Tag: 8.5 Verification Tag [Normal verification]
3992 *
3993 * The return value is the disposition of the chunk.
3994 */
Xin Long0e67ad52018-05-05 14:59:47 +08003995static sctp_ierror_t sctp_sf_authenticate(
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003996 const struct sctp_association *asoc,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003997 struct sctp_chunk *chunk)
3998{
3999 struct sctp_authhdr *auth_hdr;
4000 struct sctp_hmac *hmac;
4001 unsigned int sig_len;
4002 __u16 key_id;
4003 __u8 *save_digest;
4004 __u8 *digest;
4005
4006 /* Pull in the auth header, so we can do some more verification */
4007 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4008 chunk->subh.auth_hdr = auth_hdr;
4009 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4010
Masanari Iida02582e92012-08-22 19:11:26 +09004011 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004012 * chunk.
4013 */
4014 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4015 return SCTP_IERROR_AUTH_BAD_HMAC;
4016
4017 /* Make sure that the provided shared key identifier has been
4018 * configured
4019 */
4020 key_id = ntohs(auth_hdr->shkey_id);
4021 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4022 return SCTP_IERROR_AUTH_BAD_KEYID;
4023
4024
4025 /* Make sure that the length of the signature matches what
4026 * we expect.
4027 */
4028 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4029 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4030 if (sig_len != hmac->hmac_len)
4031 return SCTP_IERROR_PROTO_VIOLATION;
4032
4033 /* Now that we've done validation checks, we can compute and
4034 * verify the hmac. The steps involved are:
4035 * 1. Save the digest from the chunk.
4036 * 2. Zero out the digest in the chunk.
4037 * 3. Compute the new digest
4038 * 4. Compare saved and new digests.
4039 */
4040 digest = auth_hdr->hmac;
4041 skb_pull(chunk->skb, sig_len);
4042
4043 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4044 if (!save_digest)
4045 goto nomem;
4046
4047 memset(digest, 0, sig_len);
4048
4049 sctp_auth_calculate_hmac(asoc, chunk->skb,
4050 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4051 GFP_ATOMIC);
4052
4053 /* Discard the packet if the digests do not match */
4054 if (memcmp(save_digest, digest, sig_len)) {
4055 kfree(save_digest);
4056 return SCTP_IERROR_BAD_SIG;
4057 }
4058
4059 kfree(save_digest);
4060 chunk->auth = 1;
4061
4062 return SCTP_IERROR_NO_ERROR;
4063nomem:
4064 return SCTP_IERROR_NOMEM;
4065}
4066
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004067sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4068 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004069 const struct sctp_association *asoc,
4070 const sctp_subtype_t type,
4071 void *arg,
4072 sctp_cmd_seq_t *commands)
4073{
4074 struct sctp_authhdr *auth_hdr;
4075 struct sctp_chunk *chunk = arg;
4076 struct sctp_chunk *err_chunk;
4077 sctp_ierror_t error;
4078
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004079 /* Make sure that the peer has AUTH capable */
4080 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004081 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004082
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004083 if (!sctp_vtag_verify(chunk, asoc)) {
4084 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4085 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004086 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004087 }
4088
4089 /* Make sure that the AUTH chunk has valid length. */
4090 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004091 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004092 commands);
4093
4094 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Xin Long0e67ad52018-05-05 14:59:47 +08004095 error = sctp_sf_authenticate(asoc, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004096 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004097 case SCTP_IERROR_AUTH_BAD_HMAC:
4098 /* Generate the ERROR chunk and discard the rest
4099 * of the packet
4100 */
4101 err_chunk = sctp_make_op_error(asoc, chunk,
4102 SCTP_ERROR_UNSUP_HMAC,
4103 &auth_hdr->hmac_id,
4104 sizeof(__u16), 0);
4105 if (err_chunk) {
4106 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4107 SCTP_CHUNK(err_chunk));
4108 }
4109 /* Fall Through */
4110 case SCTP_IERROR_AUTH_BAD_KEYID:
4111 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004112 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004113
4114 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004115 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004116 commands);
4117
4118 case SCTP_IERROR_NOMEM:
4119 return SCTP_DISPOSITION_NOMEM;
4120
4121 default: /* Prevent gcc warnings */
4122 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004123 }
4124
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004125 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4126 struct sctp_ulpevent *ev;
4127
4128 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4129 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4130
4131 if (!ev)
4132 return -ENOMEM;
4133
4134 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4135 SCTP_ULPEVENT(ev));
4136 }
4137
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004138 return SCTP_DISPOSITION_CONSUME;
4139}
4140
4141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 * Process an unknown chunk.
4143 *
4144 * Section: 3.2. Also, 2.1 in the implementor's guide.
4145 *
4146 * Chunk Types are encoded such that the highest-order two bits specify
4147 * the action that must be taken if the processing endpoint does not
4148 * recognize the Chunk Type.
4149 *
4150 * 00 - Stop processing this SCTP packet and discard it, do not process
4151 * any further chunks within it.
4152 *
4153 * 01 - Stop processing this SCTP packet and discard it, do not process
4154 * any further chunks within it, and report the unrecognized
4155 * chunk in an 'Unrecognized Chunk Type'.
4156 *
4157 * 10 - Skip this chunk and continue processing.
4158 *
4159 * 11 - Skip this chunk and continue processing, but report in an ERROR
4160 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4161 *
4162 * The return value is the disposition of the chunk.
4163 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004164sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4165 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 const struct sctp_association *asoc,
4167 const sctp_subtype_t type,
4168 void *arg,
4169 sctp_cmd_seq_t *commands)
4170{
4171 struct sctp_chunk *unk_chunk = arg;
4172 struct sctp_chunk *err_chunk;
4173 sctp_chunkhdr_t *hdr;
4174
Daniel Borkmannbb333812013-06-28 19:49:40 +02004175 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
4177 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004178 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
4180 /* Make sure that the chunk has a valid length.
4181 * Since we don't know the chunk type, we use a general
4182 * chunkhdr structure to make a comparison.
4183 */
4184 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004185 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 commands);
4187
4188 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4189 case SCTP_CID_ACTION_DISCARD:
4190 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004191 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 /* Generate an ERROR chunk as response. */
4194 hdr = unk_chunk->chunk_hdr;
4195 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4196 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004197 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004198 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (err_chunk) {
4200 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4201 SCTP_CHUNK(err_chunk));
4202 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004203
4204 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004205 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 case SCTP_CID_ACTION_SKIP:
4208 /* Skip the chunk. */
4209 return SCTP_DISPOSITION_DISCARD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 case SCTP_CID_ACTION_SKIP_ERR:
4211 /* Generate an ERROR chunk as response. */
4212 hdr = unk_chunk->chunk_hdr;
4213 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4214 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Marcelo Ricardo Leitnere2f036a2016-09-21 08:45:55 -03004215 SCTP_PAD4(ntohs(hdr->length)),
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004216 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 if (err_chunk) {
4218 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4219 SCTP_CHUNK(err_chunk));
4220 }
4221 /* Skip the chunk. */
4222 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 default:
4224 break;
4225 }
4226
4227 return SCTP_DISPOSITION_DISCARD;
4228}
4229
4230/*
4231 * Discard the chunk.
4232 *
4233 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4234 * [Too numerous to mention...]
4235 * Verification Tag: No verification needed.
4236 * Inputs
4237 * (endpoint, asoc, chunk)
4238 *
4239 * Outputs
4240 * (asoc, reply_msg, msg_up, timers, counters)
4241 *
4242 * The return value is the disposition of the chunk.
4243 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004244sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4245 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 const struct sctp_association *asoc,
4247 const sctp_subtype_t type,
4248 void *arg,
4249 sctp_cmd_seq_t *commands)
4250{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004251 struct sctp_chunk *chunk = arg;
4252
4253 /* Make sure that the chunk has a valid length.
4254 * Since we don't know the chunk type, we use a general
4255 * chunkhdr structure to make a comparison.
4256 */
4257 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004258 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004259 commands);
4260
Daniel Borkmannbb333812013-06-28 19:49:40 +02004261 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return SCTP_DISPOSITION_DISCARD;
4264}
4265
4266/*
4267 * Discard the whole packet.
4268 *
4269 * Section: 8.4 2)
4270 *
4271 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4272 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 *
4274 * Verification Tag: No verification necessary
4275 *
4276 * Inputs
4277 * (endpoint, asoc, chunk)
4278 *
4279 * Outputs
4280 * (asoc, reply_msg, msg_up, timers, counters)
4281 *
4282 * The return value is the disposition of the chunk.
4283 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004284sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4285 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 const struct sctp_association *asoc,
4287 const sctp_subtype_t type,
4288 void *arg,
4289 sctp_cmd_seq_t *commands)
4290{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004291 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4293
4294 return SCTP_DISPOSITION_CONSUME;
4295}
4296
4297
4298/*
4299 * The other end is violating protocol.
4300 *
4301 * Section: Not specified
4302 * Verification Tag: Not specified
4303 * Inputs
4304 * (endpoint, asoc, chunk)
4305 *
4306 * Outputs
4307 * (asoc, reply_msg, msg_up, timers, counters)
4308 *
4309 * We simply tag the chunk as a violation. The state machine will log
4310 * the violation and continue.
4311 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004312sctp_disposition_t sctp_sf_violation(struct net *net,
4313 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 const struct sctp_association *asoc,
4315 const sctp_subtype_t type,
4316 void *arg,
4317 sctp_cmd_seq_t *commands)
4318{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004319 struct sctp_chunk *chunk = arg;
4320
4321 /* Make sure that the chunk has a valid length. */
4322 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004323 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004324 commands);
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 return SCTP_DISPOSITION_VIOLATION;
4327}
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004330 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004332static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004333 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004334 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004337 sctp_cmd_seq_t *commands,
4338 const __u8 *payload,
4339 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004341 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 struct sctp_chunk *chunk = arg;
4343 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004345 /* SCTP-AUTH, Section 6.3:
4346 * It should be noted that if the receiver wants to tear
4347 * down an association in an authenticated way only, the
4348 * handling of malformed packets should not result in
4349 * tearing down the association.
4350 *
4351 * This means that if we only want to abort associations
4352 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004353 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004354 * was malformed.
4355 */
4356 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4357 goto discard;
4358
Jesper Juhl9abed242007-11-11 23:57:49 +01004359 /* Make the abort chunk. */
4360 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4361 if (!abort)
4362 goto nomem;
4363
Vlad Yasevichece25df2007-09-07 16:30:54 -04004364 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004365 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4366 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4367 !asoc->peer.i.init_tag) {
4368 sctp_initack_chunk_t *initack;
4369
4370 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4371 if (!sctp_chunk_length_valid(chunk,
4372 sizeof(sctp_initack_chunk_t)))
4373 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4374 else {
4375 unsigned int inittag;
4376
4377 inittag = ntohl(initack->init_hdr.init_tag);
4378 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4379 SCTP_U32(inittag));
4380 }
4381 }
4382
Vlad Yasevichece25df2007-09-07 16:30:54 -04004383 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004384 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Vlad Yasevichece25df2007-09-07 16:30:54 -04004386 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4387 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4388 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4389 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4390 SCTP_ERROR(ECONNREFUSED));
4391 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4392 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4393 } else {
4394 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4395 SCTP_ERROR(ECONNABORTED));
4396 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4397 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004398 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004401 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004402
4403 if (!packet)
4404 goto nomem_pkt;
4405
4406 if (sctp_test_T_bit(abort))
4407 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4408
4409 abort->skb->sk = ep->base.sk;
4410
4411 sctp_packet_append_chunk(packet, abort);
4412
4413 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4414 SCTP_PACKET(packet));
4415
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004416 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 }
4418
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004419 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004420
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004421discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004422 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return SCTP_DISPOSITION_ABORT;
4424
Vlad Yasevichece25df2007-09-07 16:30:54 -04004425nomem_pkt:
4426 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427nomem:
4428 return SCTP_DISPOSITION_NOMEM;
4429}
4430
Wei Yongjunaecedea2007-08-02 16:57:44 +08004431/*
4432 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004433 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004434 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004435 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004436 *
4437 * We inform the other end by sending an ABORT with a Protocol Violation
4438 * error code.
4439 *
4440 * Section: Not specified
4441 * Verification Tag: Nothing to do
4442 * Inputs
4443 * (endpoint, asoc, chunk)
4444 *
4445 * Outputs
4446 * (reply_msg, msg_up, counters)
4447 *
4448 * Generate an ABORT chunk and terminate the association.
4449 */
4450static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004451 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004452 const struct sctp_endpoint *ep,
4453 const struct sctp_association *asoc,
4454 const sctp_subtype_t type,
4455 void *arg,
4456 sctp_cmd_seq_t *commands)
4457{
wangweidongcb3f8372013-12-23 12:16:50 +08004458 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004459
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004460 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004461 sizeof(err_str));
4462}
4463
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004464/*
4465 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004466 * If the length is smaller than the minimum length of a given parameter,
4467 * or accumulated length in multi parameters exceeds the end of the chunk,
4468 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004469 */
4470static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004471 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004472 const struct sctp_endpoint *ep,
4473 const struct sctp_association *asoc,
4474 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004475 void *arg, void *ext,
4476 sctp_cmd_seq_t *commands)
4477{
4478 struct sctp_chunk *chunk = arg;
4479 struct sctp_paramhdr *param = ext;
4480 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004481
Wei Yongjunba016672008-09-30 05:32:24 -07004482 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4483 goto discard;
4484
4485 /* Make the abort chunk. */
4486 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4487 if (!abort)
4488 goto nomem;
4489
4490 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004491 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004492
4493 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4494 SCTP_ERROR(ECONNABORTED));
4495 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4496 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004497 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4498 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004499
4500discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004501 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004502 return SCTP_DISPOSITION_ABORT;
4503nomem:
4504 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004505}
4506
Wei Yongjunaecedea2007-08-02 16:57:44 +08004507/* Handle a protocol violation when the peer trying to advance the
4508 * cumulative tsn ack to a point beyond the max tsn currently sent.
4509 *
4510 * We inform the other end by sending an ABORT with a Protocol Violation
4511 * error code.
4512 */
4513static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004514 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004515 const struct sctp_endpoint *ep,
4516 const struct sctp_association *asoc,
4517 const sctp_subtype_t type,
4518 void *arg,
4519 sctp_cmd_seq_t *commands)
4520{
wangweidongcb3f8372013-12-23 12:16:50 +08004521 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004522
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004523 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004524 sizeof(err_str));
4525}
4526
Vlad Yasevichece25df2007-09-07 16:30:54 -04004527/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004528 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004529 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004530 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004531 * on the path and we may not want to continue this communication.
4532 */
4533static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004534 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004535 const struct sctp_endpoint *ep,
4536 const struct sctp_association *asoc,
4537 const sctp_subtype_t type,
4538 void *arg,
4539 sctp_cmd_seq_t *commands)
4540{
wangweidongcb3f8372013-12-23 12:16:50 +08004541 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004542
4543 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004544 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004545
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004546 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004547 sizeof(err_str));
4548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549/***************************************************************************
4550 * These are the state functions for handling primitive (Section 10) events.
4551 ***************************************************************************/
4552/*
4553 * sctp_sf_do_prm_asoc
4554 *
4555 * Section: 10.1 ULP-to-SCTP
4556 * B) Associate
4557 *
4558 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4559 * outbound stream count)
4560 * -> association id [,destination transport addr list] [,outbound stream
4561 * count]
4562 *
4563 * This primitive allows the upper layer to initiate an association to a
4564 * specific peer endpoint.
4565 *
4566 * The peer endpoint shall be specified by one of the transport addresses
4567 * which defines the endpoint (see Section 1.4). If the local SCTP
4568 * instance has not been initialized, the ASSOCIATE is considered an
4569 * error.
4570 * [This is not relevant for the kernel implementation since we do all
4571 * initialization at boot time. It we hadn't initialized we wouldn't
4572 * get anywhere near this code.]
4573 *
4574 * An association id, which is a local handle to the SCTP association,
4575 * will be returned on successful establishment of the association. If
4576 * SCTP is not able to open an SCTP association with the peer endpoint,
4577 * an error is returned.
4578 * [In the kernel implementation, the struct sctp_association needs to
4579 * be created BEFORE causing this primitive to run.]
4580 *
4581 * Other association parameters may be returned, including the
4582 * complete destination transport addresses of the peer as well as the
4583 * outbound stream count of the local endpoint. One of the transport
4584 * address from the returned destination addresses will be selected by
4585 * the local endpoint as default primary path for sending SCTP packets
4586 * to this peer. The returned "destination transport addr list" can
4587 * be used by the ULP to change the default primary path or to force
4588 * sending a packet to a specific transport address. [All of this
4589 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4590 * function.]
4591 *
4592 * Mandatory attributes:
4593 *
4594 * o local SCTP instance name - obtained from the INITIALIZE operation.
4595 * [This is the argument asoc.]
4596 * o destination transport addr - specified as one of the transport
4597 * addresses of the peer endpoint with which the association is to be
4598 * established.
4599 * [This is asoc->peer.active_path.]
4600 * o outbound stream count - the number of outbound streams the ULP
4601 * would like to open towards this peer endpoint.
4602 * [BUG: This is not currently implemented.]
4603 * Optional attributes:
4604 *
4605 * None.
4606 *
4607 * The return value is a disposition.
4608 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004609sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4610 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 const struct sctp_association *asoc,
4612 const sctp_subtype_t type,
4613 void *arg,
4614 sctp_cmd_seq_t *commands)
4615{
4616 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004617 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
4619 /* The comment below says that we enter COOKIE-WAIT AFTER
4620 * sending the INIT, but that doesn't actually work in our
4621 * implementation...
4622 */
4623 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4624 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4625
4626 /* RFC 2960 5.1 Normal Establishment of an Association
4627 *
4628 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4629 * must provide its Verification Tag (Tag_A) in the Initiate
4630 * Tag field. Tag_A SHOULD be a random number in the range of
4631 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4632 */
4633
4634 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4635 if (!repl)
4636 goto nomem;
4637
Daniel Borkmann405426f2013-06-14 18:24:05 +02004638 /* Choose transport for INIT. */
4639 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4640 SCTP_CHUNK(repl));
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /* Cast away the const modifier, as we want to just
4643 * rerun it through as a sideffect.
4644 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004645 my_asoc = (struct sctp_association *)asoc;
4646 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
4648 /* After sending the INIT, "A" starts the T1-init timer and
4649 * enters the COOKIE-WAIT state.
4650 */
4651 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4652 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4653 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4654 return SCTP_DISPOSITION_CONSUME;
4655
4656nomem:
4657 return SCTP_DISPOSITION_NOMEM;
4658}
4659
4660/*
4661 * Process the SEND primitive.
4662 *
4663 * Section: 10.1 ULP-to-SCTP
4664 * E) Send
4665 *
4666 * Format: SEND(association id, buffer address, byte count [,context]
4667 * [,stream id] [,life time] [,destination transport address]
4668 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4669 * -> result
4670 *
4671 * This is the main method to send user data via SCTP.
4672 *
4673 * Mandatory attributes:
4674 *
4675 * o association id - local handle to the SCTP association
4676 *
4677 * o buffer address - the location where the user message to be
4678 * transmitted is stored;
4679 *
4680 * o byte count - The size of the user data in number of bytes;
4681 *
4682 * Optional attributes:
4683 *
4684 * o context - an optional 32 bit integer that will be carried in the
4685 * sending failure notification to the ULP if the transportation of
4686 * this User Message fails.
4687 *
4688 * o stream id - to indicate which stream to send the data on. If not
4689 * specified, stream 0 will be used.
4690 *
4691 * o life time - specifies the life time of the user data. The user data
4692 * will not be sent by SCTP after the life time expires. This
4693 * parameter can be used to avoid efforts to transmit stale
4694 * user messages. SCTP notifies the ULP if the data cannot be
4695 * initiated to transport (i.e. sent to the destination via SCTP's
4696 * send primitive) within the life time variable. However, the
4697 * user data will be transmitted if SCTP has attempted to transmit a
4698 * chunk before the life time expired.
4699 *
4700 * o destination transport address - specified as one of the destination
4701 * transport addresses of the peer endpoint to which this packet
4702 * should be sent. Whenever possible, SCTP should use this destination
4703 * transport address for sending the packets, instead of the current
4704 * primary path.
4705 *
4706 * o unorder flag - this flag, if present, indicates that the user
4707 * would like the data delivered in an unordered fashion to the peer
4708 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4709 * message).
4710 *
4711 * o no-bundle flag - instructs SCTP not to bundle this user data with
4712 * other outbound DATA chunks. SCTP MAY still bundle even when
4713 * this flag is present, when faced with network congestion.
4714 *
4715 * o payload protocol-id - A 32 bit unsigned integer that is to be
4716 * passed to the peer indicating the type of payload protocol data
4717 * being transmitted. This value is passed as opaque data by SCTP.
4718 *
4719 * The return value is the disposition.
4720 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004721sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4722 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 const struct sctp_association *asoc,
4724 const sctp_subtype_t type,
4725 void *arg,
4726 sctp_cmd_seq_t *commands)
4727{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004728 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004730 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 return SCTP_DISPOSITION_CONSUME;
4732}
4733
4734/*
4735 * Process the SHUTDOWN primitive.
4736 *
4737 * Section: 10.1:
4738 * C) Shutdown
4739 *
4740 * Format: SHUTDOWN(association id)
4741 * -> result
4742 *
4743 * Gracefully closes an association. Any locally queued user data
4744 * will be delivered to the peer. The association will be terminated only
4745 * after the peer acknowledges all the SCTP packets sent. A success code
4746 * will be returned on successful termination of the association. If
4747 * attempting to terminate the association results in a failure, an error
4748 * code shall be returned.
4749 *
4750 * Mandatory attributes:
4751 *
4752 * o association id - local handle to the SCTP association
4753 *
4754 * Optional attributes:
4755 *
4756 * None.
4757 *
4758 * The return value is the disposition.
4759 */
4760sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004761 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 const struct sctp_endpoint *ep,
4763 const struct sctp_association *asoc,
4764 const sctp_subtype_t type,
4765 void *arg,
4766 sctp_cmd_seq_t *commands)
4767{
4768 int disposition;
4769
4770 /* From 9.2 Shutdown of an Association
4771 * Upon receipt of the SHUTDOWN primitive from its upper
4772 * layer, the endpoint enters SHUTDOWN-PENDING state and
4773 * remains there until all outstanding data has been
4774 * acknowledged by its peer. The endpoint accepts no new data
4775 * from its upper layer, but retransmits data to the far end
4776 * if necessary to fill gaps.
4777 */
4778 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4779 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4780
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 disposition = SCTP_DISPOSITION_CONSUME;
4782 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004783 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 arg, commands);
4785 }
4786 return disposition;
4787}
4788
4789/*
4790 * Process the ABORT primitive.
4791 *
4792 * Section: 10.1:
4793 * C) Abort
4794 *
4795 * Format: Abort(association id [, cause code])
4796 * -> result
4797 *
4798 * Ungracefully closes an association. Any locally queued user data
4799 * will be discarded and an ABORT chunk is sent to the peer. A success code
4800 * will be returned on successful abortion of the association. If
4801 * attempting to abort the association results in a failure, an error
4802 * code shall be returned.
4803 *
4804 * Mandatory attributes:
4805 *
4806 * o association id - local handle to the SCTP association
4807 *
4808 * Optional attributes:
4809 *
4810 * o cause code - reason of the abort to be passed to the peer
4811 *
4812 * None.
4813 *
4814 * The return value is the disposition.
4815 */
4816sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004817 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 const struct sctp_endpoint *ep,
4819 const struct sctp_association *asoc,
4820 const sctp_subtype_t type,
4821 void *arg,
4822 sctp_cmd_seq_t *commands)
4823{
4824 /* From 9.1 Abort of an Association
4825 * Upon receipt of the ABORT primitive from its upper
4826 * layer, the endpoint enters CLOSED state and
4827 * discard all outstanding data has been
4828 * acknowledged by its peer. The endpoint accepts no new data
4829 * from its upper layer, but retransmits data to the far end
4830 * if necessary to fill gaps.
4831 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004832 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Xin Long068d8bd2015-12-29 17:49:25 +08004834 if (abort)
4835 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 /* Even if we can't send the ABORT due to low memory delete the
4838 * TCB. This is a departure from our typical NOMEM handling.
4839 */
4840
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004841 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4842 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 /* Delete the established association. */
4844 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004845 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004847 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4848 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004850 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851}
4852
4853/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004854sctp_disposition_t sctp_sf_error_closed(struct net *net,
4855 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 const struct sctp_association *asoc,
4857 const sctp_subtype_t type,
4858 void *arg,
4859 sctp_cmd_seq_t *commands)
4860{
4861 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4862 return SCTP_DISPOSITION_CONSUME;
4863}
4864
4865/* We tried an illegal operation on an association which is shutting
4866 * down.
4867 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004868sctp_disposition_t sctp_sf_error_shutdown(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,
4876 SCTP_ERROR(-ESHUTDOWN));
4877 return SCTP_DISPOSITION_CONSUME;
4878}
4879
4880/*
4881 * sctp_cookie_wait_prm_shutdown
4882 *
4883 * Section: 4 Note: 2
4884 * Verification Tag:
4885 * Inputs
4886 * (endpoint, asoc)
4887 *
4888 * The RFC does not explicitly address this issue, but is the route through the
4889 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4890 *
4891 * Outputs
4892 * (timers)
4893 */
4894sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004895 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 const struct sctp_endpoint *ep,
4897 const struct sctp_association *asoc,
4898 const sctp_subtype_t type,
4899 void *arg,
4900 sctp_cmd_seq_t *commands)
4901{
4902 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4903 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4904
4905 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4906 SCTP_STATE(SCTP_STATE_CLOSED));
4907
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004908 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4911
4912 return SCTP_DISPOSITION_DELETE_TCB;
4913}
4914
4915/*
4916 * sctp_cookie_echoed_prm_shutdown
4917 *
4918 * Section: 4 Note: 2
4919 * Verification Tag:
4920 * Inputs
4921 * (endpoint, asoc)
4922 *
4923 * The RFC does not explcitly address this issue, but is the route through the
4924 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4925 *
4926 * Outputs
4927 * (timers)
4928 */
4929sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004930 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 const struct sctp_endpoint *ep,
4932 const struct sctp_association *asoc,
4933 const sctp_subtype_t type,
4934 void *arg, sctp_cmd_seq_t *commands)
4935{
4936 /* There is a single T1 timer, so we should be able to use
4937 * common function with the COOKIE-WAIT state.
4938 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004939 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940}
4941
4942/*
4943 * sctp_sf_cookie_wait_prm_abort
4944 *
4945 * Section: 4 Note: 2
4946 * Verification Tag:
4947 * Inputs
4948 * (endpoint, asoc)
4949 *
4950 * The RFC does not explicitly address this issue, but is the route through the
4951 * state table when someone issues an abort while in COOKIE_WAIT state.
4952 *
4953 * Outputs
4954 * (timers)
4955 */
4956sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004957 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 const struct sctp_endpoint *ep,
4959 const struct sctp_association *asoc,
4960 const sctp_subtype_t type,
4961 void *arg,
4962 sctp_cmd_seq_t *commands)
4963{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004964 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965
4966 /* Stop T1-init timer */
4967 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4968 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
Xin Long068d8bd2015-12-29 17:49:25 +08004970 if (abort)
4971 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
4973 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4974 SCTP_STATE(SCTP_STATE_CLOSED));
4975
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004976 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 /* Even if we can't send the ABORT due to low memory delete the
4979 * TCB. This is a departure from our typical NOMEM handling.
4980 */
4981
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004982 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4983 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 /* Delete the established association. */
4985 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004986 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Marcelo Ricardo Leitner649621e2016-01-08 11:00:54 -02004988 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989}
4990
4991/*
4992 * sctp_sf_cookie_echoed_prm_abort
4993 *
4994 * Section: 4 Note: 3
4995 * Verification Tag:
4996 * Inputs
4997 * (endpoint, asoc)
4998 *
4999 * The RFC does not explcitly address this issue, but is the route through the
5000 * state table when someone issues an abort while in COOKIE_ECHOED state.
5001 *
5002 * Outputs
5003 * (timers)
5004 */
5005sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005006 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007 const struct sctp_endpoint *ep,
5008 const struct sctp_association *asoc,
5009 const sctp_subtype_t type,
5010 void *arg,
5011 sctp_cmd_seq_t *commands)
5012{
5013 /* There is a single T1 timer, so we should be able to use
5014 * common function with the COOKIE-WAIT state.
5015 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005016 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017}
5018
5019/*
5020 * sctp_sf_shutdown_pending_prm_abort
5021 *
5022 * Inputs
5023 * (endpoint, asoc)
5024 *
5025 * The RFC does not explicitly address this issue, but is the route through the
5026 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5027 *
5028 * Outputs
5029 * (timers)
5030 */
5031sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005032 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 const struct sctp_endpoint *ep,
5034 const struct sctp_association *asoc,
5035 const sctp_subtype_t type,
5036 void *arg,
5037 sctp_cmd_seq_t *commands)
5038{
5039 /* Stop the T5-shutdown guard timer. */
5040 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5041 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5042
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005043 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044}
5045
5046/*
5047 * sctp_sf_shutdown_sent_prm_abort
5048 *
5049 * Inputs
5050 * (endpoint, asoc)
5051 *
5052 * The RFC does not explicitly address this issue, but is the route through the
5053 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5054 *
5055 * Outputs
5056 * (timers)
5057 */
5058sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005059 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 const struct sctp_endpoint *ep,
5061 const struct sctp_association *asoc,
5062 const sctp_subtype_t type,
5063 void *arg,
5064 sctp_cmd_seq_t *commands)
5065{
5066 /* Stop the T2-shutdown timer. */
5067 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5068 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5069
5070 /* Stop the T5-shutdown guard timer. */
5071 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5072 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5073
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005074 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075}
5076
5077/*
5078 * sctp_sf_cookie_echoed_prm_abort
5079 *
5080 * Inputs
5081 * (endpoint, asoc)
5082 *
5083 * The RFC does not explcitly address this issue, but is the route through the
5084 * state table when someone issues an abort while in COOKIE_ECHOED state.
5085 *
5086 * Outputs
5087 * (timers)
5088 */
5089sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005090 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 const struct sctp_endpoint *ep,
5092 const struct sctp_association *asoc,
5093 const sctp_subtype_t type,
5094 void *arg,
5095 sctp_cmd_seq_t *commands)
5096{
5097 /* The same T2 timer, so we should be able to use
5098 * common function with the SHUTDOWN-SENT state.
5099 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005100 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101}
5102
5103/*
5104 * Process the REQUESTHEARTBEAT primitive
5105 *
5106 * 10.1 ULP-to-SCTP
5107 * J) Request Heartbeat
5108 *
5109 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5110 *
5111 * -> result
5112 *
5113 * Instructs the local endpoint to perform a HeartBeat on the specified
5114 * destination transport address of the given association. The returned
5115 * result should indicate whether the transmission of the HEARTBEAT
5116 * chunk to the destination address is successful.
5117 *
5118 * Mandatory attributes:
5119 *
5120 * o association id - local handle to the SCTP association
5121 *
5122 * o destination transport address - the transport address of the
5123 * association on which a heartbeat should be issued.
5124 */
5125sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005126 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 const struct sctp_endpoint *ep,
5128 const struct sctp_association *asoc,
5129 const sctp_subtype_t type,
5130 void *arg,
5131 sctp_cmd_seq_t *commands)
5132{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005133 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5134 (struct sctp_transport *)arg, commands))
5135 return SCTP_DISPOSITION_NOMEM;
5136
5137 /*
5138 * RFC 2960 (bis), section 8.3
5139 *
5140 * D) Request an on-demand HEARTBEAT on a specific destination
5141 * transport address of a given association.
5142 *
5143 * The endpoint should increment the respective error counter of
5144 * the destination transport address each time a HEARTBEAT is sent
5145 * to that address and not acknowledged within one RTO.
5146 *
5147 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005148 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005149 SCTP_TRANSPORT(arg));
5150 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151}
5152
5153/*
5154 * ADDIP Section 4.1 ASCONF Chunk Procedures
5155 * When an endpoint has an ASCONF signaled change to be sent to the
5156 * remote endpoint it should do A1 to A9
5157 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005158sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5159 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 const struct sctp_association *asoc,
5161 const sctp_subtype_t type,
5162 void *arg,
5163 sctp_cmd_seq_t *commands)
5164{
5165 struct sctp_chunk *chunk = arg;
5166
5167 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5168 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5169 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5170 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5171 return SCTP_DISPOSITION_CONSUME;
5172}
5173
5174/*
5175 * Ignore the primitive event
5176 *
5177 * The return value is the disposition of the primitive.
5178 */
5179sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005180 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 const struct sctp_endpoint *ep,
5182 const struct sctp_association *asoc,
5183 const sctp_subtype_t type,
5184 void *arg,
5185 sctp_cmd_seq_t *commands)
5186{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005187 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5188 type.primitive);
5189
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 return SCTP_DISPOSITION_DISCARD;
5191}
5192
5193/***************************************************************************
5194 * These are the state functions for the OTHER events.
5195 ***************************************************************************/
5196
5197/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005198 * When the SCTP stack has no more user data to send or retransmit, this
5199 * notification is given to the user. Also, at the time when a user app
5200 * subscribes to this event, if there is no data to be sent or
5201 * retransmit, the stack will immediately send up this notification.
5202 */
5203sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005204 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005205 const struct sctp_endpoint *ep,
5206 const struct sctp_association *asoc,
5207 const sctp_subtype_t type,
5208 void *arg,
5209 sctp_cmd_seq_t *commands)
5210{
5211 struct sctp_ulpevent *event;
5212
5213 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5214 if (!event)
5215 return SCTP_DISPOSITION_NOMEM;
5216
5217 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5218
5219 return SCTP_DISPOSITION_CONSUME;
5220}
5221
5222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 * Start the shutdown negotiation.
5224 *
5225 * From Section 9.2:
5226 * Once all its outstanding data has been acknowledged, the endpoint
5227 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5228 * TSN Ack field the last sequential TSN it has received from the peer.
5229 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5230 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5231 * with the updated last sequential TSN received from its peer.
5232 *
5233 * The return value is the disposition.
5234 */
5235sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005236 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 const struct sctp_endpoint *ep,
5238 const struct sctp_association *asoc,
5239 const sctp_subtype_t type,
5240 void *arg,
5241 sctp_cmd_seq_t *commands)
5242{
5243 struct sctp_chunk *reply;
5244
5245 /* Once all its outstanding data has been acknowledged, the
5246 * endpoint shall send a SHUTDOWN chunk to its peer including
5247 * in the Cumulative TSN Ack field the last sequential TSN it
5248 * has received from the peer.
5249 */
5250 reply = sctp_make_shutdown(asoc, NULL);
5251 if (!reply)
5252 goto nomem;
5253
5254 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5255 * T2-shutdown timer.
5256 */
5257 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5258
5259 /* It shall then start the T2-shutdown timer */
5260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5261 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5262
Wei Yongjun536428a2008-09-05 08:55:26 +08005263 /* RFC 4960 Section 9.2
5264 * The sender of the SHUTDOWN MAY also start an overall guard timer
5265 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5266 */
Thomas Graff8d96052011-07-07 00:28:35 +00005267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5269
Neil Horman9f70f462013-12-10 06:48:15 -05005270 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5272 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5273
5274 /* and enter the SHUTDOWN-SENT state. */
5275 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5276 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5277
5278 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5279 *
5280 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005281 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 */
5283 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5284
5285 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5286
5287 return SCTP_DISPOSITION_CONSUME;
5288
5289nomem:
5290 return SCTP_DISPOSITION_NOMEM;
5291}
5292
5293/*
5294 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5295 *
5296 * From Section 9.2:
5297 *
5298 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5299 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5300 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5301 * endpoint must re-send the SHUTDOWN ACK.
5302 *
5303 * The return value is the disposition.
5304 */
5305sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005306 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 const struct sctp_endpoint *ep,
5308 const struct sctp_association *asoc,
5309 const sctp_subtype_t type,
5310 void *arg,
5311 sctp_cmd_seq_t *commands)
5312{
5313 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5314 struct sctp_chunk *reply;
5315
5316 /* There are 2 ways of getting here:
5317 * 1) called in response to a SHUTDOWN chunk
5318 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5319 *
5320 * For the case (2), the arg parameter is set to NULL. We need
5321 * to check that we have a chunk before accessing it's fields.
5322 */
5323 if (chunk) {
5324 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005325 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 /* Make sure that the SHUTDOWN chunk has a valid length. */
5328 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005329 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 commands);
5331 }
5332
5333 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5334 * shall send a SHUTDOWN ACK ...
5335 */
5336 reply = sctp_make_shutdown_ack(asoc, chunk);
5337 if (!reply)
5338 goto nomem;
5339
5340 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5341 * the T2-shutdown timer.
5342 */
5343 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5344
5345 /* and start/restart a T2-shutdown timer of its own, */
5346 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5347 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5348
Neil Horman9f70f462013-12-10 06:48:15 -05005349 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5351 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5352
5353 /* Enter the SHUTDOWN-ACK-SENT state. */
5354 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5355 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5356
5357 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5358 *
5359 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005360 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 */
5362 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5363
5364 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5365
5366 return SCTP_DISPOSITION_CONSUME;
5367
5368nomem:
5369 return SCTP_DISPOSITION_NOMEM;
5370}
5371
5372/*
5373 * Ignore the event defined as other
5374 *
5375 * The return value is the disposition of the event.
5376 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005377sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5378 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 const struct sctp_association *asoc,
5380 const sctp_subtype_t type,
5381 void *arg,
5382 sctp_cmd_seq_t *commands)
5383{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005384 pr_debug("%s: the event other type:%d is ignored\n",
5385 __func__, type.other);
5386
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 return SCTP_DISPOSITION_DISCARD;
5388}
5389
5390/************************************************************
5391 * These are the state functions for handling timeout events.
5392 ************************************************************/
5393
5394/*
5395 * RTX Timeout
5396 *
5397 * Section: 6.3.3 Handle T3-rtx Expiration
5398 *
5399 * Whenever the retransmission timer T3-rtx expires for a destination
5400 * address, do the following:
5401 * [See below]
5402 *
5403 * The return value is the disposition of the chunk.
5404 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005405sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5406 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 const struct sctp_association *asoc,
5408 const sctp_subtype_t type,
5409 void *arg,
5410 sctp_cmd_seq_t *commands)
5411{
5412 struct sctp_transport *transport = arg;
5413
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005414 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005415
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (asoc->overall_error_count >= asoc->max_retrans) {
lucien8a0d19c2015-12-05 15:35:36 +08005417 if (asoc->peer.zero_window_announced &&
5418 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
Thomas Graff8d96052011-07-07 00:28:35 +00005419 /*
5420 * We are here likely because the receiver had its rwnd
5421 * closed for a while and we have not been able to
5422 * transmit the locally queued data within the maximum
5423 * retransmission attempts limit. Start the T5
5424 * shutdown guard timer to give the receiver one last
5425 * chance and some additional time to recover before
5426 * aborting.
5427 */
5428 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5429 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5430 } else {
5431 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5432 SCTP_ERROR(ETIMEDOUT));
5433 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5434 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5435 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005436 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5437 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005438 return SCTP_DISPOSITION_DELETE_TCB;
5439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 }
5441
5442 /* E1) For the destination address for which the timer
5443 * expires, adjust its ssthresh with rules defined in Section
5444 * 7.2.3 and set the cwnd <- MTU.
5445 */
5446
5447 /* E2) For the destination address for which the timer
5448 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5449 * maximum value discussed in rule C7 above (RTO.max) may be
5450 * used to provide an upper bound to this doubling operation.
5451 */
5452
5453 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5454 * outstanding DATA chunks for the address for which the
5455 * T3-rtx has expired will fit into a single packet, subject
5456 * to the MTU constraint for the path corresponding to the
5457 * destination transport address to which the retransmission
5458 * is being sent (this may be different from the address for
5459 * which the timer expires [see Section 6.4]). Call this
5460 * value K. Bundle and retransmit those K DATA chunks in a
5461 * single packet to the destination endpoint.
5462 *
5463 * Note: Any DATA chunks that were sent to the address for
5464 * which the T3-rtx timer expired but did not fit in one MTU
5465 * (rule E3 above), should be marked for retransmission and
5466 * sent as soon as cwnd allows (normally when a SACK arrives).
5467 */
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 /* Do some failure management (Section 8.2). */
5470 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5471
Vlad Yasevich1845a572007-02-21 02:06:19 -08005472 /* NB: Rules E4 and F1 are implicit in R1. */
5473 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 return SCTP_DISPOSITION_CONSUME;
5476}
5477
5478/*
5479 * Generate delayed SACK on timeout
5480 *
5481 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5482 *
5483 * The guidelines on delayed acknowledgement algorithm specified in
5484 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5485 * acknowledgement SHOULD be generated for at least every second packet
5486 * (not every second DATA chunk) received, and SHOULD be generated
5487 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5488 * some situations it may be beneficial for an SCTP transmitter to be
5489 * more conservative than the algorithms detailed in this document
5490 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5491 * the following algorithms allow.
5492 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005493sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5494 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 const struct sctp_association *asoc,
5496 const sctp_subtype_t type,
5497 void *arg,
5498 sctp_cmd_seq_t *commands)
5499{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005500 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5502 return SCTP_DISPOSITION_CONSUME;
5503}
5504
5505/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005506 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 *
5508 * Section: 4 Note: 2
5509 * Verification Tag:
5510 * Inputs
5511 * (endpoint, asoc)
5512 *
5513 * RFC 2960 Section 4 Notes
5514 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5515 * and re-start the T1-init timer without changing state. This MUST
5516 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5517 * endpoint MUST abort the initialization process and report the
5518 * error to SCTP user.
5519 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005520 * Outputs
5521 * (timers, events)
5522 *
5523 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005524sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5525 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005526 const struct sctp_association *asoc,
5527 const sctp_subtype_t type,
5528 void *arg,
5529 sctp_cmd_seq_t *commands)
5530{
5531 struct sctp_chunk *repl = NULL;
5532 struct sctp_bind_addr *bp;
5533 int attempts = asoc->init_err_counter + 1;
5534
Daniel Borkmannbb333812013-06-28 19:49:40 +02005535 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5536
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005537 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005538
Vlad Yasevich81845c22006-01-30 15:59:54 -08005539 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005540 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5541 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5542 if (!repl)
5543 return SCTP_DISPOSITION_NOMEM;
5544
5545 /* Choose transport for INIT. */
5546 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5547 SCTP_CHUNK(repl));
5548
5549 /* Issue a sideeffect to do the needed accounting. */
5550 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5551 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5552
5553 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5554 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005555 pr_debug("%s: giving up on INIT, attempts:%d "
5556 "max_init_attempts:%d\n", __func__, attempts,
5557 asoc->max_init_attempts);
5558
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005559 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5560 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005561 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005562 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005563 return SCTP_DISPOSITION_DELETE_TCB;
5564 }
5565
5566 return SCTP_DISPOSITION_CONSUME;
5567}
5568
5569/*
5570 * sctp_sf_t1_cookie_timer_expire
5571 *
5572 * Section: 4 Note: 2
5573 * Verification Tag:
5574 * Inputs
5575 * (endpoint, asoc)
5576 *
5577 * RFC 2960 Section 4 Notes
5578 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 * COOKIE ECHO and re-start the T1-cookie timer without changing
5580 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5581 * After that, the endpoint MUST abort the initialization process and
5582 * report the error to SCTP user.
5583 *
5584 * Outputs
5585 * (timers, events)
5586 *
5587 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005588sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5589 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 const struct sctp_association *asoc,
5591 const sctp_subtype_t type,
5592 void *arg,
5593 sctp_cmd_seq_t *commands)
5594{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005595 struct sctp_chunk *repl = NULL;
5596 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Daniel Borkmannbb333812013-06-28 19:49:40 +02005598 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5599
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005600 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Vlad Yasevich81845c22006-01-30 15:59:54 -08005602 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005603 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005605 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005607 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5608 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005610 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5611 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5614 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005615 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5616 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005618 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 return SCTP_DISPOSITION_DELETE_TCB;
5620 }
5621
5622 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623}
5624
5625/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5626 * with the updated last sequential TSN received from its peer.
5627 *
5628 * An endpoint should limit the number of retransmissions of the
5629 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5630 * If this threshold is exceeded the endpoint should destroy the TCB and
5631 * MUST report the peer endpoint unreachable to the upper layer (and
5632 * thus the association enters the CLOSED state). The reception of any
5633 * packet from its peer (i.e. as the peer sends all of its queued DATA
5634 * chunks) should clear the endpoint's retransmission count and restart
5635 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5636 * all of its queued DATA chunks that have not yet been sent.
5637 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005638sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5639 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 const struct sctp_association *asoc,
5641 const sctp_subtype_t type,
5642 void *arg,
5643 sctp_cmd_seq_t *commands)
5644{
5645 struct sctp_chunk *reply = NULL;
5646
Daniel Borkmannbb333812013-06-28 19:49:40 +02005647 pr_debug("%s: timer T2 expired\n", __func__);
5648
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005649 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005650
Neil Horman58fbbed2008-02-29 11:40:56 -08005651 ((struct sctp_association *)asoc)->shutdown_retries++;
5652
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005654 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5655 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5657 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005658 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005659 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5660 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 return SCTP_DISPOSITION_DELETE_TCB;
5662 }
5663
5664 switch (asoc->state) {
5665 case SCTP_STATE_SHUTDOWN_SENT:
5666 reply = sctp_make_shutdown(asoc, NULL);
5667 break;
5668
5669 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5670 reply = sctp_make_shutdown_ack(asoc, NULL);
5671 break;
5672
5673 default:
5674 BUG();
5675 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 if (!reply)
5679 goto nomem;
5680
Wei Yongjun6345b192009-04-26 23:13:35 +08005681 /* Do some failure management (Section 8.2).
5682 * If we remove the transport an SHUTDOWN was last sent to, don't
5683 * do failure management.
5684 */
5685 if (asoc->shutdown_last_sent_to)
5686 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5687 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688
5689 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5690 * the T2-shutdown timer.
5691 */
5692 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5693
5694 /* Restart the T2-shutdown timer. */
5695 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5696 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5697 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5698 return SCTP_DISPOSITION_CONSUME;
5699
5700nomem:
5701 return SCTP_DISPOSITION_NOMEM;
5702}
5703
5704/*
5705 * ADDIP Section 4.1 ASCONF CHunk Procedures
5706 * If the T4 RTO timer expires the endpoint should do B1 to B5
5707 */
5708sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005709 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 const struct sctp_endpoint *ep,
5711 const struct sctp_association *asoc,
5712 const sctp_subtype_t type,
5713 void *arg,
5714 sctp_cmd_seq_t *commands)
5715{
5716 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5717 struct sctp_transport *transport = chunk->transport;
5718
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005719 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5722 * detection on the appropriate destination address as defined in
5723 * RFC2960 [5] section 8.1 and 8.2.
5724 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005725 if (transport)
5726 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5727 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728
5729 /* Reconfig T4 timer and transport. */
5730 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5731
5732 /* ADDIP 4.1 B2) Increment the association error counters and perform
5733 * endpoint failure detection on the association as defined in
5734 * RFC2960 [5] section 8.1 and 8.2.
5735 * association error counter is incremented in SCTP_CMD_STRIKE.
5736 */
5737 if (asoc->overall_error_count >= asoc->max_retrans) {
5738 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5739 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005740 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5741 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005743 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005744 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5745 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 return SCTP_DISPOSITION_ABORT;
5747 }
5748
5749 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5750 * the ASCONF chunk was sent by doubling the RTO timer value.
5751 * This is done in SCTP_CMD_STRIKE.
5752 */
5753
5754 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5755 * choose an alternate destination address (please refer to RFC2960
5756 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005757 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 * ASCONF sent.
5759 */
5760 sctp_chunk_hold(asoc->addip_last_asconf);
5761 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5762 SCTP_CHUNK(asoc->addip_last_asconf));
5763
5764 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5765 * destination is selected, then the RTO used will be that of the new
5766 * destination address.
5767 */
5768 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5769 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5770
5771 return SCTP_DISPOSITION_CONSUME;
5772}
5773
5774/* sctpimpguide-05 Section 2.12.2
5775 * The sender of the SHUTDOWN MAY also start an overall guard timer
5776 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5777 * At the expiration of this timer the sender SHOULD abort the association
5778 * by sending an ABORT chunk.
5779 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005780sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5781 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 const struct sctp_association *asoc,
5783 const sctp_subtype_t type,
5784 void *arg,
5785 sctp_cmd_seq_t *commands)
5786{
5787 struct sctp_chunk *reply = NULL;
5788
Daniel Borkmannbb333812013-06-28 19:49:40 +02005789 pr_debug("%s: timer T5 expired\n", __func__);
5790
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005791 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792
5793 reply = sctp_make_abort(asoc, NULL, 0);
5794 if (!reply)
5795 goto nomem;
5796
5797 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005798 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5799 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005801 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005803 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5804 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005805
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 return SCTP_DISPOSITION_DELETE_TCB;
5807nomem:
5808 return SCTP_DISPOSITION_NOMEM;
5809}
5810
5811/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5812 * the association is automatically closed by starting the shutdown process.
5813 * The work that needs to be done is same as when SHUTDOWN is initiated by
5814 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5815 */
5816sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005817 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 const struct sctp_endpoint *ep,
5819 const struct sctp_association *asoc,
5820 const sctp_subtype_t type,
5821 void *arg,
5822 sctp_cmd_seq_t *commands)
5823{
5824 int disposition;
5825
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005826 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005827
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 /* From 9.2 Shutdown of an Association
5829 * Upon receipt of the SHUTDOWN primitive from its upper
5830 * layer, the endpoint enters SHUTDOWN-PENDING state and
5831 * remains there until all outstanding data has been
5832 * acknowledged by its peer. The endpoint accepts no new data
5833 * from its upper layer, but retransmits data to the far end
5834 * if necessary to fill gaps.
5835 */
5836 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5837 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 disposition = SCTP_DISPOSITION_CONSUME;
5840 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005841 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005842 arg, commands);
5843 }
5844 return disposition;
5845}
5846
5847/*****************************************************************************
5848 * These are sa state functions which could apply to all types of events.
5849 ****************************************************************************/
5850
5851/*
5852 * This table entry is not implemented.
5853 *
5854 * Inputs
5855 * (endpoint, asoc, chunk)
5856 *
5857 * The return value is the disposition of the chunk.
5858 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005859sctp_disposition_t sctp_sf_not_impl(struct net *net,
5860 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 const struct sctp_association *asoc,
5862 const sctp_subtype_t type,
5863 void *arg,
5864 sctp_cmd_seq_t *commands)
5865{
5866 return SCTP_DISPOSITION_NOT_IMPL;
5867}
5868
5869/*
5870 * This table entry represents a bug.
5871 *
5872 * Inputs
5873 * (endpoint, asoc, chunk)
5874 *
5875 * The return value is the disposition of the chunk.
5876 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005877sctp_disposition_t sctp_sf_bug(struct net *net,
5878 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 const struct sctp_association *asoc,
5880 const sctp_subtype_t type,
5881 void *arg,
5882 sctp_cmd_seq_t *commands)
5883{
5884 return SCTP_DISPOSITION_BUG;
5885}
5886
5887/*
5888 * This table entry represents the firing of a timer in the wrong state.
5889 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5890 * when the association is in the wrong state. This event should
5891 * be ignored, so as to prevent any rearming of the timer.
5892 *
5893 * Inputs
5894 * (endpoint, asoc, chunk)
5895 *
5896 * The return value is the disposition of the chunk.
5897 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005898sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5899 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 const struct sctp_association *asoc,
5901 const sctp_subtype_t type,
5902 void *arg,
5903 sctp_cmd_seq_t *commands)
5904{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005905 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5906
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 return SCTP_DISPOSITION_CONSUME;
5908}
5909
5910/********************************************************************
5911 * 2nd Level Abstractions
5912 ********************************************************************/
5913
5914/* Pull the SACK chunk based on the SACK header. */
5915static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5916{
5917 struct sctp_sackhdr *sack;
5918 unsigned int len;
5919 __u16 num_blocks;
5920 __u16 num_dup_tsns;
5921
5922 /* Protect ourselves from reading too far into
5923 * the skb from a bogus sender.
5924 */
5925 sack = (struct sctp_sackhdr *) chunk->skb->data;
5926
5927 num_blocks = ntohs(sack->num_gap_ack_blocks);
5928 num_dup_tsns = ntohs(sack->num_dup_tsns);
5929 len = sizeof(struct sctp_sackhdr);
5930 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5931 if (len > chunk->skb->len)
5932 return NULL;
5933
5934 skb_pull(chunk->skb, len);
5935
5936 return sack;
5937}
5938
5939/* Create an ABORT packet to be sent as a response, with the specified
5940 * error causes.
5941 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005942static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5943 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 const struct sctp_association *asoc,
5945 struct sctp_chunk *chunk,
5946 const void *payload,
5947 size_t paylen)
5948{
5949 struct sctp_packet *packet;
5950 struct sctp_chunk *abort;
5951
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005952 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953
5954 if (packet) {
5955 /* Make an ABORT.
5956 * The T bit will be set if the asoc is NULL.
5957 */
5958 abort = sctp_make_abort(asoc, chunk, paylen);
5959 if (!abort) {
5960 sctp_ootb_pkt_free(packet);
5961 return NULL;
5962 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005963
5964 /* Reflect vtag if T-Bit is set */
5965 if (sctp_test_T_bit(abort))
5966 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5967
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 /* Add specified error causes, i.e., payload, to the
5969 * end of the chunk.
5970 */
5971 sctp_addto_chunk(abort, paylen, payload);
5972
5973 /* Set the skb to the belonging sock for accounting. */
5974 abort->skb->sk = ep->base.sk;
5975
5976 sctp_packet_append_chunk(packet, abort);
5977
5978 }
5979
5980 return packet;
5981}
5982
5983/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005984static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5985 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 const struct sctp_chunk *chunk)
5987{
5988 struct sctp_packet *packet;
5989 struct sctp_transport *transport;
5990 __u16 sport;
5991 __u16 dport;
5992 __u32 vtag;
5993
5994 /* Get the source and destination port from the inbound packet. */
5995 sport = ntohs(chunk->sctp_hdr->dest);
5996 dport = ntohs(chunk->sctp_hdr->source);
5997
5998 /* The V-tag is going to be the same as the inbound packet if no
5999 * association exists, otherwise, use the peer's vtag.
6000 */
6001 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006002 /* Special case the INIT-ACK as there is no peer's vtag
6003 * yet.
6004 */
wangweidongcb3f8372013-12-23 12:16:50 +08006005 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006006 case SCTP_CID_INIT_ACK:
6007 {
6008 sctp_initack_chunk_t *initack;
6009
6010 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6011 vtag = ntohl(initack->init_hdr.init_tag);
6012 break;
6013 }
6014 default:
6015 vtag = asoc->peer.i.init_tag;
6016 break;
6017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 } else {
6019 /* Special case the INIT and stale COOKIE_ECHO as there is no
6020 * vtag yet.
6021 */
wangweidongcb3f8372013-12-23 12:16:50 +08006022 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 case SCTP_CID_INIT:
6024 {
6025 sctp_init_chunk_t *init;
6026
6027 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6028 vtag = ntohl(init->init_hdr.init_tag);
6029 break;
6030 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006031 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 vtag = ntohl(chunk->sctp_hdr->vtag);
6033 break;
6034 }
6035 }
6036
6037 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006038 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 if (!transport)
6040 goto nomem;
6041
6042 /* Cache a route for the transport with the chunk's destination as
6043 * the source address.
6044 */
Al Viro16b0a032006-11-20 17:13:38 -08006045 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006046 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
6048 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6049 packet = sctp_packet_config(packet, vtag, 0);
6050
6051 return packet;
6052
6053nomem:
6054 return NULL;
6055}
6056
6057/* Free the packet allocated earlier for responding in the OOTB condition. */
6058void sctp_ootb_pkt_free(struct sctp_packet *packet)
6059{
6060 sctp_transport_free(packet->transport);
6061}
6062
6063/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006064static void sctp_send_stale_cookie_err(struct net *net,
6065 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 const struct sctp_association *asoc,
6067 const struct sctp_chunk *chunk,
6068 sctp_cmd_seq_t *commands,
6069 struct sctp_chunk *err_chunk)
6070{
6071 struct sctp_packet *packet;
6072
6073 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006074 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 if (packet) {
6076 struct sctp_signed_cookie *cookie;
6077
6078 /* Override the OOTB vtag from the cookie. */
6079 cookie = chunk->subh.cookie_hdr;
6080 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 /* Set the skb to the belonging sock for accounting. */
6083 err_chunk->skb->sk = ep->base.sk;
6084 sctp_packet_append_chunk(packet, err_chunk);
6085 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6086 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006087 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 } else
6089 sctp_chunk_free (err_chunk);
6090 }
6091}
6092
6093
6094/* Process a data chunk */
6095static int sctp_eat_data(const struct sctp_association *asoc,
6096 struct sctp_chunk *chunk,
6097 sctp_cmd_seq_t *commands)
6098{
6099 sctp_datahdr_t *data_hdr;
6100 struct sctp_chunk *err;
6101 size_t datalen;
6102 sctp_verb_t deliver;
6103 int tmp;
6104 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006105 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006106 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006107 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006108 u16 ssn;
6109 u16 sid;
6110 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111
6112 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6113 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6114
6115 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006116 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
6118 /* ASSERT: Now skb->data is really the user data. */
6119
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 /* Process ECN based congestion.
6121 *
6122 * Since the chunk structure is reused for all chunks within
6123 * a packet, we use ecn_ce_done to track if we've already
6124 * done CE processing for this packet.
6125 *
6126 * We need to do ECN processing even if we plan to discard the
6127 * chunk later.
6128 */
6129
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006130 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
Marcelo Ricardo Leitnere7487c82016-07-13 15:08:58 -03006131 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 chunk->ecn_ce_done = 1;
6133
Marcelo Ricardo Leitner2d47fd12016-07-13 15:09:00 -03006134 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 /* Do real work as sideffect. */
6136 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6137 SCTP_U32(tsn));
6138 }
6139 }
6140
6141 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6142 if (tmp < 0) {
6143 /* The TSN is too high--silently discard the chunk and
6144 * count on it getting retransmitted later.
6145 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006146 if (chunk->asoc)
6147 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 return SCTP_IERROR_HIGH_TSN;
6149 } else if (tmp > 0) {
6150 /* This is a duplicate. Record it. */
6151 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6152 return SCTP_IERROR_DUP_TSN;
6153 }
6154
6155 /* This is a new TSN. */
6156
6157 /* Discard if there is no room in the receive window.
6158 * Actually, allow a little bit of overflow (up to a MTU).
6159 */
6160 datalen = ntohs(chunk->chunk_hdr->length);
6161 datalen -= sizeof(sctp_data_chunk_t);
6162
6163 deliver = SCTP_CMD_CHUNK_ULP;
6164
6165 /* Think about partial delivery. */
6166 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6167
6168 /* Even if we don't accept this chunk there is
6169 * memory pressure.
6170 */
6171 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6172 }
6173
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006174 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 * seems a bit troublesome in that frag_point varies based on
6176 * PMTU. In cases, such as loopback, this might be a rather
6177 * large spill over.
6178 */
Daniel Borkmann362d5202014-04-14 21:45:17 +02006179 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
Neil Horman4d93df02007-08-15 16:07:44 -07006180 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181
6182 /* If this is the next TSN, consider reneging to make
6183 * room. Note: Playing nice with a confused sender. A
6184 * malicious sender can still eat up all our buffer
6185 * space and in the future we may want to detect and
6186 * do more drastic reneging.
6187 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006188 if (sctp_tsnmap_has_gap(map) &&
6189 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006190 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 deliver = SCTP_CMD_RENEGE;
6192 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006193 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6194 __func__, tsn, datalen, asoc->rwnd);
6195
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return SCTP_IERROR_IGNORE_TSN;
6197 }
6198 }
6199
6200 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006201 * Also try to renege to limit our memory usage in the event that
6202 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006203 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006204 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6205 * memory usage too much
6206 */
6207 if (*sk->sk_prot_creator->memory_pressure) {
6208 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006209 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006210 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6211 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006212 deliver = SCTP_CMD_RENEGE;
6213 }
6214 }
6215
6216 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 * Section 3.3.10.9 No User Data (9)
6218 *
6219 * Cause of error
6220 * ---------------
6221 * No User Data: This error cause is returned to the originator of a
6222 * DATA chunk if a received DATA chunk has no user data.
6223 */
6224 if (unlikely(0 == datalen)) {
6225 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6226 if (err) {
6227 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6228 SCTP_CHUNK(err));
6229 }
6230 /* We are going to ABORT, so we might as well stop
6231 * processing the rest of the chunks in the packet.
6232 */
wangweidongcb3f8372013-12-23 12:16:50 +08006233 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006234 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6235 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006237 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006238 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6239 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 return SCTP_IERROR_NO_DATA;
6241 }
6242
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006243 chunk->data_accepted = 1;
6244
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6246 * if we renege and the chunk arrives again.
6247 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006248 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006249 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006250 if (chunk->asoc)
6251 chunk->asoc->stats.iuodchunks++;
6252 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006253 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006254 if (chunk->asoc)
6255 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006256 ordered = 1;
6257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258
6259 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6260 *
6261 * If an endpoint receive a DATA chunk with an invalid stream
6262 * identifier, it shall acknowledge the reception of the DATA chunk
6263 * following the normal procedure, immediately send an ERROR chunk
6264 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6265 * and discard the DATA chunk.
6266 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006267 sid = ntohs(data_hdr->stream);
6268 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006269 /* Mark tsn as received even though we drop it */
6270 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6271
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6273 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006274 sizeof(data_hdr->stream),
6275 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 if (err)
6277 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6278 SCTP_CHUNK(err));
6279 return SCTP_IERROR_BAD_STREAM;
6280 }
6281
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006282 /* Check to see if the SSN is possible for this TSN.
6283 * The biggest gap we can record is 4K wide. Since SSNs wrap
6284 * at an unsigned short, there is no way that an SSN can
6285 * wrap and for a valid TSN. We can simply check if the current
6286 * SSN is smaller then the next expected one. If it is, it wrapped
6287 * and is invalid.
6288 */
6289 ssn = ntohs(data_hdr->ssn);
6290 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6291 return SCTP_IERROR_PROTO_VIOLATION;
6292 }
6293
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 /* Send the data up to the user. Note: Schedule the
6295 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6296 * chunk needs the updated rwnd.
6297 */
6298 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6299
6300 return SCTP_IERROR_NO_ERROR;
6301}