blob: ee02771d8b9c3da7205d160b89897b33a9479e54 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
Jeff Kirsher4b2f13a2013-12-06 06:28:48 -080025 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
Daniel Borkmann91705c62013-07-23 14:51:47 +020030 * lksctp developers <linux-sctp@vger.kernel.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
45
Joe Perches145ce502010-08-24 13:21:08 +000046#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/types.h>
49#include <linux/kernel.h>
50#include <linux/ip.h>
51#include <linux/ipv6.h>
52#include <linux/net.h>
53#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/inet_ecn.h>
57#include <linux/skbuff.h>
58#include <net/sctp/sctp.h>
59#include <net/sctp/sm.h>
60#include <net/sctp/structs.h>
61
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000062static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
63 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 const struct sctp_association *asoc,
65 struct sctp_chunk *chunk,
66 const void *payload,
67 size_t paylen);
68static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 sctp_cmd_seq_t *commands);
Eric W. Biederman2ce95502012-08-06 08:43:06 +000071static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
72 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000074static void sctp_send_stale_cookie_err(struct net *net,
75 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk,
78 sctp_cmd_seq_t *commands,
79 struct sctp_chunk *err_chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000080static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
81 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000086static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
87 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 const struct sctp_association *asoc,
89 const sctp_subtype_t type,
90 void *arg,
91 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000092static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
93 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -040094 const struct sctp_association *asoc,
95 const sctp_subtype_t type,
96 void *arg,
97 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
99
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000100static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
101 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800102 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
105
Wei Yongjunaecedea2007-08-02 16:57:44 +0800106static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000107 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400108 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800109 const struct sctp_association *asoc,
110 void *arg,
111 sctp_cmd_seq_t *commands,
112 const __u8 *payload,
113 const size_t paylen);
114
Adrian Bunk52c1da32005-06-23 22:05:33 -0700115static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000116 struct net *net,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const sctp_subtype_t type,
120 void *arg,
121 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000124 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700128 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800129 sctp_cmd_seq_t *commands);
130
Wei Yongjunaecedea2007-08-02 16:57:44 +0800131static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000132 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichece25df2007-09-07 16:30:54 -0400139static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000140 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const sctp_subtype_t type,
144 void *arg,
145 sctp_cmd_seq_t *commands);
146
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000147static sctp_ierror_t sctp_sf_authenticate(struct net *net,
148 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 struct sctp_chunk *chunk);
152
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000153static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
154 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800155 const struct sctp_association *asoc,
156 const sctp_subtype_t type,
157 void *arg,
158 sctp_cmd_seq_t *commands);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* Small helper function that checks if the chunk length
161 * is of the appropriate length. The 'required_length' argument
162 * is set to be the size of a specific chunk we are testing.
163 * Return Values: 1 = Valid length
164 * 0 = Invalid length
165 *
166 */
167static inline int
168sctp_chunk_length_valid(struct sctp_chunk *chunk,
169 __u16 required_length)
170{
171 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
172
173 if (unlikely(chunk_length < required_length))
174 return 0;
175
176 return 1;
177}
178
179/**********************************************************
180 * These are the state functions for handling chunk events.
181 **********************************************************/
182
183/*
184 * Process the final SHUTDOWN COMPLETE.
185 *
186 * Section: 4 (C) (diagram), 9.2
187 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
188 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
189 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
190 * should stop the T2-shutdown timer and remove all knowledge of the
191 * association (and thus the association enters the CLOSED state).
192 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700193 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * C) Rules for packet carrying SHUTDOWN COMPLETE:
195 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700196 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
197 * if the Verification Tag field of the packet matches its own tag and
198 * the T bit is not set
199 * OR
200 * it is set to its peer's tag and the T bit is set in the Chunk
201 * Flags.
202 * Otherwise, the receiver MUST silently discard the packet
203 * and take no further action. An endpoint MUST ignore the
204 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 *
206 * Inputs
207 * (endpoint, asoc, chunk)
208 *
209 * Outputs
210 * (asoc, reply_msg, msg_up, timers, counters)
211 *
212 * The return value is the disposition of the chunk.
213 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000214sctp_disposition_t sctp_sf_do_4_C(struct net *net,
215 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 const struct sctp_association *asoc,
217 const sctp_subtype_t type,
218 void *arg,
219 sctp_cmd_seq_t *commands)
220{
221 struct sctp_chunk *chunk = arg;
222 struct sctp_ulpevent *ev;
223
Vlad Yasevichece25df2007-09-07 16:30:54 -0400224 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000225 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* RFC 2960 6.10 Bundling
228 *
229 * An endpoint MUST NOT bundle INIT, INIT ACK or
230 * SHUTDOWN COMPLETE with any other chunks.
231 */
232 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000233 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Vlad Yasevichece25df2007-09-07 16:30:54 -0400235 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
236 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000237 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400238 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /* RFC 2960 10.2 SCTP-to-ULP
241 *
242 * H) SHUTDOWN COMPLETE notification
243 *
244 * When SCTP completes the shutdown procedures (section 9.2) this
245 * notification is passed to the upper layer.
246 */
247 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700248 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700249 if (ev)
250 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900251 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
254 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
255 * not the chunk should be discarded. If the endpoint is in
256 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
257 * T2-shutdown timer and remove all knowledge of the
258 * association (and thus the association enters the CLOSED
259 * state).
260 */
261 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
262 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
263
264 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
265 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
266
267 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
268 SCTP_STATE(SCTP_STATE_CLOSED));
269
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000270 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
271 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
274
275 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/*
279 * Respond to a normal INIT chunk.
280 * We are the side that is being asked for an association.
281 *
282 * Section: 5.1 Normal Establishment of an Association, B
283 * B) "Z" shall respond immediately with an INIT ACK chunk. The
284 * destination IP address of the INIT ACK MUST be set to the source
285 * IP address of the INIT to which this INIT ACK is responding. In
286 * the response, besides filling in other parameters, "Z" must set the
287 * Verification Tag field to Tag_A, and also provide its own
288 * Verification Tag (Tag_Z) in the Initiate Tag field.
289 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900290 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
292 * Inputs
293 * (endpoint, asoc, chunk)
294 *
295 * Outputs
296 * (asoc, reply_msg, msg_up, timers, counters)
297 *
298 * The return value is the disposition of the chunk.
299 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000300sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
301 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 const struct sctp_association *asoc,
303 const sctp_subtype_t type,
304 void *arg,
305 sctp_cmd_seq_t *commands)
306{
307 struct sctp_chunk *chunk = arg;
308 struct sctp_chunk *repl;
309 struct sctp_association *new_asoc;
310 struct sctp_chunk *err_chunk;
311 struct sctp_packet *packet;
312 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 int len;
314
315 /* 6.10 Bundling
316 * An endpoint MUST NOT bundle INIT, INIT ACK or
317 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900318 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * IG Section 2.11.2
320 * Furthermore, we require that the receiver of an INIT chunk MUST
321 * enforce these rules by silently discarding an arriving packet
322 * with an INIT chunk that is bundled with other chunks.
323 */
324 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000325 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* If the packet is an OOTB packet which is temporarily on the
328 * control endpoint, respond with an ABORT.
329 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000330 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000331 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000332 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900336 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
338 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000339 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Make sure that the INIT chunk has a valid length.
342 * Normally, this would cause an ABORT with a Protocol Violation
343 * error, but since we don't have an association, we'll
344 * just discard the packet.
345 */
346 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000347 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Vlad Yasevichbec96402009-07-30 18:08:28 -0400349 /* If the INIT is coming toward a closing socket, we'll send back
350 * and ABORT. Essentially, this catches the race of INIT being
351 * backloged to the socket at the same time as the user isses close().
352 * Since the socket and all its associations are going away, we
353 * can treat this OOTB
354 */
355 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000356 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 /* Verify the INIT chunk before processing it. */
359 err_chunk = NULL;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +0000360 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
362 &err_chunk)) {
363 /* This chunk contains fatal error. It is to be discarded.
364 * Send an ABORT, with causes if there is any.
365 */
366 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000367 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (__u8 *)(err_chunk->chunk_hdr) +
369 sizeof(sctp_chunkhdr_t),
370 ntohs(err_chunk->chunk_hdr->length) -
371 sizeof(sctp_chunkhdr_t));
372
373 sctp_chunk_free(err_chunk);
374
375 if (packet) {
376 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
377 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000378 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return SCTP_DISPOSITION_CONSUME;
380 } else {
381 return SCTP_DISPOSITION_NOMEM;
382 }
383 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000384 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 commands);
386 }
387 }
388
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900389 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
391
392 /* Tag the variable length parameters. */
393 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
394
395 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
396 if (!new_asoc)
397 goto nomem;
398
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000399 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
400 sctp_scope(sctp_source(chunk)),
401 GFP_ATOMIC) < 0)
402 goto nomem_init;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000405 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 (sctp_init_chunk_t *)chunk->chunk_hdr,
407 GFP_ATOMIC))
408 goto nomem_init;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
411
412 /* If there are errors need to be reported for unknown parameters,
413 * make sure to reserve enough room in the INIT ACK for them.
414 */
415 len = 0;
416 if (err_chunk)
417 len = ntohs(err_chunk->chunk_hdr->length) -
418 sizeof(sctp_chunkhdr_t);
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
421 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700422 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 /* If there are errors need to be reported for unknown parameters,
425 * include them in the outgoing INIT ACK as "Unrecognized parameter"
426 * parameter.
427 */
428 if (err_chunk) {
429 /* Get the "Unrecognized parameter" parameter(s) out of the
430 * ERROR chunk generated by sctp_verify_init(). Since the
431 * error cause code for "unknown parameter" and the
432 * "Unrecognized parameter" type is the same, we can
433 * construct the parameters in INIT ACK by copying the
434 * ERROR causes over.
435 */
436 unk_param = (sctp_unrecognized_param_t *)
437 ((__u8 *)(err_chunk->chunk_hdr) +
438 sizeof(sctp_chunkhdr_t));
439 /* Replace the cause code with the "Unrecognized parameter"
440 * parameter type.
441 */
442 sctp_addto_chunk(repl, len, unk_param);
443 sctp_chunk_free(err_chunk);
444 }
445
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700446 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
449
450 /*
451 * Note: After sending out INIT ACK with the State Cookie parameter,
452 * "Z" MUST NOT allocate any resources, nor keep any states for the
453 * new association. Otherwise, "Z" will be vulnerable to resource
454 * attacks.
455 */
456 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
457
458 return SCTP_DISPOSITION_DELETE_TCB;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460nomem_init:
461 sctp_association_free(new_asoc);
462nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700463 if (err_chunk)
464 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return SCTP_DISPOSITION_NOMEM;
466}
467
468/*
469 * Respond to a normal INIT ACK chunk.
470 * We are the side that is initiating the association.
471 *
472 * Section: 5.1 Normal Establishment of an Association, C
473 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
474 * timer and leave COOKIE-WAIT state. "A" shall then send the State
475 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
476 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
477 *
478 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
479 * DATA chunks, but it MUST be the first chunk in the packet and
480 * until the COOKIE ACK is returned the sender MUST NOT send any
481 * other packets to the peer.
482 *
483 * Verification Tag: 3.3.3
484 * If the value of the Initiate Tag in a received INIT ACK chunk is
485 * found to be 0, the receiver MUST treat it as an error and close the
486 * association by transmitting an ABORT.
487 *
488 * Inputs
489 * (endpoint, asoc, chunk)
490 *
491 * Outputs
492 * (asoc, reply_msg, msg_up, timers, counters)
493 *
494 * The return value is the disposition of the chunk.
495 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000496sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
497 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 const struct sctp_association *asoc,
499 const sctp_subtype_t type,
500 void *arg,
501 sctp_cmd_seq_t *commands)
502{
503 struct sctp_chunk *chunk = arg;
504 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct sctp_chunk *err_chunk;
506 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000509 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* 6.10 Bundling
512 * An endpoint MUST NOT bundle INIT, INIT ACK or
513 * SHUTDOWN COMPLETE with any other chunks.
514 */
515 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000516 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Vlad Yasevichece25df2007-09-07 16:30:54 -0400518 /* Make sure that the INIT-ACK chunk has a valid length */
519 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000520 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400521 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Grab the INIT header. */
523 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 /* Verify the INIT chunk before processing it. */
526 err_chunk = NULL;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +0000527 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
529 &err_chunk)) {
530
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800531 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800534 * Send an ABORT, with causes. If there are no causes,
535 * then there wasn't enough memory. Just terminate
536 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 */
538 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000539 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 (__u8 *)(err_chunk->chunk_hdr) +
541 sizeof(sctp_chunkhdr_t),
542 ntohs(err_chunk->chunk_hdr->length) -
543 sizeof(sctp_chunkhdr_t));
544
545 sctp_chunk_free(err_chunk);
546
547 if (packet) {
548 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
549 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000550 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700551 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700554
555 /* SCTP-AUTH, Section 6.3:
556 * It should be noted that if the receiver wants to tear
557 * down an association in an authenticated way only, the
558 * handling of malformed packets should not result in
559 * tearing down the association.
560 *
561 * This means that if we only want to abort associations
562 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300563 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700564 * was malformed.
565 */
566 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000567 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700568
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000569 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
570 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700571 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
573
574 /* Tag the variable length parameters. Note that we never
575 * convert the parameters in an INIT chunk.
576 */
577 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
578
579 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
580
581 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
582 SCTP_PEER_INIT(initchunk));
583
Frank Filz3f7a87d2005-06-20 13:14:57 -0700584 /* Reset init error count upon receipt of INIT-ACK. */
585 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* 5.1 C) "A" shall stop the T1-init timer and leave
588 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
589 * timer, and enter the COOKIE-ECHOED state.
590 */
591 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
592 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
593 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
594 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
595 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
596 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
597
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700598 /* SCTP-AUTH: genereate the assocition shared keys so that
599 * we can potentially signe the COOKIE-ECHO.
600 */
601 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 /* 5.1 C) "A" shall then send the State Cookie received in the
604 * INIT ACK chunk in a COOKIE ECHO chunk, ...
605 */
606 /* If there is any errors to report, send the ERROR chunk generated
607 * for unknown parameters as well.
608 */
609 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
610 SCTP_CHUNK(err_chunk));
611
612 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/*
616 * Respond to a normal COOKIE ECHO chunk.
617 * We are the side that is being asked for an association.
618 *
619 * Section: 5.1 Normal Establishment of an Association, D
620 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
621 * with a COOKIE ACK chunk after building a TCB and moving to
622 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
623 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
624 * chunk MUST be the first chunk in the packet.
625 *
626 * IMPLEMENTATION NOTE: An implementation may choose to send the
627 * Communication Up notification to the SCTP user upon reception
628 * of a valid COOKIE ECHO chunk.
629 *
630 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
631 * D) Rules for packet carrying a COOKIE ECHO
632 *
633 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
634 * Initial Tag received in the INIT ACK.
635 *
636 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
637 *
638 * Inputs
639 * (endpoint, asoc, chunk)
640 *
641 * Outputs
642 * (asoc, reply_msg, msg_up, timers, counters)
643 *
644 * The return value is the disposition of the chunk.
645 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000646sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
647 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 const struct sctp_association *asoc,
649 const sctp_subtype_t type, void *arg,
650 sctp_cmd_seq_t *commands)
651{
652 struct sctp_chunk *chunk = arg;
653 struct sctp_association *new_asoc;
654 sctp_init_chunk_t *peer_init;
655 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700656 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int error = 0;
658 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900659 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /* If the packet is an OOTB packet which is temporarily on the
662 * control endpoint, respond with an ABORT.
663 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +0000664 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000665 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000666 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 /* Make sure that the COOKIE_ECHO chunk has a valid length.
670 * In this case, we check that we have enough for at least a
671 * chunk header. More detailed verification is done
672 * in sctp_unpack_cookie().
673 */
674 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000675 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Vlad Yasevich609ee462007-08-31 03:10:59 +0900677 /* If the endpoint is not listening or if the number of associations
678 * on the TCP-style socket exceed the max backlog, respond with an
679 * ABORT.
680 */
681 sk = ep->base.sk;
682 if (!sctp_sstate(sk, LISTENING) ||
683 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000684 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* "Decode" the chunk. We have no optional parameters so we
687 * are in good shape.
688 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900689 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700691 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
692 sizeof(sctp_chunkhdr_t)))
693 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
696 * "Z" will reply with a COOKIE ACK chunk after building a TCB
697 * and moving to the ESTABLISHED state.
698 */
699 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
700 &err_chk_p);
701
702 /* FIXME:
703 * If the re-build failed, what is the proper error path
704 * from here?
705 *
706 * [We should abort the association. --piggy]
707 */
708 if (!new_asoc) {
709 /* FIXME: Several errors are possible. A bad cookie should
710 * be silently discarded, but think about logging it too.
711 */
712 switch (error) {
713 case -SCTP_IERROR_NOMEM:
714 goto nomem;
715
716 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000717 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000719 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 case -SCTP_IERROR_BAD_SIG:
722 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000723 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700728 /* Delay state machine commands until later.
729 *
730 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 * the sctp_unpack_cookie() already.
732 */
733 /* This is a brand-new association, so these are not yet side
734 * effects--it is safe to run them here.
735 */
736 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
737
Wei Yongjunde6becd2011-04-19 21:30:51 +0000738 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800739 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 peer_init, GFP_ATOMIC))
741 goto nomem_init;
742
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700743 /* SCTP-AUTH: Now that we've populate required fields in
744 * sctp_process_init, set up the assocaition shared keys as
745 * necessary so that we can potentially authenticate the ACK
746 */
747 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
748 if (error)
749 goto nomem_init;
750
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700751 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
752 * is supposed to be authenticated and we have to do delayed
753 * authentication. We've just recreated the association using
754 * the information in the cookie and now it's much easier to
755 * do the authentication.
756 */
757 if (chunk->auth_chunk) {
758 struct sctp_chunk auth;
759 sctp_ierror_t ret;
760
761 /* set-up our fake chunk so that we can process it */
762 auth.skb = chunk->auth_chunk;
763 auth.asoc = chunk->asoc;
764 auth.sctp_hdr = chunk->sctp_hdr;
765 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
766 sizeof(sctp_chunkhdr_t));
767 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
768 auth.transport = chunk->transport;
769
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000770 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700771
772 /* We can now safely free the auth_chunk clone */
773 kfree_skb(chunk->auth_chunk);
774
775 if (ret != SCTP_IERROR_NO_ERROR) {
776 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000777 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700778 }
779 }
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 repl = sctp_make_cookie_ack(new_asoc, chunk);
782 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700783 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* RFC 2960 5.1 Normal Establishment of an Association
786 *
787 * D) IMPLEMENTATION NOTE: An implementation may choose to
788 * send the Communication Up notification to the SCTP user
789 * upon reception of a valid COOKIE ECHO chunk.
790 */
791 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
792 new_asoc->c.sinit_num_ostreams,
793 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700794 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!ev)
796 goto nomem_ev;
797
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900798 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800799 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800801 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800803 if (new_asoc->peer.adaptation_ind) {
804 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700806 if (!ai_ev)
807 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700810 /* Add all the state machine commands now since we've created
811 * everything. This way we don't introduce memory corruptions
812 * during side-effect processing and correclty count established
813 * associations.
814 */
815 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
816 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
817 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000818 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
819 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700820 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
821
Neil Horman9f70f462013-12-10 06:48:15 -0500822 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700823 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
824 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
825
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700826 /* This will send the COOKIE ACK */
827 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
828
829 /* Queue the ASSOC_CHANGE event */
830 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
831
832 /* Send up the Adaptation Layer Indication event */
833 if (ai_ev)
834 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
835 SCTP_ULPEVENT(ai_ev));
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return SCTP_DISPOSITION_CONSUME;
838
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700839nomem_aiev:
840 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841nomem_ev:
842 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843nomem_init:
844 sctp_association_free(new_asoc);
845nomem:
846 return SCTP_DISPOSITION_NOMEM;
847}
848
849/*
850 * Respond to a normal COOKIE ACK chunk.
851 * We are the side that is being asked for an association.
852 *
853 * RFC 2960 5.1 Normal Establishment of an Association
854 *
855 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
856 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
857 * timer. It may also notify its ULP about the successful
858 * establishment of the association with a Communication Up
859 * notification (see Section 10).
860 *
861 * Verification Tag:
862 * Inputs
863 * (endpoint, asoc, chunk)
864 *
865 * Outputs
866 * (asoc, reply_msg, msg_up, timers, counters)
867 *
868 * The return value is the disposition of the chunk.
869 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000870sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
871 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 const struct sctp_association *asoc,
873 const sctp_subtype_t type, void *arg,
874 sctp_cmd_seq_t *commands)
875{
876 struct sctp_chunk *chunk = arg;
877 struct sctp_ulpevent *ev;
878
879 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000880 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* Verify that the chunk length for the COOKIE-ACK is OK.
883 * If we don't do this, any bundled chunks may be junked.
884 */
885 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000886 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 commands);
888
889 /* Reset init error count upon receipt of COOKIE-ACK,
890 * to avoid problems with the managemement of this
891 * counter in stale cookie situations when a transition back
892 * from the COOKIE-ECHOED state to the COOKIE-WAIT
893 * state is performed.
894 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700895 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /* RFC 2960 5.1 Normal Establishment of an Association
898 *
899 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
900 * from the COOKIE-ECHOED state to the ESTABLISHED state,
901 * stopping the T1-cookie timer.
902 */
903 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
904 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
905 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
906 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000907 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
908 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500910 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
912 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* It may also notify its ULP about the successful
915 * establishment of the association with a Communication Up
916 * notification (see Section 10).
917 */
918 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
919 0, asoc->c.sinit_num_ostreams,
920 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700921 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (!ev)
924 goto nomem;
925
926 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
927
928 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800929 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800931 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800933 if (asoc->peer.adaptation_ind) {
934 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (!ev)
936 goto nomem;
937
938 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
939 SCTP_ULPEVENT(ev));
940 }
941
942 return SCTP_DISPOSITION_CONSUME;
943nomem:
944 return SCTP_DISPOSITION_NOMEM;
945}
946
947/* Generate and sendout a heartbeat packet. */
948static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
949 const struct sctp_association *asoc,
950 const sctp_subtype_t type,
951 void *arg,
952 sctp_cmd_seq_t *commands)
953{
954 struct sctp_transport *transport = (struct sctp_transport *) arg;
955 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000958 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!reply)
960 return SCTP_DISPOSITION_NOMEM;
961
962 /* Set rto_pending indicating that an RTT measurement
963 * is started with this heartbeat chunk.
964 */
965 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
966 SCTP_TRANSPORT(transport));
967
968 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
969 return SCTP_DISPOSITION_CONSUME;
970}
971
972/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000973sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
974 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 const struct sctp_association *asoc,
976 const sctp_subtype_t type,
977 void *arg,
978 sctp_cmd_seq_t *commands)
979{
980 struct sctp_transport *transport = (struct sctp_transport *) arg;
981
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400982 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700983 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
984 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
986 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800987 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000988 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
989 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return SCTP_DISPOSITION_DELETE_TCB;
991 }
992
993 /* Section 3.3.5.
994 * The Sender-specific Heartbeat Info field should normally include
995 * information about the sender's current time when this HEARTBEAT
996 * chunk is sent and the destination transport address to which this
997 * HEARTBEAT is sent (see Section 8.3).
998 */
999
Frank Filz52ccb8e2005-12-22 11:36:46 -08001000 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (SCTP_DISPOSITION_NOMEM ==
1002 sctp_sf_heartbeat(ep, asoc, type, arg,
1003 commands))
1004 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 /* Set transport error counter and association error counter
1007 * when sending heartbeat.
1008 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001009 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 SCTP_TRANSPORT(transport));
1011 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001012 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1013 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1015 SCTP_TRANSPORT(transport));
1016
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001017 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020/*
1021 * Process an heartbeat request.
1022 *
1023 * Section: 8.3 Path Heartbeat
1024 * The receiver of the HEARTBEAT should immediately respond with a
1025 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1026 * from the received HEARTBEAT chunk.
1027 *
1028 * Verification Tag: 8.5 Verification Tag [Normal verification]
1029 * When receiving an SCTP packet, the endpoint MUST ensure that the
1030 * value in the Verification Tag field of the received SCTP packet
1031 * matches its own Tag. If the received Verification Tag value does not
1032 * match the receiver's own tag value, the receiver shall silently
1033 * discard the packet and shall not process it any further except for
1034 * those cases listed in Section 8.5.1 below.
1035 *
1036 * Inputs
1037 * (endpoint, asoc, chunk)
1038 *
1039 * Outputs
1040 * (asoc, reply_msg, msg_up, timers, counters)
1041 *
1042 * The return value is the disposition of the chunk.
1043 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001044sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1045 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 const struct sctp_association *asoc,
1047 const sctp_subtype_t type,
1048 void *arg,
1049 sctp_cmd_seq_t *commands)
1050{
Thomas Graf06a31e22012-11-30 02:16:27 +00001051 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct sctp_chunk *chunk = arg;
1053 struct sctp_chunk *reply;
1054 size_t paylen = 0;
1055
1056 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001057 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /* Make sure that the HEARTBEAT chunk has a valid length. */
1060 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001061 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 commands);
1063
1064 /* 8.3 The receiver of the HEARTBEAT should immediately
1065 * respond with a HEARTBEAT ACK that contains the Heartbeat
1066 * Information field copied from the received HEARTBEAT chunk.
1067 */
1068 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001069 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001071
1072 if (ntohs(param_hdr->length) > paylen)
1073 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1074 param_hdr, commands);
1075
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001076 if (!pskb_pull(chunk->skb, paylen))
1077 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Thomas Graf06a31e22012-11-30 02:16:27 +00001079 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (!reply)
1081 goto nomem;
1082
1083 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1084 return SCTP_DISPOSITION_CONSUME;
1085
1086nomem:
1087 return SCTP_DISPOSITION_NOMEM;
1088}
1089
1090/*
1091 * Process the returning HEARTBEAT ACK.
1092 *
1093 * Section: 8.3 Path Heartbeat
1094 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1095 * should clear the error counter of the destination transport
1096 * address to which the HEARTBEAT was sent, and mark the destination
1097 * transport address as active if it is not so marked. The endpoint may
1098 * optionally report to the upper layer when an inactive destination
1099 * address is marked as active due to the reception of the latest
1100 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1101 * clear the association overall error count as well (as defined
1102 * in section 8.1).
1103 *
1104 * The receiver of the HEARTBEAT ACK should also perform an RTT
1105 * measurement for that destination transport address using the time
1106 * value carried in the HEARTBEAT ACK chunk.
1107 *
1108 * Verification Tag: 8.5 Verification Tag [Normal verification]
1109 *
1110 * Inputs
1111 * (endpoint, asoc, chunk)
1112 *
1113 * Outputs
1114 * (asoc, reply_msg, msg_up, timers, counters)
1115 *
1116 * The return value is the disposition of the chunk.
1117 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001118sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1119 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 const struct sctp_association *asoc,
1121 const sctp_subtype_t type,
1122 void *arg,
1123 sctp_cmd_seq_t *commands)
1124{
1125 struct sctp_chunk *chunk = arg;
1126 union sctp_addr from_addr;
1127 struct sctp_transport *link;
1128 sctp_sender_hb_info_t *hbinfo;
1129 unsigned long max_interval;
1130
1131 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001132 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001135 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1136 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001137 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 commands);
1139
1140 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001141 /* Make sure that the length of the parameter is what we expect */
1142 if (ntohs(hbinfo->param_hdr.length) !=
1143 sizeof(sctp_sender_hb_info_t)) {
1144 return SCTP_DISPOSITION_DISCARD;
1145 }
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001148 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001151 if (unlikely(!link)) {
1152 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001153 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1154 __func__,
1155 asoc,
1156 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001157 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001158 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1159 __func__,
1160 asoc,
1161 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return SCTP_DISPOSITION_DISCARD;
1164 }
1165
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001166 /* Validate the 64-bit random nonce. */
1167 if (hbinfo->hb_nonce != link->hb_nonce)
1168 return SCTP_DISPOSITION_DISCARD;
1169
Frank Filz52ccb8e2005-12-22 11:36:46 -08001170 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /* Check if the timestamp looks valid. */
1173 if (time_after(hbinfo->sent_at, jiffies) ||
1174 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001175 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1176 "for transport:%p\n", __func__, link);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return SCTP_DISPOSITION_DISCARD;
1179 }
1180
1181 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1182 * the HEARTBEAT should clear the error counter of the
1183 * destination transport address to which the HEARTBEAT was
1184 * sent and mark the destination transport address as active if
1185 * it is not so marked.
1186 */
1187 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1188
1189 return SCTP_DISPOSITION_CONSUME;
1190}
1191
1192/* Helper function to send out an abort for the restart
1193 * condition.
1194 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001195static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct sctp_chunk *init,
1197 sctp_cmd_seq_t *commands)
1198{
1199 int len;
1200 struct sctp_packet *pkt;
1201 union sctp_addr_param *addrparm;
1202 struct sctp_errhdr *errhdr;
1203 struct sctp_endpoint *ep;
1204 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1205 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1206
1207 /* Build the error on the stack. We are way to malloc crazy
1208 * throughout the code today.
1209 */
1210 errhdr = (struct sctp_errhdr *)buffer;
1211 addrparm = (union sctp_addr_param *)errhdr->variable;
1212
1213 /* Copy into a parm format. */
1214 len = af->to_addr_param(ssa, addrparm);
1215 len += sizeof(sctp_errhdr_t);
1216
1217 errhdr->cause = SCTP_ERROR_RESTART;
1218 errhdr->length = htons(len);
1219
1220 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001221 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 /* Association is NULL since this may be a restart attack and we
1224 * want to send back the attacker's vtag.
1225 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001226 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (!pkt)
1229 goto out;
1230 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1231
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001232 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /* Discard the rest of the inbound packet. */
1235 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1236
1237out:
1238 /* Even if there is no memory, treat as a failure so
1239 * the packet will get dropped.
1240 */
1241 return 0;
1242}
1243
Joe Perches123031c2010-09-08 11:04:21 +00001244static bool list_has_sctp_addr(const struct list_head *list,
1245 union sctp_addr *ipaddr)
1246{
1247 struct sctp_transport *addr;
1248
1249 list_for_each_entry(addr, list, transports) {
1250 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1251 return true;
1252 }
1253
1254 return false;
1255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256/* A restart is occurring, check to make sure no new addresses
1257 * are being added as we may be under a takeover attack.
1258 */
1259static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1260 const struct sctp_association *asoc,
1261 struct sctp_chunk *init,
1262 sctp_cmd_seq_t *commands)
1263{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001264 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001265 struct sctp_transport *new_addr;
1266 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Joe Perches123031c2010-09-08 11:04:21 +00001268 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * ...
1270 * Before responding the endpoint MUST check to see if the
1271 * unexpected INIT adds new addresses to the association. If new
1272 * addresses are added to the association, the endpoint MUST respond
1273 * with an ABORT..
1274 */
1275
1276 /* Search through all current addresses and make sure
1277 * we aren't adding any new ones.
1278 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001279 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001280 transports) {
1281 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1282 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001283 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001284 commands);
1285 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 break;
Joe Perches123031c2010-09-08 11:04:21 +00001287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
1290 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/* Populate the verification/tie tags based on overlapping INIT
1295 * scenario.
1296 *
1297 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1298 */
1299static void sctp_tietags_populate(struct sctp_association *new_asoc,
1300 const struct sctp_association *asoc)
1301{
1302 switch (asoc->state) {
1303
1304 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1305
1306 case SCTP_STATE_COOKIE_WAIT:
1307 new_asoc->c.my_vtag = asoc->c.my_vtag;
1308 new_asoc->c.my_ttag = asoc->c.my_vtag;
1309 new_asoc->c.peer_ttag = 0;
1310 break;
1311
1312 case SCTP_STATE_COOKIE_ECHOED:
1313 new_asoc->c.my_vtag = asoc->c.my_vtag;
1314 new_asoc->c.my_ttag = asoc->c.my_vtag;
1315 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1316 break;
1317
1318 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1319 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1320 */
1321 default:
1322 new_asoc->c.my_ttag = asoc->c.my_vtag;
1323 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1324 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 /* Other parameters for the endpoint SHOULD be copied from the
1328 * existing parameters of the association (e.g. number of
1329 * outbound streams) into the INIT ACK and cookie.
1330 */
1331 new_asoc->rwnd = asoc->rwnd;
1332 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1333 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1334 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1335}
1336
1337/*
1338 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1339 * handling action.
1340 *
1341 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1342 *
1343 * Returns value representing action to be taken. These action values
1344 * correspond to Action/Description values in RFC 2960, Table 2.
1345 */
1346static char sctp_tietags_compare(struct sctp_association *new_asoc,
1347 const struct sctp_association *asoc)
1348{
1349 /* In this case, the peer may have restarted. */
1350 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1351 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1352 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1353 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1354 return 'A';
1355
1356 /* Collision case B. */
1357 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1358 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1359 (0 == asoc->c.peer_vtag))) {
1360 return 'B';
1361 }
1362
1363 /* Collision case D. */
1364 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1365 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1366 return 'D';
1367
1368 /* Collision case C. */
1369 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1370 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1371 (0 == new_asoc->c.my_ttag) &&
1372 (0 == new_asoc->c.peer_ttag))
1373 return 'C';
1374
1375 /* No match to any of the special cases; discard this packet. */
1376 return 'E';
1377}
1378
1379/* Common helper routine for both duplicate and simulataneous INIT
1380 * chunk handling.
1381 */
1382static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001383 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 const struct sctp_endpoint *ep,
1385 const struct sctp_association *asoc,
1386 const sctp_subtype_t type,
1387 void *arg, sctp_cmd_seq_t *commands)
1388{
1389 sctp_disposition_t retval;
1390 struct sctp_chunk *chunk = arg;
1391 struct sctp_chunk *repl;
1392 struct sctp_association *new_asoc;
1393 struct sctp_chunk *err_chunk;
1394 struct sctp_packet *packet;
1395 sctp_unrecognized_param_t *unk_param;
1396 int len;
1397
1398 /* 6.10 Bundling
1399 * An endpoint MUST NOT bundle INIT, INIT ACK or
1400 * SHUTDOWN COMPLETE with any other chunks.
1401 *
1402 * IG Section 2.11.2
1403 * Furthermore, we require that the receiver of an INIT chunk MUST
1404 * enforce these rules by silently discarding an arriving packet
1405 * with an INIT chunk that is bundled with other chunks.
1406 */
1407 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001408 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001411 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 */
1413 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001414 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 /* Make sure that the INIT chunk has a valid length.
1417 * In this case, we generate a protocol violation since we have
1418 * an association established.
1419 */
1420 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001421 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 commands);
1423 /* Grab the INIT header. */
1424 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1425
1426 /* Tag the variable length parameters. */
1427 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1428
1429 /* Verify the INIT chunk before processing it. */
1430 err_chunk = NULL;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00001431 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1433 &err_chunk)) {
1434 /* This chunk contains fatal error. It is to be discarded.
1435 * Send an ABORT, with causes if there is any.
1436 */
1437 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001438 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 (__u8 *)(err_chunk->chunk_hdr) +
1440 sizeof(sctp_chunkhdr_t),
1441 ntohs(err_chunk->chunk_hdr->length) -
1442 sizeof(sctp_chunkhdr_t));
1443
1444 if (packet) {
1445 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1446 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001447 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 retval = SCTP_DISPOSITION_CONSUME;
1449 } else {
1450 retval = SCTP_DISPOSITION_NOMEM;
1451 }
1452 goto cleanup;
1453 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001454 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 commands);
1456 }
1457 }
1458
1459 /*
1460 * Other parameters for the endpoint SHOULD be copied from the
1461 * existing parameters of the association (e.g. number of
1462 * outbound streams) into the INIT ACK and cookie.
1463 * FIXME: We are copying parameters from the endpoint not the
1464 * association.
1465 */
1466 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1467 if (!new_asoc)
1468 goto nomem;
1469
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001470 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1471 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1472 goto nomem;
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 /* In the outbound INIT ACK the endpoint MUST copy its current
1475 * Verification Tag and Peers Verification tag into a reserved
1476 * place (local tie-tag and per tie-tag) within the state cookie.
1477 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001478 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001480 GFP_ATOMIC))
1481 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 /* Make sure no new addresses are being added during the
1484 * restart. Do not do this check for COOKIE-WAIT state,
1485 * since there are no peer addresses to check against.
1486 * Upon return an ABORT will have been sent if needed.
1487 */
1488 if (!sctp_state(asoc, COOKIE_WAIT)) {
1489 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1490 commands)) {
1491 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001492 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494 }
1495
1496 sctp_tietags_populate(new_asoc, asoc);
1497
1498 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1499
1500 /* If there are errors need to be reported for unknown parameters,
1501 * make sure to reserve enough room in the INIT ACK for them.
1502 */
1503 len = 0;
1504 if (err_chunk) {
1505 len = ntohs(err_chunk->chunk_hdr->length) -
1506 sizeof(sctp_chunkhdr_t);
1507 }
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1510 if (!repl)
1511 goto nomem;
1512
1513 /* If there are errors need to be reported for unknown parameters,
1514 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1515 * parameter.
1516 */
1517 if (err_chunk) {
1518 /* Get the "Unrecognized parameter" parameter(s) out of the
1519 * ERROR chunk generated by sctp_verify_init(). Since the
1520 * error cause code for "unknown parameter" and the
1521 * "Unrecognized parameter" type is the same, we can
1522 * construct the parameters in INIT ACK by copying the
1523 * ERROR causes over.
1524 */
1525 unk_param = (sctp_unrecognized_param_t *)
1526 ((__u8 *)(err_chunk->chunk_hdr) +
1527 sizeof(sctp_chunkhdr_t));
1528 /* Replace the cause code with the "Unrecognized parameter"
1529 * parameter type.
1530 */
1531 sctp_addto_chunk(repl, len, unk_param);
1532 }
1533
1534 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1535 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1536
1537 /*
1538 * Note: After sending out INIT ACK with the State Cookie parameter,
1539 * "Z" MUST NOT allocate any resources for this new association.
1540 * Otherwise, "Z" will be vulnerable to resource attacks.
1541 */
1542 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1543 retval = SCTP_DISPOSITION_CONSUME;
1544
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001545 return retval;
1546
1547nomem:
1548 retval = SCTP_DISPOSITION_NOMEM;
1549nomem_retval:
1550 if (new_asoc)
1551 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552cleanup:
1553 if (err_chunk)
1554 sctp_chunk_free(err_chunk);
1555 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
1558/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001559 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 * This means we started an INIT and then we got an INIT request from
1561 * our peer.
1562 *
1563 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1564 * This usually indicates an initialization collision, i.e., each
1565 * endpoint is attempting, at about the same time, to establish an
1566 * association with the other endpoint.
1567 *
1568 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1569 * endpoint MUST respond with an INIT ACK using the same parameters it
1570 * sent in its original INIT chunk (including its Verification Tag,
1571 * unchanged). These original parameters are combined with those from the
1572 * newly received INIT chunk. The endpoint shall also generate a State
1573 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1574 * INIT to calculate the State Cookie.
1575 *
1576 * After that, the endpoint MUST NOT change its state, the T1-init
1577 * timer shall be left running and the corresponding TCB MUST NOT be
1578 * destroyed. The normal procedures for handling State Cookies when
1579 * a TCB exists will resolve the duplicate INITs to a single association.
1580 *
1581 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1582 * its Tie-Tags with the Tag information of itself and its peer (see
1583 * section 5.2.2 for a description of the Tie-Tags).
1584 *
1585 * Verification Tag: Not explicit, but an INIT can not have a valid
1586 * verification tag, so we skip the check.
1587 *
1588 * Inputs
1589 * (endpoint, asoc, chunk)
1590 *
1591 * Outputs
1592 * (asoc, reply_msg, msg_up, timers, counters)
1593 *
1594 * The return value is the disposition of the chunk.
1595 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001596sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1597 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 const struct sctp_association *asoc,
1599 const sctp_subtype_t type,
1600 void *arg,
1601 sctp_cmd_seq_t *commands)
1602{
1603 /* Call helper to do the real work for both simulataneous and
1604 * duplicate INIT chunk handling.
1605 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001606 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609/*
1610 * Handle duplicated INIT messages. These are usually delayed
1611 * restransmissions.
1612 *
1613 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1614 * COOKIE-ECHOED and COOKIE-WAIT
1615 *
1616 * Unless otherwise stated, upon reception of an unexpected INIT for
1617 * this association, the endpoint shall generate an INIT ACK with a
1618 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1619 * current Verification Tag and peer's Verification Tag into a reserved
1620 * place within the state cookie. We shall refer to these locations as
1621 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1622 * containing this INIT ACK MUST carry a Verification Tag value equal to
1623 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1624 * MUST contain a new Initiation Tag (randomly generated see Section
1625 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1626 * existing parameters of the association (e.g. number of outbound
1627 * streams) into the INIT ACK and cookie.
1628 *
1629 * After sending out the INIT ACK, the endpoint shall take no further
1630 * actions, i.e., the existing association, including its current state,
1631 * and the corresponding TCB MUST NOT be changed.
1632 *
1633 * Note: Only when a TCB exists and the association is not in a COOKIE-
1634 * WAIT state are the Tie-Tags populated. For a normal association INIT
1635 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1636 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1637 * State Cookie are populated as specified in section 5.2.1.
1638 *
1639 * Verification Tag: Not specified, but an INIT has no way of knowing
1640 * what the verification tag could be, so we ignore it.
1641 *
1642 * Inputs
1643 * (endpoint, asoc, chunk)
1644 *
1645 * Outputs
1646 * (asoc, reply_msg, msg_up, timers, counters)
1647 *
1648 * The return value is the disposition of the chunk.
1649 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001650sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1651 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 const struct sctp_association *asoc,
1653 const sctp_subtype_t type,
1654 void *arg,
1655 sctp_cmd_seq_t *commands)
1656{
1657 /* Call helper to do the real work for both simulataneous and
1658 * duplicate INIT chunk handling.
1659 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001660 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
1663
Vlad Yasevich610ab732007-01-15 19:18:30 -08001664/*
1665 * Unexpected INIT-ACK handler.
1666 *
1667 * Section 5.2.3
1668 * If an INIT ACK received by an endpoint in any state other than the
1669 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1670 * An unexpected INIT ACK usually indicates the processing of an old or
1671 * duplicated INIT chunk.
1672*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001673sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1674 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001675 const struct sctp_association *asoc,
1676 const sctp_subtype_t type,
1677 void *arg, sctp_cmd_seq_t *commands)
1678{
1679 /* Per the above section, we'll discard the chunk if we have an
1680 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1681 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001682 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001683 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001684 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001685 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001686}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1689 *
1690 * Section 5.2.4
1691 * A) In this case, the peer may have restarted.
1692 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001693static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1694 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 const struct sctp_association *asoc,
1696 struct sctp_chunk *chunk,
1697 sctp_cmd_seq_t *commands,
1698 struct sctp_association *new_asoc)
1699{
1700 sctp_init_chunk_t *peer_init;
1701 struct sctp_ulpevent *ev;
1702 struct sctp_chunk *repl;
1703 struct sctp_chunk *err;
1704 sctp_disposition_t disposition;
1705
1706 /* new_asoc is a brand-new association, so these are not yet
1707 * side effects--it is safe to run them here.
1708 */
1709 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1710
Wei Yongjunde6becd2011-04-19 21:30:51 +00001711 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 GFP_ATOMIC))
1713 goto nomem;
1714
1715 /* Make sure no new addresses are being added during the
1716 * restart. Though this is a pretty complicated attack
1717 * since you'd have to get inside the cookie.
1718 */
1719 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1720 return SCTP_DISPOSITION_CONSUME;
1721 }
1722
1723 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1724 * the peer has restarted (Action A), it MUST NOT setup a new
1725 * association but instead resend the SHUTDOWN ACK and send an ERROR
1726 * chunk with a "Cookie Received while Shutting Down" error cause to
1727 * its peer.
1728 */
1729 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001730 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1732 chunk, commands);
1733 if (SCTP_DISPOSITION_NOMEM == disposition)
1734 goto nomem;
1735
1736 err = sctp_make_op_error(asoc, chunk,
1737 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001738 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (err)
1740 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1741 SCTP_CHUNK(err));
1742
1743 return SCTP_DISPOSITION_CONSUME;
1744 }
1745
Wei Yongjuna000c012011-05-29 23:23:36 +00001746 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1747 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001749 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1750 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1751 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1753
Wei Yongjuna000c012011-05-29 23:23:36 +00001754 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1755 * and ASCONF-ACK cache.
1756 */
1757 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1758 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1759 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 repl = sctp_make_cookie_ack(new_asoc, chunk);
1762 if (!repl)
1763 goto nomem;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* Report association restart to upper layer. */
1766 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1767 new_asoc->c.sinit_num_ostreams,
1768 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001769 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 if (!ev)
1771 goto nomem_ev;
1772
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001773 /* Update the content of current association. */
1774 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Xufeng Zhang9839ff02013-01-23 16:44:34 +00001776 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1777 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1778 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return SCTP_DISPOSITION_CONSUME;
1780
1781nomem_ev:
1782 sctp_chunk_free(repl);
1783nomem:
1784 return SCTP_DISPOSITION_NOMEM;
1785}
1786
1787/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1788 *
1789 * Section 5.2.4
1790 * B) In this case, both sides may be attempting to start an association
1791 * at about the same time but the peer endpoint started its INIT
1792 * after responding to the local endpoint's INIT
1793 */
1794/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001795static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1796 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 const struct sctp_association *asoc,
1798 struct sctp_chunk *chunk,
1799 sctp_cmd_seq_t *commands,
1800 struct sctp_association *new_asoc)
1801{
1802 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 struct sctp_chunk *repl;
1804
1805 /* new_asoc is a brand-new association, so these are not yet
1806 * side effects--it is safe to run them here.
1807 */
1808 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001809 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 GFP_ATOMIC))
1811 goto nomem;
1812
1813 /* Update the content of current association. */
1814 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1815 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1816 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001817 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1819
1820 repl = sctp_make_cookie_ack(new_asoc, chunk);
1821 if (!repl)
1822 goto nomem;
1823
1824 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* RFC 2960 5.1 Normal Establishment of an Association
1827 *
1828 * D) IMPLEMENTATION NOTE: An implementation may choose to
1829 * send the Communication Up notification to the SCTP user
1830 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001831 *
1832 * Sadly, this needs to be implemented as a side-effect, because
1833 * we are not guaranteed to have set the association id of the real
1834 * association and so these notifications need to be delayed until
1835 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Vlad Yasevich07d93962007-05-04 13:55:27 -07001838 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001841 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001843 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001844 *
1845 * This also needs to be done as a side effect for the same reason as
1846 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001848 if (asoc->peer.adaptation_ind)
1849 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 return SCTP_DISPOSITION_CONSUME;
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853nomem:
1854 return SCTP_DISPOSITION_NOMEM;
1855}
1856
1857/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1858 *
1859 * Section 5.2.4
1860 * C) In this case, the local endpoint's cookie has arrived late.
1861 * Before it arrived, the local endpoint sent an INIT and received an
1862 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1863 * but a new tag of its own.
1864 */
1865/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001866static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1867 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 const struct sctp_association *asoc,
1869 struct sctp_chunk *chunk,
1870 sctp_cmd_seq_t *commands,
1871 struct sctp_association *new_asoc)
1872{
1873 /* The cookie should be silently discarded.
1874 * The endpoint SHOULD NOT change states and should leave
1875 * any timers running.
1876 */
1877 return SCTP_DISPOSITION_DISCARD;
1878}
1879
1880/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1881 *
1882 * Section 5.2.4
1883 *
1884 * D) When both local and remote tags match the endpoint should always
1885 * enter the ESTABLISHED state, if it has not already done so.
1886 */
1887/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001888static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1889 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 const struct sctp_association *asoc,
1891 struct sctp_chunk *chunk,
1892 sctp_cmd_seq_t *commands,
1893 struct sctp_association *new_asoc)
1894{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001895 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct sctp_chunk *repl;
1897
1898 /* Clarification from Implementor's Guide:
1899 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001900 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1901 * It should stop any cookie timer that may be running and send
1902 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 */
1904
1905 /* Don't accidentally move back into established state. */
1906 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1908 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1909 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1910 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001911 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1913 SCTP_NULL());
1914
1915 /* RFC 2960 5.1 Normal Establishment of an Association
1916 *
1917 * D) IMPLEMENTATION NOTE: An implementation may choose
1918 * to send the Communication Up notification to the
1919 * SCTP user upon reception of a valid COOKIE
1920 * ECHO chunk.
1921 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001922 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001924 asoc->c.sinit_num_ostreams,
1925 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001926 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (!ev)
1928 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001931 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001933 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001935 if (asoc->peer.adaptation_ind) {
1936 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001938 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto nomem;
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 repl = sctp_make_cookie_ack(new_asoc, chunk);
1945 if (!repl)
1946 goto nomem;
1947
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001948 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1949
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001950 if (ev)
1951 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1952 SCTP_ULPEVENT(ev));
1953 if (ai_ev)
1954 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1955 SCTP_ULPEVENT(ai_ev));
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return SCTP_DISPOSITION_CONSUME;
1958
1959nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001960 if (ai_ev)
1961 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if (ev)
1963 sctp_ulpevent_free(ev);
1964 return SCTP_DISPOSITION_NOMEM;
1965}
1966
1967/*
1968 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1969 * chunk was retransmitted and then delayed in the network.
1970 *
1971 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1972 *
1973 * Verification Tag: None. Do cookie validation.
1974 *
1975 * Inputs
1976 * (endpoint, asoc, chunk)
1977 *
1978 * Outputs
1979 * (asoc, reply_msg, msg_up, timers, counters)
1980 *
1981 * The return value is the disposition of the chunk.
1982 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001983sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1984 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 const struct sctp_association *asoc,
1986 const sctp_subtype_t type,
1987 void *arg,
1988 sctp_cmd_seq_t *commands)
1989{
1990 sctp_disposition_t retval;
1991 struct sctp_chunk *chunk = arg;
1992 struct sctp_association *new_asoc;
1993 int error = 0;
1994 char action;
1995 struct sctp_chunk *err_chk_p;
1996
1997 /* Make sure that the chunk has a valid length from the protocol
1998 * perspective. In this case check to make sure we have at least
1999 * enough for the chunk header. Cookie length verification is
2000 * done later.
2001 */
2002 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002003 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 commands);
2005
2006 /* "Decode" the chunk. We have no optional parameters so we
2007 * are in good shape.
2008 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002009 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002010 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2011 sizeof(sctp_chunkhdr_t)))
2012 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2015 * of a duplicate COOKIE ECHO match the Verification Tags of the
2016 * current association, consider the State Cookie valid even if
2017 * the lifespan is exceeded.
2018 */
2019 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2020 &err_chk_p);
2021
2022 /* FIXME:
2023 * If the re-build failed, what is the proper error path
2024 * from here?
2025 *
2026 * [We should abort the association. --piggy]
2027 */
2028 if (!new_asoc) {
2029 /* FIXME: Several errors are possible. A bad cookie should
2030 * be silently discarded, but think about logging it too.
2031 */
2032 switch (error) {
2033 case -SCTP_IERROR_NOMEM:
2034 goto nomem;
2035
2036 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002037 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002039 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 case -SCTP_IERROR_BAD_SIG:
2041 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002042 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045
2046 /* Compare the tie_tag in cookie with the verification tag of
2047 * current association.
2048 */
2049 action = sctp_tietags_compare(new_asoc, asoc);
2050
2051 switch (action) {
2052 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002053 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 new_asoc);
2055 break;
2056
2057 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002058 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 new_asoc);
2060 break;
2061
2062 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002063 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 new_asoc);
2065 break;
2066
2067 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002068 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 new_asoc);
2070 break;
2071
2072 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002073 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002078 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2080
Max Matveevd5ccd492011-08-29 21:02:24 +00002081 /* Restore association pointer to provide SCTP command interpeter
2082 * with a valid context in case it needs to manipulate
2083 * the queues */
2084 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2085 SCTP_ASOC((struct sctp_association *)asoc));
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 return retval;
2088
2089nomem:
2090 return SCTP_DISPOSITION_NOMEM;
2091}
2092
2093/*
2094 * Process an ABORT. (SHUTDOWN-PENDING state)
2095 *
2096 * See sctp_sf_do_9_1_abort().
2097 */
2098sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002099 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 const struct sctp_endpoint *ep,
2101 const struct sctp_association *asoc,
2102 const sctp_subtype_t type,
2103 void *arg,
2104 sctp_cmd_seq_t *commands)
2105{
2106 struct sctp_chunk *chunk = arg;
2107
2108 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002109 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
2111 /* Make sure that the ABORT chunk has a valid length.
2112 * Since this is an ABORT chunk, we have to discard it
2113 * because of the following text:
2114 * RFC 2960, Section 3.3.7
2115 * If an endpoint receives an ABORT with a format error or for an
2116 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002117 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * as we do not know its true length. So, to be safe, discard the
2119 * packet.
2120 */
2121 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002122 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Vlad Yasevich75205f42007-12-20 14:12:59 -08002124 /* ADD-IP: Special case for ABORT chunks
2125 * F4) One special consideration is that ABORT Chunks arriving
2126 * destined to the IP address being deleted MUST be
2127 * ignored (see Section 5.3.1 for further details).
2128 */
2129 if (SCTP_ADDR_DEL ==
2130 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002131 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002132
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002133 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
2136/*
2137 * Process an ABORT. (SHUTDOWN-SENT state)
2138 *
2139 * See sctp_sf_do_9_1_abort().
2140 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002141sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2142 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 const struct sctp_association *asoc,
2144 const sctp_subtype_t type,
2145 void *arg,
2146 sctp_cmd_seq_t *commands)
2147{
2148 struct sctp_chunk *chunk = arg;
2149
2150 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002151 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 /* Make sure that the ABORT chunk has a valid length.
2154 * Since this is an ABORT chunk, we have to discard it
2155 * because of the following text:
2156 * RFC 2960, Section 3.3.7
2157 * If an endpoint receives an ABORT with a format error or for an
2158 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002159 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 * as we do not know its true length. So, to be safe, discard the
2161 * packet.
2162 */
2163 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002164 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Vlad Yasevich75205f42007-12-20 14:12:59 -08002166 /* ADD-IP: Special case for ABORT chunks
2167 * F4) One special consideration is that ABORT Chunks arriving
2168 * destined to the IP address being deleted MUST be
2169 * ignored (see Section 5.3.1 for further details).
2170 */
2171 if (SCTP_ADDR_DEL ==
2172 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002173 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 /* Stop the T2-shutdown timer. */
2176 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2177 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2178
2179 /* Stop the T5-shutdown guard timer. */
2180 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2181 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2182
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002183 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
2186/*
2187 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2188 *
2189 * See sctp_sf_do_9_1_abort().
2190 */
2191sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002192 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 const struct sctp_endpoint *ep,
2194 const struct sctp_association *asoc,
2195 const sctp_subtype_t type,
2196 void *arg,
2197 sctp_cmd_seq_t *commands)
2198{
2199 /* The same T2 timer, so we should be able to use
2200 * common function with the SHUTDOWN-SENT state.
2201 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002202 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205/*
2206 * Handle an Error received in COOKIE_ECHOED state.
2207 *
2208 * Only handle the error type of stale COOKIE Error, the other errors will
2209 * be ignored.
2210 *
2211 * Inputs
2212 * (endpoint, asoc, chunk)
2213 *
2214 * Outputs
2215 * (asoc, reply_msg, msg_up, timers, counters)
2216 *
2217 * The return value is the disposition of the chunk.
2218 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002219sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2220 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 const struct sctp_association *asoc,
2222 const sctp_subtype_t type,
2223 void *arg,
2224 sctp_cmd_seq_t *commands)
2225{
2226 struct sctp_chunk *chunk = arg;
2227 sctp_errhdr_t *err;
2228
2229 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002230 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 /* Make sure that the ERROR chunk has a valid length.
2233 * The parameter walking depends on this as well.
2234 */
2235 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002236 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 commands);
2238
2239 /* Process the error here */
2240 /* FUTURE FIXME: When PR-SCTP related and other optional
2241 * parms are emitted, this will have to change to handle multiple
2242 * errors.
2243 */
2244 sctp_walk_errors(err, chunk->chunk_hdr) {
2245 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002246 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 arg, commands);
2248 }
2249
2250 /* It is possible to have malformed error causes, and that
2251 * will cause us to end the walk early. However, since
2252 * we are discarding the packet, there should be no adverse
2253 * affects.
2254 */
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
2258/*
2259 * Handle a Stale COOKIE Error
2260 *
2261 * Section: 5.2.6 Handle Stale COOKIE Error
2262 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2263 * one of the following three alternatives.
2264 * ...
2265 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2266 * Preservative parameter requesting an extension to the lifetime of
2267 * the State Cookie. When calculating the time extension, an
2268 * implementation SHOULD use the RTT information measured based on the
2269 * previous COOKIE ECHO / ERROR exchange, and should add no more
2270 * than 1 second beyond the measured RTT, due to long State Cookie
2271 * lifetimes making the endpoint more subject to a replay attack.
2272 *
2273 * Verification Tag: Not explicit, but safe to ignore.
2274 *
2275 * Inputs
2276 * (endpoint, asoc, chunk)
2277 *
2278 * Outputs
2279 * (asoc, reply_msg, msg_up, timers, counters)
2280 *
2281 * The return value is the disposition of the chunk.
2282 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002283static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2284 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 const struct sctp_association *asoc,
2286 const sctp_subtype_t type,
2287 void *arg,
2288 sctp_cmd_seq_t *commands)
2289{
2290 struct sctp_chunk *chunk = arg;
2291 time_t stale;
2292 sctp_cookie_preserve_param_t bht;
2293 sctp_errhdr_t *err;
2294 struct sctp_chunk *reply;
2295 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002296 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Vlad Yasevich81845c22006-01-30 15:59:54 -08002298 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002299 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2300 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002302 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return SCTP_DISPOSITION_DELETE_TCB;
2304 }
2305
2306 err = (sctp_errhdr_t *)(chunk->skb->data);
2307
2308 /* When calculating the time extension, an implementation
2309 * SHOULD use the RTT information measured based on the
2310 * previous COOKIE ECHO / ERROR exchange, and should add no
2311 * more than 1 second beyond the measured RTT, due to long
2312 * State Cookie lifetimes making the endpoint more subject to
2313 * a replay attack.
2314 * Measure of Staleness's unit is usec. (1/1000000 sec)
2315 * Suggested Cookie Life-span Increment's unit is msec.
2316 * (1/1000 sec)
2317 * In general, if you use the suggested cookie life, the value
2318 * found in the field of measure of staleness should be doubled
2319 * to give ample time to retransmit the new cookie and thus
2320 * yield a higher probability of success on the reattempt.
2321 */
Al Viro34bcca22006-11-20 17:27:15 -08002322 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 stale = (stale * 2) / 1000;
2324
2325 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2326 bht.param_hdr.length = htons(sizeof(bht));
2327 bht.lifespan_increment = htonl(stale);
2328
2329 /* Build that new INIT chunk. */
2330 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2331 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2332 if (!reply)
2333 goto nomem;
2334
2335 sctp_addto_chunk(reply, sizeof(bht), &bht);
2336
2337 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2338 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2339
2340 /* Stop pending T3-rtx and heartbeat timers */
2341 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2342 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2343
2344 /* Delete non-primary peer ip addresses since we are transitioning
2345 * back to the COOKIE-WAIT state
2346 */
2347 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2348
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002349 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2350 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002352 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 SCTP_TRANSPORT(asoc->peer.primary_path));
2354
2355 /* Cast away the const modifier, as we want to just
2356 * rerun it through as a sideffect.
2357 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002358 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2361 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2362 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2363 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2364 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2365 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2366
2367 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2368
2369 return SCTP_DISPOSITION_CONSUME;
2370
2371nomem:
2372 return SCTP_DISPOSITION_NOMEM;
2373}
2374
2375/*
2376 * Process an ABORT.
2377 *
2378 * Section: 9.1
2379 * After checking the Verification Tag, the receiving endpoint shall
2380 * remove the association from its record, and shall report the
2381 * termination to its upper layer.
2382 *
2383 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2384 * B) Rules for packet carrying ABORT:
2385 *
2386 * - The endpoint shall always fill in the Verification Tag field of the
2387 * outbound packet with the destination endpoint's tag value if it
2388 * is known.
2389 *
2390 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2391 * MUST follow the procedure described in Section 8.4.
2392 *
2393 * - The receiver MUST accept the packet if the Verification Tag
2394 * matches either its own tag, OR the tag of its peer. Otherwise, the
2395 * receiver MUST silently discard the packet and take no further
2396 * action.
2397 *
2398 * Inputs
2399 * (endpoint, asoc, chunk)
2400 *
2401 * Outputs
2402 * (asoc, reply_msg, msg_up, timers, counters)
2403 *
2404 * The return value is the disposition of the chunk.
2405 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002406sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2407 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 const struct sctp_association *asoc,
2409 const sctp_subtype_t type,
2410 void *arg,
2411 sctp_cmd_seq_t *commands)
2412{
2413 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002416 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /* Make sure that the ABORT chunk has a valid length.
2419 * Since this is an ABORT chunk, we have to discard it
2420 * because of the following text:
2421 * RFC 2960, Section 3.3.7
2422 * If an endpoint receives an ABORT with a format error or for an
2423 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002424 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 * as we do not know its true length. So, to be safe, discard the
2426 * packet.
2427 */
2428 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002429 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Vlad Yasevich75205f42007-12-20 14:12:59 -08002431 /* ADD-IP: Special case for ABORT chunks
2432 * F4) One special consideration is that ABORT Chunks arriving
2433 * destined to the IP address being deleted MUST be
2434 * ignored (see Section 5.3.1 for further details).
2435 */
2436 if (SCTP_ADDR_DEL ==
2437 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002438 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002439
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002440 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002441}
2442
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002443static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2444 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002445 const struct sctp_association *asoc,
2446 const sctp_subtype_t type,
2447 void *arg,
2448 sctp_cmd_seq_t *commands)
2449{
2450 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002451 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002452 __be16 error = SCTP_ERROR_NO_ERROR;
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /* See if we have an error cause code in the chunk. */
2455 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002456 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2457
2458 sctp_errhdr_t *err;
2459 sctp_walk_errors(err, chunk->chunk_hdr);
2460 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002466 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002467 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002468 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002469 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2470 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 return SCTP_DISPOSITION_ABORT;
2473}
2474
2475/*
2476 * Process an ABORT. (COOKIE-WAIT state)
2477 *
2478 * See sctp_sf_do_9_1_abort() above.
2479 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002480sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2481 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 const struct sctp_association *asoc,
2483 const sctp_subtype_t type,
2484 void *arg,
2485 sctp_cmd_seq_t *commands)
2486{
2487 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002488 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002489 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002492 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 /* Make sure that the ABORT chunk has a valid length.
2495 * Since this is an ABORT chunk, we have to discard it
2496 * because of the following text:
2497 * RFC 2960, Section 3.3.7
2498 * If an endpoint receives an ABORT with a format error or for an
2499 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002500 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 * as we do not know its true length. So, to be safe, discard the
2502 * packet.
2503 */
2504 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002505 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 /* See if we have an error cause code in the chunk. */
2508 len = ntohs(chunk->chunk_hdr->length);
2509 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2510 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2511
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002512 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002513 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
2516/*
2517 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2518 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002519sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2520 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 const struct sctp_association *asoc,
2522 const sctp_subtype_t type,
2523 void *arg,
2524 sctp_cmd_seq_t *commands)
2525{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002526 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002527 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002528 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530
2531/*
2532 * Process an ABORT. (COOKIE-ECHOED state)
2533 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002534sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2535 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 const struct sctp_association *asoc,
2537 const sctp_subtype_t type,
2538 void *arg,
2539 sctp_cmd_seq_t *commands)
2540{
2541 /* There is a single T1 timer, so we should be able to use
2542 * common function with the COOKIE-WAIT state.
2543 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002544 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
2547/*
2548 * Stop T1 timer and abort association with "INIT failed".
2549 *
2550 * This is common code called by several sctp_sf_*_abort() functions above.
2551 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002552static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2553 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002554 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002555 const struct sctp_association *asoc,
2556 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002558 pr_debug("%s: ABORT received (INIT)\n", __func__);
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2561 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002562 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2564 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002565 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /* CMD_INIT_FAILED will DELETE_TCB. */
2567 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002568 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002569
Frank Filz3f7a87d2005-06-20 13:14:57 -07002570 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573/*
2574 * sctp_sf_do_9_2_shut
2575 *
2576 * Section: 9.2
2577 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2578 * - enter the SHUTDOWN-RECEIVED state,
2579 *
2580 * - stop accepting new data from its SCTP user
2581 *
2582 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2583 * that all its outstanding DATA chunks have been received by the
2584 * SHUTDOWN sender.
2585 *
2586 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2587 * send a SHUTDOWN in response to a ULP request. And should discard
2588 * subsequent SHUTDOWN chunks.
2589 *
2590 * If there are still outstanding DATA chunks left, the SHUTDOWN
2591 * receiver shall continue to follow normal data transmission
2592 * procedures defined in Section 6 until all outstanding DATA chunks
2593 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2594 * new data from its SCTP user.
2595 *
2596 * Verification Tag: 8.5 Verification Tag [Normal verification]
2597 *
2598 * Inputs
2599 * (endpoint, asoc, chunk)
2600 *
2601 * Outputs
2602 * (asoc, reply_msg, msg_up, timers, counters)
2603 *
2604 * The return value is the disposition of the chunk.
2605 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002606sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2607 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 const struct sctp_association *asoc,
2609 const sctp_subtype_t type,
2610 void *arg,
2611 sctp_cmd_seq_t *commands)
2612{
2613 struct sctp_chunk *chunk = arg;
2614 sctp_shutdownhdr_t *sdh;
2615 sctp_disposition_t disposition;
2616 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002617 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002620 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 /* Make sure that the SHUTDOWN chunk has a valid length. */
2623 if (!sctp_chunk_length_valid(chunk,
2624 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002625 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 commands);
2627
2628 /* Convert the elaborate header. */
2629 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2630 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2631 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002632 ctsn = ntohl(sdh->cum_tsn_ack);
2633
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002634 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002635 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2636 asoc->ctsn_ack_point);
2637
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002638 return SCTP_DISPOSITION_DISCARD;
2639 }
2640
Wei Yongjundf10eec2008-10-23 01:00:21 -07002641 /* If Cumulative TSN Ack beyond the max tsn currently
2642 * send, terminating the association and respond to the
2643 * sender with an ABORT.
2644 */
2645 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002646 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002648 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2649 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2650 * inform the application that it should cease sending data.
2651 */
2652 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2653 if (!ev) {
2654 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002655 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002656 }
2657 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2660 * - enter the SHUTDOWN-RECEIVED state,
2661 * - stop accepting new data from its SCTP user
2662 *
2663 * [This is implicit in the new state.]
2664 */
2665 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2666 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2667 disposition = SCTP_DISPOSITION_CONSUME;
2668
2669 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002670 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 arg, commands);
2672 }
2673
2674 if (SCTP_DISPOSITION_NOMEM == disposition)
2675 goto out;
2676
2677 /* - verify, by checking the Cumulative TSN Ack field of the
2678 * chunk, that all its outstanding DATA chunks have been
2679 * received by the SHUTDOWN sender.
2680 */
2681 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002682 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684out:
2685 return disposition;
2686}
2687
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002688/*
2689 * sctp_sf_do_9_2_shut_ctsn
2690 *
2691 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2692 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2693 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2694 * MUST be processed.
2695 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002696sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2697 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002698 const struct sctp_association *asoc,
2699 const sctp_subtype_t type,
2700 void *arg,
2701 sctp_cmd_seq_t *commands)
2702{
2703 struct sctp_chunk *chunk = arg;
2704 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002705 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002706
2707 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002708 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002709
2710 /* Make sure that the SHUTDOWN chunk has a valid length. */
2711 if (!sctp_chunk_length_valid(chunk,
2712 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002713 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002714 commands);
2715
2716 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002717 ctsn = ntohl(sdh->cum_tsn_ack);
2718
2719 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002720 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2721 asoc->ctsn_ack_point);
2722
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002723 return SCTP_DISPOSITION_DISCARD;
2724 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002725
2726 /* If Cumulative TSN Ack beyond the max tsn currently
2727 * send, terminating the association and respond to the
2728 * sender with an ABORT.
2729 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002730 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002731 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002732
2733 /* verify, by checking the Cumulative TSN Ack field of the
2734 * chunk, that all its outstanding DATA chunks have been
2735 * received by the SHUTDOWN sender.
2736 */
2737 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2738 SCTP_BE32(sdh->cum_tsn_ack));
2739
2740 return SCTP_DISPOSITION_CONSUME;
2741}
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/* RFC 2960 9.2
2744 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2745 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2746 * transport addresses (either in the IP addresses or in the INIT chunk)
2747 * that belong to this association, it should discard the INIT chunk and
2748 * retransmit the SHUTDOWN ACK chunk.
2749 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002750sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2751 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 const struct sctp_association *asoc,
2753 const sctp_subtype_t type,
2754 void *arg,
2755 sctp_cmd_seq_t *commands)
2756{
2757 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2758 struct sctp_chunk *reply;
2759
Vlad Yasevichece25df2007-09-07 16:30:54 -04002760 /* Make sure that the chunk has a valid length */
2761 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002762 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002763 commands);
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* Since we are not going to really process this INIT, there
2766 * is no point in verifying chunk boundries. Just generate
2767 * the SHUTDOWN ACK.
2768 */
2769 reply = sctp_make_shutdown_ack(asoc, chunk);
2770 if (NULL == reply)
2771 goto nomem;
2772
2773 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2774 * the T2-SHUTDOWN timer.
2775 */
2776 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2777
2778 /* and restart the T2-shutdown timer. */
2779 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2780 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2781
2782 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2783
2784 return SCTP_DISPOSITION_CONSUME;
2785nomem:
2786 return SCTP_DISPOSITION_NOMEM;
2787}
2788
2789/*
2790 * sctp_sf_do_ecn_cwr
2791 *
2792 * Section: Appendix A: Explicit Congestion Notification
2793 *
2794 * CWR:
2795 *
2796 * RFC 2481 details a specific bit for a sender to send in the header of
2797 * its next outbound TCP segment to indicate to its peer that it has
2798 * reduced its congestion window. This is termed the CWR bit. For
2799 * SCTP the same indication is made by including the CWR chunk.
2800 * This chunk contains one data element, i.e. the TSN number that
2801 * was sent in the ECNE chunk. This element represents the lowest
2802 * TSN number in the datagram that was originally marked with the
2803 * CE bit.
2804 *
2805 * Verification Tag: 8.5 Verification Tag [Normal verification]
2806 * Inputs
2807 * (endpoint, asoc, chunk)
2808 *
2809 * Outputs
2810 * (asoc, reply_msg, msg_up, timers, counters)
2811 *
2812 * The return value is the disposition of the chunk.
2813 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002814sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2815 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 const struct sctp_association *asoc,
2817 const sctp_subtype_t type,
2818 void *arg,
2819 sctp_cmd_seq_t *commands)
2820{
2821 sctp_cwrhdr_t *cwr;
2822 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002823 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002826 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002829 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2833 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2834
Al Viro34bcca22006-11-20 17:27:15 -08002835 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
2837 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002838 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /* Stop sending ECNE. */
2840 sctp_add_cmd_sf(commands,
2841 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002842 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
2844 return SCTP_DISPOSITION_CONSUME;
2845}
2846
2847/*
2848 * sctp_sf_do_ecne
2849 *
2850 * Section: Appendix A: Explicit Congestion Notification
2851 *
2852 * ECN-Echo
2853 *
2854 * RFC 2481 details a specific bit for a receiver to send back in its
2855 * TCP acknowledgements to notify the sender of the Congestion
2856 * Experienced (CE) bit having arrived from the network. For SCTP this
2857 * same indication is made by including the ECNE chunk. This chunk
2858 * contains one data element, i.e. the lowest TSN associated with the IP
2859 * datagram marked with the CE bit.....
2860 *
2861 * Verification Tag: 8.5 Verification Tag [Normal verification]
2862 * Inputs
2863 * (endpoint, asoc, chunk)
2864 *
2865 * Outputs
2866 * (asoc, reply_msg, msg_up, timers, counters)
2867 *
2868 * The return value is the disposition of the chunk.
2869 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002870sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2871 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 const struct sctp_association *asoc,
2873 const sctp_subtype_t type,
2874 void *arg,
2875 sctp_cmd_seq_t *commands)
2876{
2877 sctp_ecnehdr_t *ecne;
2878 struct sctp_chunk *chunk = arg;
2879
2880 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002881 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002884 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 commands);
2886
2887 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2888 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2889
2890 /* If this is a newer ECNE than the last CWR packet we sent out */
2891 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2892 SCTP_U32(ntohl(ecne->lowest_tsn)));
2893
2894 return SCTP_DISPOSITION_CONSUME;
2895}
2896
2897/*
2898 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2899 *
2900 * The SCTP endpoint MUST always acknowledge the reception of each valid
2901 * DATA chunk.
2902 *
2903 * The guidelines on delayed acknowledgement algorithm specified in
2904 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2905 * acknowledgement SHOULD be generated for at least every second packet
2906 * (not every second DATA chunk) received, and SHOULD be generated within
2907 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2908 * situations it may be beneficial for an SCTP transmitter to be more
2909 * conservative than the algorithms detailed in this document allow.
2910 * However, an SCTP transmitter MUST NOT be more aggressive than the
2911 * following algorithms allow.
2912 *
2913 * A SCTP receiver MUST NOT generate more than one SACK for every
2914 * incoming packet, other than to update the offered window as the
2915 * receiving application consumes new data.
2916 *
2917 * Verification Tag: 8.5 Verification Tag [Normal verification]
2918 *
2919 * Inputs
2920 * (endpoint, asoc, chunk)
2921 *
2922 * Outputs
2923 * (asoc, reply_msg, msg_up, timers, counters)
2924 *
2925 * The return value is the disposition of the chunk.
2926 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002927sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2928 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 const struct sctp_association *asoc,
2930 const sctp_subtype_t type,
2931 void *arg,
2932 sctp_cmd_seq_t *commands)
2933{
2934 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002935 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 int error;
2937
2938 if (!sctp_vtag_verify(chunk, asoc)) {
2939 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2940 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002941 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002945 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 commands);
2947
2948 error = sctp_eat_data(asoc, chunk, commands );
2949 switch (error) {
2950 case SCTP_IERROR_NO_ERROR:
2951 break;
2952 case SCTP_IERROR_HIGH_TSN:
2953 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002954 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 goto discard_noforce;
2956 case SCTP_IERROR_DUP_TSN:
2957 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002958 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 goto discard_force;
2960 case SCTP_IERROR_NO_DATA:
2961 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002962 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002963 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002964 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 default:
2966 BUG();
2967 }
2968
Wei Yongjun6dc76942009-11-23 15:53:53 -05002969 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2970 force = SCTP_FORCE();
2971
Neil Horman9f70f462013-12-10 06:48:15 -05002972 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2974 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2975 }
2976
2977 /* If this is the last chunk in a packet, we need to count it
2978 * toward sack generation. Note that we need to SACK every
2979 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2980 * THEM. We elect to NOT generate SACK's if the chunk fails
2981 * the verification tag test.
2982 *
2983 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2984 *
2985 * The SCTP endpoint MUST always acknowledge the reception of
2986 * each valid DATA chunk.
2987 *
2988 * The guidelines on delayed acknowledgement algorithm
2989 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2990 * Specifically, an acknowledgement SHOULD be generated for at
2991 * least every second packet (not every second DATA chunk)
2992 * received, and SHOULD be generated within 200 ms of the
2993 * arrival of any unacknowledged DATA chunk. In some
2994 * situations it may be beneficial for an SCTP transmitter to
2995 * be more conservative than the algorithms detailed in this
2996 * document allow. However, an SCTP transmitter MUST NOT be
2997 * more aggressive than the following algorithms allow.
2998 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002999 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003000 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return SCTP_DISPOSITION_CONSUME;
3003
3004discard_force:
3005 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3006 *
3007 * When a packet arrives with duplicate DATA chunk(s) and with
3008 * no new DATA chunk(s), the endpoint MUST immediately send a
3009 * SACK with no delay. If a packet arrives with duplicate
3010 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3011 * MAY immediately send a SACK. Normally receipt of duplicate
3012 * DATA chunks will occur when the original SACK chunk was lost
3013 * and the peer's RTO has expired. The duplicate TSN number(s)
3014 * SHOULD be reported in the SACK as duplicate.
3015 */
3016 /* In our case, we split the MAY SACK advice up whether or not
3017 * the last chunk is a duplicate.'
3018 */
3019 if (chunk->end_of_packet)
3020 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3021 return SCTP_DISPOSITION_DISCARD;
3022
3023discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003024 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003025 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return SCTP_DISPOSITION_DISCARD;
3028consume:
3029 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033/*
3034 * sctp_sf_eat_data_fast_4_4
3035 *
3036 * Section: 4 (4)
3037 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3038 * DATA chunks without delay.
3039 *
3040 * Verification Tag: 8.5 Verification Tag [Normal verification]
3041 * Inputs
3042 * (endpoint, asoc, chunk)
3043 *
3044 * Outputs
3045 * (asoc, reply_msg, msg_up, timers, counters)
3046 *
3047 * The return value is the disposition of the chunk.
3048 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003049sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3050 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 const struct sctp_association *asoc,
3052 const sctp_subtype_t type,
3053 void *arg,
3054 sctp_cmd_seq_t *commands)
3055{
3056 struct sctp_chunk *chunk = arg;
3057 int error;
3058
3059 if (!sctp_vtag_verify(chunk, asoc)) {
3060 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3061 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003062 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
3064
3065 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003066 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 commands);
3068
3069 error = sctp_eat_data(asoc, chunk, commands );
3070 switch (error) {
3071 case SCTP_IERROR_NO_ERROR:
3072 case SCTP_IERROR_HIGH_TSN:
3073 case SCTP_IERROR_DUP_TSN:
3074 case SCTP_IERROR_IGNORE_TSN:
3075 case SCTP_IERROR_BAD_STREAM:
3076 break;
3077 case SCTP_IERROR_NO_DATA:
3078 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003079 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003080 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003081 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 default:
3083 BUG();
3084 }
3085
3086 /* Go a head and force a SACK, since we are shutting down. */
3087
3088 /* Implementor's Guide.
3089 *
3090 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3091 * respond to each received packet containing one or more DATA chunk(s)
3092 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3093 */
3094 if (chunk->end_of_packet) {
3095 /* We must delay the chunk creation since the cumulative
3096 * TSN has not been updated yet.
3097 */
3098 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3099 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3100 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3101 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3102 }
3103
3104consume:
3105 return SCTP_DISPOSITION_CONSUME;
3106}
3107
3108/*
3109 * Section: 6.2 Processing a Received SACK
3110 * D) Any time a SACK arrives, the endpoint performs the following:
3111 *
3112 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3113 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3114 * increasing, a SACK whose Cumulative TSN Ack is less than the
3115 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3116 *
3117 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3118 * of bytes still outstanding after processing the Cumulative TSN Ack
3119 * and the Gap Ack Blocks.
3120 *
3121 * iii) If the SACK is missing a TSN that was previously
3122 * acknowledged via a Gap Ack Block (e.g., the data receiver
3123 * reneged on the data), then mark the corresponding DATA chunk
3124 * as available for retransmit: Mark it as missing for fast
3125 * retransmit as described in Section 7.2.4 and if no retransmit
3126 * timer is running for the destination address to which the DATA
3127 * chunk was originally transmitted, then T3-rtx is started for
3128 * that destination address.
3129 *
3130 * Verification Tag: 8.5 Verification Tag [Normal verification]
3131 *
3132 * Inputs
3133 * (endpoint, asoc, chunk)
3134 *
3135 * Outputs
3136 * (asoc, reply_msg, msg_up, timers, counters)
3137 *
3138 * The return value is the disposition of the chunk.
3139 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003140sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3141 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 const struct sctp_association *asoc,
3143 const sctp_subtype_t type,
3144 void *arg,
3145 sctp_cmd_seq_t *commands)
3146{
3147 struct sctp_chunk *chunk = arg;
3148 sctp_sackhdr_t *sackh;
3149 __u32 ctsn;
3150
3151 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003152 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
3154 /* Make sure that the SACK chunk has a valid length. */
3155 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003156 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 commands);
3158
3159 /* Pull the SACK chunk from the data buffer */
3160 sackh = sctp_sm_pull_sack(chunk);
3161 /* Was this a bogus SACK? */
3162 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003163 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 chunk->subh.sack_hdr = sackh;
3165 ctsn = ntohl(sackh->cum_tsn_ack);
3166
3167 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3168 * Ack Point, then drop the SACK. Since Cumulative TSN
3169 * Ack is monotonically increasing, a SACK whose
3170 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3171 * Point indicates an out-of-order SACK.
3172 */
3173 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003174 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3175 asoc->ctsn_ack_point);
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return SCTP_DISPOSITION_DISCARD;
3178 }
3179
Wei Yongjunaecedea2007-08-02 16:57:44 +08003180 /* If Cumulative TSN Ack beyond the max tsn currently
3181 * send, terminating the association and respond to the
3182 * sender with an ABORT.
3183 */
3184 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003185 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003188 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Note: We do the rest of the work on the PROCESS_SACK
3191 * sideeffect.
3192 */
3193 return SCTP_DISPOSITION_CONSUME;
3194}
3195
3196/*
3197 * Generate an ABORT in response to a packet.
3198 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003199 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003201 * 8) The receiver should respond to the sender of the OOTB packet with
3202 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3203 * MUST fill in the Verification Tag field of the outbound packet
3204 * with the value found in the Verification Tag field of the OOTB
3205 * packet and set the T-bit in the Chunk Flags to indicate that the
3206 * Verification Tag is reflected. After sending this ABORT, the
3207 * receiver of the OOTB packet shall discard the OOTB packet and take
3208 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 *
3210 * Verification Tag:
3211 *
3212 * The return value is the disposition of the chunk.
3213*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003214static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3215 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 const struct sctp_association *asoc,
3217 const sctp_subtype_t type,
3218 void *arg,
3219 sctp_cmd_seq_t *commands)
3220{
3221 struct sctp_packet *packet = NULL;
3222 struct sctp_chunk *chunk = arg;
3223 struct sctp_chunk *abort;
3224
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003225 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 if (packet) {
3228 /* Make an ABORT. The T bit will be set if the asoc
3229 * is NULL.
3230 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003231 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (!abort) {
3233 sctp_ootb_pkt_free(packet);
3234 return SCTP_DISPOSITION_NOMEM;
3235 }
3236
Jerome Forissier047a2422005-04-28 11:58:43 -07003237 /* Reflect vtag if T-Bit is set */
3238 if (sctp_test_T_bit(abort))
3239 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* Set the skb to the belonging sock for accounting. */
3242 abort->skb->sk = ep->base.sk;
3243
3244 sctp_packet_append_chunk(packet, abort);
3245
3246 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3247 SCTP_PACKET(packet));
3248
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003249 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003251 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return SCTP_DISPOSITION_CONSUME;
3253 }
3254
3255 return SCTP_DISPOSITION_NOMEM;
3256}
3257
3258/*
3259 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3260 * event as ULP notification for each cause included in the chunk.
3261 *
3262 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3263 *
3264 * The return value is the disposition of the chunk.
3265*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003266sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3267 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 const struct sctp_association *asoc,
3269 const sctp_subtype_t type,
3270 void *arg,
3271 sctp_cmd_seq_t *commands)
3272{
3273 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003274 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003277 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 /* Make sure that the ERROR chunk has a valid length. */
3280 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003281 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003283 sctp_walk_errors(err, chunk->chunk_hdr);
3284 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003285 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003286 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Wei Yongjun3df26782009-03-02 06:46:51 +00003288 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3289 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294/*
3295 * Process an inbound SHUTDOWN ACK.
3296 *
3297 * From Section 9.2:
3298 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3299 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3300 * peer, and remove all record of the association.
3301 *
3302 * The return value is the disposition.
3303 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003304sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3305 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 const struct sctp_association *asoc,
3307 const sctp_subtype_t type,
3308 void *arg,
3309 sctp_cmd_seq_t *commands)
3310{
3311 struct sctp_chunk *chunk = arg;
3312 struct sctp_chunk *reply;
3313 struct sctp_ulpevent *ev;
3314
3315 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3319 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003320 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /* 10.2 H) SHUTDOWN COMPLETE notification
3323 *
3324 * When SCTP completes the shutdown procedures (section 9.2) this
3325 * notification is passed to the upper layer.
3326 */
3327 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003328 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (!ev)
3330 goto nomem;
3331
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003332 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3333 reply = sctp_make_shutdown_complete(asoc, chunk);
3334 if (!reply)
3335 goto nomem_chunk;
3336
3337 /* Do all the commands now (after allocation), so that we
3338 * have consistent state if memory allocation failes
3339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3341
3342 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3343 * stop the T2-shutdown timer,
3344 */
3345 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3346 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3347
3348 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3349 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3352 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003353 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3354 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3356
3357 /* ...and remove all record of the association. */
3358 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3359 return SCTP_DISPOSITION_DELETE_TCB;
3360
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003361nomem_chunk:
3362 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363nomem:
3364 return SCTP_DISPOSITION_NOMEM;
3365}
3366
3367/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003368 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3371 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3372 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3373 * packet must fill in the Verification Tag field of the outbound
3374 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003375 * set the T-bit in the Chunk Flags to indicate that the Verification
3376 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 *
3378 * 8) The receiver should respond to the sender of the OOTB packet with
3379 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3380 * MUST fill in the Verification Tag field of the outbound packet
3381 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003382 * packet and set the T-bit in the Chunk Flags to indicate that the
3383 * Verification Tag is reflected. After sending this ABORT, the
3384 * receiver of the OOTB packet shall discard the OOTB packet and take
3385 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003387sctp_disposition_t sctp_sf_ootb(struct net *net,
3388 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 const struct sctp_association *asoc,
3390 const sctp_subtype_t type,
3391 void *arg,
3392 sctp_cmd_seq_t *commands)
3393{
3394 struct sctp_chunk *chunk = arg;
3395 struct sk_buff *skb = chunk->skb;
3396 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003397 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 __u8 *ch_end;
3399 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003400 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003402 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3405 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003406 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003408 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003409 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Vlad Yasevichece25df2007-09-07 16:30:54 -04003411 /* Now that we know we at least have a chunk header,
3412 * do things that are type appropriate.
3413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3415 ootb_shut_ack = 1;
3416
3417 /* RFC 2960, Section 3.3.7
3418 * Moreover, under any circumstances, an endpoint that
3419 * receives an ABORT MUST NOT respond to that ABORT by
3420 * sending an ABORT of its own.
3421 */
3422 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003423 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003424
Shan Wei85c5ed42011-04-19 21:30:01 +00003425 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3426 * or a COOKIE ACK the SCTP Packet should be silently
3427 * discarded.
3428 */
3429
3430 if (SCTP_CID_COOKIE_ACK == ch->type)
3431 ootb_cookie_ack = 1;
3432
3433 if (SCTP_CID_ERROR == ch->type) {
3434 sctp_walk_errors(err, ch) {
3435 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3436 ootb_cookie_ack = 1;
3437 break;
3438 }
3439 }
3440 }
3441
Vlad Yasevichece25df2007-09-07 16:30:54 -04003442 /* Report violation if chunk len overflows */
3443 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3444 if (ch_end > skb_tail_pointer(skb))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003445 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003446 commands);
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003449 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003452 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003453 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003454 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003456 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
3459/*
3460 * Handle an "Out of the blue" SHUTDOWN ACK.
3461 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003462 * Section: 8.4 5, sctpimpguide 2.41.
3463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003465 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3466 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3467 * packet must fill in the Verification Tag field of the outbound
3468 * packet with the Verification Tag received in the SHUTDOWN ACK and
3469 * set the T-bit in the Chunk Flags to indicate that the Verification
3470 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 *
3472 * Inputs
3473 * (endpoint, asoc, type, arg, commands)
3474 *
3475 * Outputs
3476 * (sctp_disposition_t)
3477 *
3478 * The return value is the disposition of the chunk.
3479 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003480static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3481 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 const struct sctp_association *asoc,
3483 const sctp_subtype_t type,
3484 void *arg,
3485 sctp_cmd_seq_t *commands)
3486{
3487 struct sctp_packet *packet = NULL;
3488 struct sctp_chunk *chunk = arg;
3489 struct sctp_chunk *shut;
3490
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003491 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 if (packet) {
3494 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003495 * The T bit will be set if the asoc is NULL.
3496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 shut = sctp_make_shutdown_complete(asoc, chunk);
3498 if (!shut) {
3499 sctp_ootb_pkt_free(packet);
3500 return SCTP_DISPOSITION_NOMEM;
3501 }
3502
Jerome Forissier047a2422005-04-28 11:58:43 -07003503 /* Reflect vtag if T-Bit is set */
3504 if (sctp_test_T_bit(shut))
3505 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 /* Set the skb to the belonging sock for accounting. */
3508 shut->skb->sk = ep->base.sk;
3509
3510 sctp_packet_append_chunk(packet, shut);
3511
3512 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3513 SCTP_PACKET(packet));
3514
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003515 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 /* If the chunk length is invalid, we don't want to process
3518 * the reset of the packet.
3519 */
3520 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003521 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003523 /* We need to discard the rest of the packet to prevent
3524 * potential bomming attacks from additional bundled chunks.
3525 * This is documented in SCTP Threats ID.
3526 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003527 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529
3530 return SCTP_DISPOSITION_NOMEM;
3531}
3532
3533/*
3534 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3535 *
3536 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3537 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3538 * procedures in section 8.4 SHOULD be followed, in other words it
3539 * should be treated as an Out Of The Blue packet.
3540 * [This means that we do NOT check the Verification Tag on these
3541 * chunks. --piggy ]
3542 *
3543 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003544sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3545 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 const struct sctp_association *asoc,
3547 const sctp_subtype_t type,
3548 void *arg,
3549 sctp_cmd_seq_t *commands)
3550{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003551 struct sctp_chunk *chunk = arg;
3552
3553 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3554 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003555 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003556 commands);
3557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /* Although we do have an association in this case, it corresponds
3559 * to a restarted association. So the packet is treated as an OOTB
3560 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3561 * called with a NULL association.
3562 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003563 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003564
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003565 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
3568/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003569sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3570 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 const struct sctp_association *asoc,
3572 const sctp_subtype_t type, void *arg,
3573 sctp_cmd_seq_t *commands)
3574{
3575 struct sctp_chunk *chunk = arg;
3576 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003577 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003579 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003581 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 if (!sctp_vtag_verify(chunk, asoc)) {
3584 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3585 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003586 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
3588
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003589 /* ADD-IP: Section 4.1.1
3590 * This chunk MUST be sent in an authenticated way by using
3591 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3592 * is received unauthenticated it MUST be silently discarded as
3593 * described in [I-D.ietf-tsvwg-sctp-auth].
3594 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003595 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003596 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3599 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003600 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 commands);
3602
3603 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3604 serial = ntohl(hdr->serial);
3605
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003606 addr_param = (union sctp_addr_param *)hdr->params;
3607 length = ntohs(addr_param->p.length);
3608 if (length < sizeof(sctp_paramhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003609 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003610 (void *)addr_param, commands);
3611
3612 /* Verify the ASCONF chunk before processing it. */
3613 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003614 (sctp_paramhdr_t *)((void *)addr_param + length),
3615 (void *)chunk->chunk_end,
3616 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003617 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003618 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003619
Vlad Yasevicha08de642007-12-20 14:11:47 -08003620 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003622 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003625 /* If this is the first instance of ASCONF in the packet,
3626 * we can clean our old ASCONF-ACKs.
3627 */
3628 if (!chunk->has_asconf)
3629 sctp_assoc_clean_asconf_ack_cache(asoc);
3630
3631 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3632 * expected, process the ASCONF as described below and after
3633 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3634 * the response packet and cache a copy of it (in the event it
3635 * later needs to be retransmitted).
3636 *
3637 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 */
3639 asconf_ack = sctp_process_asconf((struct sctp_association *)
3640 asoc, chunk);
3641 if (!asconf_ack)
3642 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003643 } else if (serial < asoc->peer.addip_serial + 1) {
3644 /* ADDIP 5.2 E2)
3645 * If the value found in the Sequence Number is less than the
3646 * ('Peer- Sequence-Number' + 1), simply skip to the next
3647 * ASCONF, and include in the outbound response packet
3648 * any previously cached ASCONF-ACK response that was
3649 * sent and saved that matches the Sequence Number of the
3650 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3651 * Chunk exists. This will occur when an older ASCONF
3652 * arrives out of order. In such a case, the receiver
3653 * should skip the ASCONF Chunk and not include ASCONF-ACK
3654 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003656 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3657 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003659
3660 /* Reset the transport so that we select the correct one
3661 * this time around. This is to make sure that we don't
3662 * accidentally use a stale transport that's been removed.
3663 */
3664 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003666 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return SCTP_DISPOSITION_DISCARD;
3670 }
3671
Vlad Yasevicha08de642007-12-20 14:11:47 -08003672 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3673 * containing the ASCONF-ACK Chunks MUST be the source address of
3674 * the SCTP packet that held the ASCONF Chunks.
3675 *
3676 * To do this properly, we'll set the destination address of the chunk
3677 * and at the transmit time, will try look up the transport to use.
3678 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003679 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003681 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003683 if (asoc->new_transport) {
3684 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3685 commands);
3686 ((struct sctp_association *)asoc)->new_transport = NULL;
3687 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return SCTP_DISPOSITION_CONSUME;
3690}
3691
3692/*
3693 * ADDIP Section 4.3 General rules for address manipulation
3694 * When building TLV parameters for the ASCONF Chunk that will add or
3695 * delete IP addresses the D0 to D13 rules should be applied:
3696 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003697sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3698 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003700 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 sctp_cmd_seq_t *commands)
3702{
3703 struct sctp_chunk *asconf_ack = arg;
3704 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3705 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003706 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 sctp_addiphdr_t *addip_hdr;
3708 __u32 sent_serial, rcvd_serial;
3709
3710 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3711 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3712 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003713 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
3715
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003716 /* ADD-IP, Section 4.1.2:
3717 * This chunk MUST be sent in an authenticated way by using
3718 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3719 * is received unauthenticated it MUST be silently discarded as
3720 * described in [I-D.ietf-tsvwg-sctp-auth].
3721 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003722 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003723 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 /* Make sure that the ADDIP chunk has a valid length. */
3726 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003727 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 commands);
3729
3730 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3731 rcvd_serial = ntohl(addip_hdr->serial);
3732
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003733 /* Verify the ASCONF-ACK chunk before processing it. */
3734 if (!sctp_verify_asconf(asoc,
3735 (sctp_paramhdr_t *)addip_hdr->params,
3736 (void *)asconf_ack->chunk_end,
3737 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003738 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003739 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (last_asconf) {
3742 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3743 sent_serial = ntohl(addip_hdr->serial);
3744 } else {
3745 sent_serial = asoc->addip_serial - 1;
3746 }
3747
3748 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3749 * equal to the next serial number to be used but no ASCONF chunk is
3750 * outstanding the endpoint MUST ABORT the association. Note that a
3751 * sequence number is greater than if it is no more than 2^^31-1
3752 * larger than the current sequence number (using serial arithmetic).
3753 */
3754 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3755 !(asoc->addip_last_asconf)) {
3756 abort = sctp_make_abort(asoc, asconf_ack,
3757 sizeof(sctp_errhdr_t));
3758 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003759 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3761 SCTP_CHUNK(abort));
3762 }
3763 /* We are going to ABORT, so we might as well stop
3764 * processing the rest of the chunks in the packet.
3765 */
3766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3768 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003769 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003770 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003772 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003773 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3774 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return SCTP_DISPOSITION_ABORT;
3776 }
3777
3778 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3779 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3780 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3781
3782 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003783 asconf_ack)) {
3784 /* Successfully processed ASCONF_ACK. We can
3785 * release the next asconf if we have one.
3786 */
3787 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3788 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 abort = sctp_make_abort(asoc, asconf_ack,
3793 sizeof(sctp_errhdr_t));
3794 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003795 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3797 SCTP_CHUNK(abort));
3798 }
3799 /* We are going to ABORT, so we might as well stop
3800 * processing the rest of the chunks in the packet.
3801 */
3802 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003803 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003804 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003806 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003807 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3808 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 return SCTP_DISPOSITION_ABORT;
3810 }
3811
3812 return SCTP_DISPOSITION_DISCARD;
3813}
3814
3815/*
3816 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3817 *
3818 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3819 * its cumulative TSN point to the value carried in the FORWARD TSN
3820 * chunk, and then MUST further advance its cumulative TSN point locally
3821 * if possible.
3822 * After the above processing, the data receiver MUST stop reporting any
3823 * missing TSNs earlier than or equal to the new cumulative TSN point.
3824 *
3825 * Verification Tag: 8.5 Verification Tag [Normal verification]
3826 *
3827 * The return value is the disposition of the chunk.
3828 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003829sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3830 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 const struct sctp_association *asoc,
3832 const sctp_subtype_t type,
3833 void *arg,
3834 sctp_cmd_seq_t *commands)
3835{
3836 struct sctp_chunk *chunk = arg;
3837 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003838 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 __u16 len;
3840 __u32 tsn;
3841
3842 if (!sctp_vtag_verify(chunk, asoc)) {
3843 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3844 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003845 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
3847
3848 /* Make sure that the FORWARD_TSN chunk has valid length. */
3849 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003850 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 commands);
3852
3853 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3854 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3855 len = ntohs(chunk->chunk_hdr->length);
3856 len -= sizeof(struct sctp_chunkhdr);
3857 skb_pull(chunk->skb, len);
3858
3859 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003860 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 /* The TSN is too high--silently discard the chunk and count on it
3863 * getting retransmitted later.
3864 */
3865 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3866 goto discard_noforce;
3867
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003868 /* Silently discard the chunk if stream-id is not valid */
3869 sctp_walk_fwdtsn(skip, chunk) {
3870 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3871 goto discard_noforce;
3872 }
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3875 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003876 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003880 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3882 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3883 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003886 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
3888 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 return SCTP_DISPOSITION_CONSUME;
3891
3892discard_noforce:
3893 return SCTP_DISPOSITION_DISCARD;
3894}
3895
3896sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003897 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 const struct sctp_endpoint *ep,
3899 const struct sctp_association *asoc,
3900 const sctp_subtype_t type,
3901 void *arg,
3902 sctp_cmd_seq_t *commands)
3903{
3904 struct sctp_chunk *chunk = arg;
3905 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003906 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 __u16 len;
3908 __u32 tsn;
3909
3910 if (!sctp_vtag_verify(chunk, asoc)) {
3911 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3912 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003913 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
3915
3916 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3917 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003918 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 commands);
3920
3921 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3922 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3923 len = ntohs(chunk->chunk_hdr->length);
3924 len -= sizeof(struct sctp_chunkhdr);
3925 skb_pull(chunk->skb, len);
3926
3927 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003928 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 /* The TSN is too high--silently discard the chunk and count on it
3931 * getting retransmitted later.
3932 */
3933 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3934 goto gen_shutdown;
3935
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003936 /* Silently discard the chunk if stream-id is not valid */
3937 sctp_walk_fwdtsn(skip, chunk) {
3938 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3939 goto gen_shutdown;
3940 }
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3943 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003944 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 /* Go a head and force a SACK, since we are shutting down. */
3948gen_shutdown:
3949 /* Implementor's Guide.
3950 *
3951 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3952 * respond to each received packet containing one or more DATA chunk(s)
3953 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3954 */
3955 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3956 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3957 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3958 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3959
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003960 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
3963/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003964 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003965 *
3966 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3967 * Identifier field. If this algorithm was not specified by the
3968 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3969 * during association setup, the AUTH chunk and all chunks after it MUST
3970 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3971 * defined in Section 4.1.
3972 *
3973 * If an endpoint with no shared key receives a Shared Key Identifier
3974 * other than 0, it MUST silently discard all authenticated chunks. If
3975 * the endpoint has at least one endpoint pair shared key for the peer,
3976 * it MUST use the key specified by the Shared Key Identifier if a
3977 * key has been configured for that Shared Key Identifier. If no
3978 * endpoint pair shared key has been configured for that Shared Key
3979 * Identifier, all authenticated chunks MUST be silently discarded.
3980 *
3981 * Verification Tag: 8.5 Verification Tag [Normal verification]
3982 *
3983 * The return value is the disposition of the chunk.
3984 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003985static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3986 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003987 const struct sctp_association *asoc,
3988 const sctp_subtype_t type,
3989 struct sctp_chunk *chunk)
3990{
3991 struct sctp_authhdr *auth_hdr;
3992 struct sctp_hmac *hmac;
3993 unsigned int sig_len;
3994 __u16 key_id;
3995 __u8 *save_digest;
3996 __u8 *digest;
3997
3998 /* Pull in the auth header, so we can do some more verification */
3999 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4000 chunk->subh.auth_hdr = auth_hdr;
4001 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4002
Masanari Iida02582e92012-08-22 19:11:26 +09004003 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004004 * chunk.
4005 */
4006 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4007 return SCTP_IERROR_AUTH_BAD_HMAC;
4008
4009 /* Make sure that the provided shared key identifier has been
4010 * configured
4011 */
4012 key_id = ntohs(auth_hdr->shkey_id);
4013 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4014 return SCTP_IERROR_AUTH_BAD_KEYID;
4015
4016
4017 /* Make sure that the length of the signature matches what
4018 * we expect.
4019 */
4020 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4021 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4022 if (sig_len != hmac->hmac_len)
4023 return SCTP_IERROR_PROTO_VIOLATION;
4024
4025 /* Now that we've done validation checks, we can compute and
4026 * verify the hmac. The steps involved are:
4027 * 1. Save the digest from the chunk.
4028 * 2. Zero out the digest in the chunk.
4029 * 3. Compute the new digest
4030 * 4. Compare saved and new digests.
4031 */
4032 digest = auth_hdr->hmac;
4033 skb_pull(chunk->skb, sig_len);
4034
4035 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4036 if (!save_digest)
4037 goto nomem;
4038
4039 memset(digest, 0, sig_len);
4040
4041 sctp_auth_calculate_hmac(asoc, chunk->skb,
4042 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4043 GFP_ATOMIC);
4044
4045 /* Discard the packet if the digests do not match */
4046 if (memcmp(save_digest, digest, sig_len)) {
4047 kfree(save_digest);
4048 return SCTP_IERROR_BAD_SIG;
4049 }
4050
4051 kfree(save_digest);
4052 chunk->auth = 1;
4053
4054 return SCTP_IERROR_NO_ERROR;
4055nomem:
4056 return SCTP_IERROR_NOMEM;
4057}
4058
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004059sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4060 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004061 const struct sctp_association *asoc,
4062 const sctp_subtype_t type,
4063 void *arg,
4064 sctp_cmd_seq_t *commands)
4065{
4066 struct sctp_authhdr *auth_hdr;
4067 struct sctp_chunk *chunk = arg;
4068 struct sctp_chunk *err_chunk;
4069 sctp_ierror_t error;
4070
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004071 /* Make sure that the peer has AUTH capable */
4072 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004073 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004074
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004075 if (!sctp_vtag_verify(chunk, asoc)) {
4076 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4077 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004078 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004079 }
4080
4081 /* Make sure that the AUTH chunk has valid length. */
4082 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004083 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004084 commands);
4085
4086 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004087 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004088 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004089 case SCTP_IERROR_AUTH_BAD_HMAC:
4090 /* Generate the ERROR chunk and discard the rest
4091 * of the packet
4092 */
4093 err_chunk = sctp_make_op_error(asoc, chunk,
4094 SCTP_ERROR_UNSUP_HMAC,
4095 &auth_hdr->hmac_id,
4096 sizeof(__u16), 0);
4097 if (err_chunk) {
4098 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4099 SCTP_CHUNK(err_chunk));
4100 }
4101 /* Fall Through */
4102 case SCTP_IERROR_AUTH_BAD_KEYID:
4103 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004104 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004105
4106 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004107 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004108 commands);
4109
4110 case SCTP_IERROR_NOMEM:
4111 return SCTP_DISPOSITION_NOMEM;
4112
4113 default: /* Prevent gcc warnings */
4114 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004115 }
4116
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004117 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4118 struct sctp_ulpevent *ev;
4119
4120 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4121 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4122
4123 if (!ev)
4124 return -ENOMEM;
4125
4126 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4127 SCTP_ULPEVENT(ev));
4128 }
4129
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004130 return SCTP_DISPOSITION_CONSUME;
4131}
4132
4133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 * Process an unknown chunk.
4135 *
4136 * Section: 3.2. Also, 2.1 in the implementor's guide.
4137 *
4138 * Chunk Types are encoded such that the highest-order two bits specify
4139 * the action that must be taken if the processing endpoint does not
4140 * recognize the Chunk Type.
4141 *
4142 * 00 - Stop processing this SCTP packet and discard it, do not process
4143 * any further chunks within it.
4144 *
4145 * 01 - Stop processing this SCTP packet and discard it, do not process
4146 * any further chunks within it, and report the unrecognized
4147 * chunk in an 'Unrecognized Chunk Type'.
4148 *
4149 * 10 - Skip this chunk and continue processing.
4150 *
4151 * 11 - Skip this chunk and continue processing, but report in an ERROR
4152 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4153 *
4154 * The return value is the disposition of the chunk.
4155 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004156sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4157 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 const struct sctp_association *asoc,
4159 const sctp_subtype_t type,
4160 void *arg,
4161 sctp_cmd_seq_t *commands)
4162{
4163 struct sctp_chunk *unk_chunk = arg;
4164 struct sctp_chunk *err_chunk;
4165 sctp_chunkhdr_t *hdr;
4166
Daniel Borkmannbb333812013-06-28 19:49:40 +02004167 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
4169 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 /* Make sure that the chunk has a valid length.
4173 * Since we don't know the chunk type, we use a general
4174 * chunkhdr structure to make a comparison.
4175 */
4176 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004177 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 commands);
4179
4180 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4181 case SCTP_CID_ACTION_DISCARD:
4182 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004183 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 break;
4185 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 /* Generate an ERROR chunk as response. */
4187 hdr = unk_chunk->chunk_hdr;
4188 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4189 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004190 WORD_ROUND(ntohs(hdr->length)),
4191 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (err_chunk) {
4193 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4194 SCTP_CHUNK(err_chunk));
4195 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004196
4197 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004198 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return SCTP_DISPOSITION_CONSUME;
4200 break;
4201 case SCTP_CID_ACTION_SKIP:
4202 /* Skip the chunk. */
4203 return SCTP_DISPOSITION_DISCARD;
4204 break;
4205 case SCTP_CID_ACTION_SKIP_ERR:
4206 /* Generate an ERROR chunk as response. */
4207 hdr = unk_chunk->chunk_hdr;
4208 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4209 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004210 WORD_ROUND(ntohs(hdr->length)),
4211 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 if (err_chunk) {
4213 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4214 SCTP_CHUNK(err_chunk));
4215 }
4216 /* Skip the chunk. */
4217 return SCTP_DISPOSITION_CONSUME;
4218 break;
4219 default:
4220 break;
4221 }
4222
4223 return SCTP_DISPOSITION_DISCARD;
4224}
4225
4226/*
4227 * Discard the chunk.
4228 *
4229 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4230 * [Too numerous to mention...]
4231 * Verification Tag: No verification needed.
4232 * Inputs
4233 * (endpoint, asoc, chunk)
4234 *
4235 * Outputs
4236 * (asoc, reply_msg, msg_up, timers, counters)
4237 *
4238 * The return value is the disposition of the chunk.
4239 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004240sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4241 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 const struct sctp_association *asoc,
4243 const sctp_subtype_t type,
4244 void *arg,
4245 sctp_cmd_seq_t *commands)
4246{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004247 struct sctp_chunk *chunk = arg;
4248
4249 /* Make sure that the chunk has a valid length.
4250 * Since we don't know the chunk type, we use a general
4251 * chunkhdr structure to make a comparison.
4252 */
4253 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004254 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004255 commands);
4256
Daniel Borkmannbb333812013-06-28 19:49:40 +02004257 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return SCTP_DISPOSITION_DISCARD;
4260}
4261
4262/*
4263 * Discard the whole packet.
4264 *
4265 * Section: 8.4 2)
4266 *
4267 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4268 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 *
4270 * Verification Tag: No verification necessary
4271 *
4272 * Inputs
4273 * (endpoint, asoc, chunk)
4274 *
4275 * Outputs
4276 * (asoc, reply_msg, msg_up, timers, counters)
4277 *
4278 * The return value is the disposition of the chunk.
4279 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004280sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4281 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 const struct sctp_association *asoc,
4283 const sctp_subtype_t type,
4284 void *arg,
4285 sctp_cmd_seq_t *commands)
4286{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004287 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4289
4290 return SCTP_DISPOSITION_CONSUME;
4291}
4292
4293
4294/*
4295 * The other end is violating protocol.
4296 *
4297 * Section: Not specified
4298 * Verification Tag: Not specified
4299 * Inputs
4300 * (endpoint, asoc, chunk)
4301 *
4302 * Outputs
4303 * (asoc, reply_msg, msg_up, timers, counters)
4304 *
4305 * We simply tag the chunk as a violation. The state machine will log
4306 * the violation and continue.
4307 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004308sctp_disposition_t sctp_sf_violation(struct net *net,
4309 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 const struct sctp_association *asoc,
4311 const sctp_subtype_t type,
4312 void *arg,
4313 sctp_cmd_seq_t *commands)
4314{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004315 struct sctp_chunk *chunk = arg;
4316
4317 /* Make sure that the chunk has a valid length. */
4318 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004319 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004320 commands);
4321
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 return SCTP_DISPOSITION_VIOLATION;
4323}
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004326 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004328static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004329 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004330 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004333 sctp_cmd_seq_t *commands,
4334 const __u8 *payload,
4335 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004337 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 struct sctp_chunk *chunk = arg;
4339 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004341 /* SCTP-AUTH, Section 6.3:
4342 * It should be noted that if the receiver wants to tear
4343 * down an association in an authenticated way only, the
4344 * handling of malformed packets should not result in
4345 * tearing down the association.
4346 *
4347 * This means that if we only want to abort associations
4348 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004349 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004350 * was malformed.
4351 */
4352 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4353 goto discard;
4354
Jesper Juhl9abed242007-11-11 23:57:49 +01004355 /* Make the abort chunk. */
4356 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4357 if (!abort)
4358 goto nomem;
4359
Vlad Yasevichece25df2007-09-07 16:30:54 -04004360 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004361 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4362 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4363 !asoc->peer.i.init_tag) {
4364 sctp_initack_chunk_t *initack;
4365
4366 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4367 if (!sctp_chunk_length_valid(chunk,
4368 sizeof(sctp_initack_chunk_t)))
4369 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4370 else {
4371 unsigned int inittag;
4372
4373 inittag = ntohl(initack->init_hdr.init_tag);
4374 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4375 SCTP_U32(inittag));
4376 }
4377 }
4378
Vlad Yasevichece25df2007-09-07 16:30:54 -04004379 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004380 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Vlad Yasevichece25df2007-09-07 16:30:54 -04004382 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4383 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4384 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4385 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4386 SCTP_ERROR(ECONNREFUSED));
4387 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4388 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4389 } else {
4390 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4391 SCTP_ERROR(ECONNABORTED));
4392 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4393 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004394 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004397 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004398
4399 if (!packet)
4400 goto nomem_pkt;
4401
4402 if (sctp_test_T_bit(abort))
4403 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4404
4405 abort->skb->sk = ep->base.sk;
4406
4407 sctp_packet_append_chunk(packet, abort);
4408
4409 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4410 SCTP_PACKET(packet));
4411
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004412 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 }
4414
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004415 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004416
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004417discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004418 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return SCTP_DISPOSITION_ABORT;
4420
Vlad Yasevichece25df2007-09-07 16:30:54 -04004421nomem_pkt:
4422 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423nomem:
4424 return SCTP_DISPOSITION_NOMEM;
4425}
4426
Wei Yongjunaecedea2007-08-02 16:57:44 +08004427/*
4428 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004429 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004430 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004431 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004432 *
4433 * We inform the other end by sending an ABORT with a Protocol Violation
4434 * error code.
4435 *
4436 * Section: Not specified
4437 * Verification Tag: Nothing to do
4438 * Inputs
4439 * (endpoint, asoc, chunk)
4440 *
4441 * Outputs
4442 * (reply_msg, msg_up, counters)
4443 *
4444 * Generate an ABORT chunk and terminate the association.
4445 */
4446static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004447 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004448 const struct sctp_endpoint *ep,
4449 const struct sctp_association *asoc,
4450 const sctp_subtype_t type,
4451 void *arg,
4452 sctp_cmd_seq_t *commands)
4453{
Florian Westphal24448442008-03-23 22:46:34 -07004454 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004455
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004456 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004457 sizeof(err_str));
4458}
4459
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004460/*
4461 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004462 * If the length is smaller than the minimum length of a given parameter,
4463 * or accumulated length in multi parameters exceeds the end of the chunk,
4464 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004465 */
4466static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004467 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004468 const struct sctp_endpoint *ep,
4469 const struct sctp_association *asoc,
4470 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004471 void *arg, void *ext,
4472 sctp_cmd_seq_t *commands)
4473{
4474 struct sctp_chunk *chunk = arg;
4475 struct sctp_paramhdr *param = ext;
4476 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004477
Wei Yongjunba016672008-09-30 05:32:24 -07004478 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4479 goto discard;
4480
4481 /* Make the abort chunk. */
4482 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4483 if (!abort)
4484 goto nomem;
4485
4486 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004487 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004488
4489 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4490 SCTP_ERROR(ECONNABORTED));
4491 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4492 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004493 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4494 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004495
4496discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004497 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004498 return SCTP_DISPOSITION_ABORT;
4499nomem:
4500 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004501}
4502
Wei Yongjunaecedea2007-08-02 16:57:44 +08004503/* Handle a protocol violation when the peer trying to advance the
4504 * cumulative tsn ack to a point beyond the max tsn currently sent.
4505 *
4506 * We inform the other end by sending an ABORT with a Protocol Violation
4507 * error code.
4508 */
4509static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004510 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004511 const struct sctp_endpoint *ep,
4512 const struct sctp_association *asoc,
4513 const sctp_subtype_t type,
4514 void *arg,
4515 sctp_cmd_seq_t *commands)
4516{
Florian Westphal24448442008-03-23 22:46:34 -07004517 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004518
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004519 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004520 sizeof(err_str));
4521}
4522
Vlad Yasevichece25df2007-09-07 16:30:54 -04004523/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004524 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004525 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004526 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004527 * on the path and we may not want to continue this communication.
4528 */
4529static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004530 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004531 const struct sctp_endpoint *ep,
4532 const struct sctp_association *asoc,
4533 const sctp_subtype_t type,
4534 void *arg,
4535 sctp_cmd_seq_t *commands)
4536{
Florian Westphal24448442008-03-23 22:46:34 -07004537 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004538
4539 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004540 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004541
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004542 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004543 sizeof(err_str));
4544}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545/***************************************************************************
4546 * These are the state functions for handling primitive (Section 10) events.
4547 ***************************************************************************/
4548/*
4549 * sctp_sf_do_prm_asoc
4550 *
4551 * Section: 10.1 ULP-to-SCTP
4552 * B) Associate
4553 *
4554 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4555 * outbound stream count)
4556 * -> association id [,destination transport addr list] [,outbound stream
4557 * count]
4558 *
4559 * This primitive allows the upper layer to initiate an association to a
4560 * specific peer endpoint.
4561 *
4562 * The peer endpoint shall be specified by one of the transport addresses
4563 * which defines the endpoint (see Section 1.4). If the local SCTP
4564 * instance has not been initialized, the ASSOCIATE is considered an
4565 * error.
4566 * [This is not relevant for the kernel implementation since we do all
4567 * initialization at boot time. It we hadn't initialized we wouldn't
4568 * get anywhere near this code.]
4569 *
4570 * An association id, which is a local handle to the SCTP association,
4571 * will be returned on successful establishment of the association. If
4572 * SCTP is not able to open an SCTP association with the peer endpoint,
4573 * an error is returned.
4574 * [In the kernel implementation, the struct sctp_association needs to
4575 * be created BEFORE causing this primitive to run.]
4576 *
4577 * Other association parameters may be returned, including the
4578 * complete destination transport addresses of the peer as well as the
4579 * outbound stream count of the local endpoint. One of the transport
4580 * address from the returned destination addresses will be selected by
4581 * the local endpoint as default primary path for sending SCTP packets
4582 * to this peer. The returned "destination transport addr list" can
4583 * be used by the ULP to change the default primary path or to force
4584 * sending a packet to a specific transport address. [All of this
4585 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4586 * function.]
4587 *
4588 * Mandatory attributes:
4589 *
4590 * o local SCTP instance name - obtained from the INITIALIZE operation.
4591 * [This is the argument asoc.]
4592 * o destination transport addr - specified as one of the transport
4593 * addresses of the peer endpoint with which the association is to be
4594 * established.
4595 * [This is asoc->peer.active_path.]
4596 * o outbound stream count - the number of outbound streams the ULP
4597 * would like to open towards this peer endpoint.
4598 * [BUG: This is not currently implemented.]
4599 * Optional attributes:
4600 *
4601 * None.
4602 *
4603 * The return value is a disposition.
4604 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004605sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4606 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 const struct sctp_association *asoc,
4608 const sctp_subtype_t type,
4609 void *arg,
4610 sctp_cmd_seq_t *commands)
4611{
4612 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004613 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
4615 /* The comment below says that we enter COOKIE-WAIT AFTER
4616 * sending the INIT, but that doesn't actually work in our
4617 * implementation...
4618 */
4619 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4620 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4621
4622 /* RFC 2960 5.1 Normal Establishment of an Association
4623 *
4624 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4625 * must provide its Verification Tag (Tag_A) in the Initiate
4626 * Tag field. Tag_A SHOULD be a random number in the range of
4627 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4628 */
4629
4630 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4631 if (!repl)
4632 goto nomem;
4633
Daniel Borkmann405426f2013-06-14 18:24:05 +02004634 /* Choose transport for INIT. */
4635 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4636 SCTP_CHUNK(repl));
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 /* Cast away the const modifier, as we want to just
4639 * rerun it through as a sideffect.
4640 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004641 my_asoc = (struct sctp_association *)asoc;
4642 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
4644 /* After sending the INIT, "A" starts the T1-init timer and
4645 * enters the COOKIE-WAIT state.
4646 */
4647 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4648 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4649 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4650 return SCTP_DISPOSITION_CONSUME;
4651
4652nomem:
4653 return SCTP_DISPOSITION_NOMEM;
4654}
4655
4656/*
4657 * Process the SEND primitive.
4658 *
4659 * Section: 10.1 ULP-to-SCTP
4660 * E) Send
4661 *
4662 * Format: SEND(association id, buffer address, byte count [,context]
4663 * [,stream id] [,life time] [,destination transport address]
4664 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4665 * -> result
4666 *
4667 * This is the main method to send user data via SCTP.
4668 *
4669 * Mandatory attributes:
4670 *
4671 * o association id - local handle to the SCTP association
4672 *
4673 * o buffer address - the location where the user message to be
4674 * transmitted is stored;
4675 *
4676 * o byte count - The size of the user data in number of bytes;
4677 *
4678 * Optional attributes:
4679 *
4680 * o context - an optional 32 bit integer that will be carried in the
4681 * sending failure notification to the ULP if the transportation of
4682 * this User Message fails.
4683 *
4684 * o stream id - to indicate which stream to send the data on. If not
4685 * specified, stream 0 will be used.
4686 *
4687 * o life time - specifies the life time of the user data. The user data
4688 * will not be sent by SCTP after the life time expires. This
4689 * parameter can be used to avoid efforts to transmit stale
4690 * user messages. SCTP notifies the ULP if the data cannot be
4691 * initiated to transport (i.e. sent to the destination via SCTP's
4692 * send primitive) within the life time variable. However, the
4693 * user data will be transmitted if SCTP has attempted to transmit a
4694 * chunk before the life time expired.
4695 *
4696 * o destination transport address - specified as one of the destination
4697 * transport addresses of the peer endpoint to which this packet
4698 * should be sent. Whenever possible, SCTP should use this destination
4699 * transport address for sending the packets, instead of the current
4700 * primary path.
4701 *
4702 * o unorder flag - this flag, if present, indicates that the user
4703 * would like the data delivered in an unordered fashion to the peer
4704 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4705 * message).
4706 *
4707 * o no-bundle flag - instructs SCTP not to bundle this user data with
4708 * other outbound DATA chunks. SCTP MAY still bundle even when
4709 * this flag is present, when faced with network congestion.
4710 *
4711 * o payload protocol-id - A 32 bit unsigned integer that is to be
4712 * passed to the peer indicating the type of payload protocol data
4713 * being transmitted. This value is passed as opaque data by SCTP.
4714 *
4715 * The return value is the disposition.
4716 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004717sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4718 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 const struct sctp_association *asoc,
4720 const sctp_subtype_t type,
4721 void *arg,
4722 sctp_cmd_seq_t *commands)
4723{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004724 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004726 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 return SCTP_DISPOSITION_CONSUME;
4728}
4729
4730/*
4731 * Process the SHUTDOWN primitive.
4732 *
4733 * Section: 10.1:
4734 * C) Shutdown
4735 *
4736 * Format: SHUTDOWN(association id)
4737 * -> result
4738 *
4739 * Gracefully closes an association. Any locally queued user data
4740 * will be delivered to the peer. The association will be terminated only
4741 * after the peer acknowledges all the SCTP packets sent. A success code
4742 * will be returned on successful termination of the association. If
4743 * attempting to terminate the association results in a failure, an error
4744 * code shall be returned.
4745 *
4746 * Mandatory attributes:
4747 *
4748 * o association id - local handle to the SCTP association
4749 *
4750 * Optional attributes:
4751 *
4752 * None.
4753 *
4754 * The return value is the disposition.
4755 */
4756sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004757 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 const struct sctp_endpoint *ep,
4759 const struct sctp_association *asoc,
4760 const sctp_subtype_t type,
4761 void *arg,
4762 sctp_cmd_seq_t *commands)
4763{
4764 int disposition;
4765
4766 /* From 9.2 Shutdown of an Association
4767 * Upon receipt of the SHUTDOWN primitive from its upper
4768 * layer, the endpoint enters SHUTDOWN-PENDING state and
4769 * remains there until all outstanding data has been
4770 * acknowledged by its peer. The endpoint accepts no new data
4771 * from its upper layer, but retransmits data to the far end
4772 * if necessary to fill gaps.
4773 */
4774 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4775 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4776
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 disposition = SCTP_DISPOSITION_CONSUME;
4778 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004779 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 arg, commands);
4781 }
4782 return disposition;
4783}
4784
4785/*
4786 * Process the ABORT primitive.
4787 *
4788 * Section: 10.1:
4789 * C) Abort
4790 *
4791 * Format: Abort(association id [, cause code])
4792 * -> result
4793 *
4794 * Ungracefully closes an association. Any locally queued user data
4795 * will be discarded and an ABORT chunk is sent to the peer. A success code
4796 * will be returned on successful abortion of the association. If
4797 * attempting to abort the association results in a failure, an error
4798 * code shall be returned.
4799 *
4800 * Mandatory attributes:
4801 *
4802 * o association id - local handle to the SCTP association
4803 *
4804 * Optional attributes:
4805 *
4806 * o cause code - reason of the abort to be passed to the peer
4807 *
4808 * None.
4809 *
4810 * The return value is the disposition.
4811 */
4812sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004813 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 const struct sctp_endpoint *ep,
4815 const struct sctp_association *asoc,
4816 const sctp_subtype_t type,
4817 void *arg,
4818 sctp_cmd_seq_t *commands)
4819{
4820 /* From 9.1 Abort of an Association
4821 * Upon receipt of the ABORT primitive from its upper
4822 * layer, the endpoint enters CLOSED state and
4823 * discard all outstanding data has been
4824 * acknowledged by its peer. The endpoint accepts no new data
4825 * from its upper layer, but retransmits data to the far end
4826 * if necessary to fill gaps.
4827 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004828 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 sctp_disposition_t retval;
4830
4831 retval = SCTP_DISPOSITION_CONSUME;
4832
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004833 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
4835 /* Even if we can't send the ABORT due to low memory delete the
4836 * TCB. This is a departure from our typical NOMEM handling.
4837 */
4838
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004839 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4840 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 /* Delete the established association. */
4842 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004843 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004845 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4846 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 return retval;
4849}
4850
4851/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004852sctp_disposition_t sctp_sf_error_closed(struct net *net,
4853 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 const struct sctp_association *asoc,
4855 const sctp_subtype_t type,
4856 void *arg,
4857 sctp_cmd_seq_t *commands)
4858{
4859 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4860 return SCTP_DISPOSITION_CONSUME;
4861}
4862
4863/* We tried an illegal operation on an association which is shutting
4864 * down.
4865 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004866sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4867 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 const struct sctp_association *asoc,
4869 const sctp_subtype_t type,
4870 void *arg,
4871 sctp_cmd_seq_t *commands)
4872{
4873 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4874 SCTP_ERROR(-ESHUTDOWN));
4875 return SCTP_DISPOSITION_CONSUME;
4876}
4877
4878/*
4879 * sctp_cookie_wait_prm_shutdown
4880 *
4881 * Section: 4 Note: 2
4882 * Verification Tag:
4883 * Inputs
4884 * (endpoint, asoc)
4885 *
4886 * The RFC does not explicitly address this issue, but is the route through the
4887 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4888 *
4889 * Outputs
4890 * (timers)
4891 */
4892sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004893 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 const struct sctp_endpoint *ep,
4895 const struct sctp_association *asoc,
4896 const sctp_subtype_t type,
4897 void *arg,
4898 sctp_cmd_seq_t *commands)
4899{
4900 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4901 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4902
4903 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4904 SCTP_STATE(SCTP_STATE_CLOSED));
4905
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004906 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4909
4910 return SCTP_DISPOSITION_DELETE_TCB;
4911}
4912
4913/*
4914 * sctp_cookie_echoed_prm_shutdown
4915 *
4916 * Section: 4 Note: 2
4917 * Verification Tag:
4918 * Inputs
4919 * (endpoint, asoc)
4920 *
4921 * The RFC does not explcitly address this issue, but is the route through the
4922 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4923 *
4924 * Outputs
4925 * (timers)
4926 */
4927sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004928 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 const struct sctp_endpoint *ep,
4930 const struct sctp_association *asoc,
4931 const sctp_subtype_t type,
4932 void *arg, sctp_cmd_seq_t *commands)
4933{
4934 /* There is a single T1 timer, so we should be able to use
4935 * common function with the COOKIE-WAIT state.
4936 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004937 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
4940/*
4941 * sctp_sf_cookie_wait_prm_abort
4942 *
4943 * Section: 4 Note: 2
4944 * Verification Tag:
4945 * Inputs
4946 * (endpoint, asoc)
4947 *
4948 * The RFC does not explicitly address this issue, but is the route through the
4949 * state table when someone issues an abort while in COOKIE_WAIT state.
4950 *
4951 * Outputs
4952 * (timers)
4953 */
4954sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004955 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 const struct sctp_endpoint *ep,
4957 const struct sctp_association *asoc,
4958 const sctp_subtype_t type,
4959 void *arg,
4960 sctp_cmd_seq_t *commands)
4961{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004962 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 sctp_disposition_t retval;
4964
4965 /* Stop T1-init timer */
4966 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4967 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4968 retval = SCTP_DISPOSITION_CONSUME;
4969
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004970 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971
4972 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4973 SCTP_STATE(SCTP_STATE_CLOSED));
4974
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004975 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976
4977 /* Even if we can't send the ABORT due to low memory delete the
4978 * TCB. This is a departure from our typical NOMEM handling.
4979 */
4980
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004981 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4982 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 /* Delete the established association. */
4984 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004985 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986
4987 return retval;
4988}
4989
4990/*
4991 * sctp_sf_cookie_echoed_prm_abort
4992 *
4993 * Section: 4 Note: 3
4994 * Verification Tag:
4995 * Inputs
4996 * (endpoint, asoc)
4997 *
4998 * The RFC does not explcitly address this issue, but is the route through the
4999 * state table when someone issues an abort while in COOKIE_ECHOED state.
5000 *
5001 * Outputs
5002 * (timers)
5003 */
5004sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005005 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 const struct sctp_endpoint *ep,
5007 const struct sctp_association *asoc,
5008 const sctp_subtype_t type,
5009 void *arg,
5010 sctp_cmd_seq_t *commands)
5011{
5012 /* There is a single T1 timer, so we should be able to use
5013 * common function with the COOKIE-WAIT state.
5014 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005015 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016}
5017
5018/*
5019 * sctp_sf_shutdown_pending_prm_abort
5020 *
5021 * Inputs
5022 * (endpoint, asoc)
5023 *
5024 * The RFC does not explicitly address this issue, but is the route through the
5025 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5026 *
5027 * Outputs
5028 * (timers)
5029 */
5030sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005031 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 const struct sctp_endpoint *ep,
5033 const struct sctp_association *asoc,
5034 const sctp_subtype_t type,
5035 void *arg,
5036 sctp_cmd_seq_t *commands)
5037{
5038 /* Stop the T5-shutdown guard timer. */
5039 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5040 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5041
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005042 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043}
5044
5045/*
5046 * sctp_sf_shutdown_sent_prm_abort
5047 *
5048 * Inputs
5049 * (endpoint, asoc)
5050 *
5051 * The RFC does not explicitly address this issue, but is the route through the
5052 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5053 *
5054 * Outputs
5055 * (timers)
5056 */
5057sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005058 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 const struct sctp_endpoint *ep,
5060 const struct sctp_association *asoc,
5061 const sctp_subtype_t type,
5062 void *arg,
5063 sctp_cmd_seq_t *commands)
5064{
5065 /* Stop the T2-shutdown timer. */
5066 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5067 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5068
5069 /* Stop the T5-shutdown guard timer. */
5070 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5071 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5072
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005073 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074}
5075
5076/*
5077 * sctp_sf_cookie_echoed_prm_abort
5078 *
5079 * Inputs
5080 * (endpoint, asoc)
5081 *
5082 * The RFC does not explcitly address this issue, but is the route through the
5083 * state table when someone issues an abort while in COOKIE_ECHOED state.
5084 *
5085 * Outputs
5086 * (timers)
5087 */
5088sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005089 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 const struct sctp_endpoint *ep,
5091 const struct sctp_association *asoc,
5092 const sctp_subtype_t type,
5093 void *arg,
5094 sctp_cmd_seq_t *commands)
5095{
5096 /* The same T2 timer, so we should be able to use
5097 * common function with the SHUTDOWN-SENT state.
5098 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005099 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100}
5101
5102/*
5103 * Process the REQUESTHEARTBEAT primitive
5104 *
5105 * 10.1 ULP-to-SCTP
5106 * J) Request Heartbeat
5107 *
5108 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5109 *
5110 * -> result
5111 *
5112 * Instructs the local endpoint to perform a HeartBeat on the specified
5113 * destination transport address of the given association. The returned
5114 * result should indicate whether the transmission of the HEARTBEAT
5115 * chunk to the destination address is successful.
5116 *
5117 * Mandatory attributes:
5118 *
5119 * o association id - local handle to the SCTP association
5120 *
5121 * o destination transport address - the transport address of the
5122 * association on which a heartbeat should be issued.
5123 */
5124sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005125 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 const struct sctp_endpoint *ep,
5127 const struct sctp_association *asoc,
5128 const sctp_subtype_t type,
5129 void *arg,
5130 sctp_cmd_seq_t *commands)
5131{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005132 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5133 (struct sctp_transport *)arg, commands))
5134 return SCTP_DISPOSITION_NOMEM;
5135
5136 /*
5137 * RFC 2960 (bis), section 8.3
5138 *
5139 * D) Request an on-demand HEARTBEAT on a specific destination
5140 * transport address of a given association.
5141 *
5142 * The endpoint should increment the respective error counter of
5143 * the destination transport address each time a HEARTBEAT is sent
5144 * to that address and not acknowledged within one RTO.
5145 *
5146 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005147 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005148 SCTP_TRANSPORT(arg));
5149 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150}
5151
5152/*
5153 * ADDIP Section 4.1 ASCONF Chunk Procedures
5154 * When an endpoint has an ASCONF signaled change to be sent to the
5155 * remote endpoint it should do A1 to A9
5156 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005157sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5158 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 const struct sctp_association *asoc,
5160 const sctp_subtype_t type,
5161 void *arg,
5162 sctp_cmd_seq_t *commands)
5163{
5164 struct sctp_chunk *chunk = arg;
5165
5166 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5167 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5168 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5169 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5170 return SCTP_DISPOSITION_CONSUME;
5171}
5172
5173/*
5174 * Ignore the primitive event
5175 *
5176 * The return value is the disposition of the primitive.
5177 */
5178sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005179 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 const struct sctp_endpoint *ep,
5181 const struct sctp_association *asoc,
5182 const sctp_subtype_t type,
5183 void *arg,
5184 sctp_cmd_seq_t *commands)
5185{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005186 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5187 type.primitive);
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 return SCTP_DISPOSITION_DISCARD;
5190}
5191
5192/***************************************************************************
5193 * These are the state functions for the OTHER events.
5194 ***************************************************************************/
5195
5196/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005197 * When the SCTP stack has no more user data to send or retransmit, this
5198 * notification is given to the user. Also, at the time when a user app
5199 * subscribes to this event, if there is no data to be sent or
5200 * retransmit, the stack will immediately send up this notification.
5201 */
5202sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005203 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005204 const struct sctp_endpoint *ep,
5205 const struct sctp_association *asoc,
5206 const sctp_subtype_t type,
5207 void *arg,
5208 sctp_cmd_seq_t *commands)
5209{
5210 struct sctp_ulpevent *event;
5211
5212 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5213 if (!event)
5214 return SCTP_DISPOSITION_NOMEM;
5215
5216 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5217
5218 return SCTP_DISPOSITION_CONSUME;
5219}
5220
5221/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 * Start the shutdown negotiation.
5223 *
5224 * From Section 9.2:
5225 * Once all its outstanding data has been acknowledged, the endpoint
5226 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5227 * TSN Ack field the last sequential TSN it has received from the peer.
5228 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5229 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5230 * with the updated last sequential TSN received from its peer.
5231 *
5232 * The return value is the disposition.
5233 */
5234sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005235 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 const struct sctp_endpoint *ep,
5237 const struct sctp_association *asoc,
5238 const sctp_subtype_t type,
5239 void *arg,
5240 sctp_cmd_seq_t *commands)
5241{
5242 struct sctp_chunk *reply;
5243
5244 /* Once all its outstanding data has been acknowledged, the
5245 * endpoint shall send a SHUTDOWN chunk to its peer including
5246 * in the Cumulative TSN Ack field the last sequential TSN it
5247 * has received from the peer.
5248 */
5249 reply = sctp_make_shutdown(asoc, NULL);
5250 if (!reply)
5251 goto nomem;
5252
5253 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5254 * T2-shutdown timer.
5255 */
5256 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5257
5258 /* It shall then start the T2-shutdown timer */
5259 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5260 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5261
Wei Yongjun536428a2008-09-05 08:55:26 +08005262 /* RFC 4960 Section 9.2
5263 * The sender of the SHUTDOWN MAY also start an overall guard timer
5264 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5265 */
Thomas Graff8d96052011-07-07 00:28:35 +00005266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005267 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5268
Neil Horman9f70f462013-12-10 06:48:15 -05005269 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5271 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5272
5273 /* and enter the SHUTDOWN-SENT state. */
5274 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5275 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5276
5277 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5278 *
5279 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005280 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 */
5282 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5283
5284 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5285
5286 return SCTP_DISPOSITION_CONSUME;
5287
5288nomem:
5289 return SCTP_DISPOSITION_NOMEM;
5290}
5291
5292/*
5293 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5294 *
5295 * From Section 9.2:
5296 *
5297 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5298 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5299 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5300 * endpoint must re-send the SHUTDOWN ACK.
5301 *
5302 * The return value is the disposition.
5303 */
5304sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005305 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 const struct sctp_endpoint *ep,
5307 const struct sctp_association *asoc,
5308 const sctp_subtype_t type,
5309 void *arg,
5310 sctp_cmd_seq_t *commands)
5311{
5312 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5313 struct sctp_chunk *reply;
5314
5315 /* There are 2 ways of getting here:
5316 * 1) called in response to a SHUTDOWN chunk
5317 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5318 *
5319 * For the case (2), the arg parameter is set to NULL. We need
5320 * to check that we have a chunk before accessing it's fields.
5321 */
5322 if (chunk) {
5323 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005324 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325
5326 /* Make sure that the SHUTDOWN chunk has a valid length. */
5327 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005328 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 commands);
5330 }
5331
5332 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5333 * shall send a SHUTDOWN ACK ...
5334 */
5335 reply = sctp_make_shutdown_ack(asoc, chunk);
5336 if (!reply)
5337 goto nomem;
5338
5339 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5340 * the T2-shutdown timer.
5341 */
5342 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5343
5344 /* and start/restart a T2-shutdown timer of its own, */
5345 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5346 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5347
Neil Horman9f70f462013-12-10 06:48:15 -05005348 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5350 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5351
5352 /* Enter the SHUTDOWN-ACK-SENT state. */
5353 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5354 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5355
5356 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5357 *
5358 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005359 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 */
5361 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5362
5363 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5364
5365 return SCTP_DISPOSITION_CONSUME;
5366
5367nomem:
5368 return SCTP_DISPOSITION_NOMEM;
5369}
5370
5371/*
5372 * Ignore the event defined as other
5373 *
5374 * The return value is the disposition of the event.
5375 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005376sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5377 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 const struct sctp_association *asoc,
5379 const sctp_subtype_t type,
5380 void *arg,
5381 sctp_cmd_seq_t *commands)
5382{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005383 pr_debug("%s: the event other type:%d is ignored\n",
5384 __func__, type.other);
5385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 return SCTP_DISPOSITION_DISCARD;
5387}
5388
5389/************************************************************
5390 * These are the state functions for handling timeout events.
5391 ************************************************************/
5392
5393/*
5394 * RTX Timeout
5395 *
5396 * Section: 6.3.3 Handle T3-rtx Expiration
5397 *
5398 * Whenever the retransmission timer T3-rtx expires for a destination
5399 * address, do the following:
5400 * [See below]
5401 *
5402 * The return value is the disposition of the chunk.
5403 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005404sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5405 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 const struct sctp_association *asoc,
5407 const sctp_subtype_t type,
5408 void *arg,
5409 sctp_cmd_seq_t *commands)
5410{
5411 struct sctp_transport *transport = arg;
5412
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005413 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005414
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 if (asoc->overall_error_count >= asoc->max_retrans) {
Thomas Graff8d96052011-07-07 00:28:35 +00005416 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5417 /*
5418 * We are here likely because the receiver had its rwnd
5419 * closed for a while and we have not been able to
5420 * transmit the locally queued data within the maximum
5421 * retransmission attempts limit. Start the T5
5422 * shutdown guard timer to give the receiver one last
5423 * chance and some additional time to recover before
5424 * aborting.
5425 */
5426 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5427 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5428 } else {
5429 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5430 SCTP_ERROR(ETIMEDOUT));
5431 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5432 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5433 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005434 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5435 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005436 return SCTP_DISPOSITION_DELETE_TCB;
5437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 }
5439
5440 /* E1) For the destination address for which the timer
5441 * expires, adjust its ssthresh with rules defined in Section
5442 * 7.2.3 and set the cwnd <- MTU.
5443 */
5444
5445 /* E2) For the destination address for which the timer
5446 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5447 * maximum value discussed in rule C7 above (RTO.max) may be
5448 * used to provide an upper bound to this doubling operation.
5449 */
5450
5451 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5452 * outstanding DATA chunks for the address for which the
5453 * T3-rtx has expired will fit into a single packet, subject
5454 * to the MTU constraint for the path corresponding to the
5455 * destination transport address to which the retransmission
5456 * is being sent (this may be different from the address for
5457 * which the timer expires [see Section 6.4]). Call this
5458 * value K. Bundle and retransmit those K DATA chunks in a
5459 * single packet to the destination endpoint.
5460 *
5461 * Note: Any DATA chunks that were sent to the address for
5462 * which the T3-rtx timer expired but did not fit in one MTU
5463 * (rule E3 above), should be marked for retransmission and
5464 * sent as soon as cwnd allows (normally when a SACK arrives).
5465 */
5466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 /* Do some failure management (Section 8.2). */
5468 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5469
Vlad Yasevich1845a572007-02-21 02:06:19 -08005470 /* NB: Rules E4 and F1 are implicit in R1. */
5471 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 return SCTP_DISPOSITION_CONSUME;
5474}
5475
5476/*
5477 * Generate delayed SACK on timeout
5478 *
5479 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5480 *
5481 * The guidelines on delayed acknowledgement algorithm specified in
5482 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5483 * acknowledgement SHOULD be generated for at least every second packet
5484 * (not every second DATA chunk) received, and SHOULD be generated
5485 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5486 * some situations it may be beneficial for an SCTP transmitter to be
5487 * more conservative than the algorithms detailed in this document
5488 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5489 * the following algorithms allow.
5490 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005491sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5492 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 const struct sctp_association *asoc,
5494 const sctp_subtype_t type,
5495 void *arg,
5496 sctp_cmd_seq_t *commands)
5497{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005498 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5500 return SCTP_DISPOSITION_CONSUME;
5501}
5502
5503/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005504 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 *
5506 * Section: 4 Note: 2
5507 * Verification Tag:
5508 * Inputs
5509 * (endpoint, asoc)
5510 *
5511 * RFC 2960 Section 4 Notes
5512 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5513 * and re-start the T1-init timer without changing state. This MUST
5514 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5515 * endpoint MUST abort the initialization process and report the
5516 * error to SCTP user.
5517 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005518 * Outputs
5519 * (timers, events)
5520 *
5521 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005522sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5523 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005524 const struct sctp_association *asoc,
5525 const sctp_subtype_t type,
5526 void *arg,
5527 sctp_cmd_seq_t *commands)
5528{
5529 struct sctp_chunk *repl = NULL;
5530 struct sctp_bind_addr *bp;
5531 int attempts = asoc->init_err_counter + 1;
5532
Daniel Borkmannbb333812013-06-28 19:49:40 +02005533 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5534
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 Yasevich81845c22006-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 {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005553 pr_debug("%s: giving up on INIT, attempts:%d "
5554 "max_init_attempts:%d\n", __func__, attempts,
5555 asoc->max_init_attempts);
5556
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005557 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5558 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005559 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005560 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005561 return SCTP_DISPOSITION_DELETE_TCB;
5562 }
5563
5564 return SCTP_DISPOSITION_CONSUME;
5565}
5566
5567/*
5568 * sctp_sf_t1_cookie_timer_expire
5569 *
5570 * Section: 4 Note: 2
5571 * Verification Tag:
5572 * Inputs
5573 * (endpoint, asoc)
5574 *
5575 * RFC 2960 Section 4 Notes
5576 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 * COOKIE ECHO and re-start the T1-cookie timer without changing
5578 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5579 * After that, the endpoint MUST abort the initialization process and
5580 * report the error to SCTP user.
5581 *
5582 * Outputs
5583 * (timers, events)
5584 *
5585 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005586sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5587 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 const struct sctp_association *asoc,
5589 const sctp_subtype_t type,
5590 void *arg,
5591 sctp_cmd_seq_t *commands)
5592{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005593 struct sctp_chunk *repl = NULL;
5594 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Daniel Borkmannbb333812013-06-28 19:49:40 +02005596 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5597
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005598 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599
Vlad Yasevich81845c22006-01-30 15:59:54 -08005600 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005601 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005603 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005605 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5606 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005608 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5609 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5612 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005613 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5614 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005616 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 return SCTP_DISPOSITION_DELETE_TCB;
5618 }
5619
5620 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621}
5622
5623/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5624 * with the updated last sequential TSN received from its peer.
5625 *
5626 * An endpoint should limit the number of retransmissions of the
5627 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5628 * If this threshold is exceeded the endpoint should destroy the TCB and
5629 * MUST report the peer endpoint unreachable to the upper layer (and
5630 * thus the association enters the CLOSED state). The reception of any
5631 * packet from its peer (i.e. as the peer sends all of its queued DATA
5632 * chunks) should clear the endpoint's retransmission count and restart
5633 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5634 * all of its queued DATA chunks that have not yet been sent.
5635 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005636sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5637 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 const struct sctp_association *asoc,
5639 const sctp_subtype_t type,
5640 void *arg,
5641 sctp_cmd_seq_t *commands)
5642{
5643 struct sctp_chunk *reply = NULL;
5644
Daniel Borkmannbb333812013-06-28 19:49:40 +02005645 pr_debug("%s: timer T2 expired\n", __func__);
5646
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005647 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005648
Neil Horman58fbbed2008-02-29 11:40:56 -08005649 ((struct sctp_association *)asoc)->shutdown_retries++;
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005652 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5653 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5655 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005656 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005657 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5658 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 return SCTP_DISPOSITION_DELETE_TCB;
5660 }
5661
5662 switch (asoc->state) {
5663 case SCTP_STATE_SHUTDOWN_SENT:
5664 reply = sctp_make_shutdown(asoc, NULL);
5665 break;
5666
5667 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5668 reply = sctp_make_shutdown_ack(asoc, NULL);
5669 break;
5670
5671 default:
5672 BUG();
5673 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675
5676 if (!reply)
5677 goto nomem;
5678
Wei Yongjun6345b192009-04-26 23:13:35 +08005679 /* Do some failure management (Section 8.2).
5680 * If we remove the transport an SHUTDOWN was last sent to, don't
5681 * do failure management.
5682 */
5683 if (asoc->shutdown_last_sent_to)
5684 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5685 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5688 * the T2-shutdown timer.
5689 */
5690 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5691
5692 /* Restart the T2-shutdown timer. */
5693 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5694 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5695 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5696 return SCTP_DISPOSITION_CONSUME;
5697
5698nomem:
5699 return SCTP_DISPOSITION_NOMEM;
5700}
5701
5702/*
5703 * ADDIP Section 4.1 ASCONF CHunk Procedures
5704 * If the T4 RTO timer expires the endpoint should do B1 to B5
5705 */
5706sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005707 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 const struct sctp_endpoint *ep,
5709 const struct sctp_association *asoc,
5710 const sctp_subtype_t type,
5711 void *arg,
5712 sctp_cmd_seq_t *commands)
5713{
5714 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5715 struct sctp_transport *transport = chunk->transport;
5716
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005717 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005718
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5720 * detection on the appropriate destination address as defined in
5721 * RFC2960 [5] section 8.1 and 8.2.
5722 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005723 if (transport)
5724 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5725 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
5727 /* Reconfig T4 timer and transport. */
5728 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5729
5730 /* ADDIP 4.1 B2) Increment the association error counters and perform
5731 * endpoint failure detection on the association as defined in
5732 * RFC2960 [5] section 8.1 and 8.2.
5733 * association error counter is incremented in SCTP_CMD_STRIKE.
5734 */
5735 if (asoc->overall_error_count >= asoc->max_retrans) {
5736 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5737 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005738 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5739 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005741 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005742 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5743 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 return SCTP_DISPOSITION_ABORT;
5745 }
5746
5747 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5748 * the ASCONF chunk was sent by doubling the RTO timer value.
5749 * This is done in SCTP_CMD_STRIKE.
5750 */
5751
5752 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5753 * choose an alternate destination address (please refer to RFC2960
5754 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005755 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 * ASCONF sent.
5757 */
5758 sctp_chunk_hold(asoc->addip_last_asconf);
5759 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5760 SCTP_CHUNK(asoc->addip_last_asconf));
5761
5762 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5763 * destination is selected, then the RTO used will be that of the new
5764 * destination address.
5765 */
5766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5768
5769 return SCTP_DISPOSITION_CONSUME;
5770}
5771
5772/* sctpimpguide-05 Section 2.12.2
5773 * The sender of the SHUTDOWN MAY also start an overall guard timer
5774 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5775 * At the expiration of this timer the sender SHOULD abort the association
5776 * by sending an ABORT chunk.
5777 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005778sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5779 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 const struct sctp_association *asoc,
5781 const sctp_subtype_t type,
5782 void *arg,
5783 sctp_cmd_seq_t *commands)
5784{
5785 struct sctp_chunk *reply = NULL;
5786
Daniel Borkmannbb333812013-06-28 19:49:40 +02005787 pr_debug("%s: timer T5 expired\n", __func__);
5788
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005789 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
5791 reply = sctp_make_abort(asoc, NULL, 0);
5792 if (!reply)
5793 goto nomem;
5794
5795 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005796 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5797 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005799 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005801 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5802 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005803
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 return SCTP_DISPOSITION_DELETE_TCB;
5805nomem:
5806 return SCTP_DISPOSITION_NOMEM;
5807}
5808
5809/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5810 * the association is automatically closed by starting the shutdown process.
5811 * The work that needs to be done is same as when SHUTDOWN is initiated by
5812 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5813 */
5814sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005815 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 const struct sctp_endpoint *ep,
5817 const struct sctp_association *asoc,
5818 const sctp_subtype_t type,
5819 void *arg,
5820 sctp_cmd_seq_t *commands)
5821{
5822 int disposition;
5823
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005824 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005825
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 /* From 9.2 Shutdown of an Association
5827 * Upon receipt of the SHUTDOWN primitive from its upper
5828 * layer, the endpoint enters SHUTDOWN-PENDING state and
5829 * remains there until all outstanding data has been
5830 * acknowledged by its peer. The endpoint accepts no new data
5831 * from its upper layer, but retransmits data to the far end
5832 * if necessary to fill gaps.
5833 */
5834 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5835 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5836
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 disposition = SCTP_DISPOSITION_CONSUME;
5838 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005839 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840 arg, commands);
5841 }
5842 return disposition;
5843}
5844
5845/*****************************************************************************
5846 * These are sa state functions which could apply to all types of events.
5847 ****************************************************************************/
5848
5849/*
5850 * This table entry is not implemented.
5851 *
5852 * Inputs
5853 * (endpoint, asoc, chunk)
5854 *
5855 * The return value is the disposition of the chunk.
5856 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005857sctp_disposition_t sctp_sf_not_impl(struct net *net,
5858 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 const struct sctp_association *asoc,
5860 const sctp_subtype_t type,
5861 void *arg,
5862 sctp_cmd_seq_t *commands)
5863{
5864 return SCTP_DISPOSITION_NOT_IMPL;
5865}
5866
5867/*
5868 * This table entry represents a bug.
5869 *
5870 * Inputs
5871 * (endpoint, asoc, chunk)
5872 *
5873 * The return value is the disposition of the chunk.
5874 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005875sctp_disposition_t sctp_sf_bug(struct net *net,
5876 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 const struct sctp_association *asoc,
5878 const sctp_subtype_t type,
5879 void *arg,
5880 sctp_cmd_seq_t *commands)
5881{
5882 return SCTP_DISPOSITION_BUG;
5883}
5884
5885/*
5886 * This table entry represents the firing of a timer in the wrong state.
5887 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5888 * when the association is in the wrong state. This event should
5889 * be ignored, so as to prevent any rearming of the timer.
5890 *
5891 * Inputs
5892 * (endpoint, asoc, chunk)
5893 *
5894 * The return value is the disposition of the chunk.
5895 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005896sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5897 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 const struct sctp_association *asoc,
5899 const sctp_subtype_t type,
5900 void *arg,
5901 sctp_cmd_seq_t *commands)
5902{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005903 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5904
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 return SCTP_DISPOSITION_CONSUME;
5906}
5907
5908/********************************************************************
5909 * 2nd Level Abstractions
5910 ********************************************************************/
5911
5912/* Pull the SACK chunk based on the SACK header. */
5913static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5914{
5915 struct sctp_sackhdr *sack;
5916 unsigned int len;
5917 __u16 num_blocks;
5918 __u16 num_dup_tsns;
5919
5920 /* Protect ourselves from reading too far into
5921 * the skb from a bogus sender.
5922 */
5923 sack = (struct sctp_sackhdr *) chunk->skb->data;
5924
5925 num_blocks = ntohs(sack->num_gap_ack_blocks);
5926 num_dup_tsns = ntohs(sack->num_dup_tsns);
5927 len = sizeof(struct sctp_sackhdr);
5928 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5929 if (len > chunk->skb->len)
5930 return NULL;
5931
5932 skb_pull(chunk->skb, len);
5933
5934 return sack;
5935}
5936
5937/* Create an ABORT packet to be sent as a response, with the specified
5938 * error causes.
5939 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005940static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5941 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 const struct sctp_association *asoc,
5943 struct sctp_chunk *chunk,
5944 const void *payload,
5945 size_t paylen)
5946{
5947 struct sctp_packet *packet;
5948 struct sctp_chunk *abort;
5949
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005950 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
5952 if (packet) {
5953 /* Make an ABORT.
5954 * The T bit will be set if the asoc is NULL.
5955 */
5956 abort = sctp_make_abort(asoc, chunk, paylen);
5957 if (!abort) {
5958 sctp_ootb_pkt_free(packet);
5959 return NULL;
5960 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005961
5962 /* Reflect vtag if T-Bit is set */
5963 if (sctp_test_T_bit(abort))
5964 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 /* Add specified error causes, i.e., payload, to the
5967 * end of the chunk.
5968 */
5969 sctp_addto_chunk(abort, paylen, payload);
5970
5971 /* Set the skb to the belonging sock for accounting. */
5972 abort->skb->sk = ep->base.sk;
5973
5974 sctp_packet_append_chunk(packet, abort);
5975
5976 }
5977
5978 return packet;
5979}
5980
5981/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005982static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5983 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 const struct sctp_chunk *chunk)
5985{
5986 struct sctp_packet *packet;
5987 struct sctp_transport *transport;
5988 __u16 sport;
5989 __u16 dport;
5990 __u32 vtag;
5991
5992 /* Get the source and destination port from the inbound packet. */
5993 sport = ntohs(chunk->sctp_hdr->dest);
5994 dport = ntohs(chunk->sctp_hdr->source);
5995
5996 /* The V-tag is going to be the same as the inbound packet if no
5997 * association exists, otherwise, use the peer's vtag.
5998 */
5999 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006000 /* Special case the INIT-ACK as there is no peer's vtag
6001 * yet.
6002 */
6003 switch(chunk->chunk_hdr->type) {
6004 case SCTP_CID_INIT_ACK:
6005 {
6006 sctp_initack_chunk_t *initack;
6007
6008 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6009 vtag = ntohl(initack->init_hdr.init_tag);
6010 break;
6011 }
6012 default:
6013 vtag = asoc->peer.i.init_tag;
6014 break;
6015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 } else {
6017 /* Special case the INIT and stale COOKIE_ECHO as there is no
6018 * vtag yet.
6019 */
6020 switch(chunk->chunk_hdr->type) {
6021 case SCTP_CID_INIT:
6022 {
6023 sctp_init_chunk_t *init;
6024
6025 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6026 vtag = ntohl(init->init_hdr.init_tag);
6027 break;
6028 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006029 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 vtag = ntohl(chunk->sctp_hdr->vtag);
6031 break;
6032 }
6033 }
6034
6035 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006036 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 if (!transport)
6038 goto nomem;
6039
6040 /* Cache a route for the transport with the chunk's destination as
6041 * the source address.
6042 */
Al Viro16b0a032006-11-20 17:13:38 -08006043 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006044 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045
6046 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6047 packet = sctp_packet_config(packet, vtag, 0);
6048
6049 return packet;
6050
6051nomem:
6052 return NULL;
6053}
6054
6055/* Free the packet allocated earlier for responding in the OOTB condition. */
6056void sctp_ootb_pkt_free(struct sctp_packet *packet)
6057{
6058 sctp_transport_free(packet->transport);
6059}
6060
6061/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006062static void sctp_send_stale_cookie_err(struct net *net,
6063 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 const struct sctp_association *asoc,
6065 const struct sctp_chunk *chunk,
6066 sctp_cmd_seq_t *commands,
6067 struct sctp_chunk *err_chunk)
6068{
6069 struct sctp_packet *packet;
6070
6071 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006072 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 if (packet) {
6074 struct sctp_signed_cookie *cookie;
6075
6076 /* Override the OOTB vtag from the cookie. */
6077 cookie = chunk->subh.cookie_hdr;
6078 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006079
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 /* Set the skb to the belonging sock for accounting. */
6081 err_chunk->skb->sk = ep->base.sk;
6082 sctp_packet_append_chunk(packet, err_chunk);
6083 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6084 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006085 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 } else
6087 sctp_chunk_free (err_chunk);
6088 }
6089}
6090
6091
6092/* Process a data chunk */
6093static int sctp_eat_data(const struct sctp_association *asoc,
6094 struct sctp_chunk *chunk,
6095 sctp_cmd_seq_t *commands)
6096{
6097 sctp_datahdr_t *data_hdr;
6098 struct sctp_chunk *err;
6099 size_t datalen;
6100 sctp_verb_t deliver;
6101 int tmp;
6102 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006103 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006104 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006105 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006106 u16 ssn;
6107 u16 sid;
6108 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
6110 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6111 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6112
6113 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006114 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 /* ASSERT: Now skb->data is really the user data. */
6117
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 /* Process ECN based congestion.
6119 *
6120 * Since the chunk structure is reused for all chunks within
6121 * a packet, we use ecn_ce_done to track if we've already
6122 * done CE processing for this packet.
6123 *
6124 * We need to do ECN processing even if we plan to discard the
6125 * chunk later.
6126 */
6127
6128 if (!chunk->ecn_ce_done) {
6129 struct sctp_af *af;
6130 chunk->ecn_ce_done = 1;
6131
6132 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006133 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134
6135 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6136 /* Do real work as sideffect. */
6137 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6138 SCTP_U32(tsn));
6139 }
6140 }
6141
6142 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6143 if (tmp < 0) {
6144 /* The TSN is too high--silently discard the chunk and
6145 * count on it getting retransmitted later.
6146 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006147 if (chunk->asoc)
6148 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 return SCTP_IERROR_HIGH_TSN;
6150 } else if (tmp > 0) {
6151 /* This is a duplicate. Record it. */
6152 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6153 return SCTP_IERROR_DUP_TSN;
6154 }
6155
6156 /* This is a new TSN. */
6157
6158 /* Discard if there is no room in the receive window.
6159 * Actually, allow a little bit of overflow (up to a MTU).
6160 */
6161 datalen = ntohs(chunk->chunk_hdr->length);
6162 datalen -= sizeof(sctp_data_chunk_t);
6163
6164 deliver = SCTP_CMD_CHUNK_ULP;
6165
6166 /* Think about partial delivery. */
6167 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6168
6169 /* Even if we don't accept this chunk there is
6170 * memory pressure.
6171 */
6172 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6173 }
6174
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006175 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 * seems a bit troublesome in that frag_point varies based on
6177 * PMTU. In cases, such as loopback, this might be a rather
6178 * large spill over.
6179 */
Neil Horman4d93df02007-08-15 16:07:44 -07006180 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6181 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
6183 /* If this is the next TSN, consider reneging to make
6184 * room. Note: Playing nice with a confused sender. A
6185 * malicious sender can still eat up all our buffer
6186 * space and in the future we may want to detect and
6187 * do more drastic reneging.
6188 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006189 if (sctp_tsnmap_has_gap(map) &&
6190 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006191 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 deliver = SCTP_CMD_RENEGE;
6193 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006194 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6195 __func__, tsn, datalen, asoc->rwnd);
6196
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 return SCTP_IERROR_IGNORE_TSN;
6198 }
6199 }
6200
6201 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006202 * Also try to renege to limit our memory usage in the event that
6203 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006204 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006205 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6206 * memory usage too much
6207 */
6208 if (*sk->sk_prot_creator->memory_pressure) {
6209 if (sctp_tsnmap_has_gap(map) &&
6210 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006211 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6212 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006213 deliver = SCTP_CMD_RENEGE;
6214 }
6215 }
6216
6217 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 * Section 3.3.10.9 No User Data (9)
6219 *
6220 * Cause of error
6221 * ---------------
6222 * No User Data: This error cause is returned to the originator of a
6223 * DATA chunk if a received DATA chunk has no user data.
6224 */
6225 if (unlikely(0 == datalen)) {
6226 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6227 if (err) {
6228 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6229 SCTP_CHUNK(err));
6230 }
6231 /* We are going to ABORT, so we might as well stop
6232 * processing the rest of the chunks in the packet.
6233 */
6234 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006235 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6236 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006238 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006239 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6240 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 return SCTP_IERROR_NO_DATA;
6242 }
6243
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006244 chunk->data_accepted = 1;
6245
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6247 * if we renege and the chunk arrives again.
6248 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006249 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006250 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006251 if (chunk->asoc)
6252 chunk->asoc->stats.iuodchunks++;
6253 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006254 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006255 if (chunk->asoc)
6256 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006257 ordered = 1;
6258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259
6260 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6261 *
6262 * If an endpoint receive a DATA chunk with an invalid stream
6263 * identifier, it shall acknowledge the reception of the DATA chunk
6264 * following the normal procedure, immediately send an ERROR chunk
6265 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6266 * and discard the DATA chunk.
6267 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006268 sid = ntohs(data_hdr->stream);
6269 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006270 /* Mark tsn as received even though we drop it */
6271 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6272
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6274 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006275 sizeof(data_hdr->stream),
6276 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 if (err)
6278 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6279 SCTP_CHUNK(err));
6280 return SCTP_IERROR_BAD_STREAM;
6281 }
6282
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006283 /* Check to see if the SSN is possible for this TSN.
6284 * The biggest gap we can record is 4K wide. Since SSNs wrap
6285 * at an unsigned short, there is no way that an SSN can
6286 * wrap and for a valid TSN. We can simply check if the current
6287 * SSN is smaller then the next expected one. If it is, it wrapped
6288 * and is invalid.
6289 */
6290 ssn = ntohs(data_hdr->ssn);
6291 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6292 return SCTP_IERROR_PROTO_VIOLATION;
6293 }
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 /* Send the data up to the user. Note: Schedule the
6296 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6297 * chunk needs the updated rwnd.
6298 */
6299 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6300
6301 return SCTP_IERROR_NO_ERROR;
6302}