blob: c52763a2629719d38afa624f85cd83756d51969f [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
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
28 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 *
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
35 *
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/types.h>
56#include <linux/kernel.h>
57#include <linux/ip.h>
58#include <linux/ipv6.h>
59#include <linux/net.h>
60#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/sock.h>
63#include <net/inet_ecn.h>
64#include <linux/skbuff.h>
65#include <net/sctp/sctp.h>
66#include <net/sctp/sm.h>
67#include <net/sctp/structs.h>
68
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000069static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
70 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 const void *payload,
74 size_t paylen);
75static int sctp_eat_data(const struct sctp_association *asoc,
76 struct sctp_chunk *chunk,
77 sctp_cmd_seq_t *commands);
Eric W. Biederman2ce955032012-08-06 08:43:06 +000078static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
79 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000081static void sctp_send_stale_cookie_err(struct net *net,
82 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 const struct sctp_association *asoc,
84 const struct sctp_chunk *chunk,
85 sctp_cmd_seq_t *commands,
86 struct sctp_chunk *err_chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000087static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
88 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 const struct sctp_association *asoc,
90 const sctp_subtype_t type,
91 void *arg,
92 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000093static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
94 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 const struct sctp_association *asoc,
96 const sctp_subtype_t type,
97 void *arg,
98 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000099static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
100 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400101 const struct sctp_association *asoc,
102 const sctp_subtype_t type,
103 void *arg,
104 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
106
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000107static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
108 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800109 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700110 const struct sctp_association *asoc,
111 struct sctp_transport *transport);
112
Wei Yongjunaecedea2007-08-02 16:57:44 +0800113static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000114 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400115 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800116 const struct sctp_association *asoc,
117 void *arg,
118 sctp_cmd_seq_t *commands,
119 const __u8 *payload,
120 const size_t paylen);
121
Adrian Bunk52c1da32005-06-23 22:05:33 -0700122static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000123 struct net *net,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700124 const struct sctp_endpoint *ep,
125 const struct sctp_association *asoc,
126 const sctp_subtype_t type,
127 void *arg,
128 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800130static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000131 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800132 const struct sctp_endpoint *ep,
133 const struct sctp_association *asoc,
134 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700135 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800136 sctp_cmd_seq_t *commands);
137
Wei Yongjunaecedea2007-08-02 16:57:44 +0800138static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000139 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800140 const struct sctp_endpoint *ep,
141 const struct sctp_association *asoc,
142 const sctp_subtype_t type,
143 void *arg,
144 sctp_cmd_seq_t *commands);
145
Vlad Yasevichece25df2007-09-07 16:30:54 -0400146static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000147 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400148 const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 void *arg,
152 sctp_cmd_seq_t *commands);
153
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000154static sctp_ierror_t sctp_sf_authenticate(struct net *net,
155 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700156 const struct sctp_association *asoc,
157 const sctp_subtype_t type,
158 struct sctp_chunk *chunk);
159
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000160static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
161 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800162 const struct sctp_association *asoc,
163 const sctp_subtype_t type,
164 void *arg,
165 sctp_cmd_seq_t *commands);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Small helper function that checks if the chunk length
168 * is of the appropriate length. The 'required_length' argument
169 * is set to be the size of a specific chunk we are testing.
170 * Return Values: 1 = Valid length
171 * 0 = Invalid length
172 *
173 */
174static inline int
175sctp_chunk_length_valid(struct sctp_chunk *chunk,
176 __u16 required_length)
177{
178 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
179
Daniel Borkmannbf539322014-10-09 22:55:33 +0200180 /* Previously already marked? */
181 if (unlikely(chunk->pdiscard))
182 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (unlikely(chunk_length < required_length))
184 return 0;
185
186 return 1;
187}
188
189/**********************************************************
190 * These are the state functions for handling chunk events.
191 **********************************************************/
192
193/*
194 * Process the final SHUTDOWN COMPLETE.
195 *
196 * Section: 4 (C) (diagram), 9.2
197 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
198 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
199 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
200 * should stop the T2-shutdown timer and remove all knowledge of the
201 * association (and thus the association enters the CLOSED state).
202 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700203 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * C) Rules for packet carrying SHUTDOWN COMPLETE:
205 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700206 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
207 * if the Verification Tag field of the packet matches its own tag and
208 * the T bit is not set
209 * OR
210 * it is set to its peer's tag and the T bit is set in the Chunk
211 * Flags.
212 * Otherwise, the receiver MUST silently discard the packet
213 * and take no further action. An endpoint MUST ignore the
214 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *
216 * Inputs
217 * (endpoint, asoc, chunk)
218 *
219 * Outputs
220 * (asoc, reply_msg, msg_up, timers, counters)
221 *
222 * The return value is the disposition of the chunk.
223 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000224sctp_disposition_t sctp_sf_do_4_C(struct net *net,
225 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 const struct sctp_association *asoc,
227 const sctp_subtype_t type,
228 void *arg,
229 sctp_cmd_seq_t *commands)
230{
231 struct sctp_chunk *chunk = arg;
232 struct sctp_ulpevent *ev;
233
Vlad Yasevichece25df2007-09-07 16:30:54 -0400234 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000235 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 /* RFC 2960 6.10 Bundling
238 *
239 * An endpoint MUST NOT bundle INIT, INIT ACK or
240 * SHUTDOWN COMPLETE with any other chunks.
241 */
242 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000243 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Vlad Yasevichece25df2007-09-07 16:30:54 -0400245 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
246 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000247 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400248 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 /* RFC 2960 10.2 SCTP-to-ULP
251 *
252 * H) SHUTDOWN COMPLETE notification
253 *
254 * When SCTP completes the shutdown procedures (section 9.2) this
255 * notification is passed to the upper layer.
256 */
257 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700258 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700259 if (ev)
260 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900261 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
264 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
265 * not the chunk should be discarded. If the endpoint is in
266 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
267 * T2-shutdown timer and remove all knowledge of the
268 * association (and thus the association enters the CLOSED
269 * state).
270 */
271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
272 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
273
274 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
275 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
276
277 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
278 SCTP_STATE(SCTP_STATE_CLOSED));
279
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000280 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
281 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
284
285 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288/*
289 * Respond to a normal INIT chunk.
290 * We are the side that is being asked for an association.
291 *
292 * Section: 5.1 Normal Establishment of an Association, B
293 * B) "Z" shall respond immediately with an INIT ACK chunk. The
294 * destination IP address of the INIT ACK MUST be set to the source
295 * IP address of the INIT to which this INIT ACK is responding. In
296 * the response, besides filling in other parameters, "Z" must set the
297 * Verification Tag field to Tag_A, and also provide its own
298 * Verification Tag (Tag_Z) in the Initiate Tag field.
299 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900300 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
302 * Inputs
303 * (endpoint, asoc, chunk)
304 *
305 * Outputs
306 * (asoc, reply_msg, msg_up, timers, counters)
307 *
308 * The return value is the disposition of the chunk.
309 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000310sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
311 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 const struct sctp_association *asoc,
313 const sctp_subtype_t type,
314 void *arg,
315 sctp_cmd_seq_t *commands)
316{
317 struct sctp_chunk *chunk = arg;
318 struct sctp_chunk *repl;
319 struct sctp_association *new_asoc;
320 struct sctp_chunk *err_chunk;
321 struct sctp_packet *packet;
322 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int len;
324
325 /* 6.10 Bundling
326 * An endpoint MUST NOT bundle INIT, INIT ACK or
327 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900328 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 * IG Section 2.11.2
330 * Furthermore, we require that the receiver of an INIT chunk MUST
331 * enforce these rules by silently discarding an arriving packet
332 * with an INIT chunk that is bundled with other chunks.
333 */
334 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000335 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* If the packet is an OOTB packet which is temporarily on the
338 * control endpoint, respond with an ABORT.
339 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +0000340 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000341 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000342 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900346 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 */
348 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000349 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* Make sure that the INIT chunk has a valid length.
352 * Normally, this would cause an ABORT with a Protocol Violation
353 * error, but since we don't have an association, we'll
354 * just discard the packet.
355 */
356 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000357 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Vlad Yasevichbec96402009-07-30 18:08:28 -0400359 /* If the INIT is coming toward a closing socket, we'll send back
360 * and ABORT. Essentially, this catches the race of INIT being
361 * backloged to the socket at the same time as the user isses close().
362 * Since the socket and all its associations are going away, we
363 * can treat this OOTB
364 */
365 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000366 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Verify the INIT chunk before processing it. */
369 err_chunk = NULL;
Vlad Yaseviche5eae4a2014-04-17 17:26:50 +0200370 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
372 &err_chunk)) {
373 /* This chunk contains fatal error. It is to be discarded.
374 * Send an ABORT, with causes if there is any.
375 */
376 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000377 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 (__u8 *)(err_chunk->chunk_hdr) +
379 sizeof(sctp_chunkhdr_t),
380 ntohs(err_chunk->chunk_hdr->length) -
381 sizeof(sctp_chunkhdr_t));
382
383 sctp_chunk_free(err_chunk);
384
385 if (packet) {
386 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
387 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000388 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return SCTP_DISPOSITION_CONSUME;
390 } else {
391 return SCTP_DISPOSITION_NOMEM;
392 }
393 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000394 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 commands);
396 }
397 }
398
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900399 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
401
402 /* Tag the variable length parameters. */
403 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
404
405 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
406 if (!new_asoc)
407 goto nomem;
408
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000409 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
410 sctp_scope(sctp_source(chunk)),
411 GFP_ATOMIC) < 0)
412 goto nomem_init;
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000415 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 (sctp_init_chunk_t *)chunk->chunk_hdr,
417 GFP_ATOMIC))
418 goto nomem_init;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
421
422 /* If there are errors need to be reported for unknown parameters,
423 * make sure to reserve enough room in the INIT ACK for them.
424 */
425 len = 0;
426 if (err_chunk)
427 len = ntohs(err_chunk->chunk_hdr->length) -
428 sizeof(sctp_chunkhdr_t);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
431 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700432 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* If there are errors need to be reported for unknown parameters,
435 * include them in the outgoing INIT ACK as "Unrecognized parameter"
436 * parameter.
437 */
438 if (err_chunk) {
439 /* Get the "Unrecognized parameter" parameter(s) out of the
440 * ERROR chunk generated by sctp_verify_init(). Since the
441 * error cause code for "unknown parameter" and the
442 * "Unrecognized parameter" type is the same, we can
443 * construct the parameters in INIT ACK by copying the
444 * ERROR causes over.
445 */
446 unk_param = (sctp_unrecognized_param_t *)
447 ((__u8 *)(err_chunk->chunk_hdr) +
448 sizeof(sctp_chunkhdr_t));
449 /* Replace the cause code with the "Unrecognized parameter"
450 * parameter type.
451 */
452 sctp_addto_chunk(repl, len, unk_param);
453 sctp_chunk_free(err_chunk);
454 }
455
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700456 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
459
460 /*
461 * Note: After sending out INIT ACK with the State Cookie parameter,
462 * "Z" MUST NOT allocate any resources, nor keep any states for the
463 * new association. Otherwise, "Z" will be vulnerable to resource
464 * attacks.
465 */
466 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
467
468 return SCTP_DISPOSITION_DELETE_TCB;
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470nomem_init:
471 sctp_association_free(new_asoc);
472nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700473 if (err_chunk)
474 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return SCTP_DISPOSITION_NOMEM;
476}
477
478/*
479 * Respond to a normal INIT ACK chunk.
480 * We are the side that is initiating the association.
481 *
482 * Section: 5.1 Normal Establishment of an Association, C
483 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
484 * timer and leave COOKIE-WAIT state. "A" shall then send the State
485 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
486 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
487 *
488 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
489 * DATA chunks, but it MUST be the first chunk in the packet and
490 * until the COOKIE ACK is returned the sender MUST NOT send any
491 * other packets to the peer.
492 *
493 * Verification Tag: 3.3.3
494 * If the value of the Initiate Tag in a received INIT ACK chunk is
495 * found to be 0, the receiver MUST treat it as an error and close the
496 * association by transmitting an ABORT.
497 *
498 * Inputs
499 * (endpoint, asoc, chunk)
500 *
501 * Outputs
502 * (asoc, reply_msg, msg_up, timers, counters)
503 *
504 * The return value is the disposition of the chunk.
505 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000506sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
507 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 const struct sctp_association *asoc,
509 const sctp_subtype_t type,
510 void *arg,
511 sctp_cmd_seq_t *commands)
512{
513 struct sctp_chunk *chunk = arg;
514 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct sctp_chunk *err_chunk;
516 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000519 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* 6.10 Bundling
522 * An endpoint MUST NOT bundle INIT, INIT ACK or
523 * SHUTDOWN COMPLETE with any other chunks.
524 */
525 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000526 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Vlad Yasevichece25df2007-09-07 16:30:54 -0400528 /* Make sure that the INIT-ACK chunk has a valid length */
529 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000530 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400531 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Grab the INIT header. */
533 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 /* Verify the INIT chunk before processing it. */
536 err_chunk = NULL;
Vlad Yaseviche5eae4a2014-04-17 17:26:50 +0200537 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
539 &err_chunk)) {
540
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800541 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800544 * Send an ABORT, with causes. If there are no causes,
545 * then there wasn't enough memory. Just terminate
546 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
548 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000549 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 (__u8 *)(err_chunk->chunk_hdr) +
551 sizeof(sctp_chunkhdr_t),
552 ntohs(err_chunk->chunk_hdr->length) -
553 sizeof(sctp_chunkhdr_t));
554
555 sctp_chunk_free(err_chunk);
556
557 if (packet) {
558 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
559 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000560 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700561 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700564
565 /* SCTP-AUTH, Section 6.3:
566 * It should be noted that if the receiver wants to tear
567 * down an association in an authenticated way only, the
568 * handling of malformed packets should not result in
569 * tearing down the association.
570 *
571 * This means that if we only want to abort associations
572 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300573 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700574 * was malformed.
575 */
576 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000577 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700578
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000579 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
580 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700581 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 /* Tag the variable length parameters. Note that we never
585 * convert the parameters in an INIT chunk.
586 */
587 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
588
589 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
590
591 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
592 SCTP_PEER_INIT(initchunk));
593
Frank Filz3f7a87d2005-06-20 13:14:57 -0700594 /* Reset init error count upon receipt of INIT-ACK. */
595 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 /* 5.1 C) "A" shall stop the T1-init timer and leave
598 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
599 * timer, and enter the COOKIE-ECHOED state.
600 */
601 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
602 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
603 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
604 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
605 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
606 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
607
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700608 /* SCTP-AUTH: genereate the assocition shared keys so that
609 * we can potentially signe the COOKIE-ECHO.
610 */
611 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* 5.1 C) "A" shall then send the State Cookie received in the
614 * INIT ACK chunk in a COOKIE ECHO chunk, ...
615 */
616 /* If there is any errors to report, send the ERROR chunk generated
617 * for unknown parameters as well.
618 */
619 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
620 SCTP_CHUNK(err_chunk));
621
622 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625/*
626 * Respond to a normal COOKIE ECHO chunk.
627 * We are the side that is being asked for an association.
628 *
629 * Section: 5.1 Normal Establishment of an Association, D
630 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
631 * with a COOKIE ACK chunk after building a TCB and moving to
632 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
633 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
634 * chunk MUST be the first chunk in the packet.
635 *
636 * IMPLEMENTATION NOTE: An implementation may choose to send the
637 * Communication Up notification to the SCTP user upon reception
638 * of a valid COOKIE ECHO chunk.
639 *
640 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
641 * D) Rules for packet carrying a COOKIE ECHO
642 *
643 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
644 * Initial Tag received in the INIT ACK.
645 *
646 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
647 *
648 * Inputs
649 * (endpoint, asoc, chunk)
650 *
651 * Outputs
652 * (asoc, reply_msg, msg_up, timers, counters)
653 *
654 * The return value is the disposition of the chunk.
655 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000656sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
657 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 const struct sctp_association *asoc,
659 const sctp_subtype_t type, void *arg,
660 sctp_cmd_seq_t *commands)
661{
662 struct sctp_chunk *chunk = arg;
663 struct sctp_association *new_asoc;
664 sctp_init_chunk_t *peer_init;
665 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700666 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 int error = 0;
668 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900669 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* If the packet is an OOTB packet which is temporarily on the
672 * control endpoint, respond with an ABORT.
673 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +0000674 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000675 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000676 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 /* Make sure that the COOKIE_ECHO chunk has a valid length.
680 * In this case, we check that we have enough for at least a
681 * chunk header. More detailed verification is done
682 * in sctp_unpack_cookie().
683 */
684 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000685 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Vlad Yasevich609ee462007-08-31 03:10:59 +0900687 /* If the endpoint is not listening or if the number of associations
688 * on the TCP-style socket exceed the max backlog, respond with an
689 * ABORT.
690 */
691 sk = ep->base.sk;
692 if (!sctp_sstate(sk, LISTENING) ||
693 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000694 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 /* "Decode" the chunk. We have no optional parameters so we
697 * are in good shape.
698 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900699 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700701 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
702 sizeof(sctp_chunkhdr_t)))
703 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
706 * "Z" will reply with a COOKIE ACK chunk after building a TCB
707 * and moving to the ESTABLISHED state.
708 */
709 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
710 &err_chk_p);
711
712 /* FIXME:
713 * If the re-build failed, what is the proper error path
714 * from here?
715 *
716 * [We should abort the association. --piggy]
717 */
718 if (!new_asoc) {
719 /* FIXME: Several errors are possible. A bad cookie should
720 * be silently discarded, but think about logging it too.
721 */
722 switch (error) {
723 case -SCTP_IERROR_NOMEM:
724 goto nomem;
725
726 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000727 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000729 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 case -SCTP_IERROR_BAD_SIG:
732 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000733 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700738 /* Delay state machine commands until later.
739 *
740 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 * the sctp_unpack_cookie() already.
742 */
743 /* This is a brand-new association, so these are not yet side
744 * effects--it is safe to run them here.
745 */
746 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
747
Wei Yongjunde6becd2011-04-19 21:30:51 +0000748 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800749 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 peer_init, GFP_ATOMIC))
751 goto nomem_init;
752
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700753 /* SCTP-AUTH: Now that we've populate required fields in
754 * sctp_process_init, set up the assocaition shared keys as
755 * necessary so that we can potentially authenticate the ACK
756 */
757 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
758 if (error)
759 goto nomem_init;
760
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700761 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
762 * is supposed to be authenticated and we have to do delayed
763 * authentication. We've just recreated the association using
764 * the information in the cookie and now it's much easier to
765 * do the authentication.
766 */
767 if (chunk->auth_chunk) {
768 struct sctp_chunk auth;
769 sctp_ierror_t ret;
770
Daniel Borkmann892b46a2014-03-03 17:23:04 +0100771 /* Make sure that we and the peer are AUTH capable */
772 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
Daniel Borkmann892b46a2014-03-03 17:23:04 +0100773 sctp_association_free(new_asoc);
774 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
775 }
776
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700777 /* set-up our fake chunk so that we can process it */
778 auth.skb = chunk->auth_chunk;
779 auth.asoc = chunk->asoc;
780 auth.sctp_hdr = chunk->sctp_hdr;
781 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
782 sizeof(sctp_chunkhdr_t));
783 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
784 auth.transport = chunk->transport;
785
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000786 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700787 if (ret != SCTP_IERROR_NO_ERROR) {
788 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000789 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700790 }
791 }
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 repl = sctp_make_cookie_ack(new_asoc, chunk);
794 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700795 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* RFC 2960 5.1 Normal Establishment of an Association
798 *
799 * D) IMPLEMENTATION NOTE: An implementation may choose to
800 * send the Communication Up notification to the SCTP user
801 * upon reception of a valid COOKIE ECHO chunk.
802 */
803 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
804 new_asoc->c.sinit_num_ostreams,
805 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700806 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!ev)
808 goto nomem_ev;
809
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900810 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800811 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800813 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800815 if (new_asoc->peer.adaptation_ind) {
816 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700818 if (!ai_ev)
819 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700822 /* Add all the state machine commands now since we've created
823 * everything. This way we don't introduce memory corruptions
824 * during side-effect processing and correclty count established
825 * associations.
826 */
827 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
828 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
829 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000830 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
831 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700832 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
833
834 if (new_asoc->autoclose)
835 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
836 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
837
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700838 /* This will send the COOKIE ACK */
839 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
840
841 /* Queue the ASSOC_CHANGE event */
842 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
843
844 /* Send up the Adaptation Layer Indication event */
845 if (ai_ev)
846 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
847 SCTP_ULPEVENT(ai_ev));
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return SCTP_DISPOSITION_CONSUME;
850
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700851nomem_aiev:
852 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853nomem_ev:
854 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855nomem_init:
856 sctp_association_free(new_asoc);
857nomem:
858 return SCTP_DISPOSITION_NOMEM;
859}
860
861/*
862 * Respond to a normal COOKIE ACK chunk.
863 * We are the side that is being asked for an association.
864 *
865 * RFC 2960 5.1 Normal Establishment of an Association
866 *
867 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
868 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
869 * timer. It may also notify its ULP about the successful
870 * establishment of the association with a Communication Up
871 * notification (see Section 10).
872 *
873 * Verification Tag:
874 * Inputs
875 * (endpoint, asoc, chunk)
876 *
877 * Outputs
878 * (asoc, reply_msg, msg_up, timers, counters)
879 *
880 * The return value is the disposition of the chunk.
881 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000882sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
883 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 const struct sctp_association *asoc,
885 const sctp_subtype_t type, void *arg,
886 sctp_cmd_seq_t *commands)
887{
888 struct sctp_chunk *chunk = arg;
889 struct sctp_ulpevent *ev;
890
891 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000892 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Verify that the chunk length for the COOKIE-ACK is OK.
895 * If we don't do this, any bundled chunks may be junked.
896 */
897 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000898 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 commands);
900
901 /* Reset init error count upon receipt of COOKIE-ACK,
902 * to avoid problems with the managemement of this
903 * counter in stale cookie situations when a transition back
904 * from the COOKIE-ECHOED state to the COOKIE-WAIT
905 * state is performed.
906 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700907 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 /* RFC 2960 5.1 Normal Establishment of an Association
910 *
911 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
912 * from the COOKIE-ECHOED state to the ESTABLISHED state,
913 * stopping the T1-cookie timer.
914 */
915 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
916 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
917 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
918 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000919 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
920 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
922 if (asoc->autoclose)
923 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
924 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 /* It may also notify its ULP about the successful
927 * establishment of the association with a Communication Up
928 * notification (see Section 10).
929 */
930 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
931 0, asoc->c.sinit_num_ostreams,
932 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700933 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 if (!ev)
936 goto nomem;
937
938 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
939
940 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800941 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800943 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800945 if (asoc->peer.adaptation_ind) {
946 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 if (!ev)
948 goto nomem;
949
950 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
951 SCTP_ULPEVENT(ev));
952 }
953
954 return SCTP_DISPOSITION_CONSUME;
955nomem:
956 return SCTP_DISPOSITION_NOMEM;
957}
958
959/* Generate and sendout a heartbeat packet. */
960static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
961 const struct sctp_association *asoc,
962 const sctp_subtype_t type,
963 void *arg,
964 sctp_cmd_seq_t *commands)
965{
966 struct sctp_transport *transport = (struct sctp_transport *) arg;
967 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000970 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 if (!reply)
972 return SCTP_DISPOSITION_NOMEM;
973
974 /* Set rto_pending indicating that an RTT measurement
975 * is started with this heartbeat chunk.
976 */
977 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
978 SCTP_TRANSPORT(transport));
979
980 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
981 return SCTP_DISPOSITION_CONSUME;
982}
983
984/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000985sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
986 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 const struct sctp_association *asoc,
988 const sctp_subtype_t type,
989 void *arg,
990 sctp_cmd_seq_t *commands)
991{
992 struct sctp_transport *transport = (struct sctp_transport *) arg;
993
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400994 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700995 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
996 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
998 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800999 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001000 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1001 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return SCTP_DISPOSITION_DELETE_TCB;
1003 }
1004
1005 /* Section 3.3.5.
1006 * The Sender-specific Heartbeat Info field should normally include
1007 * information about the sender's current time when this HEARTBEAT
1008 * chunk is sent and the destination transport address to which this
1009 * HEARTBEAT is sent (see Section 8.3).
1010 */
1011
Frank Filz52ccb8e2005-12-22 11:36:46 -08001012 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 if (SCTP_DISPOSITION_NOMEM ==
1014 sctp_sf_heartbeat(ep, asoc, type, arg,
1015 commands))
1016 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /* Set transport error counter and association error counter
1019 * when sending heartbeat.
1020 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001021 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 SCTP_TRANSPORT(transport));
1023 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001024 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1025 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1027 SCTP_TRANSPORT(transport));
1028
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001029 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
1032/*
1033 * Process an heartbeat request.
1034 *
1035 * Section: 8.3 Path Heartbeat
1036 * The receiver of the HEARTBEAT should immediately respond with a
1037 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1038 * from the received HEARTBEAT chunk.
1039 *
1040 * Verification Tag: 8.5 Verification Tag [Normal verification]
1041 * When receiving an SCTP packet, the endpoint MUST ensure that the
1042 * value in the Verification Tag field of the received SCTP packet
1043 * matches its own Tag. If the received Verification Tag value does not
1044 * match the receiver's own tag value, the receiver shall silently
1045 * discard the packet and shall not process it any further except for
1046 * those cases listed in Section 8.5.1 below.
1047 *
1048 * Inputs
1049 * (endpoint, asoc, chunk)
1050 *
1051 * Outputs
1052 * (asoc, reply_msg, msg_up, timers, counters)
1053 *
1054 * The return value is the disposition of the chunk.
1055 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001056sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1057 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 const struct sctp_association *asoc,
1059 const sctp_subtype_t type,
1060 void *arg,
1061 sctp_cmd_seq_t *commands)
1062{
Thomas Graf06a31e22012-11-30 02:16:27 +00001063 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct sctp_chunk *chunk = arg;
1065 struct sctp_chunk *reply;
1066 size_t paylen = 0;
1067
1068 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001069 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /* Make sure that the HEARTBEAT chunk has a valid length. */
1072 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001073 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 commands);
1075
1076 /* 8.3 The receiver of the HEARTBEAT should immediately
1077 * respond with a HEARTBEAT ACK that contains the Heartbeat
1078 * Information field copied from the received HEARTBEAT chunk.
1079 */
1080 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001081 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001083
1084 if (ntohs(param_hdr->length) > paylen)
1085 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1086 param_hdr, commands);
1087
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001088 if (!pskb_pull(chunk->skb, paylen))
1089 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Thomas Graf06a31e22012-11-30 02:16:27 +00001091 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!reply)
1093 goto nomem;
1094
1095 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1096 return SCTP_DISPOSITION_CONSUME;
1097
1098nomem:
1099 return SCTP_DISPOSITION_NOMEM;
1100}
1101
1102/*
1103 * Process the returning HEARTBEAT ACK.
1104 *
1105 * Section: 8.3 Path Heartbeat
1106 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1107 * should clear the error counter of the destination transport
1108 * address to which the HEARTBEAT was sent, and mark the destination
1109 * transport address as active if it is not so marked. The endpoint may
1110 * optionally report to the upper layer when an inactive destination
1111 * address is marked as active due to the reception of the latest
1112 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1113 * clear the association overall error count as well (as defined
1114 * in section 8.1).
1115 *
1116 * The receiver of the HEARTBEAT ACK should also perform an RTT
1117 * measurement for that destination transport address using the time
1118 * value carried in the HEARTBEAT ACK chunk.
1119 *
1120 * Verification Tag: 8.5 Verification Tag [Normal verification]
1121 *
1122 * Inputs
1123 * (endpoint, asoc, chunk)
1124 *
1125 * Outputs
1126 * (asoc, reply_msg, msg_up, timers, counters)
1127 *
1128 * The return value is the disposition of the chunk.
1129 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001130sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1131 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 const struct sctp_association *asoc,
1133 const sctp_subtype_t type,
1134 void *arg,
1135 sctp_cmd_seq_t *commands)
1136{
1137 struct sctp_chunk *chunk = arg;
1138 union sctp_addr from_addr;
1139 struct sctp_transport *link;
1140 sctp_sender_hb_info_t *hbinfo;
1141 unsigned long max_interval;
1142
1143 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001144 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001147 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1148 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001149 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 commands);
1151
1152 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001153 /* Make sure that the length of the parameter is what we expect */
1154 if (ntohs(hbinfo->param_hdr.length) !=
1155 sizeof(sctp_sender_hb_info_t)) {
1156 return SCTP_DISPOSITION_DISCARD;
1157 }
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001160 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001163 if (unlikely(!link)) {
1164 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001165 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1166 __func__,
1167 asoc,
1168 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001170 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1171 __func__,
1172 asoc,
1173 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return SCTP_DISPOSITION_DISCARD;
1176 }
1177
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001178 /* Validate the 64-bit random nonce. */
1179 if (hbinfo->hb_nonce != link->hb_nonce)
1180 return SCTP_DISPOSITION_DISCARD;
1181
Frank Filz52ccb8e2005-12-22 11:36:46 -08001182 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 /* Check if the timestamp looks valid. */
1185 if (time_after(hbinfo->sent_at, jiffies) ||
1186 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001187 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001189 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return SCTP_DISPOSITION_DISCARD;
1191 }
1192
1193 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1194 * the HEARTBEAT should clear the error counter of the
1195 * destination transport address to which the HEARTBEAT was
1196 * sent and mark the destination transport address as active if
1197 * it is not so marked.
1198 */
1199 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1200
1201 return SCTP_DISPOSITION_CONSUME;
1202}
1203
1204/* Helper function to send out an abort for the restart
1205 * condition.
1206 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001207static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 struct sctp_chunk *init,
1209 sctp_cmd_seq_t *commands)
1210{
1211 int len;
1212 struct sctp_packet *pkt;
1213 union sctp_addr_param *addrparm;
1214 struct sctp_errhdr *errhdr;
1215 struct sctp_endpoint *ep;
1216 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1217 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1218
1219 /* Build the error on the stack. We are way to malloc crazy
1220 * throughout the code today.
1221 */
1222 errhdr = (struct sctp_errhdr *)buffer;
1223 addrparm = (union sctp_addr_param *)errhdr->variable;
1224
1225 /* Copy into a parm format. */
1226 len = af->to_addr_param(ssa, addrparm);
1227 len += sizeof(sctp_errhdr_t);
1228
1229 errhdr->cause = SCTP_ERROR_RESTART;
1230 errhdr->length = htons(len);
1231
1232 /* Assign to the control socket. */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001233 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Association is NULL since this may be a restart attack and we
1236 * want to send back the attacker's vtag.
1237 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001238 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (!pkt)
1241 goto out;
1242 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1243
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001244 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 /* Discard the rest of the inbound packet. */
1247 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1248
1249out:
1250 /* Even if there is no memory, treat as a failure so
1251 * the packet will get dropped.
1252 */
1253 return 0;
1254}
1255
Joe Perches123031c2010-09-08 11:04:21 +00001256static bool list_has_sctp_addr(const struct list_head *list,
1257 union sctp_addr *ipaddr)
1258{
1259 struct sctp_transport *addr;
1260
1261 list_for_each_entry(addr, list, transports) {
1262 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1263 return true;
1264 }
1265
1266 return false;
1267}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268/* A restart is occurring, check to make sure no new addresses
1269 * are being added as we may be under a takeover attack.
1270 */
1271static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1272 const struct sctp_association *asoc,
1273 struct sctp_chunk *init,
1274 sctp_cmd_seq_t *commands)
1275{
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001276 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001277 struct sctp_transport *new_addr;
1278 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Joe Perches123031c2010-09-08 11:04:21 +00001280 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 * ...
1282 * Before responding the endpoint MUST check to see if the
1283 * unexpected INIT adds new addresses to the association. If new
1284 * addresses are added to the association, the endpoint MUST respond
1285 * with an ABORT..
1286 */
1287
1288 /* Search through all current addresses and make sure
1289 * we aren't adding any new ones.
1290 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001291 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001292 transports) {
1293 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1294 &new_addr->ipaddr)) {
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001295 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001296 commands);
1297 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 break;
Joe Perches123031c2010-09-08 11:04:21 +00001299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
1302 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306/* Populate the verification/tie tags based on overlapping INIT
1307 * scenario.
1308 *
1309 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1310 */
1311static void sctp_tietags_populate(struct sctp_association *new_asoc,
1312 const struct sctp_association *asoc)
1313{
1314 switch (asoc->state) {
1315
1316 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1317
1318 case SCTP_STATE_COOKIE_WAIT:
1319 new_asoc->c.my_vtag = asoc->c.my_vtag;
1320 new_asoc->c.my_ttag = asoc->c.my_vtag;
1321 new_asoc->c.peer_ttag = 0;
1322 break;
1323
1324 case SCTP_STATE_COOKIE_ECHOED:
1325 new_asoc->c.my_vtag = asoc->c.my_vtag;
1326 new_asoc->c.my_ttag = asoc->c.my_vtag;
1327 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1328 break;
1329
1330 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1331 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1332 */
1333 default:
1334 new_asoc->c.my_ttag = asoc->c.my_vtag;
1335 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1336 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* Other parameters for the endpoint SHOULD be copied from the
1340 * existing parameters of the association (e.g. number of
1341 * outbound streams) into the INIT ACK and cookie.
1342 */
1343 new_asoc->rwnd = asoc->rwnd;
1344 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1345 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1346 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1347}
1348
1349/*
1350 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1351 * handling action.
1352 *
1353 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1354 *
1355 * Returns value representing action to be taken. These action values
1356 * correspond to Action/Description values in RFC 2960, Table 2.
1357 */
1358static char sctp_tietags_compare(struct sctp_association *new_asoc,
1359 const struct sctp_association *asoc)
1360{
1361 /* In this case, the peer may have restarted. */
1362 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1363 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1364 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1365 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1366 return 'A';
1367
1368 /* Collision case B. */
1369 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1370 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1371 (0 == asoc->c.peer_vtag))) {
1372 return 'B';
1373 }
1374
1375 /* Collision case D. */
1376 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1377 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1378 return 'D';
1379
1380 /* Collision case C. */
1381 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1382 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1383 (0 == new_asoc->c.my_ttag) &&
1384 (0 == new_asoc->c.peer_ttag))
1385 return 'C';
1386
1387 /* No match to any of the special cases; discard this packet. */
1388 return 'E';
1389}
1390
1391/* Common helper routine for both duplicate and simulataneous INIT
1392 * chunk handling.
1393 */
1394static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001395 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 const struct sctp_endpoint *ep,
1397 const struct sctp_association *asoc,
1398 const sctp_subtype_t type,
1399 void *arg, sctp_cmd_seq_t *commands)
1400{
1401 sctp_disposition_t retval;
1402 struct sctp_chunk *chunk = arg;
1403 struct sctp_chunk *repl;
1404 struct sctp_association *new_asoc;
1405 struct sctp_chunk *err_chunk;
1406 struct sctp_packet *packet;
1407 sctp_unrecognized_param_t *unk_param;
1408 int len;
1409
1410 /* 6.10 Bundling
1411 * An endpoint MUST NOT bundle INIT, INIT ACK or
1412 * SHUTDOWN COMPLETE with any other chunks.
1413 *
1414 * IG Section 2.11.2
1415 * Furthermore, we require that the receiver of an INIT chunk MUST
1416 * enforce these rules by silently discarding an arriving packet
1417 * with an INIT chunk that is bundled with other chunks.
1418 */
1419 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001420 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001423 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 */
1425 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001426 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
1428 /* Make sure that the INIT chunk has a valid length.
1429 * In this case, we generate a protocol violation since we have
1430 * an association established.
1431 */
1432 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001433 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 commands);
1435 /* Grab the INIT header. */
1436 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1437
1438 /* Tag the variable length parameters. */
1439 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1440
1441 /* Verify the INIT chunk before processing it. */
1442 err_chunk = NULL;
Vlad Yaseviche5eae4a2014-04-17 17:26:50 +02001443 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1445 &err_chunk)) {
1446 /* This chunk contains fatal error. It is to be discarded.
1447 * Send an ABORT, with causes if there is any.
1448 */
1449 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001450 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 (__u8 *)(err_chunk->chunk_hdr) +
1452 sizeof(sctp_chunkhdr_t),
1453 ntohs(err_chunk->chunk_hdr->length) -
1454 sizeof(sctp_chunkhdr_t));
1455
1456 if (packet) {
1457 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1458 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001459 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 retval = SCTP_DISPOSITION_CONSUME;
1461 } else {
1462 retval = SCTP_DISPOSITION_NOMEM;
1463 }
1464 goto cleanup;
1465 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001466 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 commands);
1468 }
1469 }
1470
1471 /*
1472 * Other parameters for the endpoint SHOULD be copied from the
1473 * existing parameters of the association (e.g. number of
1474 * outbound streams) into the INIT ACK and cookie.
1475 * FIXME: We are copying parameters from the endpoint not the
1476 * association.
1477 */
1478 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1479 if (!new_asoc)
1480 goto nomem;
1481
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001482 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1483 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1484 goto nomem;
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* In the outbound INIT ACK the endpoint MUST copy its current
1487 * Verification Tag and Peers Verification tag into a reserved
1488 * place (local tie-tag and per tie-tag) within the state cookie.
1489 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001490 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001492 GFP_ATOMIC))
1493 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
1495 /* Make sure no new addresses are being added during the
1496 * restart. Do not do this check for COOKIE-WAIT state,
1497 * since there are no peer addresses to check against.
1498 * Upon return an ABORT will have been sent if needed.
1499 */
1500 if (!sctp_state(asoc, COOKIE_WAIT)) {
1501 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1502 commands)) {
1503 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001504 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
1506 }
1507
1508 sctp_tietags_populate(new_asoc, asoc);
1509
1510 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1511
1512 /* If there are errors need to be reported for unknown parameters,
1513 * make sure to reserve enough room in the INIT ACK for them.
1514 */
1515 len = 0;
1516 if (err_chunk) {
1517 len = ntohs(err_chunk->chunk_hdr->length) -
1518 sizeof(sctp_chunkhdr_t);
1519 }
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1522 if (!repl)
1523 goto nomem;
1524
1525 /* If there are errors need to be reported for unknown parameters,
1526 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1527 * parameter.
1528 */
1529 if (err_chunk) {
1530 /* Get the "Unrecognized parameter" parameter(s) out of the
1531 * ERROR chunk generated by sctp_verify_init(). Since the
1532 * error cause code for "unknown parameter" and the
1533 * "Unrecognized parameter" type is the same, we can
1534 * construct the parameters in INIT ACK by copying the
1535 * ERROR causes over.
1536 */
1537 unk_param = (sctp_unrecognized_param_t *)
1538 ((__u8 *)(err_chunk->chunk_hdr) +
1539 sizeof(sctp_chunkhdr_t));
1540 /* Replace the cause code with the "Unrecognized parameter"
1541 * parameter type.
1542 */
1543 sctp_addto_chunk(repl, len, unk_param);
1544 }
1545
1546 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1547 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1548
1549 /*
1550 * Note: After sending out INIT ACK with the State Cookie parameter,
1551 * "Z" MUST NOT allocate any resources for this new association.
1552 * Otherwise, "Z" will be vulnerable to resource attacks.
1553 */
1554 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1555 retval = SCTP_DISPOSITION_CONSUME;
1556
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001557 return retval;
1558
1559nomem:
1560 retval = SCTP_DISPOSITION_NOMEM;
1561nomem_retval:
1562 if (new_asoc)
1563 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564cleanup:
1565 if (err_chunk)
1566 sctp_chunk_free(err_chunk);
1567 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001571 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * This means we started an INIT and then we got an INIT request from
1573 * our peer.
1574 *
1575 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1576 * This usually indicates an initialization collision, i.e., each
1577 * endpoint is attempting, at about the same time, to establish an
1578 * association with the other endpoint.
1579 *
1580 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1581 * endpoint MUST respond with an INIT ACK using the same parameters it
1582 * sent in its original INIT chunk (including its Verification Tag,
1583 * unchanged). These original parameters are combined with those from the
1584 * newly received INIT chunk. The endpoint shall also generate a State
1585 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1586 * INIT to calculate the State Cookie.
1587 *
1588 * After that, the endpoint MUST NOT change its state, the T1-init
1589 * timer shall be left running and the corresponding TCB MUST NOT be
1590 * destroyed. The normal procedures for handling State Cookies when
1591 * a TCB exists will resolve the duplicate INITs to a single association.
1592 *
1593 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1594 * its Tie-Tags with the Tag information of itself and its peer (see
1595 * section 5.2.2 for a description of the Tie-Tags).
1596 *
1597 * Verification Tag: Not explicit, but an INIT can not have a valid
1598 * verification tag, so we skip the check.
1599 *
1600 * Inputs
1601 * (endpoint, asoc, chunk)
1602 *
1603 * Outputs
1604 * (asoc, reply_msg, msg_up, timers, counters)
1605 *
1606 * The return value is the disposition of the chunk.
1607 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001608sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1609 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 const struct sctp_association *asoc,
1611 const sctp_subtype_t type,
1612 void *arg,
1613 sctp_cmd_seq_t *commands)
1614{
1615 /* Call helper to do the real work for both simulataneous and
1616 * duplicate INIT chunk handling.
1617 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001618 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621/*
1622 * Handle duplicated INIT messages. These are usually delayed
1623 * restransmissions.
1624 *
1625 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1626 * COOKIE-ECHOED and COOKIE-WAIT
1627 *
1628 * Unless otherwise stated, upon reception of an unexpected INIT for
1629 * this association, the endpoint shall generate an INIT ACK with a
1630 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1631 * current Verification Tag and peer's Verification Tag into a reserved
1632 * place within the state cookie. We shall refer to these locations as
1633 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1634 * containing this INIT ACK MUST carry a Verification Tag value equal to
1635 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1636 * MUST contain a new Initiation Tag (randomly generated see Section
1637 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1638 * existing parameters of the association (e.g. number of outbound
1639 * streams) into the INIT ACK and cookie.
1640 *
1641 * After sending out the INIT ACK, the endpoint shall take no further
1642 * actions, i.e., the existing association, including its current state,
1643 * and the corresponding TCB MUST NOT be changed.
1644 *
1645 * Note: Only when a TCB exists and the association is not in a COOKIE-
1646 * WAIT state are the Tie-Tags populated. For a normal association INIT
1647 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1648 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1649 * State Cookie are populated as specified in section 5.2.1.
1650 *
1651 * Verification Tag: Not specified, but an INIT has no way of knowing
1652 * what the verification tag could be, so we ignore it.
1653 *
1654 * Inputs
1655 * (endpoint, asoc, chunk)
1656 *
1657 * Outputs
1658 * (asoc, reply_msg, msg_up, timers, counters)
1659 *
1660 * The return value is the disposition of the chunk.
1661 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001662sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1663 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 const struct sctp_association *asoc,
1665 const sctp_subtype_t type,
1666 void *arg,
1667 sctp_cmd_seq_t *commands)
1668{
1669 /* Call helper to do the real work for both simulataneous and
1670 * duplicate INIT chunk handling.
1671 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001672 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674
1675
Vlad Yasevich610ab732007-01-15 19:18:30 -08001676/*
1677 * Unexpected INIT-ACK handler.
1678 *
1679 * Section 5.2.3
1680 * If an INIT ACK received by an endpoint in any state other than the
1681 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1682 * An unexpected INIT ACK usually indicates the processing of an old or
1683 * duplicated INIT chunk.
1684*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001685sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1686 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001687 const struct sctp_association *asoc,
1688 const sctp_subtype_t type,
1689 void *arg, sctp_cmd_seq_t *commands)
1690{
1691 /* Per the above section, we'll discard the chunk if we have an
1692 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1693 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001694 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001695 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001696 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001697 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1701 *
1702 * Section 5.2.4
1703 * A) In this case, the peer may have restarted.
1704 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001705static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1706 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 const struct sctp_association *asoc,
1708 struct sctp_chunk *chunk,
1709 sctp_cmd_seq_t *commands,
1710 struct sctp_association *new_asoc)
1711{
1712 sctp_init_chunk_t *peer_init;
1713 struct sctp_ulpevent *ev;
1714 struct sctp_chunk *repl;
1715 struct sctp_chunk *err;
1716 sctp_disposition_t disposition;
1717
1718 /* new_asoc is a brand-new association, so these are not yet
1719 * side effects--it is safe to run them here.
1720 */
1721 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1722
Wei Yongjunde6becd2011-04-19 21:30:51 +00001723 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 GFP_ATOMIC))
1725 goto nomem;
1726
1727 /* Make sure no new addresses are being added during the
1728 * restart. Though this is a pretty complicated attack
1729 * since you'd have to get inside the cookie.
1730 */
1731 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1732 return SCTP_DISPOSITION_CONSUME;
1733 }
1734
1735 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1736 * the peer has restarted (Action A), it MUST NOT setup a new
1737 * association but instead resend the SHUTDOWN ACK and send an ERROR
1738 * chunk with a "Cookie Received while Shutting Down" error cause to
1739 * its peer.
1740 */
1741 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001742 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1744 chunk, commands);
1745 if (SCTP_DISPOSITION_NOMEM == disposition)
1746 goto nomem;
1747
1748 err = sctp_make_op_error(asoc, chunk,
1749 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001750 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (err)
1752 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1753 SCTP_CHUNK(err));
1754
1755 return SCTP_DISPOSITION_CONSUME;
1756 }
1757
Wei Yongjuna000c012011-05-29 23:23:36 +00001758 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1759 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001761 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1762 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1763 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1765
Wei Yongjuna000c012011-05-29 23:23:36 +00001766 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1767 * and ASCONF-ACK cache.
1768 */
1769 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1770 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1771 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 repl = sctp_make_cookie_ack(new_asoc, chunk);
1774 if (!repl)
1775 goto nomem;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* Report association restart to upper layer. */
1778 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1779 new_asoc->c.sinit_num_ostreams,
1780 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001781 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (!ev)
1783 goto nomem_ev;
1784
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001785 /* Update the content of current association. */
1786 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Vlad Yasevich2d435f02014-10-03 18:16:20 -04001788 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1789 (sctp_sstate(asoc->base.sk, CLOSING) ||
1790 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1791 /* if were currently in SHUTDOWN_PENDING, but the socket
1792 * has been closed by user, don't transition to ESTABLISHED.
1793 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1794 */
1795 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1796 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1797 SCTP_ST_CHUNK(0), NULL,
1798 commands);
1799 } else {
1800 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1801 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1802 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return SCTP_DISPOSITION_CONSUME;
1805
1806nomem_ev:
1807 sctp_chunk_free(repl);
1808nomem:
1809 return SCTP_DISPOSITION_NOMEM;
1810}
1811
1812/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1813 *
1814 * Section 5.2.4
1815 * B) In this case, both sides may be attempting to start an association
1816 * at about the same time but the peer endpoint started its INIT
1817 * after responding to the local endpoint's INIT
1818 */
1819/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001820static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1821 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 const struct sctp_association *asoc,
1823 struct sctp_chunk *chunk,
1824 sctp_cmd_seq_t *commands,
1825 struct sctp_association *new_asoc)
1826{
1827 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct sctp_chunk *repl;
1829
1830 /* new_asoc is a brand-new association, so these are not yet
1831 * side effects--it is safe to run them here.
1832 */
1833 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001834 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 GFP_ATOMIC))
1836 goto nomem;
1837
1838 /* Update the content of current association. */
1839 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1840 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1841 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001842 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1844
1845 repl = sctp_make_cookie_ack(new_asoc, chunk);
1846 if (!repl)
1847 goto nomem;
1848
1849 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* RFC 2960 5.1 Normal Establishment of an Association
1852 *
1853 * D) IMPLEMENTATION NOTE: An implementation may choose to
1854 * send the Communication Up notification to the SCTP user
1855 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001856 *
1857 * Sadly, this needs to be implemented as a side-effect, because
1858 * we are not guaranteed to have set the association id of the real
1859 * association and so these notifications need to be delayed until
1860 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Vlad Yasevich07d93962007-05-04 13:55:27 -07001863 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001866 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001868 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001869 *
1870 * This also needs to be done as a side effect for the same reason as
1871 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001873 if (asoc->peer.adaptation_ind)
1874 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 return SCTP_DISPOSITION_CONSUME;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878nomem:
1879 return SCTP_DISPOSITION_NOMEM;
1880}
1881
1882/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1883 *
1884 * Section 5.2.4
1885 * C) In this case, the local endpoint's cookie has arrived late.
1886 * Before it arrived, the local endpoint sent an INIT and received an
1887 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1888 * but a new tag of its own.
1889 */
1890/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001891static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1892 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 const struct sctp_association *asoc,
1894 struct sctp_chunk *chunk,
1895 sctp_cmd_seq_t *commands,
1896 struct sctp_association *new_asoc)
1897{
1898 /* The cookie should be silently discarded.
1899 * The endpoint SHOULD NOT change states and should leave
1900 * any timers running.
1901 */
1902 return SCTP_DISPOSITION_DISCARD;
1903}
1904
1905/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1906 *
1907 * Section 5.2.4
1908 *
1909 * D) When both local and remote tags match the endpoint should always
1910 * enter the ESTABLISHED state, if it has not already done so.
1911 */
1912/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001913static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1914 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 const struct sctp_association *asoc,
1916 struct sctp_chunk *chunk,
1917 sctp_cmd_seq_t *commands,
1918 struct sctp_association *new_asoc)
1919{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001920 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 struct sctp_chunk *repl;
1922
1923 /* Clarification from Implementor's Guide:
1924 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001925 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1926 * It should stop any cookie timer that may be running and send
1927 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
1929
1930 /* Don't accidentally move back into established state. */
1931 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1932 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1933 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1934 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1935 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001936 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1938 SCTP_NULL());
1939
1940 /* RFC 2960 5.1 Normal Establishment of an Association
1941 *
1942 * D) IMPLEMENTATION NOTE: An implementation may choose
1943 * to send the Communication Up notification to the
1944 * SCTP user upon reception of a valid COOKIE
1945 * ECHO chunk.
1946 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001947 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001949 asoc->c.sinit_num_ostreams,
1950 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001951 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (!ev)
1953 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001956 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001958 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001960 if (asoc->peer.adaptation_ind) {
1961 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001963 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto nomem;
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 repl = sctp_make_cookie_ack(new_asoc, chunk);
1970 if (!repl)
1971 goto nomem;
1972
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001973 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1974
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001975 if (ev)
1976 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1977 SCTP_ULPEVENT(ev));
1978 if (ai_ev)
1979 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1980 SCTP_ULPEVENT(ai_ev));
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return SCTP_DISPOSITION_CONSUME;
1983
1984nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001985 if (ai_ev)
1986 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 if (ev)
1988 sctp_ulpevent_free(ev);
1989 return SCTP_DISPOSITION_NOMEM;
1990}
1991
1992/*
1993 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1994 * chunk was retransmitted and then delayed in the network.
1995 *
1996 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1997 *
1998 * Verification Tag: None. Do cookie validation.
1999 *
2000 * Inputs
2001 * (endpoint, asoc, chunk)
2002 *
2003 * Outputs
2004 * (asoc, reply_msg, msg_up, timers, counters)
2005 *
2006 * The return value is the disposition of the chunk.
2007 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002008sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2009 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 const struct sctp_association *asoc,
2011 const sctp_subtype_t type,
2012 void *arg,
2013 sctp_cmd_seq_t *commands)
2014{
2015 sctp_disposition_t retval;
2016 struct sctp_chunk *chunk = arg;
2017 struct sctp_association *new_asoc;
2018 int error = 0;
2019 char action;
2020 struct sctp_chunk *err_chk_p;
2021
2022 /* Make sure that the chunk has a valid length from the protocol
2023 * perspective. In this case check to make sure we have at least
2024 * enough for the chunk header. Cookie length verification is
2025 * done later.
2026 */
2027 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002028 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 commands);
2030
2031 /* "Decode" the chunk. We have no optional parameters so we
2032 * are in good shape.
2033 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002034 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002035 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2036 sizeof(sctp_chunkhdr_t)))
2037 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2040 * of a duplicate COOKIE ECHO match the Verification Tags of the
2041 * current association, consider the State Cookie valid even if
2042 * the lifespan is exceeded.
2043 */
2044 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2045 &err_chk_p);
2046
2047 /* FIXME:
2048 * If the re-build failed, what is the proper error path
2049 * from here?
2050 *
2051 * [We should abort the association. --piggy]
2052 */
2053 if (!new_asoc) {
2054 /* FIXME: Several errors are possible. A bad cookie should
2055 * be silently discarded, but think about logging it too.
2056 */
2057 switch (error) {
2058 case -SCTP_IERROR_NOMEM:
2059 goto nomem;
2060
2061 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002062 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002064 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 case -SCTP_IERROR_BAD_SIG:
2066 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002067 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
2071 /* Compare the tie_tag in cookie with the verification tag of
2072 * current association.
2073 */
2074 action = sctp_tietags_compare(new_asoc, asoc);
2075
2076 switch (action) {
2077 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002078 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 new_asoc);
2080 break;
2081
2082 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002083 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 new_asoc);
2085 break;
2086
2087 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002088 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 new_asoc);
2090 break;
2091
2092 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002093 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 new_asoc);
2095 break;
2096
2097 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002098 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002103 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2105
Max Matveevd5ccd492011-08-29 21:02:24 +00002106 /* Restore association pointer to provide SCTP command interpeter
2107 * with a valid context in case it needs to manipulate
2108 * the queues */
2109 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2110 SCTP_ASOC((struct sctp_association *)asoc));
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return retval;
2113
2114nomem:
2115 return SCTP_DISPOSITION_NOMEM;
2116}
2117
2118/*
2119 * Process an ABORT. (SHUTDOWN-PENDING state)
2120 *
2121 * See sctp_sf_do_9_1_abort().
2122 */
2123sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002124 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 const struct sctp_endpoint *ep,
2126 const struct sctp_association *asoc,
2127 const sctp_subtype_t type,
2128 void *arg,
2129 sctp_cmd_seq_t *commands)
2130{
2131 struct sctp_chunk *chunk = arg;
2132
2133 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002134 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 /* Make sure that the ABORT chunk has a valid length.
2137 * Since this is an ABORT chunk, we have to discard it
2138 * because of the following text:
2139 * RFC 2960, Section 3.3.7
2140 * If an endpoint receives an ABORT with a format error or for an
2141 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002142 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 * as we do not know its true length. So, to be safe, discard the
2144 * packet.
2145 */
2146 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002147 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Vlad Yasevich75205f42007-12-20 14:12:59 -08002149 /* ADD-IP: Special case for ABORT chunks
2150 * F4) One special consideration is that ABORT Chunks arriving
2151 * destined to the IP address being deleted MUST be
2152 * ignored (see Section 5.3.1 for further details).
2153 */
2154 if (SCTP_ADDR_DEL ==
2155 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002156 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002157
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002158 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
2161/*
2162 * Process an ABORT. (SHUTDOWN-SENT state)
2163 *
2164 * See sctp_sf_do_9_1_abort().
2165 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002166sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2167 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 const struct sctp_association *asoc,
2169 const sctp_subtype_t type,
2170 void *arg,
2171 sctp_cmd_seq_t *commands)
2172{
2173 struct sctp_chunk *chunk = arg;
2174
2175 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002176 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 /* Make sure that the ABORT chunk has a valid length.
2179 * Since this is an ABORT chunk, we have to discard it
2180 * because of the following text:
2181 * RFC 2960, Section 3.3.7
2182 * If an endpoint receives an ABORT with a format error or for an
2183 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002184 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 * as we do not know its true length. So, to be safe, discard the
2186 * packet.
2187 */
2188 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002189 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Vlad Yasevich75205f42007-12-20 14:12:59 -08002191 /* ADD-IP: Special case for ABORT chunks
2192 * F4) One special consideration is that ABORT Chunks arriving
2193 * destined to the IP address being deleted MUST be
2194 * ignored (see Section 5.3.1 for further details).
2195 */
2196 if (SCTP_ADDR_DEL ==
2197 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002198 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 /* Stop the T2-shutdown timer. */
2201 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2202 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2203
2204 /* Stop the T5-shutdown guard timer. */
2205 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2206 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2207
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002208 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211/*
2212 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2213 *
2214 * See sctp_sf_do_9_1_abort().
2215 */
2216sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002217 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 const struct sctp_endpoint *ep,
2219 const struct sctp_association *asoc,
2220 const sctp_subtype_t type,
2221 void *arg,
2222 sctp_cmd_seq_t *commands)
2223{
2224 /* The same T2 timer, so we should be able to use
2225 * common function with the SHUTDOWN-SENT state.
2226 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002227 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
2230/*
2231 * Handle an Error received in COOKIE_ECHOED state.
2232 *
2233 * Only handle the error type of stale COOKIE Error, the other errors will
2234 * be ignored.
2235 *
2236 * Inputs
2237 * (endpoint, asoc, chunk)
2238 *
2239 * Outputs
2240 * (asoc, reply_msg, msg_up, timers, counters)
2241 *
2242 * The return value is the disposition of the chunk.
2243 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002244sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2245 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 const struct sctp_association *asoc,
2247 const sctp_subtype_t type,
2248 void *arg,
2249 sctp_cmd_seq_t *commands)
2250{
2251 struct sctp_chunk *chunk = arg;
2252 sctp_errhdr_t *err;
2253
2254 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002255 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 /* Make sure that the ERROR chunk has a valid length.
2258 * The parameter walking depends on this as well.
2259 */
2260 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002261 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 commands);
2263
2264 /* Process the error here */
2265 /* FUTURE FIXME: When PR-SCTP related and other optional
2266 * parms are emitted, this will have to change to handle multiple
2267 * errors.
2268 */
2269 sctp_walk_errors(err, chunk->chunk_hdr) {
2270 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002271 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 arg, commands);
2273 }
2274
2275 /* It is possible to have malformed error causes, and that
2276 * will cause us to end the walk early. However, since
2277 * we are discarding the packet, there should be no adverse
2278 * affects.
2279 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002280 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
2283/*
2284 * Handle a Stale COOKIE Error
2285 *
2286 * Section: 5.2.6 Handle Stale COOKIE Error
2287 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2288 * one of the following three alternatives.
2289 * ...
2290 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2291 * Preservative parameter requesting an extension to the lifetime of
2292 * the State Cookie. When calculating the time extension, an
2293 * implementation SHOULD use the RTT information measured based on the
2294 * previous COOKIE ECHO / ERROR exchange, and should add no more
2295 * than 1 second beyond the measured RTT, due to long State Cookie
2296 * lifetimes making the endpoint more subject to a replay attack.
2297 *
2298 * Verification Tag: Not explicit, but safe to ignore.
2299 *
2300 * Inputs
2301 * (endpoint, asoc, chunk)
2302 *
2303 * Outputs
2304 * (asoc, reply_msg, msg_up, timers, counters)
2305 *
2306 * The return value is the disposition of the chunk.
2307 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002308static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2309 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 const struct sctp_association *asoc,
2311 const sctp_subtype_t type,
2312 void *arg,
2313 sctp_cmd_seq_t *commands)
2314{
2315 struct sctp_chunk *chunk = arg;
2316 time_t stale;
2317 sctp_cookie_preserve_param_t bht;
2318 sctp_errhdr_t *err;
2319 struct sctp_chunk *reply;
2320 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002321 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Vlad Yasevich81845c212006-01-30 15:59:54 -08002323 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002324 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2325 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002327 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 return SCTP_DISPOSITION_DELETE_TCB;
2329 }
2330
2331 err = (sctp_errhdr_t *)(chunk->skb->data);
2332
2333 /* When calculating the time extension, an implementation
2334 * SHOULD use the RTT information measured based on the
2335 * previous COOKIE ECHO / ERROR exchange, and should add no
2336 * more than 1 second beyond the measured RTT, due to long
2337 * State Cookie lifetimes making the endpoint more subject to
2338 * a replay attack.
2339 * Measure of Staleness's unit is usec. (1/1000000 sec)
2340 * Suggested Cookie Life-span Increment's unit is msec.
2341 * (1/1000 sec)
2342 * In general, if you use the suggested cookie life, the value
2343 * found in the field of measure of staleness should be doubled
2344 * to give ample time to retransmit the new cookie and thus
2345 * yield a higher probability of success on the reattempt.
2346 */
Al Viro34bcca22006-11-20 17:27:15 -08002347 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 stale = (stale * 2) / 1000;
2349
2350 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2351 bht.param_hdr.length = htons(sizeof(bht));
2352 bht.lifespan_increment = htonl(stale);
2353
2354 /* Build that new INIT chunk. */
2355 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2356 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2357 if (!reply)
2358 goto nomem;
2359
2360 sctp_addto_chunk(reply, sizeof(bht), &bht);
2361
2362 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2363 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2364
2365 /* Stop pending T3-rtx and heartbeat timers */
2366 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2367 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2368
2369 /* Delete non-primary peer ip addresses since we are transitioning
2370 * back to the COOKIE-WAIT state
2371 */
2372 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2373
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002374 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2375 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002377 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 SCTP_TRANSPORT(asoc->peer.primary_path));
2379
2380 /* Cast away the const modifier, as we want to just
2381 * rerun it through as a sideffect.
2382 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002383 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2386 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2387 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2388 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2389 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2390 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2391
2392 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2393
2394 return SCTP_DISPOSITION_CONSUME;
2395
2396nomem:
2397 return SCTP_DISPOSITION_NOMEM;
2398}
2399
2400/*
2401 * Process an ABORT.
2402 *
2403 * Section: 9.1
2404 * After checking the Verification Tag, the receiving endpoint shall
2405 * remove the association from its record, and shall report the
2406 * termination to its upper layer.
2407 *
2408 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2409 * B) Rules for packet carrying ABORT:
2410 *
2411 * - The endpoint shall always fill in the Verification Tag field of the
2412 * outbound packet with the destination endpoint's tag value if it
2413 * is known.
2414 *
2415 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2416 * MUST follow the procedure described in Section 8.4.
2417 *
2418 * - The receiver MUST accept the packet if the Verification Tag
2419 * matches either its own tag, OR the tag of its peer. Otherwise, the
2420 * receiver MUST silently discard the packet and take no further
2421 * action.
2422 *
2423 * Inputs
2424 * (endpoint, asoc, chunk)
2425 *
2426 * Outputs
2427 * (asoc, reply_msg, msg_up, timers, counters)
2428 *
2429 * The return value is the disposition of the chunk.
2430 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002431sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2432 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 const struct sctp_association *asoc,
2434 const sctp_subtype_t type,
2435 void *arg,
2436 sctp_cmd_seq_t *commands)
2437{
2438 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002441 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 /* Make sure that the ABORT chunk has a valid length.
2444 * Since this is an ABORT chunk, we have to discard it
2445 * because of the following text:
2446 * RFC 2960, Section 3.3.7
2447 * If an endpoint receives an ABORT with a format error or for an
2448 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002449 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 * as we do not know its true length. So, to be safe, discard the
2451 * packet.
2452 */
2453 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002454 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Vlad Yasevich75205f42007-12-20 14:12:59 -08002456 /* ADD-IP: Special case for ABORT chunks
2457 * F4) One special consideration is that ABORT Chunks arriving
2458 * destined to the IP address being deleted MUST be
2459 * ignored (see Section 5.3.1 for further details).
2460 */
2461 if (SCTP_ADDR_DEL ==
2462 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002463 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002464
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002465 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002466}
2467
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002468static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2469 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002470 const struct sctp_association *asoc,
2471 const sctp_subtype_t type,
2472 void *arg,
2473 sctp_cmd_seq_t *commands)
2474{
2475 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002476 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002477 __be16 error = SCTP_ERROR_NO_ERROR;
2478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 /* See if we have an error cause code in the chunk. */
2480 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002481 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2482
2483 sctp_errhdr_t *err;
2484 sctp_walk_errors(err, chunk->chunk_hdr);
2485 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002486 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002491 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002492 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002493 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002494 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2495 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 return SCTP_DISPOSITION_ABORT;
2498}
2499
2500/*
2501 * Process an ABORT. (COOKIE-WAIT state)
2502 *
2503 * See sctp_sf_do_9_1_abort() above.
2504 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002505sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2506 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 const struct sctp_association *asoc,
2508 const sctp_subtype_t type,
2509 void *arg,
2510 sctp_cmd_seq_t *commands)
2511{
2512 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002513 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002514 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002517 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /* Make sure that the ABORT chunk has a valid length.
2520 * Since this is an ABORT chunk, we have to discard it
2521 * because of the following text:
2522 * RFC 2960, Section 3.3.7
2523 * If an endpoint receives an ABORT with a format error or for an
2524 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002525 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 * as we do not know its true length. So, to be safe, discard the
2527 * packet.
2528 */
2529 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002530 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 /* See if we have an error cause code in the chunk. */
2533 len = ntohs(chunk->chunk_hdr->length);
2534 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2535 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2536
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002537 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002538 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539}
2540
2541/*
2542 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2543 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002544sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2545 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 const struct sctp_association *asoc,
2547 const sctp_subtype_t type,
2548 void *arg,
2549 sctp_cmd_seq_t *commands)
2550{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002551 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002552 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002553 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
2556/*
2557 * Process an ABORT. (COOKIE-ECHOED state)
2558 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002559sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2560 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 const struct sctp_association *asoc,
2562 const sctp_subtype_t type,
2563 void *arg,
2564 sctp_cmd_seq_t *commands)
2565{
2566 /* There is a single T1 timer, so we should be able to use
2567 * common function with the COOKIE-WAIT state.
2568 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002569 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571
2572/*
2573 * Stop T1 timer and abort association with "INIT failed".
2574 *
2575 * This is common code called by several sctp_sf_*_abort() functions above.
2576 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002577static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2578 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002579 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002580 const struct sctp_association *asoc,
2581 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002583 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2585 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002586 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2588 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002589 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* CMD_INIT_FAILED will DELETE_TCB. */
2591 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002592 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002593 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
2596/*
2597 * sctp_sf_do_9_2_shut
2598 *
2599 * Section: 9.2
2600 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2601 * - enter the SHUTDOWN-RECEIVED state,
2602 *
2603 * - stop accepting new data from its SCTP user
2604 *
2605 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2606 * that all its outstanding DATA chunks have been received by the
2607 * SHUTDOWN sender.
2608 *
2609 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2610 * send a SHUTDOWN in response to a ULP request. And should discard
2611 * subsequent SHUTDOWN chunks.
2612 *
2613 * If there are still outstanding DATA chunks left, the SHUTDOWN
2614 * receiver shall continue to follow normal data transmission
2615 * procedures defined in Section 6 until all outstanding DATA chunks
2616 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2617 * new data from its SCTP user.
2618 *
2619 * Verification Tag: 8.5 Verification Tag [Normal verification]
2620 *
2621 * Inputs
2622 * (endpoint, asoc, chunk)
2623 *
2624 * Outputs
2625 * (asoc, reply_msg, msg_up, timers, counters)
2626 *
2627 * The return value is the disposition of the chunk.
2628 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002629sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2630 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 const struct sctp_association *asoc,
2632 const sctp_subtype_t type,
2633 void *arg,
2634 sctp_cmd_seq_t *commands)
2635{
2636 struct sctp_chunk *chunk = arg;
2637 sctp_shutdownhdr_t *sdh;
2638 sctp_disposition_t disposition;
2639 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002640 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002643 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644
2645 /* Make sure that the SHUTDOWN chunk has a valid length. */
2646 if (!sctp_chunk_length_valid(chunk,
2647 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002648 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 commands);
2650
2651 /* Convert the elaborate header. */
2652 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2653 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2654 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002655 ctsn = ntohl(sdh->cum_tsn_ack);
2656
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002657 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2658 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2659 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2660 return SCTP_DISPOSITION_DISCARD;
2661 }
2662
Wei Yongjundf10eec2008-10-23 01:00:21 -07002663 /* If Cumulative TSN Ack beyond the max tsn currently
2664 * send, terminating the association and respond to the
2665 * sender with an ABORT.
2666 */
2667 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002668 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002670 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2671 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2672 * inform the application that it should cease sending data.
2673 */
2674 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2675 if (!ev) {
2676 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002677 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002678 }
2679 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2682 * - enter the SHUTDOWN-RECEIVED state,
2683 * - stop accepting new data from its SCTP user
2684 *
2685 * [This is implicit in the new state.]
2686 */
2687 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2688 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2689 disposition = SCTP_DISPOSITION_CONSUME;
2690
2691 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002692 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 arg, commands);
2694 }
2695
2696 if (SCTP_DISPOSITION_NOMEM == disposition)
2697 goto out;
2698
2699 /* - verify, by checking the Cumulative TSN Ack field of the
2700 * chunk, that all its outstanding DATA chunks have been
2701 * received by the SHUTDOWN sender.
2702 */
2703 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002704 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706out:
2707 return disposition;
2708}
2709
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002710/*
2711 * sctp_sf_do_9_2_shut_ctsn
2712 *
2713 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2714 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2715 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2716 * MUST be processed.
2717 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002718sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2719 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002720 const struct sctp_association *asoc,
2721 const sctp_subtype_t type,
2722 void *arg,
2723 sctp_cmd_seq_t *commands)
2724{
2725 struct sctp_chunk *chunk = arg;
2726 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002727 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002728
2729 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002730 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002731
2732 /* Make sure that the SHUTDOWN chunk has a valid length. */
2733 if (!sctp_chunk_length_valid(chunk,
2734 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002735 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002736 commands);
2737
2738 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002739 ctsn = ntohl(sdh->cum_tsn_ack);
2740
2741 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2742 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2743 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2744 return SCTP_DISPOSITION_DISCARD;
2745 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002746
2747 /* If Cumulative TSN Ack beyond the max tsn currently
2748 * send, terminating the association and respond to the
2749 * sender with an ABORT.
2750 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002751 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002752 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002753
2754 /* verify, by checking the Cumulative TSN Ack field of the
2755 * chunk, that all its outstanding DATA chunks have been
2756 * received by the SHUTDOWN sender.
2757 */
2758 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2759 SCTP_BE32(sdh->cum_tsn_ack));
2760
2761 return SCTP_DISPOSITION_CONSUME;
2762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764/* RFC 2960 9.2
2765 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2766 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2767 * transport addresses (either in the IP addresses or in the INIT chunk)
2768 * that belong to this association, it should discard the INIT chunk and
2769 * retransmit the SHUTDOWN ACK chunk.
2770 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002771sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2772 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 const struct sctp_association *asoc,
2774 const sctp_subtype_t type,
2775 void *arg,
2776 sctp_cmd_seq_t *commands)
2777{
2778 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2779 struct sctp_chunk *reply;
2780
Vlad Yasevichece25df2007-09-07 16:30:54 -04002781 /* Make sure that the chunk has a valid length */
2782 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002783 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002784 commands);
2785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 /* Since we are not going to really process this INIT, there
2787 * is no point in verifying chunk boundries. Just generate
2788 * the SHUTDOWN ACK.
2789 */
2790 reply = sctp_make_shutdown_ack(asoc, chunk);
2791 if (NULL == reply)
2792 goto nomem;
2793
2794 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2795 * the T2-SHUTDOWN timer.
2796 */
2797 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2798
2799 /* and restart the T2-shutdown timer. */
2800 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2801 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2802
2803 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2804
2805 return SCTP_DISPOSITION_CONSUME;
2806nomem:
2807 return SCTP_DISPOSITION_NOMEM;
2808}
2809
2810/*
2811 * sctp_sf_do_ecn_cwr
2812 *
2813 * Section: Appendix A: Explicit Congestion Notification
2814 *
2815 * CWR:
2816 *
2817 * RFC 2481 details a specific bit for a sender to send in the header of
2818 * its next outbound TCP segment to indicate to its peer that it has
2819 * reduced its congestion window. This is termed the CWR bit. For
2820 * SCTP the same indication is made by including the CWR chunk.
2821 * This chunk contains one data element, i.e. the TSN number that
2822 * was sent in the ECNE chunk. This element represents the lowest
2823 * TSN number in the datagram that was originally marked with the
2824 * CE bit.
2825 *
2826 * Verification Tag: 8.5 Verification Tag [Normal verification]
2827 * Inputs
2828 * (endpoint, asoc, chunk)
2829 *
2830 * Outputs
2831 * (asoc, reply_msg, msg_up, timers, counters)
2832 *
2833 * The return value is the disposition of the chunk.
2834 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002835sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2836 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 const struct sctp_association *asoc,
2838 const sctp_subtype_t type,
2839 void *arg,
2840 sctp_cmd_seq_t *commands)
2841{
2842 sctp_cwrhdr_t *cwr;
2843 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002844 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002847 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002850 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2854 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2855
Al Viro34bcca22006-11-20 17:27:15 -08002856 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002859 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 /* Stop sending ECNE. */
2861 sctp_add_cmd_sf(commands,
2862 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002863 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865 return SCTP_DISPOSITION_CONSUME;
2866}
2867
2868/*
2869 * sctp_sf_do_ecne
2870 *
2871 * Section: Appendix A: Explicit Congestion Notification
2872 *
2873 * ECN-Echo
2874 *
2875 * RFC 2481 details a specific bit for a receiver to send back in its
2876 * TCP acknowledgements to notify the sender of the Congestion
2877 * Experienced (CE) bit having arrived from the network. For SCTP this
2878 * same indication is made by including the ECNE chunk. This chunk
2879 * contains one data element, i.e. the lowest TSN associated with the IP
2880 * datagram marked with the CE bit.....
2881 *
2882 * Verification Tag: 8.5 Verification Tag [Normal verification]
2883 * Inputs
2884 * (endpoint, asoc, chunk)
2885 *
2886 * Outputs
2887 * (asoc, reply_msg, msg_up, timers, counters)
2888 *
2889 * The return value is the disposition of the chunk.
2890 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002891sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2892 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 const struct sctp_association *asoc,
2894 const sctp_subtype_t type,
2895 void *arg,
2896 sctp_cmd_seq_t *commands)
2897{
2898 sctp_ecnehdr_t *ecne;
2899 struct sctp_chunk *chunk = arg;
2900
2901 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002902 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002905 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 commands);
2907
2908 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2909 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2910
2911 /* If this is a newer ECNE than the last CWR packet we sent out */
2912 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2913 SCTP_U32(ntohl(ecne->lowest_tsn)));
2914
2915 return SCTP_DISPOSITION_CONSUME;
2916}
2917
2918/*
2919 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2920 *
2921 * The SCTP endpoint MUST always acknowledge the reception of each valid
2922 * DATA chunk.
2923 *
2924 * The guidelines on delayed acknowledgement algorithm specified in
2925 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2926 * acknowledgement SHOULD be generated for at least every second packet
2927 * (not every second DATA chunk) received, and SHOULD be generated within
2928 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2929 * situations it may be beneficial for an SCTP transmitter to be more
2930 * conservative than the algorithms detailed in this document allow.
2931 * However, an SCTP transmitter MUST NOT be more aggressive than the
2932 * following algorithms allow.
2933 *
2934 * A SCTP receiver MUST NOT generate more than one SACK for every
2935 * incoming packet, other than to update the offered window as the
2936 * receiving application consumes new data.
2937 *
2938 * Verification Tag: 8.5 Verification Tag [Normal verification]
2939 *
2940 * Inputs
2941 * (endpoint, asoc, chunk)
2942 *
2943 * Outputs
2944 * (asoc, reply_msg, msg_up, timers, counters)
2945 *
2946 * The return value is the disposition of the chunk.
2947 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002948sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2949 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 const struct sctp_association *asoc,
2951 const sctp_subtype_t type,
2952 void *arg,
2953 sctp_cmd_seq_t *commands)
2954{
2955 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002956 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 int error;
2958
2959 if (!sctp_vtag_verify(chunk, asoc)) {
2960 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2961 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002962 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002966 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 commands);
2968
2969 error = sctp_eat_data(asoc, chunk, commands );
2970 switch (error) {
2971 case SCTP_IERROR_NO_ERROR:
2972 break;
2973 case SCTP_IERROR_HIGH_TSN:
2974 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002975 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto discard_noforce;
2977 case SCTP_IERROR_DUP_TSN:
2978 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002979 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 goto discard_force;
2981 case SCTP_IERROR_NO_DATA:
2982 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002983 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002984 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002985 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 default:
2987 BUG();
2988 }
2989
Wei Yongjun6dc76942009-11-23 15:53:53 -05002990 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2991 force = SCTP_FORCE();
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 if (asoc->autoclose) {
2994 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2995 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2996 }
2997
2998 /* If this is the last chunk in a packet, we need to count it
2999 * toward sack generation. Note that we need to SACK every
3000 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3001 * THEM. We elect to NOT generate SACK's if the chunk fails
3002 * the verification tag test.
3003 *
3004 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3005 *
3006 * The SCTP endpoint MUST always acknowledge the reception of
3007 * each valid DATA chunk.
3008 *
3009 * The guidelines on delayed acknowledgement algorithm
3010 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3011 * Specifically, an acknowledgement SHOULD be generated for at
3012 * least every second packet (not every second DATA chunk)
3013 * received, and SHOULD be generated within 200 ms of the
3014 * arrival of any unacknowledged DATA chunk. In some
3015 * situations it may be beneficial for an SCTP transmitter to
3016 * be more conservative than the algorithms detailed in this
3017 * document allow. However, an SCTP transmitter MUST NOT be
3018 * more aggressive than the following algorithms allow.
3019 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003020 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003021 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return SCTP_DISPOSITION_CONSUME;
3024
3025discard_force:
3026 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3027 *
3028 * When a packet arrives with duplicate DATA chunk(s) and with
3029 * no new DATA chunk(s), the endpoint MUST immediately send a
3030 * SACK with no delay. If a packet arrives with duplicate
3031 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3032 * MAY immediately send a SACK. Normally receipt of duplicate
3033 * DATA chunks will occur when the original SACK chunk was lost
3034 * and the peer's RTO has expired. The duplicate TSN number(s)
3035 * SHOULD be reported in the SACK as duplicate.
3036 */
3037 /* In our case, we split the MAY SACK advice up whether or not
3038 * the last chunk is a duplicate.'
3039 */
3040 if (chunk->end_of_packet)
3041 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3042 return SCTP_DISPOSITION_DISCARD;
3043
3044discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003045 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003046 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return SCTP_DISPOSITION_DISCARD;
3049consume:
3050 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003051
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052}
3053
3054/*
3055 * sctp_sf_eat_data_fast_4_4
3056 *
3057 * Section: 4 (4)
3058 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3059 * DATA chunks without delay.
3060 *
3061 * Verification Tag: 8.5 Verification Tag [Normal verification]
3062 * Inputs
3063 * (endpoint, asoc, chunk)
3064 *
3065 * Outputs
3066 * (asoc, reply_msg, msg_up, timers, counters)
3067 *
3068 * The return value is the disposition of the chunk.
3069 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003070sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3071 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 const struct sctp_association *asoc,
3073 const sctp_subtype_t type,
3074 void *arg,
3075 sctp_cmd_seq_t *commands)
3076{
3077 struct sctp_chunk *chunk = arg;
3078 int error;
3079
3080 if (!sctp_vtag_verify(chunk, asoc)) {
3081 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3082 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003083 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
3085
3086 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003087 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 commands);
3089
3090 error = sctp_eat_data(asoc, chunk, commands );
3091 switch (error) {
3092 case SCTP_IERROR_NO_ERROR:
3093 case SCTP_IERROR_HIGH_TSN:
3094 case SCTP_IERROR_DUP_TSN:
3095 case SCTP_IERROR_IGNORE_TSN:
3096 case SCTP_IERROR_BAD_STREAM:
3097 break;
3098 case SCTP_IERROR_NO_DATA:
3099 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003100 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003101 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003102 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 default:
3104 BUG();
3105 }
3106
3107 /* Go a head and force a SACK, since we are shutting down. */
3108
3109 /* Implementor's Guide.
3110 *
3111 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3112 * respond to each received packet containing one or more DATA chunk(s)
3113 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3114 */
3115 if (chunk->end_of_packet) {
3116 /* We must delay the chunk creation since the cumulative
3117 * TSN has not been updated yet.
3118 */
3119 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3120 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3121 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3122 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3123 }
3124
3125consume:
3126 return SCTP_DISPOSITION_CONSUME;
3127}
3128
3129/*
3130 * Section: 6.2 Processing a Received SACK
3131 * D) Any time a SACK arrives, the endpoint performs the following:
3132 *
3133 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3134 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3135 * increasing, a SACK whose Cumulative TSN Ack is less than the
3136 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3137 *
3138 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3139 * of bytes still outstanding after processing the Cumulative TSN Ack
3140 * and the Gap Ack Blocks.
3141 *
3142 * iii) If the SACK is missing a TSN that was previously
3143 * acknowledged via a Gap Ack Block (e.g., the data receiver
3144 * reneged on the data), then mark the corresponding DATA chunk
3145 * as available for retransmit: Mark it as missing for fast
3146 * retransmit as described in Section 7.2.4 and if no retransmit
3147 * timer is running for the destination address to which the DATA
3148 * chunk was originally transmitted, then T3-rtx is started for
3149 * that destination address.
3150 *
3151 * Verification Tag: 8.5 Verification Tag [Normal verification]
3152 *
3153 * Inputs
3154 * (endpoint, asoc, chunk)
3155 *
3156 * Outputs
3157 * (asoc, reply_msg, msg_up, timers, counters)
3158 *
3159 * The return value is the disposition of the chunk.
3160 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003161sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3162 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 const struct sctp_association *asoc,
3164 const sctp_subtype_t type,
3165 void *arg,
3166 sctp_cmd_seq_t *commands)
3167{
3168 struct sctp_chunk *chunk = arg;
3169 sctp_sackhdr_t *sackh;
3170 __u32 ctsn;
3171
3172 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003173 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
3175 /* Make sure that the SACK chunk has a valid length. */
3176 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003177 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 commands);
3179
3180 /* Pull the SACK chunk from the data buffer */
3181 sackh = sctp_sm_pull_sack(chunk);
3182 /* Was this a bogus SACK? */
3183 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003184 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 chunk->subh.sack_hdr = sackh;
3186 ctsn = ntohl(sackh->cum_tsn_ack);
3187
3188 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3189 * Ack Point, then drop the SACK. Since Cumulative TSN
3190 * Ack is monotonically increasing, a SACK whose
3191 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3192 * Point indicates an out-of-order SACK.
3193 */
3194 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3195 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3196 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3197 return SCTP_DISPOSITION_DISCARD;
3198 }
3199
Wei Yongjunaecedea2007-08-02 16:57:44 +08003200 /* If Cumulative TSN Ack beyond the max tsn currently
3201 * send, terminating the association and respond to the
3202 * sender with an ABORT.
3203 */
3204 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003205 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003206
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003208 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209
3210 /* Note: We do the rest of the work on the PROCESS_SACK
3211 * sideeffect.
3212 */
3213 return SCTP_DISPOSITION_CONSUME;
3214}
3215
3216/*
3217 * Generate an ABORT in response to a packet.
3218 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003219 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003221 * 8) The receiver should respond to the sender of the OOTB packet with
3222 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3223 * MUST fill in the Verification Tag field of the outbound packet
3224 * with the value found in the Verification Tag field of the OOTB
3225 * packet and set the T-bit in the Chunk Flags to indicate that the
3226 * Verification Tag is reflected. After sending this ABORT, the
3227 * receiver of the OOTB packet shall discard the OOTB packet and take
3228 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 *
3230 * Verification Tag:
3231 *
3232 * The return value is the disposition of the chunk.
3233*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003234static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3235 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 const struct sctp_association *asoc,
3237 const sctp_subtype_t type,
3238 void *arg,
3239 sctp_cmd_seq_t *commands)
3240{
3241 struct sctp_packet *packet = NULL;
3242 struct sctp_chunk *chunk = arg;
3243 struct sctp_chunk *abort;
3244
Eric W. Biederman2ce955032012-08-06 08:43:06 +00003245 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 if (packet) {
3248 /* Make an ABORT. The T bit will be set if the asoc
3249 * is NULL.
3250 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003251 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 if (!abort) {
3253 sctp_ootb_pkt_free(packet);
3254 return SCTP_DISPOSITION_NOMEM;
3255 }
3256
Jerome Forissier047a2422005-04-28 11:58:43 -07003257 /* Reflect vtag if T-Bit is set */
3258 if (sctp_test_T_bit(abort))
3259 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 /* Set the skb to the belonging sock for accounting. */
3262 abort->skb->sk = ep->base.sk;
3263
3264 sctp_packet_append_chunk(packet, abort);
3265
3266 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3267 SCTP_PACKET(packet));
3268
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003269 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003271 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return SCTP_DISPOSITION_CONSUME;
3273 }
3274
3275 return SCTP_DISPOSITION_NOMEM;
3276}
3277
3278/*
3279 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3280 * event as ULP notification for each cause included in the chunk.
3281 *
3282 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3283 *
3284 * The return value is the disposition of the chunk.
3285*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003286sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3287 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 const struct sctp_association *asoc,
3289 const sctp_subtype_t type,
3290 void *arg,
3291 sctp_cmd_seq_t *commands)
3292{
3293 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003294 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
3296 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003297 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298
3299 /* Make sure that the ERROR chunk has a valid length. */
3300 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003301 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003303 sctp_walk_errors(err, chunk->chunk_hdr);
3304 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003305 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003306 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Wei Yongjun3df26782009-03-02 06:46:51 +00003308 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3309 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
3314/*
3315 * Process an inbound SHUTDOWN ACK.
3316 *
3317 * From Section 9.2:
3318 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3319 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3320 * peer, and remove all record of the association.
3321 *
3322 * The return value is the disposition.
3323 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003324sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3325 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 const struct sctp_association *asoc,
3327 const sctp_subtype_t type,
3328 void *arg,
3329 sctp_cmd_seq_t *commands)
3330{
3331 struct sctp_chunk *chunk = arg;
3332 struct sctp_chunk *reply;
3333 struct sctp_ulpevent *ev;
3334
3335 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3339 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003340 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 /* 10.2 H) SHUTDOWN COMPLETE notification
3343 *
3344 * When SCTP completes the shutdown procedures (section 9.2) this
3345 * notification is passed to the upper layer.
3346 */
3347 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003348 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 if (!ev)
3350 goto nomem;
3351
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003352 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3353 reply = sctp_make_shutdown_complete(asoc, chunk);
3354 if (!reply)
3355 goto nomem_chunk;
3356
3357 /* Do all the commands now (after allocation), so that we
3358 * have consistent state if memory allocation failes
3359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3361
3362 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3363 * stop the T2-shutdown timer,
3364 */
3365 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3366 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3367
3368 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3369 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3372 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003373 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3374 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3376
3377 /* ...and remove all record of the association. */
3378 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3379 return SCTP_DISPOSITION_DELETE_TCB;
3380
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003381nomem_chunk:
3382 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383nomem:
3384 return SCTP_DISPOSITION_NOMEM;
3385}
3386
3387/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003388 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3389 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3391 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3392 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3393 * packet must fill in the Verification Tag field of the outbound
3394 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003395 * set the T-bit in the Chunk Flags to indicate that the Verification
3396 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 *
3398 * 8) The receiver should respond to the sender of the OOTB packet with
3399 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3400 * MUST fill in the Verification Tag field of the outbound packet
3401 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003402 * packet and set the T-bit in the Chunk Flags to indicate that the
3403 * Verification Tag is reflected. After sending this ABORT, the
3404 * receiver of the OOTB packet shall discard the OOTB packet and take
3405 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003407sctp_disposition_t sctp_sf_ootb(struct net *net,
3408 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 const struct sctp_association *asoc,
3410 const sctp_subtype_t type,
3411 void *arg,
3412 sctp_cmd_seq_t *commands)
3413{
3414 struct sctp_chunk *chunk = arg;
3415 struct sk_buff *skb = chunk->skb;
3416 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003417 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 __u8 *ch_end;
3419 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003420 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003422 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3425 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003426 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003428 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003429 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Vlad Yasevichece25df2007-09-07 16:30:54 -04003431 /* Now that we know we at least have a chunk header,
3432 * do things that are type appropriate.
3433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3435 ootb_shut_ack = 1;
3436
3437 /* RFC 2960, Section 3.3.7
3438 * Moreover, under any circumstances, an endpoint that
3439 * receives an ABORT MUST NOT respond to that ABORT by
3440 * sending an ABORT of its own.
3441 */
3442 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003443 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003444
Shan Wei85c5ed42011-04-19 21:30:01 +00003445 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3446 * or a COOKIE ACK the SCTP Packet should be silently
3447 * discarded.
3448 */
3449
3450 if (SCTP_CID_COOKIE_ACK == ch->type)
3451 ootb_cookie_ack = 1;
3452
3453 if (SCTP_CID_ERROR == ch->type) {
3454 sctp_walk_errors(err, ch) {
3455 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3456 ootb_cookie_ack = 1;
3457 break;
3458 }
3459 }
3460 }
3461
Vlad Yasevichece25df2007-09-07 16:30:54 -04003462 /* Report violation if chunk len overflows */
3463 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3464 if (ch_end > skb_tail_pointer(skb))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003465 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003466 commands);
3467
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003469 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
3471 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003472 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003473 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003474 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003476 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
3479/*
3480 * Handle an "Out of the blue" SHUTDOWN ACK.
3481 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003482 * Section: 8.4 5, sctpimpguide 2.41.
3483 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003485 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3486 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3487 * packet must fill in the Verification Tag field of the outbound
3488 * packet with the Verification Tag received in the SHUTDOWN ACK and
3489 * set the T-bit in the Chunk Flags to indicate that the Verification
3490 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 *
3492 * Inputs
3493 * (endpoint, asoc, type, arg, commands)
3494 *
3495 * Outputs
3496 * (sctp_disposition_t)
3497 *
3498 * The return value is the disposition of the chunk.
3499 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003500static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3501 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 const struct sctp_association *asoc,
3503 const sctp_subtype_t type,
3504 void *arg,
3505 sctp_cmd_seq_t *commands)
3506{
3507 struct sctp_packet *packet = NULL;
3508 struct sctp_chunk *chunk = arg;
3509 struct sctp_chunk *shut;
3510
Eric W. Biederman2ce955032012-08-06 08:43:06 +00003511 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 if (packet) {
3514 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003515 * The T bit will be set if the asoc is NULL.
3516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 shut = sctp_make_shutdown_complete(asoc, chunk);
3518 if (!shut) {
3519 sctp_ootb_pkt_free(packet);
3520 return SCTP_DISPOSITION_NOMEM;
3521 }
3522
Jerome Forissier047a2422005-04-28 11:58:43 -07003523 /* Reflect vtag if T-Bit is set */
3524 if (sctp_test_T_bit(shut))
3525 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 /* Set the skb to the belonging sock for accounting. */
3528 shut->skb->sk = ep->base.sk;
3529
3530 sctp_packet_append_chunk(packet, shut);
3531
3532 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3533 SCTP_PACKET(packet));
3534
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003535 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 /* If the chunk length is invalid, we don't want to process
3538 * the reset of the packet.
3539 */
3540 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003541 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003543 /* We need to discard the rest of the packet to prevent
3544 * potential bomming attacks from additional bundled chunks.
3545 * This is documented in SCTP Threats ID.
3546 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003547 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 }
3549
3550 return SCTP_DISPOSITION_NOMEM;
3551}
3552
3553/*
3554 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3555 *
3556 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3557 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3558 * procedures in section 8.4 SHOULD be followed, in other words it
3559 * should be treated as an Out Of The Blue packet.
3560 * [This means that we do NOT check the Verification Tag on these
3561 * chunks. --piggy ]
3562 *
3563 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003564sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3565 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 const struct sctp_association *asoc,
3567 const sctp_subtype_t type,
3568 void *arg,
3569 sctp_cmd_seq_t *commands)
3570{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003571 struct sctp_chunk *chunk = arg;
3572
3573 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3574 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003575 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003576 commands);
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 /* Although we do have an association in this case, it corresponds
3579 * to a restarted association. So the packet is treated as an OOTB
3580 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3581 * called with a NULL association.
3582 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003583 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003584
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003585 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
3588/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003589sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3590 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 const struct sctp_association *asoc,
3592 const sctp_subtype_t type, void *arg,
3593 sctp_cmd_seq_t *commands)
3594{
3595 struct sctp_chunk *chunk = arg;
3596 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003597 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 sctp_addiphdr_t *hdr;
3599 __u32 serial;
3600
3601 if (!sctp_vtag_verify(chunk, asoc)) {
3602 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3603 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003604 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
3606
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003607 /* ADD-IP: Section 4.1.1
3608 * This chunk MUST be sent in an authenticated way by using
3609 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3610 * is received unauthenticated it MUST be silently discarded as
3611 * described in [I-D.ietf-tsvwg-sctp-auth].
3612 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003613 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003614 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3617 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003618 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 commands);
3620
3621 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3622 serial = ntohl(hdr->serial);
3623
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003624 /* Verify the ASCONF chunk before processing it. */
Daniel Borkmanncda702d2014-10-09 22:55:31 +02003625 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003626 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003627 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003628
Vlad Yasevicha08de642007-12-20 14:11:47 -08003629 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003631 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 */
3633 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003634 /* If this is the first instance of ASCONF in the packet,
3635 * we can clean our old ASCONF-ACKs.
3636 */
3637 if (!chunk->has_asconf)
3638 sctp_assoc_clean_asconf_ack_cache(asoc);
3639
3640 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3641 * expected, process the ASCONF as described below and after
3642 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3643 * the response packet and cache a copy of it (in the event it
3644 * later needs to be retransmitted).
3645 *
3646 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 */
3648 asconf_ack = sctp_process_asconf((struct sctp_association *)
3649 asoc, chunk);
3650 if (!asconf_ack)
3651 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003652 } else if (serial < asoc->peer.addip_serial + 1) {
3653 /* ADDIP 5.2 E2)
3654 * If the value found in the Sequence Number is less than the
3655 * ('Peer- Sequence-Number' + 1), simply skip to the next
3656 * ASCONF, and include in the outbound response packet
3657 * any previously cached ASCONF-ACK response that was
3658 * sent and saved that matches the Sequence Number of the
3659 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3660 * Chunk exists. This will occur when an older ASCONF
3661 * arrives out of order. In such a case, the receiver
3662 * should skip the ASCONF Chunk and not include ASCONF-ACK
3663 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003665 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3666 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003668
3669 /* Reset the transport so that we select the correct one
3670 * this time around. This is to make sure that we don't
3671 * accidentally use a stale transport that's been removed.
3672 */
3673 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003675 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003677 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return SCTP_DISPOSITION_DISCARD;
3679 }
3680
Vlad Yasevicha08de642007-12-20 14:11:47 -08003681 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3682 * containing the ASCONF-ACK Chunks MUST be the source address of
3683 * the SCTP packet that held the ASCONF Chunks.
3684 *
3685 * To do this properly, we'll set the destination address of the chunk
3686 * and at the transmit time, will try look up the transport to use.
3687 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003688 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003690 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003692 if (asoc->new_transport) {
3693 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3694 commands);
3695 ((struct sctp_association *)asoc)->new_transport = NULL;
3696 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return SCTP_DISPOSITION_CONSUME;
3699}
3700
3701/*
3702 * ADDIP Section 4.3 General rules for address manipulation
3703 * When building TLV parameters for the ASCONF Chunk that will add or
3704 * delete IP addresses the D0 to D13 rules should be applied:
3705 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003706sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3707 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003709 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 sctp_cmd_seq_t *commands)
3711{
3712 struct sctp_chunk *asconf_ack = arg;
3713 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3714 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003715 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 sctp_addiphdr_t *addip_hdr;
3717 __u32 sent_serial, rcvd_serial;
3718
3719 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3720 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3721 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003722 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 }
3724
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003725 /* ADD-IP, Section 4.1.2:
3726 * This chunk MUST be sent in an authenticated way by using
3727 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3728 * is received unauthenticated it MUST be silently discarded as
3729 * described in [I-D.ietf-tsvwg-sctp-auth].
3730 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003731 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003732 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 /* Make sure that the ADDIP chunk has a valid length. */
3735 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003736 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 commands);
3738
3739 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3740 rcvd_serial = ntohl(addip_hdr->serial);
3741
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003742 /* Verify the ASCONF-ACK chunk before processing it. */
Daniel Borkmanncda702d2014-10-09 22:55:31 +02003743 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003744 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003745 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 if (last_asconf) {
3748 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3749 sent_serial = ntohl(addip_hdr->serial);
3750 } else {
3751 sent_serial = asoc->addip_serial - 1;
3752 }
3753
3754 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3755 * equal to the next serial number to be used but no ASCONF chunk is
3756 * outstanding the endpoint MUST ABORT the association. Note that a
3757 * sequence number is greater than if it is no more than 2^^31-1
3758 * larger than the current sequence number (using serial arithmetic).
3759 */
3760 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3761 !(asoc->addip_last_asconf)) {
3762 abort = sctp_make_abort(asoc, asconf_ack,
3763 sizeof(sctp_errhdr_t));
3764 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003765 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3767 SCTP_CHUNK(abort));
3768 }
3769 /* We are going to ABORT, so we might as well stop
3770 * processing the rest of the chunks in the packet.
3771 */
3772 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3773 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3774 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003775 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003776 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003778 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003779 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3780 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return SCTP_DISPOSITION_ABORT;
3782 }
3783
3784 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3785 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3786 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3787
3788 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003789 asconf_ack)) {
3790 /* Successfully processed ASCONF_ACK. We can
3791 * release the next asconf if we have one.
3792 */
3793 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3794 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 abort = sctp_make_abort(asoc, asconf_ack,
3799 sizeof(sctp_errhdr_t));
3800 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003801 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3803 SCTP_CHUNK(abort));
3804 }
3805 /* We are going to ABORT, so we might as well stop
3806 * processing the rest of the chunks in the packet.
3807 */
3808 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003809 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003810 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003812 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003813 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3814 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 return SCTP_DISPOSITION_ABORT;
3816 }
3817
3818 return SCTP_DISPOSITION_DISCARD;
3819}
3820
3821/*
3822 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3823 *
3824 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3825 * its cumulative TSN point to the value carried in the FORWARD TSN
3826 * chunk, and then MUST further advance its cumulative TSN point locally
3827 * if possible.
3828 * After the above processing, the data receiver MUST stop reporting any
3829 * missing TSNs earlier than or equal to the new cumulative TSN point.
3830 *
3831 * Verification Tag: 8.5 Verification Tag [Normal verification]
3832 *
3833 * The return value is the disposition of the chunk.
3834 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003835sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3836 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 const struct sctp_association *asoc,
3838 const sctp_subtype_t type,
3839 void *arg,
3840 sctp_cmd_seq_t *commands)
3841{
3842 struct sctp_chunk *chunk = arg;
3843 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003844 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 __u16 len;
3846 __u32 tsn;
3847
3848 if (!sctp_vtag_verify(chunk, asoc)) {
3849 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3850 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003851 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853
3854 /* Make sure that the FORWARD_TSN chunk has valid length. */
3855 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003856 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 commands);
3858
3859 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3860 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3861 len = ntohs(chunk->chunk_hdr->length);
3862 len -= sizeof(struct sctp_chunkhdr);
3863 skb_pull(chunk->skb, len);
3864
3865 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003866 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 /* The TSN is too high--silently discard the chunk and count on it
3869 * getting retransmitted later.
3870 */
3871 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3872 goto discard_noforce;
3873
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003874 /* Silently discard the chunk if stream-id is not valid */
3875 sctp_walk_fwdtsn(skip, chunk) {
3876 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3877 goto discard_noforce;
3878 }
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3881 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003882 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 /* Count this as receiving DATA. */
3886 if (asoc->autoclose) {
3887 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3888 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3889 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003892 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 */
3894 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 return SCTP_DISPOSITION_CONSUME;
3897
3898discard_noforce:
3899 return SCTP_DISPOSITION_DISCARD;
3900}
3901
3902sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003903 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 const struct sctp_endpoint *ep,
3905 const struct sctp_association *asoc,
3906 const sctp_subtype_t type,
3907 void *arg,
3908 sctp_cmd_seq_t *commands)
3909{
3910 struct sctp_chunk *chunk = arg;
3911 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003912 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 __u16 len;
3914 __u32 tsn;
3915
3916 if (!sctp_vtag_verify(chunk, asoc)) {
3917 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3918 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003919 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
3921
3922 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3923 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003924 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 commands);
3926
3927 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3928 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3929 len = ntohs(chunk->chunk_hdr->length);
3930 len -= sizeof(struct sctp_chunkhdr);
3931 skb_pull(chunk->skb, len);
3932
3933 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003934 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 /* The TSN is too high--silently discard the chunk and count on it
3937 * getting retransmitted later.
3938 */
3939 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3940 goto gen_shutdown;
3941
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003942 /* Silently discard the chunk if stream-id is not valid */
3943 sctp_walk_fwdtsn(skip, chunk) {
3944 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3945 goto gen_shutdown;
3946 }
3947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3949 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003950 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 /* Go a head and force a SACK, since we are shutting down. */
3954gen_shutdown:
3955 /* Implementor's Guide.
3956 *
3957 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3958 * respond to each received packet containing one or more DATA chunk(s)
3959 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3960 */
3961 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3962 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3963 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3964 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3965
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003966 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
3969/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003970 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003971 *
3972 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3973 * Identifier field. If this algorithm was not specified by the
3974 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3975 * during association setup, the AUTH chunk and all chunks after it MUST
3976 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3977 * defined in Section 4.1.
3978 *
3979 * If an endpoint with no shared key receives a Shared Key Identifier
3980 * other than 0, it MUST silently discard all authenticated chunks. If
3981 * the endpoint has at least one endpoint pair shared key for the peer,
3982 * it MUST use the key specified by the Shared Key Identifier if a
3983 * key has been configured for that Shared Key Identifier. If no
3984 * endpoint pair shared key has been configured for that Shared Key
3985 * Identifier, all authenticated chunks MUST be silently discarded.
3986 *
3987 * Verification Tag: 8.5 Verification Tag [Normal verification]
3988 *
3989 * The return value is the disposition of the chunk.
3990 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003991static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3992 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003993 const struct sctp_association *asoc,
3994 const sctp_subtype_t type,
3995 struct sctp_chunk *chunk)
3996{
3997 struct sctp_authhdr *auth_hdr;
3998 struct sctp_hmac *hmac;
3999 unsigned int sig_len;
4000 __u16 key_id;
4001 __u8 *save_digest;
4002 __u8 *digest;
4003
4004 /* Pull in the auth header, so we can do some more verification */
4005 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4006 chunk->subh.auth_hdr = auth_hdr;
4007 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4008
Masanari Iida02582e92012-08-22 19:11:26 +09004009 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004010 * chunk.
4011 */
4012 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4013 return SCTP_IERROR_AUTH_BAD_HMAC;
4014
4015 /* Make sure that the provided shared key identifier has been
4016 * configured
4017 */
4018 key_id = ntohs(auth_hdr->shkey_id);
4019 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4020 return SCTP_IERROR_AUTH_BAD_KEYID;
4021
4022
4023 /* Make sure that the length of the signature matches what
4024 * we expect.
4025 */
4026 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4027 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4028 if (sig_len != hmac->hmac_len)
4029 return SCTP_IERROR_PROTO_VIOLATION;
4030
4031 /* Now that we've done validation checks, we can compute and
4032 * verify the hmac. The steps involved are:
4033 * 1. Save the digest from the chunk.
4034 * 2. Zero out the digest in the chunk.
4035 * 3. Compute the new digest
4036 * 4. Compare saved and new digests.
4037 */
4038 digest = auth_hdr->hmac;
4039 skb_pull(chunk->skb, sig_len);
4040
4041 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4042 if (!save_digest)
4043 goto nomem;
4044
4045 memset(digest, 0, sig_len);
4046
4047 sctp_auth_calculate_hmac(asoc, chunk->skb,
4048 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4049 GFP_ATOMIC);
4050
4051 /* Discard the packet if the digests do not match */
4052 if (memcmp(save_digest, digest, sig_len)) {
4053 kfree(save_digest);
4054 return SCTP_IERROR_BAD_SIG;
4055 }
4056
4057 kfree(save_digest);
4058 chunk->auth = 1;
4059
4060 return SCTP_IERROR_NO_ERROR;
4061nomem:
4062 return SCTP_IERROR_NOMEM;
4063}
4064
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004065sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4066 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004067 const struct sctp_association *asoc,
4068 const sctp_subtype_t type,
4069 void *arg,
4070 sctp_cmd_seq_t *commands)
4071{
4072 struct sctp_authhdr *auth_hdr;
4073 struct sctp_chunk *chunk = arg;
4074 struct sctp_chunk *err_chunk;
4075 sctp_ierror_t error;
4076
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004077 /* Make sure that the peer has AUTH capable */
4078 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004079 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004080
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004081 if (!sctp_vtag_verify(chunk, asoc)) {
4082 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4083 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004084 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004085 }
4086
4087 /* Make sure that the AUTH chunk has valid length. */
4088 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004089 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004090 commands);
4091
4092 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004093 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004094 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004095 case SCTP_IERROR_AUTH_BAD_HMAC:
4096 /* Generate the ERROR chunk and discard the rest
4097 * of the packet
4098 */
4099 err_chunk = sctp_make_op_error(asoc, chunk,
4100 SCTP_ERROR_UNSUP_HMAC,
4101 &auth_hdr->hmac_id,
4102 sizeof(__u16), 0);
4103 if (err_chunk) {
4104 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4105 SCTP_CHUNK(err_chunk));
4106 }
4107 /* Fall Through */
4108 case SCTP_IERROR_AUTH_BAD_KEYID:
4109 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004110 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004111
4112 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004113 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004114 commands);
4115
4116 case SCTP_IERROR_NOMEM:
4117 return SCTP_DISPOSITION_NOMEM;
4118
4119 default: /* Prevent gcc warnings */
4120 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004121 }
4122
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004123 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4124 struct sctp_ulpevent *ev;
4125
4126 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4127 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4128
4129 if (!ev)
4130 return -ENOMEM;
4131
4132 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4133 SCTP_ULPEVENT(ev));
4134 }
4135
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004136 return SCTP_DISPOSITION_CONSUME;
4137}
4138
4139/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 * Process an unknown chunk.
4141 *
4142 * Section: 3.2. Also, 2.1 in the implementor's guide.
4143 *
4144 * Chunk Types are encoded such that the highest-order two bits specify
4145 * the action that must be taken if the processing endpoint does not
4146 * recognize the Chunk Type.
4147 *
4148 * 00 - Stop processing this SCTP packet and discard it, do not process
4149 * any further chunks within it.
4150 *
4151 * 01 - Stop processing this SCTP packet and discard it, do not process
4152 * any further chunks within it, and report the unrecognized
4153 * chunk in an 'Unrecognized Chunk Type'.
4154 *
4155 * 10 - Skip this chunk and continue processing.
4156 *
4157 * 11 - Skip this chunk and continue processing, but report in an ERROR
4158 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4159 *
4160 * The return value is the disposition of the chunk.
4161 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004162sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4163 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 const struct sctp_association *asoc,
4165 const sctp_subtype_t type,
4166 void *arg,
4167 sctp_cmd_seq_t *commands)
4168{
4169 struct sctp_chunk *unk_chunk = arg;
4170 struct sctp_chunk *err_chunk;
4171 sctp_chunkhdr_t *hdr;
4172
4173 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4174
4175 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004176 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 /* Make sure that the chunk has a valid length.
4179 * Since we don't know the chunk type, we use a general
4180 * chunkhdr structure to make a comparison.
4181 */
4182 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004183 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 commands);
4185
4186 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4187 case SCTP_CID_ACTION_DISCARD:
4188 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004189 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 break;
4191 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 /* Generate an ERROR chunk as response. */
4193 hdr = unk_chunk->chunk_hdr;
4194 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4195 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004196 WORD_ROUND(ntohs(hdr->length)),
4197 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 if (err_chunk) {
4199 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4200 SCTP_CHUNK(err_chunk));
4201 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004202
4203 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004204 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return SCTP_DISPOSITION_CONSUME;
4206 break;
4207 case SCTP_CID_ACTION_SKIP:
4208 /* Skip the chunk. */
4209 return SCTP_DISPOSITION_DISCARD;
4210 break;
4211 case SCTP_CID_ACTION_SKIP_ERR:
4212 /* Generate an ERROR chunk as response. */
4213 hdr = unk_chunk->chunk_hdr;
4214 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4215 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004216 WORD_ROUND(ntohs(hdr->length)),
4217 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (err_chunk) {
4219 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4220 SCTP_CHUNK(err_chunk));
4221 }
4222 /* Skip the chunk. */
4223 return SCTP_DISPOSITION_CONSUME;
4224 break;
4225 default:
4226 break;
4227 }
4228
4229 return SCTP_DISPOSITION_DISCARD;
4230}
4231
4232/*
4233 * Discard the chunk.
4234 *
4235 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4236 * [Too numerous to mention...]
4237 * Verification Tag: No verification needed.
4238 * Inputs
4239 * (endpoint, asoc, chunk)
4240 *
4241 * Outputs
4242 * (asoc, reply_msg, msg_up, timers, counters)
4243 *
4244 * The return value is the disposition of the chunk.
4245 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004246sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4247 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 const struct sctp_association *asoc,
4249 const sctp_subtype_t type,
4250 void *arg,
4251 sctp_cmd_seq_t *commands)
4252{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004253 struct sctp_chunk *chunk = arg;
4254
4255 /* Make sure that the chunk has a valid length.
4256 * Since we don't know the chunk type, we use a general
4257 * chunkhdr structure to make a comparison.
4258 */
4259 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004260 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004261 commands);
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4264 return SCTP_DISPOSITION_DISCARD;
4265}
4266
4267/*
4268 * Discard the whole packet.
4269 *
4270 * Section: 8.4 2)
4271 *
4272 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4273 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 *
4275 * Verification Tag: No verification necessary
4276 *
4277 * Inputs
4278 * (endpoint, asoc, chunk)
4279 *
4280 * Outputs
4281 * (asoc, reply_msg, msg_up, timers, counters)
4282 *
4283 * The return value is the disposition of the chunk.
4284 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004285sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4286 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 const struct sctp_association *asoc,
4288 const sctp_subtype_t type,
4289 void *arg,
4290 sctp_cmd_seq_t *commands)
4291{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004292 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4294
4295 return SCTP_DISPOSITION_CONSUME;
4296}
4297
4298
4299/*
4300 * The other end is violating protocol.
4301 *
4302 * Section: Not specified
4303 * Verification Tag: Not specified
4304 * Inputs
4305 * (endpoint, asoc, chunk)
4306 *
4307 * Outputs
4308 * (asoc, reply_msg, msg_up, timers, counters)
4309 *
4310 * We simply tag the chunk as a violation. The state machine will log
4311 * the violation and continue.
4312 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004313sctp_disposition_t sctp_sf_violation(struct net *net,
4314 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 const struct sctp_association *asoc,
4316 const sctp_subtype_t type,
4317 void *arg,
4318 sctp_cmd_seq_t *commands)
4319{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004320 struct sctp_chunk *chunk = arg;
4321
4322 /* Make sure that the chunk has a valid length. */
4323 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004324 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004325 commands);
4326
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 return SCTP_DISPOSITION_VIOLATION;
4328}
4329
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004331 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004333static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004334 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004335 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004338 sctp_cmd_seq_t *commands,
4339 const __u8 *payload,
4340 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004342 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 struct sctp_chunk *chunk = arg;
4344 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004346 /* SCTP-AUTH, Section 6.3:
4347 * It should be noted that if the receiver wants to tear
4348 * down an association in an authenticated way only, the
4349 * handling of malformed packets should not result in
4350 * tearing down the association.
4351 *
4352 * This means that if we only want to abort associations
4353 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004354 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004355 * was malformed.
4356 */
4357 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4358 goto discard;
4359
Jesper Juhl9abed242007-11-11 23:57:49 +01004360 /* Make the abort chunk. */
4361 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4362 if (!abort)
4363 goto nomem;
4364
Vlad Yasevichece25df2007-09-07 16:30:54 -04004365 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004366 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4367 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4368 !asoc->peer.i.init_tag) {
4369 sctp_initack_chunk_t *initack;
4370
4371 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4372 if (!sctp_chunk_length_valid(chunk,
4373 sizeof(sctp_initack_chunk_t)))
4374 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4375 else {
4376 unsigned int inittag;
4377
4378 inittag = ntohl(initack->init_hdr.init_tag);
4379 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4380 SCTP_U32(inittag));
4381 }
4382 }
4383
Vlad Yasevichece25df2007-09-07 16:30:54 -04004384 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004385 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
Vlad Yasevichece25df2007-09-07 16:30:54 -04004387 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4388 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4389 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4390 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4391 SCTP_ERROR(ECONNREFUSED));
4392 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4393 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4394 } else {
4395 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4396 SCTP_ERROR(ECONNABORTED));
4397 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4398 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004399 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 } else {
Eric W. Biederman2ce955032012-08-06 08:43:06 +00004402 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004403
4404 if (!packet)
4405 goto nomem_pkt;
4406
4407 if (sctp_test_T_bit(abort))
4408 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4409
4410 abort->skb->sk = ep->base.sk;
4411
4412 sctp_packet_append_chunk(packet, abort);
4413
4414 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4415 SCTP_PACKET(packet));
4416
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004417 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 }
4419
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004420 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004421
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004422discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004423 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 return SCTP_DISPOSITION_ABORT;
4425
Vlad Yasevichece25df2007-09-07 16:30:54 -04004426nomem_pkt:
4427 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428nomem:
4429 return SCTP_DISPOSITION_NOMEM;
4430}
4431
Wei Yongjunaecedea2007-08-02 16:57:44 +08004432/*
4433 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004434 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004435 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004436 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004437 *
4438 * We inform the other end by sending an ABORT with a Protocol Violation
4439 * error code.
4440 *
4441 * Section: Not specified
4442 * Verification Tag: Nothing to do
4443 * Inputs
4444 * (endpoint, asoc, chunk)
4445 *
4446 * Outputs
4447 * (reply_msg, msg_up, counters)
4448 *
4449 * Generate an ABORT chunk and terminate the association.
4450 */
4451static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004452 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004453 const struct sctp_endpoint *ep,
4454 const struct sctp_association *asoc,
4455 const sctp_subtype_t type,
4456 void *arg,
4457 sctp_cmd_seq_t *commands)
4458{
Florian Westphal24448442008-03-23 22:46:34 -07004459 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004460
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004461 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004462 sizeof(err_str));
4463}
4464
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004465/*
4466 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004467 * If the length is smaller than the minimum length of a given parameter,
4468 * or accumulated length in multi parameters exceeds the end of the chunk,
4469 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004470 */
4471static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004472 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004473 const struct sctp_endpoint *ep,
4474 const struct sctp_association *asoc,
4475 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004476 void *arg, void *ext,
4477 sctp_cmd_seq_t *commands)
4478{
4479 struct sctp_chunk *chunk = arg;
4480 struct sctp_paramhdr *param = ext;
4481 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004482
Wei Yongjunba016672008-09-30 05:32:24 -07004483 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4484 goto discard;
4485
4486 /* Make the abort chunk. */
4487 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4488 if (!abort)
4489 goto nomem;
4490
4491 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004492 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004493
4494 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4495 SCTP_ERROR(ECONNABORTED));
4496 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4497 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004498 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4499 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004500
4501discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004502 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004503 return SCTP_DISPOSITION_ABORT;
4504nomem:
4505 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004506}
4507
Wei Yongjunaecedea2007-08-02 16:57:44 +08004508/* Handle a protocol violation when the peer trying to advance the
4509 * cumulative tsn ack to a point beyond the max tsn currently sent.
4510 *
4511 * We inform the other end by sending an ABORT with a Protocol Violation
4512 * error code.
4513 */
4514static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004515 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004516 const struct sctp_endpoint *ep,
4517 const struct sctp_association *asoc,
4518 const sctp_subtype_t type,
4519 void *arg,
4520 sctp_cmd_seq_t *commands)
4521{
Florian Westphal24448442008-03-23 22:46:34 -07004522 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004523
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004524 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004525 sizeof(err_str));
4526}
4527
Vlad Yasevichece25df2007-09-07 16:30:54 -04004528/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004529 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004530 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004531 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004532 * on the path and we may not want to continue this communication.
4533 */
4534static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004535 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004536 const struct sctp_endpoint *ep,
4537 const struct sctp_association *asoc,
4538 const sctp_subtype_t type,
4539 void *arg,
4540 sctp_cmd_seq_t *commands)
4541{
Florian Westphal24448442008-03-23 22:46:34 -07004542 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004543
4544 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004545 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004546
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004547 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004548 sizeof(err_str));
4549}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550/***************************************************************************
4551 * These are the state functions for handling primitive (Section 10) events.
4552 ***************************************************************************/
4553/*
4554 * sctp_sf_do_prm_asoc
4555 *
4556 * Section: 10.1 ULP-to-SCTP
4557 * B) Associate
4558 *
4559 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4560 * outbound stream count)
4561 * -> association id [,destination transport addr list] [,outbound stream
4562 * count]
4563 *
4564 * This primitive allows the upper layer to initiate an association to a
4565 * specific peer endpoint.
4566 *
4567 * The peer endpoint shall be specified by one of the transport addresses
4568 * which defines the endpoint (see Section 1.4). If the local SCTP
4569 * instance has not been initialized, the ASSOCIATE is considered an
4570 * error.
4571 * [This is not relevant for the kernel implementation since we do all
4572 * initialization at boot time. It we hadn't initialized we wouldn't
4573 * get anywhere near this code.]
4574 *
4575 * An association id, which is a local handle to the SCTP association,
4576 * will be returned on successful establishment of the association. If
4577 * SCTP is not able to open an SCTP association with the peer endpoint,
4578 * an error is returned.
4579 * [In the kernel implementation, the struct sctp_association needs to
4580 * be created BEFORE causing this primitive to run.]
4581 *
4582 * Other association parameters may be returned, including the
4583 * complete destination transport addresses of the peer as well as the
4584 * outbound stream count of the local endpoint. One of the transport
4585 * address from the returned destination addresses will be selected by
4586 * the local endpoint as default primary path for sending SCTP packets
4587 * to this peer. The returned "destination transport addr list" can
4588 * be used by the ULP to change the default primary path or to force
4589 * sending a packet to a specific transport address. [All of this
4590 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4591 * function.]
4592 *
4593 * Mandatory attributes:
4594 *
4595 * o local SCTP instance name - obtained from the INITIALIZE operation.
4596 * [This is the argument asoc.]
4597 * o destination transport addr - specified as one of the transport
4598 * addresses of the peer endpoint with which the association is to be
4599 * established.
4600 * [This is asoc->peer.active_path.]
4601 * o outbound stream count - the number of outbound streams the ULP
4602 * would like to open towards this peer endpoint.
4603 * [BUG: This is not currently implemented.]
4604 * Optional attributes:
4605 *
4606 * None.
4607 *
4608 * The return value is a disposition.
4609 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004610sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4611 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 const struct sctp_association *asoc,
4613 const sctp_subtype_t type,
4614 void *arg,
4615 sctp_cmd_seq_t *commands)
4616{
4617 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004618 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
4620 /* The comment below says that we enter COOKIE-WAIT AFTER
4621 * sending the INIT, but that doesn't actually work in our
4622 * implementation...
4623 */
4624 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4625 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4626
4627 /* RFC 2960 5.1 Normal Establishment of an Association
4628 *
4629 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4630 * must provide its Verification Tag (Tag_A) in the Initiate
4631 * Tag field. Tag_A SHOULD be a random number in the range of
4632 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4633 */
4634
4635 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4636 if (!repl)
4637 goto nomem;
4638
4639 /* Cast away the const modifier, as we want to just
4640 * rerun it through as a sideffect.
4641 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004642 my_asoc = (struct sctp_association *)asoc;
4643 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644
Frank Filz3f7a87d2005-06-20 13:14:57 -07004645 /* Choose transport for INIT. */
4646 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4647 SCTP_CHUNK(repl));
4648
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 /* After sending the INIT, "A" starts the T1-init timer and
4650 * enters the COOKIE-WAIT state.
4651 */
4652 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4653 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4654 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4655 return SCTP_DISPOSITION_CONSUME;
4656
4657nomem:
4658 return SCTP_DISPOSITION_NOMEM;
4659}
4660
4661/*
4662 * Process the SEND primitive.
4663 *
4664 * Section: 10.1 ULP-to-SCTP
4665 * E) Send
4666 *
4667 * Format: SEND(association id, buffer address, byte count [,context]
4668 * [,stream id] [,life time] [,destination transport address]
4669 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4670 * -> result
4671 *
4672 * This is the main method to send user data via SCTP.
4673 *
4674 * Mandatory attributes:
4675 *
4676 * o association id - local handle to the SCTP association
4677 *
4678 * o buffer address - the location where the user message to be
4679 * transmitted is stored;
4680 *
4681 * o byte count - The size of the user data in number of bytes;
4682 *
4683 * Optional attributes:
4684 *
4685 * o context - an optional 32 bit integer that will be carried in the
4686 * sending failure notification to the ULP if the transportation of
4687 * this User Message fails.
4688 *
4689 * o stream id - to indicate which stream to send the data on. If not
4690 * specified, stream 0 will be used.
4691 *
4692 * o life time - specifies the life time of the user data. The user data
4693 * will not be sent by SCTP after the life time expires. This
4694 * parameter can be used to avoid efforts to transmit stale
4695 * user messages. SCTP notifies the ULP if the data cannot be
4696 * initiated to transport (i.e. sent to the destination via SCTP's
4697 * send primitive) within the life time variable. However, the
4698 * user data will be transmitted if SCTP has attempted to transmit a
4699 * chunk before the life time expired.
4700 *
4701 * o destination transport address - specified as one of the destination
4702 * transport addresses of the peer endpoint to which this packet
4703 * should be sent. Whenever possible, SCTP should use this destination
4704 * transport address for sending the packets, instead of the current
4705 * primary path.
4706 *
4707 * o unorder flag - this flag, if present, indicates that the user
4708 * would like the data delivered in an unordered fashion to the peer
4709 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4710 * message).
4711 *
4712 * o no-bundle flag - instructs SCTP not to bundle this user data with
4713 * other outbound DATA chunks. SCTP MAY still bundle even when
4714 * this flag is present, when faced with network congestion.
4715 *
4716 * o payload protocol-id - A 32 bit unsigned integer that is to be
4717 * passed to the peer indicating the type of payload protocol data
4718 * being transmitted. This value is passed as opaque data by SCTP.
4719 *
4720 * The return value is the disposition.
4721 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004722sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4723 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 const struct sctp_association *asoc,
4725 const sctp_subtype_t type,
4726 void *arg,
4727 sctp_cmd_seq_t *commands)
4728{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004729 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004731 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 return SCTP_DISPOSITION_CONSUME;
4733}
4734
4735/*
4736 * Process the SHUTDOWN primitive.
4737 *
4738 * Section: 10.1:
4739 * C) Shutdown
4740 *
4741 * Format: SHUTDOWN(association id)
4742 * -> result
4743 *
4744 * Gracefully closes an association. Any locally queued user data
4745 * will be delivered to the peer. The association will be terminated only
4746 * after the peer acknowledges all the SCTP packets sent. A success code
4747 * will be returned on successful termination of the association. If
4748 * attempting to terminate the association results in a failure, an error
4749 * code shall be returned.
4750 *
4751 * Mandatory attributes:
4752 *
4753 * o association id - local handle to the SCTP association
4754 *
4755 * Optional attributes:
4756 *
4757 * None.
4758 *
4759 * The return value is the disposition.
4760 */
4761sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004762 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 const struct sctp_endpoint *ep,
4764 const struct sctp_association *asoc,
4765 const sctp_subtype_t type,
4766 void *arg,
4767 sctp_cmd_seq_t *commands)
4768{
4769 int disposition;
4770
4771 /* From 9.2 Shutdown of an Association
4772 * Upon receipt of the SHUTDOWN primitive from its upper
4773 * layer, the endpoint enters SHUTDOWN-PENDING state and
4774 * remains there until all outstanding data has been
4775 * acknowledged by its peer. The endpoint accepts no new data
4776 * from its upper layer, but retransmits data to the far end
4777 * if necessary to fill gaps.
4778 */
4779 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4780 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 disposition = SCTP_DISPOSITION_CONSUME;
4783 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004784 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 arg, commands);
4786 }
4787 return disposition;
4788}
4789
4790/*
4791 * Process the ABORT primitive.
4792 *
4793 * Section: 10.1:
4794 * C) Abort
4795 *
4796 * Format: Abort(association id [, cause code])
4797 * -> result
4798 *
4799 * Ungracefully closes an association. Any locally queued user data
4800 * will be discarded and an ABORT chunk is sent to the peer. A success code
4801 * will be returned on successful abortion of the association. If
4802 * attempting to abort the association results in a failure, an error
4803 * code shall be returned.
4804 *
4805 * Mandatory attributes:
4806 *
4807 * o association id - local handle to the SCTP association
4808 *
4809 * Optional attributes:
4810 *
4811 * o cause code - reason of the abort to be passed to the peer
4812 *
4813 * None.
4814 *
4815 * The return value is the disposition.
4816 */
4817sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004818 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 const struct sctp_endpoint *ep,
4820 const struct sctp_association *asoc,
4821 const sctp_subtype_t type,
4822 void *arg,
4823 sctp_cmd_seq_t *commands)
4824{
4825 /* From 9.1 Abort of an Association
4826 * Upon receipt of the ABORT primitive from its upper
4827 * layer, the endpoint enters CLOSED state and
4828 * discard all outstanding data has been
4829 * acknowledged by its peer. The endpoint accepts no new data
4830 * from its upper layer, but retransmits data to the far end
4831 * if necessary to fill gaps.
4832 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004833 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 sctp_disposition_t retval;
4835
4836 retval = SCTP_DISPOSITION_CONSUME;
4837
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004838 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
4840 /* Even if we can't send the ABORT due to low memory delete the
4841 * TCB. This is a departure from our typical NOMEM handling.
4842 */
4843
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004844 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4845 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 /* Delete the established association. */
4847 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004848 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004850 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4851 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
4853 return retval;
4854}
4855
4856/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004857sctp_disposition_t sctp_sf_error_closed(struct net *net,
4858 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 const struct sctp_association *asoc,
4860 const sctp_subtype_t type,
4861 void *arg,
4862 sctp_cmd_seq_t *commands)
4863{
4864 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4865 return SCTP_DISPOSITION_CONSUME;
4866}
4867
4868/* We tried an illegal operation on an association which is shutting
4869 * down.
4870 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004871sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4872 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 const struct sctp_association *asoc,
4874 const sctp_subtype_t type,
4875 void *arg,
4876 sctp_cmd_seq_t *commands)
4877{
4878 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4879 SCTP_ERROR(-ESHUTDOWN));
4880 return SCTP_DISPOSITION_CONSUME;
4881}
4882
4883/*
4884 * sctp_cookie_wait_prm_shutdown
4885 *
4886 * Section: 4 Note: 2
4887 * Verification Tag:
4888 * Inputs
4889 * (endpoint, asoc)
4890 *
4891 * The RFC does not explicitly address this issue, but is the route through the
4892 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4893 *
4894 * Outputs
4895 * (timers)
4896 */
4897sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004898 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 const struct sctp_endpoint *ep,
4900 const struct sctp_association *asoc,
4901 const sctp_subtype_t type,
4902 void *arg,
4903 sctp_cmd_seq_t *commands)
4904{
4905 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4906 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4907
4908 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4909 SCTP_STATE(SCTP_STATE_CLOSED));
4910
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004911 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
4913 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4914
4915 return SCTP_DISPOSITION_DELETE_TCB;
4916}
4917
4918/*
4919 * sctp_cookie_echoed_prm_shutdown
4920 *
4921 * Section: 4 Note: 2
4922 * Verification Tag:
4923 * Inputs
4924 * (endpoint, asoc)
4925 *
4926 * The RFC does not explcitly address this issue, but is the route through the
4927 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4928 *
4929 * Outputs
4930 * (timers)
4931 */
4932sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004933 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 const struct sctp_endpoint *ep,
4935 const struct sctp_association *asoc,
4936 const sctp_subtype_t type,
4937 void *arg, sctp_cmd_seq_t *commands)
4938{
4939 /* There is a single T1 timer, so we should be able to use
4940 * common function with the COOKIE-WAIT state.
4941 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004942 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943}
4944
4945/*
4946 * sctp_sf_cookie_wait_prm_abort
4947 *
4948 * Section: 4 Note: 2
4949 * Verification Tag:
4950 * Inputs
4951 * (endpoint, asoc)
4952 *
4953 * The RFC does not explicitly address this issue, but is the route through the
4954 * state table when someone issues an abort while in COOKIE_WAIT state.
4955 *
4956 * Outputs
4957 * (timers)
4958 */
4959sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004960 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 const struct sctp_endpoint *ep,
4962 const struct sctp_association *asoc,
4963 const sctp_subtype_t type,
4964 void *arg,
4965 sctp_cmd_seq_t *commands)
4966{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004967 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 sctp_disposition_t retval;
4969
4970 /* Stop T1-init timer */
4971 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4972 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4973 retval = SCTP_DISPOSITION_CONSUME;
4974
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004975 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4978 SCTP_STATE(SCTP_STATE_CLOSED));
4979
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004980 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981
4982 /* Even if we can't send the ABORT due to low memory delete the
4983 * TCB. This is a departure from our typical NOMEM handling.
4984 */
4985
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004986 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4987 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 /* Delete the established association. */
4989 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004990 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991
4992 return retval;
4993}
4994
4995/*
4996 * sctp_sf_cookie_echoed_prm_abort
4997 *
4998 * Section: 4 Note: 3
4999 * Verification Tag:
5000 * Inputs
5001 * (endpoint, asoc)
5002 *
5003 * The RFC does not explcitly address this issue, but is the route through the
5004 * state table when someone issues an abort while in COOKIE_ECHOED state.
5005 *
5006 * Outputs
5007 * (timers)
5008 */
5009sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005010 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 const struct sctp_endpoint *ep,
5012 const struct sctp_association *asoc,
5013 const sctp_subtype_t type,
5014 void *arg,
5015 sctp_cmd_seq_t *commands)
5016{
5017 /* There is a single T1 timer, so we should be able to use
5018 * common function with the COOKIE-WAIT state.
5019 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005020 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021}
5022
5023/*
5024 * sctp_sf_shutdown_pending_prm_abort
5025 *
5026 * Inputs
5027 * (endpoint, asoc)
5028 *
5029 * The RFC does not explicitly address this issue, but is the route through the
5030 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5031 *
5032 * Outputs
5033 * (timers)
5034 */
5035sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005036 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 const struct sctp_endpoint *ep,
5038 const struct sctp_association *asoc,
5039 const sctp_subtype_t type,
5040 void *arg,
5041 sctp_cmd_seq_t *commands)
5042{
5043 /* Stop the T5-shutdown guard timer. */
5044 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5045 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5046
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005047 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
5050/*
5051 * sctp_sf_shutdown_sent_prm_abort
5052 *
5053 * Inputs
5054 * (endpoint, asoc)
5055 *
5056 * The RFC does not explicitly address this issue, but is the route through the
5057 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5058 *
5059 * Outputs
5060 * (timers)
5061 */
5062sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005063 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 const struct sctp_endpoint *ep,
5065 const struct sctp_association *asoc,
5066 const sctp_subtype_t type,
5067 void *arg,
5068 sctp_cmd_seq_t *commands)
5069{
5070 /* Stop the T2-shutdown timer. */
5071 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5072 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5073
5074 /* Stop the T5-shutdown guard timer. */
5075 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5076 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5077
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005078 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079}
5080
5081/*
5082 * sctp_sf_cookie_echoed_prm_abort
5083 *
5084 * Inputs
5085 * (endpoint, asoc)
5086 *
5087 * The RFC does not explcitly address this issue, but is the route through the
5088 * state table when someone issues an abort while in COOKIE_ECHOED state.
5089 *
5090 * Outputs
5091 * (timers)
5092 */
5093sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005094 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 const struct sctp_endpoint *ep,
5096 const struct sctp_association *asoc,
5097 const sctp_subtype_t type,
5098 void *arg,
5099 sctp_cmd_seq_t *commands)
5100{
5101 /* The same T2 timer, so we should be able to use
5102 * common function with the SHUTDOWN-SENT state.
5103 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005104 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105}
5106
5107/*
5108 * Process the REQUESTHEARTBEAT primitive
5109 *
5110 * 10.1 ULP-to-SCTP
5111 * J) Request Heartbeat
5112 *
5113 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5114 *
5115 * -> result
5116 *
5117 * Instructs the local endpoint to perform a HeartBeat on the specified
5118 * destination transport address of the given association. The returned
5119 * result should indicate whether the transmission of the HEARTBEAT
5120 * chunk to the destination address is successful.
5121 *
5122 * Mandatory attributes:
5123 *
5124 * o association id - local handle to the SCTP association
5125 *
5126 * o destination transport address - the transport address of the
5127 * association on which a heartbeat should be issued.
5128 */
5129sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005130 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 const struct sctp_endpoint *ep,
5132 const struct sctp_association *asoc,
5133 const sctp_subtype_t type,
5134 void *arg,
5135 sctp_cmd_seq_t *commands)
5136{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005137 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5138 (struct sctp_transport *)arg, commands))
5139 return SCTP_DISPOSITION_NOMEM;
5140
5141 /*
5142 * RFC 2960 (bis), section 8.3
5143 *
5144 * D) Request an on-demand HEARTBEAT on a specific destination
5145 * transport address of a given association.
5146 *
5147 * The endpoint should increment the respective error counter of
5148 * the destination transport address each time a HEARTBEAT is sent
5149 * to that address and not acknowledged within one RTO.
5150 *
5151 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005152 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005153 SCTP_TRANSPORT(arg));
5154 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155}
5156
5157/*
5158 * ADDIP Section 4.1 ASCONF Chunk Procedures
5159 * When an endpoint has an ASCONF signaled change to be sent to the
5160 * remote endpoint it should do A1 to A9
5161 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005162sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5163 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 const struct sctp_association *asoc,
5165 const sctp_subtype_t type,
5166 void *arg,
5167 sctp_cmd_seq_t *commands)
5168{
5169 struct sctp_chunk *chunk = arg;
5170
5171 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5172 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5173 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5174 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5175 return SCTP_DISPOSITION_CONSUME;
5176}
5177
5178/*
5179 * Ignore the primitive event
5180 *
5181 * The return value is the disposition of the primitive.
5182 */
5183sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005184 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 const struct sctp_endpoint *ep,
5186 const struct sctp_association *asoc,
5187 const sctp_subtype_t type,
5188 void *arg,
5189 sctp_cmd_seq_t *commands)
5190{
5191 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5192 return SCTP_DISPOSITION_DISCARD;
5193}
5194
5195/***************************************************************************
5196 * These are the state functions for the OTHER events.
5197 ***************************************************************************/
5198
5199/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005200 * When the SCTP stack has no more user data to send or retransmit, this
5201 * notification is given to the user. Also, at the time when a user app
5202 * subscribes to this event, if there is no data to be sent or
5203 * retransmit, the stack will immediately send up this notification.
5204 */
5205sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005206 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005207 const struct sctp_endpoint *ep,
5208 const struct sctp_association *asoc,
5209 const sctp_subtype_t type,
5210 void *arg,
5211 sctp_cmd_seq_t *commands)
5212{
5213 struct sctp_ulpevent *event;
5214
5215 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5216 if (!event)
5217 return SCTP_DISPOSITION_NOMEM;
5218
5219 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5220
5221 return SCTP_DISPOSITION_CONSUME;
5222}
5223
5224/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 * Start the shutdown negotiation.
5226 *
5227 * From Section 9.2:
5228 * Once all its outstanding data has been acknowledged, the endpoint
5229 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5230 * TSN Ack field the last sequential TSN it has received from the peer.
5231 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5232 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5233 * with the updated last sequential TSN received from its peer.
5234 *
5235 * The return value is the disposition.
5236 */
5237sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005238 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 const struct sctp_endpoint *ep,
5240 const struct sctp_association *asoc,
5241 const sctp_subtype_t type,
5242 void *arg,
5243 sctp_cmd_seq_t *commands)
5244{
5245 struct sctp_chunk *reply;
5246
5247 /* Once all its outstanding data has been acknowledged, the
5248 * endpoint shall send a SHUTDOWN chunk to its peer including
5249 * in the Cumulative TSN Ack field the last sequential TSN it
5250 * has received from the peer.
5251 */
5252 reply = sctp_make_shutdown(asoc, NULL);
5253 if (!reply)
5254 goto nomem;
5255
5256 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5257 * T2-shutdown timer.
5258 */
5259 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5260
5261 /* It shall then start the T2-shutdown timer */
5262 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5263 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5264
Wei Yongjun536428a2008-09-05 08:55:26 +08005265 /* RFC 4960 Section 9.2
5266 * The sender of the SHUTDOWN MAY also start an overall guard timer
5267 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5268 */
Thomas Graff8d96052011-07-07 00:28:35 +00005269 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005270 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5271
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 if (asoc->autoclose)
5273 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5274 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5275
5276 /* and enter the SHUTDOWN-SENT state. */
5277 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5278 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5279
5280 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5281 *
5282 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005283 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 */
5285 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5286
5287 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5288
5289 return SCTP_DISPOSITION_CONSUME;
5290
5291nomem:
5292 return SCTP_DISPOSITION_NOMEM;
5293}
5294
5295/*
5296 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5297 *
5298 * From Section 9.2:
5299 *
5300 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5301 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5302 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5303 * endpoint must re-send the SHUTDOWN ACK.
5304 *
5305 * The return value is the disposition.
5306 */
5307sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005308 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 const struct sctp_endpoint *ep,
5310 const struct sctp_association *asoc,
5311 const sctp_subtype_t type,
5312 void *arg,
5313 sctp_cmd_seq_t *commands)
5314{
5315 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5316 struct sctp_chunk *reply;
5317
5318 /* There are 2 ways of getting here:
5319 * 1) called in response to a SHUTDOWN chunk
5320 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5321 *
5322 * For the case (2), the arg parameter is set to NULL. We need
5323 * to check that we have a chunk before accessing it's fields.
5324 */
5325 if (chunk) {
5326 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005327 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328
5329 /* Make sure that the SHUTDOWN chunk has a valid length. */
5330 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005331 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 commands);
5333 }
5334
5335 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5336 * shall send a SHUTDOWN ACK ...
5337 */
5338 reply = sctp_make_shutdown_ack(asoc, chunk);
5339 if (!reply)
5340 goto nomem;
5341
5342 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5343 * the T2-shutdown timer.
5344 */
5345 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5346
5347 /* and start/restart a T2-shutdown timer of its own, */
5348 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5349 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5350
5351 if (asoc->autoclose)
5352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5353 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5354
5355 /* Enter the SHUTDOWN-ACK-SENT state. */
5356 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5357 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5358
5359 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5360 *
5361 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005362 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 */
5364 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5365
5366 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5367
5368 return SCTP_DISPOSITION_CONSUME;
5369
5370nomem:
5371 return SCTP_DISPOSITION_NOMEM;
5372}
5373
5374/*
5375 * Ignore the event defined as other
5376 *
5377 * The return value is the disposition of the event.
5378 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005379sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5380 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 const struct sctp_association *asoc,
5382 const sctp_subtype_t type,
5383 void *arg,
5384 sctp_cmd_seq_t *commands)
5385{
5386 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5387 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) {
Thomas Graff8d96052011-07-07 00:28:35 +00005417 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5418 /*
5419 * We are here likely because the receiver had its rwnd
5420 * closed for a while and we have not been able to
5421 * transmit the locally queued data within the maximum
5422 * retransmission attempts limit. Start the T5
5423 * shutdown guard timer to give the receiver one last
5424 * chance and some additional time to recover before
5425 * aborting.
5426 */
5427 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5428 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5429 } else {
5430 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5431 SCTP_ERROR(ETIMEDOUT));
5432 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5433 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5434 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005435 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5436 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005437 return SCTP_DISPOSITION_DELETE_TCB;
5438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 }
5440
5441 /* E1) For the destination address for which the timer
5442 * expires, adjust its ssthresh with rules defined in Section
5443 * 7.2.3 and set the cwnd <- MTU.
5444 */
5445
5446 /* E2) For the destination address for which the timer
5447 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5448 * maximum value discussed in rule C7 above (RTO.max) may be
5449 * used to provide an upper bound to this doubling operation.
5450 */
5451
5452 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5453 * outstanding DATA chunks for the address for which the
5454 * T3-rtx has expired will fit into a single packet, subject
5455 * to the MTU constraint for the path corresponding to the
5456 * destination transport address to which the retransmission
5457 * is being sent (this may be different from the address for
5458 * which the timer expires [see Section 6.4]). Call this
5459 * value K. Bundle and retransmit those K DATA chunks in a
5460 * single packet to the destination endpoint.
5461 *
5462 * Note: Any DATA chunks that were sent to the address for
5463 * which the T3-rtx timer expired but did not fit in one MTU
5464 * (rule E3 above), should be marked for retransmission and
5465 * sent as soon as cwnd allows (normally when a SACK arrives).
5466 */
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /* Do some failure management (Section 8.2). */
5469 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5470
Vlad Yasevich1845a572007-02-21 02:06:19 -08005471 /* NB: Rules E4 and F1 are implicit in R1. */
5472 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5473
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return SCTP_DISPOSITION_CONSUME;
5475}
5476
5477/*
5478 * Generate delayed SACK on timeout
5479 *
5480 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5481 *
5482 * The guidelines on delayed acknowledgement algorithm specified in
5483 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5484 * acknowledgement SHOULD be generated for at least every second packet
5485 * (not every second DATA chunk) received, and SHOULD be generated
5486 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5487 * some situations it may be beneficial for an SCTP transmitter to be
5488 * more conservative than the algorithms detailed in this document
5489 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5490 * the following algorithms allow.
5491 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005492sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5493 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 const struct sctp_association *asoc,
5495 const sctp_subtype_t type,
5496 void *arg,
5497 sctp_cmd_seq_t *commands)
5498{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005499 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5501 return SCTP_DISPOSITION_CONSUME;
5502}
5503
5504/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005505 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 *
5507 * Section: 4 Note: 2
5508 * Verification Tag:
5509 * Inputs
5510 * (endpoint, asoc)
5511 *
5512 * RFC 2960 Section 4 Notes
5513 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5514 * and re-start the T1-init timer without changing state. This MUST
5515 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5516 * endpoint MUST abort the initialization process and report the
5517 * error to SCTP user.
5518 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005519 * Outputs
5520 * (timers, events)
5521 *
5522 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005523sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5524 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005525 const struct sctp_association *asoc,
5526 const sctp_subtype_t type,
5527 void *arg,
5528 sctp_cmd_seq_t *commands)
5529{
5530 struct sctp_chunk *repl = NULL;
5531 struct sctp_bind_addr *bp;
5532 int attempts = asoc->init_err_counter + 1;
5533
5534 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005535 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005536
Vlad Yasevich81845c212006-01-30 15:59:54 -08005537 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005538 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5539 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5540 if (!repl)
5541 return SCTP_DISPOSITION_NOMEM;
5542
5543 /* Choose transport for INIT. */
5544 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5545 SCTP_CHUNK(repl));
5546
5547 /* Issue a sideeffect to do the needed accounting. */
5548 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5549 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5550
5551 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5552 } else {
5553 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5554 " max_init_attempts: %d\n",
5555 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005556 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5557 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005558 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005559 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005560 return SCTP_DISPOSITION_DELETE_TCB;
5561 }
5562
5563 return SCTP_DISPOSITION_CONSUME;
5564}
5565
5566/*
5567 * sctp_sf_t1_cookie_timer_expire
5568 *
5569 * Section: 4 Note: 2
5570 * Verification Tag:
5571 * Inputs
5572 * (endpoint, asoc)
5573 *
5574 * RFC 2960 Section 4 Notes
5575 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 * COOKIE ECHO and re-start the T1-cookie timer without changing
5577 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5578 * After that, the endpoint MUST abort the initialization process and
5579 * report the error to SCTP user.
5580 *
5581 * Outputs
5582 * (timers, events)
5583 *
5584 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005585sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5586 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 const struct sctp_association *asoc,
5588 const sctp_subtype_t type,
5589 void *arg,
5590 sctp_cmd_seq_t *commands)
5591{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005592 struct sctp_chunk *repl = NULL;
5593 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594
Frank Filz3f7a87d2005-06-20 13:14:57 -07005595 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005596 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Vlad Yasevich81845c212006-01-30 15:59:54 -08005598 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005599 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005601 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005603 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5604 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005606 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5607 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5610 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005611 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5612 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005614 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 return SCTP_DISPOSITION_DELETE_TCB;
5616 }
5617
5618 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
5621/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5622 * with the updated last sequential TSN received from its peer.
5623 *
5624 * An endpoint should limit the number of retransmissions of the
5625 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5626 * If this threshold is exceeded the endpoint should destroy the TCB and
5627 * MUST report the peer endpoint unreachable to the upper layer (and
5628 * thus the association enters the CLOSED state). The reception of any
5629 * packet from its peer (i.e. as the peer sends all of its queued DATA
5630 * chunks) should clear the endpoint's retransmission count and restart
5631 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5632 * all of its queued DATA chunks that have not yet been sent.
5633 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005634sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5635 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 const struct sctp_association *asoc,
5637 const sctp_subtype_t type,
5638 void *arg,
5639 sctp_cmd_seq_t *commands)
5640{
5641 struct sctp_chunk *reply = NULL;
5642
5643 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005644 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005645
Neil Horman58fbbed2008-02-29 11:40:56 -08005646 ((struct sctp_association *)asoc)->shutdown_retries++;
5647
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005649 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5650 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5652 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005653 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005654 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5655 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 return SCTP_DISPOSITION_DELETE_TCB;
5657 }
5658
5659 switch (asoc->state) {
5660 case SCTP_STATE_SHUTDOWN_SENT:
5661 reply = sctp_make_shutdown(asoc, NULL);
5662 break;
5663
5664 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5665 reply = sctp_make_shutdown_ack(asoc, NULL);
5666 break;
5667
5668 default:
5669 BUG();
5670 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673 if (!reply)
5674 goto nomem;
5675
Wei Yongjun6345b192009-04-26 23:13:35 +08005676 /* Do some failure management (Section 8.2).
5677 * If we remove the transport an SHUTDOWN was last sent to, don't
5678 * do failure management.
5679 */
5680 if (asoc->shutdown_last_sent_to)
5681 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5682 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
5684 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5685 * the T2-shutdown timer.
5686 */
5687 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5688
5689 /* Restart the T2-shutdown timer. */
5690 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5691 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5692 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5693 return SCTP_DISPOSITION_CONSUME;
5694
5695nomem:
5696 return SCTP_DISPOSITION_NOMEM;
5697}
5698
5699/*
5700 * ADDIP Section 4.1 ASCONF CHunk Procedures
5701 * If the T4 RTO timer expires the endpoint should do B1 to B5
5702 */
5703sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005704 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 const struct sctp_endpoint *ep,
5706 const struct sctp_association *asoc,
5707 const sctp_subtype_t type,
5708 void *arg,
5709 sctp_cmd_seq_t *commands)
5710{
5711 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5712 struct sctp_transport *transport = chunk->transport;
5713
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005714 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005715
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5717 * detection on the appropriate destination address as defined in
5718 * RFC2960 [5] section 8.1 and 8.2.
5719 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005720 if (transport)
5721 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5722 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
5724 /* Reconfig T4 timer and transport. */
5725 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5726
5727 /* ADDIP 4.1 B2) Increment the association error counters and perform
5728 * endpoint failure detection on the association as defined in
5729 * RFC2960 [5] section 8.1 and 8.2.
5730 * association error counter is incremented in SCTP_CMD_STRIKE.
5731 */
5732 if (asoc->overall_error_count >= asoc->max_retrans) {
5733 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5734 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005735 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5736 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005738 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005739 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5740 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 return SCTP_DISPOSITION_ABORT;
5742 }
5743
5744 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5745 * the ASCONF chunk was sent by doubling the RTO timer value.
5746 * This is done in SCTP_CMD_STRIKE.
5747 */
5748
5749 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5750 * choose an alternate destination address (please refer to RFC2960
5751 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005752 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 * ASCONF sent.
5754 */
5755 sctp_chunk_hold(asoc->addip_last_asconf);
5756 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5757 SCTP_CHUNK(asoc->addip_last_asconf));
5758
5759 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5760 * destination is selected, then the RTO used will be that of the new
5761 * destination address.
5762 */
5763 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5764 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5765
5766 return SCTP_DISPOSITION_CONSUME;
5767}
5768
5769/* sctpimpguide-05 Section 2.12.2
5770 * The sender of the SHUTDOWN MAY also start an overall guard timer
5771 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5772 * At the expiration of this timer the sender SHOULD abort the association
5773 * by sending an ABORT chunk.
5774 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005775sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5776 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 const struct sctp_association *asoc,
5778 const sctp_subtype_t type,
5779 void *arg,
5780 sctp_cmd_seq_t *commands)
5781{
5782 struct sctp_chunk *reply = NULL;
5783
5784 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005785 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786
5787 reply = sctp_make_abort(asoc, NULL, 0);
5788 if (!reply)
5789 goto nomem;
5790
5791 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005792 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5793 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005795 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005797 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5798 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 return SCTP_DISPOSITION_DELETE_TCB;
5801nomem:
5802 return SCTP_DISPOSITION_NOMEM;
5803}
5804
5805/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5806 * the association is automatically closed by starting the shutdown process.
5807 * The work that needs to be done is same as when SHUTDOWN is initiated by
5808 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5809 */
5810sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005811 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 const struct sctp_endpoint *ep,
5813 const struct sctp_association *asoc,
5814 const sctp_subtype_t type,
5815 void *arg,
5816 sctp_cmd_seq_t *commands)
5817{
5818 int disposition;
5819
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005820 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 /* From 9.2 Shutdown of an Association
5823 * Upon receipt of the SHUTDOWN primitive from its upper
5824 * layer, the endpoint enters SHUTDOWN-PENDING state and
5825 * remains there until all outstanding data has been
5826 * acknowledged by its peer. The endpoint accepts no new data
5827 * from its upper layer, but retransmits data to the far end
5828 * if necessary to fill gaps.
5829 */
5830 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5831 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 disposition = SCTP_DISPOSITION_CONSUME;
5834 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005835 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 arg, commands);
5837 }
5838 return disposition;
5839}
5840
5841/*****************************************************************************
5842 * These are sa state functions which could apply to all types of events.
5843 ****************************************************************************/
5844
5845/*
5846 * This table entry is not implemented.
5847 *
5848 * Inputs
5849 * (endpoint, asoc, chunk)
5850 *
5851 * The return value is the disposition of the chunk.
5852 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005853sctp_disposition_t sctp_sf_not_impl(struct net *net,
5854 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 const struct sctp_association *asoc,
5856 const sctp_subtype_t type,
5857 void *arg,
5858 sctp_cmd_seq_t *commands)
5859{
5860 return SCTP_DISPOSITION_NOT_IMPL;
5861}
5862
5863/*
5864 * This table entry represents a bug.
5865 *
5866 * Inputs
5867 * (endpoint, asoc, chunk)
5868 *
5869 * The return value is the disposition of the chunk.
5870 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005871sctp_disposition_t sctp_sf_bug(struct net *net,
5872 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 const struct sctp_association *asoc,
5874 const sctp_subtype_t type,
5875 void *arg,
5876 sctp_cmd_seq_t *commands)
5877{
5878 return SCTP_DISPOSITION_BUG;
5879}
5880
5881/*
5882 * This table entry represents the firing of a timer in the wrong state.
5883 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5884 * when the association is in the wrong state. This event should
5885 * be ignored, so as to prevent any rearming of the timer.
5886 *
5887 * Inputs
5888 * (endpoint, asoc, chunk)
5889 *
5890 * The return value is the disposition of the chunk.
5891 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005892sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5893 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 const struct sctp_association *asoc,
5895 const sctp_subtype_t type,
5896 void *arg,
5897 sctp_cmd_seq_t *commands)
5898{
5899 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5900 return SCTP_DISPOSITION_CONSUME;
5901}
5902
5903/********************************************************************
5904 * 2nd Level Abstractions
5905 ********************************************************************/
5906
5907/* Pull the SACK chunk based on the SACK header. */
5908static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5909{
5910 struct sctp_sackhdr *sack;
5911 unsigned int len;
5912 __u16 num_blocks;
5913 __u16 num_dup_tsns;
5914
5915 /* Protect ourselves from reading too far into
5916 * the skb from a bogus sender.
5917 */
5918 sack = (struct sctp_sackhdr *) chunk->skb->data;
5919
5920 num_blocks = ntohs(sack->num_gap_ack_blocks);
5921 num_dup_tsns = ntohs(sack->num_dup_tsns);
5922 len = sizeof(struct sctp_sackhdr);
5923 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5924 if (len > chunk->skb->len)
5925 return NULL;
5926
5927 skb_pull(chunk->skb, len);
5928
5929 return sack;
5930}
5931
5932/* Create an ABORT packet to be sent as a response, with the specified
5933 * error causes.
5934 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005935static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5936 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 const struct sctp_association *asoc,
5938 struct sctp_chunk *chunk,
5939 const void *payload,
5940 size_t paylen)
5941{
5942 struct sctp_packet *packet;
5943 struct sctp_chunk *abort;
5944
Eric W. Biederman2ce955032012-08-06 08:43:06 +00005945 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
5947 if (packet) {
5948 /* Make an ABORT.
5949 * The T bit will be set if the asoc is NULL.
5950 */
5951 abort = sctp_make_abort(asoc, chunk, paylen);
5952 if (!abort) {
5953 sctp_ootb_pkt_free(packet);
5954 return NULL;
5955 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005956
5957 /* Reflect vtag if T-Bit is set */
5958 if (sctp_test_T_bit(abort))
5959 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 /* Add specified error causes, i.e., payload, to the
5962 * end of the chunk.
5963 */
5964 sctp_addto_chunk(abort, paylen, payload);
5965
5966 /* Set the skb to the belonging sock for accounting. */
5967 abort->skb->sk = ep->base.sk;
5968
5969 sctp_packet_append_chunk(packet, abort);
5970
5971 }
5972
5973 return packet;
5974}
5975
5976/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00005977static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5978 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 const struct sctp_chunk *chunk)
5980{
5981 struct sctp_packet *packet;
5982 struct sctp_transport *transport;
5983 __u16 sport;
5984 __u16 dport;
5985 __u32 vtag;
5986
5987 /* Get the source and destination port from the inbound packet. */
5988 sport = ntohs(chunk->sctp_hdr->dest);
5989 dport = ntohs(chunk->sctp_hdr->source);
5990
5991 /* The V-tag is going to be the same as the inbound packet if no
5992 * association exists, otherwise, use the peer's vtag.
5993 */
5994 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005995 /* Special case the INIT-ACK as there is no peer's vtag
5996 * yet.
5997 */
5998 switch(chunk->chunk_hdr->type) {
5999 case SCTP_CID_INIT_ACK:
6000 {
6001 sctp_initack_chunk_t *initack;
6002
6003 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6004 vtag = ntohl(initack->init_hdr.init_tag);
6005 break;
6006 }
6007 default:
6008 vtag = asoc->peer.i.init_tag;
6009 break;
6010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 } else {
6012 /* Special case the INIT and stale COOKIE_ECHO as there is no
6013 * vtag yet.
6014 */
6015 switch(chunk->chunk_hdr->type) {
6016 case SCTP_CID_INIT:
6017 {
6018 sctp_init_chunk_t *init;
6019
6020 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6021 vtag = ntohl(init->init_hdr.init_tag);
6022 break;
6023 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006024 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 vtag = ntohl(chunk->sctp_hdr->vtag);
6026 break;
6027 }
6028 }
6029
6030 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006031 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 if (!transport)
6033 goto nomem;
6034
6035 /* Cache a route for the transport with the chunk's destination as
6036 * the source address.
6037 */
Al Viro16b0a032006-11-20 17:13:38 -08006038 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce955032012-08-06 08:43:06 +00006039 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
6041 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6042 packet = sctp_packet_config(packet, vtag, 0);
6043
6044 return packet;
6045
6046nomem:
6047 return NULL;
6048}
6049
6050/* Free the packet allocated earlier for responding in the OOTB condition. */
6051void sctp_ootb_pkt_free(struct sctp_packet *packet)
6052{
6053 sctp_transport_free(packet->transport);
6054}
6055
6056/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006057static void sctp_send_stale_cookie_err(struct net *net,
6058 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 const struct sctp_association *asoc,
6060 const struct sctp_chunk *chunk,
6061 sctp_cmd_seq_t *commands,
6062 struct sctp_chunk *err_chunk)
6063{
6064 struct sctp_packet *packet;
6065
6066 if (err_chunk) {
Eric W. Biederman2ce955032012-08-06 08:43:06 +00006067 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 if (packet) {
6069 struct sctp_signed_cookie *cookie;
6070
6071 /* Override the OOTB vtag from the cookie. */
6072 cookie = chunk->subh.cookie_hdr;
6073 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 /* Set the skb to the belonging sock for accounting. */
6076 err_chunk->skb->sk = ep->base.sk;
6077 sctp_packet_append_chunk(packet, err_chunk);
6078 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6079 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006080 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 } else
6082 sctp_chunk_free (err_chunk);
6083 }
6084}
6085
6086
6087/* Process a data chunk */
6088static int sctp_eat_data(const struct sctp_association *asoc,
6089 struct sctp_chunk *chunk,
6090 sctp_cmd_seq_t *commands)
6091{
6092 sctp_datahdr_t *data_hdr;
6093 struct sctp_chunk *err;
6094 size_t datalen;
6095 sctp_verb_t deliver;
6096 int tmp;
6097 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006098 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006099 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006100 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006101 u16 ssn;
6102 u16 sid;
6103 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104
6105 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6106 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6107
6108 tsn = ntohl(data_hdr->tsn);
6109 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
6110
6111 /* ASSERT: Now skb->data is really the user data. */
6112
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 /* Process ECN based congestion.
6114 *
6115 * Since the chunk structure is reused for all chunks within
6116 * a packet, we use ecn_ce_done to track if we've already
6117 * done CE processing for this packet.
6118 *
6119 * We need to do ECN processing even if we plan to discard the
6120 * chunk later.
6121 */
6122
6123 if (!chunk->ecn_ce_done) {
6124 struct sctp_af *af;
6125 chunk->ecn_ce_done = 1;
6126
6127 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006128 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
6130 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6131 /* Do real work as sideffect. */
6132 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6133 SCTP_U32(tsn));
6134 }
6135 }
6136
6137 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6138 if (tmp < 0) {
6139 /* The TSN is too high--silently discard the chunk and
6140 * count on it getting retransmitted later.
6141 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006142 if (chunk->asoc)
6143 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 return SCTP_IERROR_HIGH_TSN;
6145 } else if (tmp > 0) {
6146 /* This is a duplicate. Record it. */
6147 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6148 return SCTP_IERROR_DUP_TSN;
6149 }
6150
6151 /* This is a new TSN. */
6152
6153 /* Discard if there is no room in the receive window.
6154 * Actually, allow a little bit of overflow (up to a MTU).
6155 */
6156 datalen = ntohs(chunk->chunk_hdr->length);
6157 datalen -= sizeof(sctp_data_chunk_t);
6158
6159 deliver = SCTP_CMD_CHUNK_ULP;
6160
6161 /* Think about partial delivery. */
6162 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6163
6164 /* Even if we don't accept this chunk there is
6165 * memory pressure.
6166 */
6167 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6168 }
6169
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006170 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 * seems a bit troublesome in that frag_point varies based on
6172 * PMTU. In cases, such as loopback, this might be a rather
6173 * large spill over.
6174 */
Neil Horman4d93df02007-08-15 16:07:44 -07006175 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6176 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
6178 /* If this is the next TSN, consider reneging to make
6179 * room. Note: Playing nice with a confused sender. A
6180 * malicious sender can still eat up all our buffer
6181 * space and in the future we may want to detect and
6182 * do more drastic reneging.
6183 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006184 if (sctp_tsnmap_has_gap(map) &&
6185 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6187 deliver = SCTP_CMD_RENEGE;
6188 } else {
6189 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6190 "rwnd: %d\n", tsn, datalen,
6191 asoc->rwnd);
6192 return SCTP_IERROR_IGNORE_TSN;
6193 }
6194 }
6195
6196 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006197 * Also try to renege to limit our memory usage in the event that
6198 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006199 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006200 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6201 * memory usage too much
6202 */
6203 if (*sk->sk_prot_creator->memory_pressure) {
6204 if (sctp_tsnmap_has_gap(map) &&
6205 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6206 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6207 deliver = SCTP_CMD_RENEGE;
6208 }
6209 }
6210
6211 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 * Section 3.3.10.9 No User Data (9)
6213 *
6214 * Cause of error
6215 * ---------------
6216 * No User Data: This error cause is returned to the originator of a
6217 * DATA chunk if a received DATA chunk has no user data.
6218 */
6219 if (unlikely(0 == datalen)) {
6220 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6221 if (err) {
6222 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6223 SCTP_CHUNK(err));
6224 }
6225 /* We are going to ABORT, so we might as well stop
6226 * processing the rest of the chunks in the packet.
6227 */
6228 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006229 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6230 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006232 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006233 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6234 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 return SCTP_IERROR_NO_DATA;
6236 }
6237
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006238 chunk->data_accepted = 1;
6239
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6241 * if we renege and the chunk arrives again.
6242 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006243 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006244 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006245 if (chunk->asoc)
6246 chunk->asoc->stats.iuodchunks++;
6247 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006248 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006249 if (chunk->asoc)
6250 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006251 ordered = 1;
6252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
6254 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6255 *
6256 * If an endpoint receive a DATA chunk with an invalid stream
6257 * identifier, it shall acknowledge the reception of the DATA chunk
6258 * following the normal procedure, immediately send an ERROR chunk
6259 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6260 * and discard the DATA chunk.
6261 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006262 sid = ntohs(data_hdr->stream);
6263 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006264 /* Mark tsn as received even though we drop it */
6265 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6266
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6268 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006269 sizeof(data_hdr->stream),
6270 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 if (err)
6272 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6273 SCTP_CHUNK(err));
6274 return SCTP_IERROR_BAD_STREAM;
6275 }
6276
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006277 /* Check to see if the SSN is possible for this TSN.
6278 * The biggest gap we can record is 4K wide. Since SSNs wrap
6279 * at an unsigned short, there is no way that an SSN can
6280 * wrap and for a valid TSN. We can simply check if the current
6281 * SSN is smaller then the next expected one. If it is, it wrapped
6282 * and is invalid.
6283 */
6284 ssn = ntohs(data_hdr->ssn);
6285 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6286 return SCTP_IERROR_PROTO_VIOLATION;
6287 }
6288
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 /* Send the data up to the user. Note: Schedule the
6290 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6291 * chunk needs the updated rwnd.
6292 */
6293 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6294
6295 return SCTP_IERROR_NO_ERROR;
6296}