blob: ae65b6b5973a9bceca7825037317f9fc9f39e2db [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
Daniel Borkmannec0223e2014-03-03 17:23:04 +0100761 /* Make sure that we and the peer are AUTH capable */
762 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
763 kfree_skb(chunk->auth_chunk);
764 sctp_association_free(new_asoc);
765 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
766 }
767
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700768 /* set-up our fake chunk so that we can process it */
769 auth.skb = chunk->auth_chunk;
770 auth.asoc = chunk->asoc;
771 auth.sctp_hdr = chunk->sctp_hdr;
772 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
773 sizeof(sctp_chunkhdr_t));
774 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
775 auth.transport = chunk->transport;
776
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000777 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700778
779 /* We can now safely free the auth_chunk clone */
780 kfree_skb(chunk->auth_chunk);
781
782 if (ret != SCTP_IERROR_NO_ERROR) {
783 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000784 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700785 }
786 }
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 repl = sctp_make_cookie_ack(new_asoc, chunk);
789 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700790 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* RFC 2960 5.1 Normal Establishment of an Association
793 *
794 * D) IMPLEMENTATION NOTE: An implementation may choose to
795 * send the Communication Up notification to the SCTP user
796 * upon reception of a valid COOKIE ECHO chunk.
797 */
798 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
799 new_asoc->c.sinit_num_ostreams,
800 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700801 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!ev)
803 goto nomem_ev;
804
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900805 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800806 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800808 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800810 if (new_asoc->peer.adaptation_ind) {
811 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700813 if (!ai_ev)
814 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700817 /* Add all the state machine commands now since we've created
818 * everything. This way we don't introduce memory corruptions
819 * during side-effect processing and correclty count established
820 * associations.
821 */
822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
823 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
824 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000825 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
826 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700827 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
828
Neil Horman9f70f462013-12-10 06:48:15 -0500829 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700830 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
831 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
832
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700833 /* This will send the COOKIE ACK */
834 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
835
836 /* Queue the ASSOC_CHANGE event */
837 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
838
839 /* Send up the Adaptation Layer Indication event */
840 if (ai_ev)
841 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
842 SCTP_ULPEVENT(ai_ev));
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return SCTP_DISPOSITION_CONSUME;
845
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700846nomem_aiev:
847 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848nomem_ev:
849 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850nomem_init:
851 sctp_association_free(new_asoc);
852nomem:
853 return SCTP_DISPOSITION_NOMEM;
854}
855
856/*
857 * Respond to a normal COOKIE ACK chunk.
858 * We are the side that is being asked for an association.
859 *
860 * RFC 2960 5.1 Normal Establishment of an Association
861 *
862 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
863 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
864 * timer. It may also notify its ULP about the successful
865 * establishment of the association with a Communication Up
866 * notification (see Section 10).
867 *
868 * Verification Tag:
869 * Inputs
870 * (endpoint, asoc, chunk)
871 *
872 * Outputs
873 * (asoc, reply_msg, msg_up, timers, counters)
874 *
875 * The return value is the disposition of the chunk.
876 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000877sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
878 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 const struct sctp_association *asoc,
880 const sctp_subtype_t type, void *arg,
881 sctp_cmd_seq_t *commands)
882{
883 struct sctp_chunk *chunk = arg;
884 struct sctp_ulpevent *ev;
885
886 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000887 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* Verify that the chunk length for the COOKIE-ACK is OK.
890 * If we don't do this, any bundled chunks may be junked.
891 */
892 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000893 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 commands);
895
896 /* Reset init error count upon receipt of COOKIE-ACK,
897 * to avoid problems with the managemement of this
898 * counter in stale cookie situations when a transition back
899 * from the COOKIE-ECHOED state to the COOKIE-WAIT
900 * state is performed.
901 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700902 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* RFC 2960 5.1 Normal Establishment of an Association
905 *
906 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
907 * from the COOKIE-ECHOED state to the ESTABLISHED state,
908 * stopping the T1-cookie timer.
909 */
910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
911 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
912 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
913 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000914 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
915 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
Neil Horman9f70f462013-12-10 06:48:15 -0500917 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
919 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /* It may also notify its ULP about the successful
922 * establishment of the association with a Communication Up
923 * notification (see Section 10).
924 */
925 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
926 0, asoc->c.sinit_num_ostreams,
927 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700928 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (!ev)
931 goto nomem;
932
933 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
934
935 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800936 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800938 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800940 if (asoc->peer.adaptation_ind) {
941 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!ev)
943 goto nomem;
944
945 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
946 SCTP_ULPEVENT(ev));
947 }
948
949 return SCTP_DISPOSITION_CONSUME;
950nomem:
951 return SCTP_DISPOSITION_NOMEM;
952}
953
954/* Generate and sendout a heartbeat packet. */
955static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
956 const struct sctp_association *asoc,
957 const sctp_subtype_t type,
958 void *arg,
959 sctp_cmd_seq_t *commands)
960{
961 struct sctp_transport *transport = (struct sctp_transport *) arg;
962 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000965 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!reply)
967 return SCTP_DISPOSITION_NOMEM;
968
969 /* Set rto_pending indicating that an RTT measurement
970 * is started with this heartbeat chunk.
971 */
972 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
973 SCTP_TRANSPORT(transport));
974
975 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
976 return SCTP_DISPOSITION_CONSUME;
977}
978
979/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000980sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
981 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 const struct sctp_association *asoc,
983 const sctp_subtype_t type,
984 void *arg,
985 sctp_cmd_seq_t *commands)
986{
987 struct sctp_transport *transport = (struct sctp_transport *) arg;
988
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400989 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700990 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
991 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
993 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800994 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000995 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
996 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return SCTP_DISPOSITION_DELETE_TCB;
998 }
999
1000 /* Section 3.3.5.
1001 * The Sender-specific Heartbeat Info field should normally include
1002 * information about the sender's current time when this HEARTBEAT
1003 * chunk is sent and the destination transport address to which this
1004 * HEARTBEAT is sent (see Section 8.3).
1005 */
1006
Frank Filz52ccb8e2005-12-22 11:36:46 -08001007 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (SCTP_DISPOSITION_NOMEM ==
1009 sctp_sf_heartbeat(ep, asoc, type, arg,
1010 commands))
1011 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* Set transport error counter and association error counter
1014 * when sending heartbeat.
1015 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001016 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 SCTP_TRANSPORT(transport));
1018 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001019 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1020 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1022 SCTP_TRANSPORT(transport));
1023
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001024 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
1028 * Process an heartbeat request.
1029 *
1030 * Section: 8.3 Path Heartbeat
1031 * The receiver of the HEARTBEAT should immediately respond with a
1032 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1033 * from the received HEARTBEAT chunk.
1034 *
1035 * Verification Tag: 8.5 Verification Tag [Normal verification]
1036 * When receiving an SCTP packet, the endpoint MUST ensure that the
1037 * value in the Verification Tag field of the received SCTP packet
1038 * matches its own Tag. If the received Verification Tag value does not
1039 * match the receiver's own tag value, the receiver shall silently
1040 * discard the packet and shall not process it any further except for
1041 * those cases listed in Section 8.5.1 below.
1042 *
1043 * Inputs
1044 * (endpoint, asoc, chunk)
1045 *
1046 * Outputs
1047 * (asoc, reply_msg, msg_up, timers, counters)
1048 *
1049 * The return value is the disposition of the chunk.
1050 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001051sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1052 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 const struct sctp_association *asoc,
1054 const sctp_subtype_t type,
1055 void *arg,
1056 sctp_cmd_seq_t *commands)
1057{
Thomas Graf06a31e22012-11-30 02:16:27 +00001058 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct sctp_chunk *chunk = arg;
1060 struct sctp_chunk *reply;
1061 size_t paylen = 0;
1062
1063 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001064 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* Make sure that the HEARTBEAT chunk has a valid length. */
1067 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001068 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 commands);
1070
1071 /* 8.3 The receiver of the HEARTBEAT should immediately
1072 * respond with a HEARTBEAT ACK that contains the Heartbeat
1073 * Information field copied from the received HEARTBEAT chunk.
1074 */
1075 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001076 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001078
1079 if (ntohs(param_hdr->length) > paylen)
1080 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1081 param_hdr, commands);
1082
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001083 if (!pskb_pull(chunk->skb, paylen))
1084 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Thomas Graf06a31e22012-11-30 02:16:27 +00001086 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!reply)
1088 goto nomem;
1089
1090 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1091 return SCTP_DISPOSITION_CONSUME;
1092
1093nomem:
1094 return SCTP_DISPOSITION_NOMEM;
1095}
1096
1097/*
1098 * Process the returning HEARTBEAT ACK.
1099 *
1100 * Section: 8.3 Path Heartbeat
1101 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1102 * should clear the error counter of the destination transport
1103 * address to which the HEARTBEAT was sent, and mark the destination
1104 * transport address as active if it is not so marked. The endpoint may
1105 * optionally report to the upper layer when an inactive destination
1106 * address is marked as active due to the reception of the latest
1107 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1108 * clear the association overall error count as well (as defined
1109 * in section 8.1).
1110 *
1111 * The receiver of the HEARTBEAT ACK should also perform an RTT
1112 * measurement for that destination transport address using the time
1113 * value carried in the HEARTBEAT ACK chunk.
1114 *
1115 * Verification Tag: 8.5 Verification Tag [Normal verification]
1116 *
1117 * Inputs
1118 * (endpoint, asoc, chunk)
1119 *
1120 * Outputs
1121 * (asoc, reply_msg, msg_up, timers, counters)
1122 *
1123 * The return value is the disposition of the chunk.
1124 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001125sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1126 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 const struct sctp_association *asoc,
1128 const sctp_subtype_t type,
1129 void *arg,
1130 sctp_cmd_seq_t *commands)
1131{
1132 struct sctp_chunk *chunk = arg;
1133 union sctp_addr from_addr;
1134 struct sctp_transport *link;
1135 sctp_sender_hb_info_t *hbinfo;
1136 unsigned long max_interval;
1137
1138 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001139 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001142 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1143 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001144 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 commands);
1146
1147 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001148 /* Make sure that the length of the parameter is what we expect */
1149 if (ntohs(hbinfo->param_hdr.length) !=
1150 sizeof(sctp_sender_hb_info_t)) {
1151 return SCTP_DISPOSITION_DISCARD;
1152 }
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001155 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001158 if (unlikely(!link)) {
1159 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001160 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1161 __func__,
1162 asoc,
1163 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001164 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001165 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1166 __func__,
1167 asoc,
1168 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return SCTP_DISPOSITION_DISCARD;
1171 }
1172
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001173 /* Validate the 64-bit random nonce. */
1174 if (hbinfo->hb_nonce != link->hb_nonce)
1175 return SCTP_DISPOSITION_DISCARD;
1176
Frank Filz52ccb8e2005-12-22 11:36:46 -08001177 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* Check if the timestamp looks valid. */
1180 if (time_after(hbinfo->sent_at, jiffies) ||
1181 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02001182 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1183 "for transport:%p\n", __func__, link);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return SCTP_DISPOSITION_DISCARD;
1186 }
1187
1188 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1189 * the HEARTBEAT should clear the error counter of the
1190 * destination transport address to which the HEARTBEAT was
1191 * sent and mark the destination transport address as active if
1192 * it is not so marked.
1193 */
1194 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1195
1196 return SCTP_DISPOSITION_CONSUME;
1197}
1198
1199/* Helper function to send out an abort for the restart
1200 * condition.
1201 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001202static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct sctp_chunk *init,
1204 sctp_cmd_seq_t *commands)
1205{
1206 int len;
1207 struct sctp_packet *pkt;
1208 union sctp_addr_param *addrparm;
1209 struct sctp_errhdr *errhdr;
1210 struct sctp_endpoint *ep;
1211 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1212 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1213
1214 /* Build the error on the stack. We are way to malloc crazy
1215 * throughout the code today.
1216 */
1217 errhdr = (struct sctp_errhdr *)buffer;
1218 addrparm = (union sctp_addr_param *)errhdr->variable;
1219
1220 /* Copy into a parm format. */
1221 len = af->to_addr_param(ssa, addrparm);
1222 len += sizeof(sctp_errhdr_t);
1223
1224 errhdr->cause = SCTP_ERROR_RESTART;
1225 errhdr->length = htons(len);
1226
1227 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001228 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Association is NULL since this may be a restart attack and we
1231 * want to send back the attacker's vtag.
1232 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001233 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (!pkt)
1236 goto out;
1237 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1238
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001239 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Discard the rest of the inbound packet. */
1242 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1243
1244out:
1245 /* Even if there is no memory, treat as a failure so
1246 * the packet will get dropped.
1247 */
1248 return 0;
1249}
1250
Joe Perches123031c2010-09-08 11:04:21 +00001251static bool list_has_sctp_addr(const struct list_head *list,
1252 union sctp_addr *ipaddr)
1253{
1254 struct sctp_transport *addr;
1255
1256 list_for_each_entry(addr, list, transports) {
1257 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1258 return true;
1259 }
1260
1261 return false;
1262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263/* A restart is occurring, check to make sure no new addresses
1264 * are being added as we may be under a takeover attack.
1265 */
1266static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1267 const struct sctp_association *asoc,
1268 struct sctp_chunk *init,
1269 sctp_cmd_seq_t *commands)
1270{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001271 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001272 struct sctp_transport *new_addr;
1273 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Joe Perches123031c2010-09-08 11:04:21 +00001275 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * ...
1277 * Before responding the endpoint MUST check to see if the
1278 * unexpected INIT adds new addresses to the association. If new
1279 * addresses are added to the association, the endpoint MUST respond
1280 * with an ABORT..
1281 */
1282
1283 /* Search through all current addresses and make sure
1284 * we aren't adding any new ones.
1285 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001286 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001287 transports) {
1288 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1289 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001290 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001291 commands);
1292 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
Joe Perches123031c2010-09-08 11:04:21 +00001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
1297 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301/* Populate the verification/tie tags based on overlapping INIT
1302 * scenario.
1303 *
1304 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1305 */
1306static void sctp_tietags_populate(struct sctp_association *new_asoc,
1307 const struct sctp_association *asoc)
1308{
1309 switch (asoc->state) {
1310
1311 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1312
1313 case SCTP_STATE_COOKIE_WAIT:
1314 new_asoc->c.my_vtag = asoc->c.my_vtag;
1315 new_asoc->c.my_ttag = asoc->c.my_vtag;
1316 new_asoc->c.peer_ttag = 0;
1317 break;
1318
1319 case SCTP_STATE_COOKIE_ECHOED:
1320 new_asoc->c.my_vtag = asoc->c.my_vtag;
1321 new_asoc->c.my_ttag = asoc->c.my_vtag;
1322 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1323 break;
1324
1325 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1326 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1327 */
1328 default:
1329 new_asoc->c.my_ttag = asoc->c.my_vtag;
1330 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1331 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* Other parameters for the endpoint SHOULD be copied from the
1335 * existing parameters of the association (e.g. number of
1336 * outbound streams) into the INIT ACK and cookie.
1337 */
1338 new_asoc->rwnd = asoc->rwnd;
1339 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1340 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1341 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1342}
1343
1344/*
1345 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1346 * handling action.
1347 *
1348 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1349 *
1350 * Returns value representing action to be taken. These action values
1351 * correspond to Action/Description values in RFC 2960, Table 2.
1352 */
1353static char sctp_tietags_compare(struct sctp_association *new_asoc,
1354 const struct sctp_association *asoc)
1355{
1356 /* In this case, the peer may have restarted. */
1357 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1358 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1359 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1360 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1361 return 'A';
1362
1363 /* Collision case B. */
1364 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1365 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1366 (0 == asoc->c.peer_vtag))) {
1367 return 'B';
1368 }
1369
1370 /* Collision case D. */
1371 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1372 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1373 return 'D';
1374
1375 /* Collision case C. */
1376 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1377 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1378 (0 == new_asoc->c.my_ttag) &&
1379 (0 == new_asoc->c.peer_ttag))
1380 return 'C';
1381
1382 /* No match to any of the special cases; discard this packet. */
1383 return 'E';
1384}
1385
1386/* Common helper routine for both duplicate and simulataneous INIT
1387 * chunk handling.
1388 */
1389static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001390 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 const struct sctp_endpoint *ep,
1392 const struct sctp_association *asoc,
1393 const sctp_subtype_t type,
1394 void *arg, sctp_cmd_seq_t *commands)
1395{
1396 sctp_disposition_t retval;
1397 struct sctp_chunk *chunk = arg;
1398 struct sctp_chunk *repl;
1399 struct sctp_association *new_asoc;
1400 struct sctp_chunk *err_chunk;
1401 struct sctp_packet *packet;
1402 sctp_unrecognized_param_t *unk_param;
1403 int len;
1404
1405 /* 6.10 Bundling
1406 * An endpoint MUST NOT bundle INIT, INIT ACK or
1407 * SHUTDOWN COMPLETE with any other chunks.
1408 *
1409 * IG Section 2.11.2
1410 * Furthermore, we require that the receiver of an INIT chunk MUST
1411 * enforce these rules by silently discarding an arriving packet
1412 * with an INIT chunk that is bundled with other chunks.
1413 */
1414 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001415 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001418 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
1420 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001421 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* Make sure that the INIT chunk has a valid length.
1424 * In this case, we generate a protocol violation since we have
1425 * an association established.
1426 */
1427 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001428 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 commands);
1430 /* Grab the INIT header. */
1431 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1432
1433 /* Tag the variable length parameters. */
1434 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1435
1436 /* Verify the INIT chunk before processing it. */
1437 err_chunk = NULL;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00001438 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1440 &err_chunk)) {
1441 /* This chunk contains fatal error. It is to be discarded.
1442 * Send an ABORT, with causes if there is any.
1443 */
1444 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001445 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 (__u8 *)(err_chunk->chunk_hdr) +
1447 sizeof(sctp_chunkhdr_t),
1448 ntohs(err_chunk->chunk_hdr->length) -
1449 sizeof(sctp_chunkhdr_t));
1450
1451 if (packet) {
1452 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1453 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001454 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 retval = SCTP_DISPOSITION_CONSUME;
1456 } else {
1457 retval = SCTP_DISPOSITION_NOMEM;
1458 }
1459 goto cleanup;
1460 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001461 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 commands);
1463 }
1464 }
1465
1466 /*
1467 * Other parameters for the endpoint SHOULD be copied from the
1468 * existing parameters of the association (e.g. number of
1469 * outbound streams) into the INIT ACK and cookie.
1470 * FIXME: We are copying parameters from the endpoint not the
1471 * association.
1472 */
1473 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1474 if (!new_asoc)
1475 goto nomem;
1476
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001477 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1478 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1479 goto nomem;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* In the outbound INIT ACK the endpoint MUST copy its current
1482 * Verification Tag and Peers Verification tag into a reserved
1483 * place (local tie-tag and per tie-tag) within the state cookie.
1484 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001485 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001487 GFP_ATOMIC))
1488 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /* Make sure no new addresses are being added during the
1491 * restart. Do not do this check for COOKIE-WAIT state,
1492 * since there are no peer addresses to check against.
1493 * Upon return an ABORT will have been sent if needed.
1494 */
1495 if (!sctp_state(asoc, COOKIE_WAIT)) {
1496 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1497 commands)) {
1498 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001499 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 }
1502
1503 sctp_tietags_populate(new_asoc, asoc);
1504
1505 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1506
1507 /* If there are errors need to be reported for unknown parameters,
1508 * make sure to reserve enough room in the INIT ACK for them.
1509 */
1510 len = 0;
1511 if (err_chunk) {
1512 len = ntohs(err_chunk->chunk_hdr->length) -
1513 sizeof(sctp_chunkhdr_t);
1514 }
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1517 if (!repl)
1518 goto nomem;
1519
1520 /* If there are errors need to be reported for unknown parameters,
1521 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1522 * parameter.
1523 */
1524 if (err_chunk) {
1525 /* Get the "Unrecognized parameter" parameter(s) out of the
1526 * ERROR chunk generated by sctp_verify_init(). Since the
1527 * error cause code for "unknown parameter" and the
1528 * "Unrecognized parameter" type is the same, we can
1529 * construct the parameters in INIT ACK by copying the
1530 * ERROR causes over.
1531 */
1532 unk_param = (sctp_unrecognized_param_t *)
1533 ((__u8 *)(err_chunk->chunk_hdr) +
1534 sizeof(sctp_chunkhdr_t));
1535 /* Replace the cause code with the "Unrecognized parameter"
1536 * parameter type.
1537 */
1538 sctp_addto_chunk(repl, len, unk_param);
1539 }
1540
1541 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1542 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1543
1544 /*
1545 * Note: After sending out INIT ACK with the State Cookie parameter,
1546 * "Z" MUST NOT allocate any resources for this new association.
1547 * Otherwise, "Z" will be vulnerable to resource attacks.
1548 */
1549 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1550 retval = SCTP_DISPOSITION_CONSUME;
1551
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001552 return retval;
1553
1554nomem:
1555 retval = SCTP_DISPOSITION_NOMEM;
1556nomem_retval:
1557 if (new_asoc)
1558 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559cleanup:
1560 if (err_chunk)
1561 sctp_chunk_free(err_chunk);
1562 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001566 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * This means we started an INIT and then we got an INIT request from
1568 * our peer.
1569 *
1570 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1571 * This usually indicates an initialization collision, i.e., each
1572 * endpoint is attempting, at about the same time, to establish an
1573 * association with the other endpoint.
1574 *
1575 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1576 * endpoint MUST respond with an INIT ACK using the same parameters it
1577 * sent in its original INIT chunk (including its Verification Tag,
1578 * unchanged). These original parameters are combined with those from the
1579 * newly received INIT chunk. The endpoint shall also generate a State
1580 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1581 * INIT to calculate the State Cookie.
1582 *
1583 * After that, the endpoint MUST NOT change its state, the T1-init
1584 * timer shall be left running and the corresponding TCB MUST NOT be
1585 * destroyed. The normal procedures for handling State Cookies when
1586 * a TCB exists will resolve the duplicate INITs to a single association.
1587 *
1588 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1589 * its Tie-Tags with the Tag information of itself and its peer (see
1590 * section 5.2.2 for a description of the Tie-Tags).
1591 *
1592 * Verification Tag: Not explicit, but an INIT can not have a valid
1593 * verification tag, so we skip the check.
1594 *
1595 * Inputs
1596 * (endpoint, asoc, chunk)
1597 *
1598 * Outputs
1599 * (asoc, reply_msg, msg_up, timers, counters)
1600 *
1601 * The return value is the disposition of the chunk.
1602 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001603sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1604 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 const struct sctp_association *asoc,
1606 const sctp_subtype_t type,
1607 void *arg,
1608 sctp_cmd_seq_t *commands)
1609{
1610 /* Call helper to do the real work for both simulataneous and
1611 * duplicate INIT chunk handling.
1612 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001613 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/*
1617 * Handle duplicated INIT messages. These are usually delayed
1618 * restransmissions.
1619 *
1620 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1621 * COOKIE-ECHOED and COOKIE-WAIT
1622 *
1623 * Unless otherwise stated, upon reception of an unexpected INIT for
1624 * this association, the endpoint shall generate an INIT ACK with a
1625 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1626 * current Verification Tag and peer's Verification Tag into a reserved
1627 * place within the state cookie. We shall refer to these locations as
1628 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1629 * containing this INIT ACK MUST carry a Verification Tag value equal to
1630 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1631 * MUST contain a new Initiation Tag (randomly generated see Section
1632 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1633 * existing parameters of the association (e.g. number of outbound
1634 * streams) into the INIT ACK and cookie.
1635 *
1636 * After sending out the INIT ACK, the endpoint shall take no further
1637 * actions, i.e., the existing association, including its current state,
1638 * and the corresponding TCB MUST NOT be changed.
1639 *
1640 * Note: Only when a TCB exists and the association is not in a COOKIE-
1641 * WAIT state are the Tie-Tags populated. For a normal association INIT
1642 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1643 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1644 * State Cookie are populated as specified in section 5.2.1.
1645 *
1646 * Verification Tag: Not specified, but an INIT has no way of knowing
1647 * what the verification tag could be, so we ignore it.
1648 *
1649 * Inputs
1650 * (endpoint, asoc, chunk)
1651 *
1652 * Outputs
1653 * (asoc, reply_msg, msg_up, timers, counters)
1654 *
1655 * The return value is the disposition of the chunk.
1656 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001657sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1658 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 const struct sctp_association *asoc,
1660 const sctp_subtype_t type,
1661 void *arg,
1662 sctp_cmd_seq_t *commands)
1663{
1664 /* Call helper to do the real work for both simulataneous and
1665 * duplicate INIT chunk handling.
1666 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001667 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
1670
Vlad Yasevich610ab732007-01-15 19:18:30 -08001671/*
1672 * Unexpected INIT-ACK handler.
1673 *
1674 * Section 5.2.3
1675 * If an INIT ACK received by an endpoint in any state other than the
1676 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1677 * An unexpected INIT ACK usually indicates the processing of an old or
1678 * duplicated INIT chunk.
1679*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001680sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1681 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001682 const struct sctp_association *asoc,
1683 const sctp_subtype_t type,
1684 void *arg, sctp_cmd_seq_t *commands)
1685{
1686 /* Per the above section, we'll discard the chunk if we have an
1687 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1688 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001689 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001690 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001691 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001692 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1696 *
1697 * Section 5.2.4
1698 * A) In this case, the peer may have restarted.
1699 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001700static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1701 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 const struct sctp_association *asoc,
1703 struct sctp_chunk *chunk,
1704 sctp_cmd_seq_t *commands,
1705 struct sctp_association *new_asoc)
1706{
1707 sctp_init_chunk_t *peer_init;
1708 struct sctp_ulpevent *ev;
1709 struct sctp_chunk *repl;
1710 struct sctp_chunk *err;
1711 sctp_disposition_t disposition;
1712
1713 /* new_asoc is a brand-new association, so these are not yet
1714 * side effects--it is safe to run them here.
1715 */
1716 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1717
Wei Yongjunde6becd2011-04-19 21:30:51 +00001718 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 GFP_ATOMIC))
1720 goto nomem;
1721
1722 /* Make sure no new addresses are being added during the
1723 * restart. Though this is a pretty complicated attack
1724 * since you'd have to get inside the cookie.
1725 */
1726 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1727 return SCTP_DISPOSITION_CONSUME;
1728 }
1729
1730 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1731 * the peer has restarted (Action A), it MUST NOT setup a new
1732 * association but instead resend the SHUTDOWN ACK and send an ERROR
1733 * chunk with a "Cookie Received while Shutting Down" error cause to
1734 * its peer.
1735 */
1736 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001737 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1739 chunk, commands);
1740 if (SCTP_DISPOSITION_NOMEM == disposition)
1741 goto nomem;
1742
1743 err = sctp_make_op_error(asoc, chunk,
1744 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001745 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (err)
1747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1748 SCTP_CHUNK(err));
1749
1750 return SCTP_DISPOSITION_CONSUME;
1751 }
1752
Wei Yongjuna000c012011-05-29 23:23:36 +00001753 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1754 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001756 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1757 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1758 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1760
Wei Yongjuna000c012011-05-29 23:23:36 +00001761 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1762 * and ASCONF-ACK cache.
1763 */
1764 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1765 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1766 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 repl = sctp_make_cookie_ack(new_asoc, chunk);
1769 if (!repl)
1770 goto nomem;
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* Report association restart to upper layer. */
1773 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1774 new_asoc->c.sinit_num_ostreams,
1775 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001776 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!ev)
1778 goto nomem_ev;
1779
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001780 /* Update the content of current association. */
1781 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Xufeng Zhang9839ff02013-01-23 16:44:34 +00001783 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1784 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return SCTP_DISPOSITION_CONSUME;
1787
1788nomem_ev:
1789 sctp_chunk_free(repl);
1790nomem:
1791 return SCTP_DISPOSITION_NOMEM;
1792}
1793
1794/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1795 *
1796 * Section 5.2.4
1797 * B) In this case, both sides may be attempting to start an association
1798 * at about the same time but the peer endpoint started its INIT
1799 * after responding to the local endpoint's INIT
1800 */
1801/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001802static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1803 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 const struct sctp_association *asoc,
1805 struct sctp_chunk *chunk,
1806 sctp_cmd_seq_t *commands,
1807 struct sctp_association *new_asoc)
1808{
1809 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct sctp_chunk *repl;
1811
1812 /* new_asoc is a brand-new association, so these are not yet
1813 * side effects--it is safe to run them here.
1814 */
1815 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001816 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 GFP_ATOMIC))
1818 goto nomem;
1819
1820 /* Update the content of current association. */
1821 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1823 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001824 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1826
1827 repl = sctp_make_cookie_ack(new_asoc, chunk);
1828 if (!repl)
1829 goto nomem;
1830
1831 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /* RFC 2960 5.1 Normal Establishment of an Association
1834 *
1835 * D) IMPLEMENTATION NOTE: An implementation may choose to
1836 * send the Communication Up notification to the SCTP user
1837 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001838 *
1839 * Sadly, this needs to be implemented as a side-effect, because
1840 * we are not guaranteed to have set the association id of the real
1841 * association and so these notifications need to be delayed until
1842 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Vlad Yasevich07d93962007-05-04 13:55:27 -07001845 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
1847 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001848 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001850 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001851 *
1852 * This also needs to be done as a side effect for the same reason as
1853 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001855 if (asoc->peer.adaptation_ind)
1856 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858 return SCTP_DISPOSITION_CONSUME;
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860nomem:
1861 return SCTP_DISPOSITION_NOMEM;
1862}
1863
1864/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1865 *
1866 * Section 5.2.4
1867 * C) In this case, the local endpoint's cookie has arrived late.
1868 * Before it arrived, the local endpoint sent an INIT and received an
1869 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1870 * but a new tag of its own.
1871 */
1872/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001873static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1874 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 const struct sctp_association *asoc,
1876 struct sctp_chunk *chunk,
1877 sctp_cmd_seq_t *commands,
1878 struct sctp_association *new_asoc)
1879{
1880 /* The cookie should be silently discarded.
1881 * The endpoint SHOULD NOT change states and should leave
1882 * any timers running.
1883 */
1884 return SCTP_DISPOSITION_DISCARD;
1885}
1886
1887/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1888 *
1889 * Section 5.2.4
1890 *
1891 * D) When both local and remote tags match the endpoint should always
1892 * enter the ESTABLISHED state, if it has not already done so.
1893 */
1894/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001895static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1896 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 const struct sctp_association *asoc,
1898 struct sctp_chunk *chunk,
1899 sctp_cmd_seq_t *commands,
1900 struct sctp_association *new_asoc)
1901{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001902 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct sctp_chunk *repl;
1904
1905 /* Clarification from Implementor's Guide:
1906 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001907 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1908 * It should stop any cookie timer that may be running and send
1909 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 */
1911
1912 /* Don't accidentally move back into established state. */
1913 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1914 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1915 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1916 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1917 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001918 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1920 SCTP_NULL());
1921
1922 /* RFC 2960 5.1 Normal Establishment of an Association
1923 *
1924 * D) IMPLEMENTATION NOTE: An implementation may choose
1925 * to send the Communication Up notification to the
1926 * SCTP user upon reception of a valid COOKIE
1927 * ECHO chunk.
1928 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001929 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001931 asoc->c.sinit_num_ostreams,
1932 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001933 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (!ev)
1935 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001938 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001940 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001942 if (asoc->peer.adaptation_ind) {
1943 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001945 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto nomem;
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 repl = sctp_make_cookie_ack(new_asoc, chunk);
1952 if (!repl)
1953 goto nomem;
1954
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001955 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1956
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001957 if (ev)
1958 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1959 SCTP_ULPEVENT(ev));
1960 if (ai_ev)
1961 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1962 SCTP_ULPEVENT(ai_ev));
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return SCTP_DISPOSITION_CONSUME;
1965
1966nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001967 if (ai_ev)
1968 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (ev)
1970 sctp_ulpevent_free(ev);
1971 return SCTP_DISPOSITION_NOMEM;
1972}
1973
1974/*
1975 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1976 * chunk was retransmitted and then delayed in the network.
1977 *
1978 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1979 *
1980 * Verification Tag: None. Do cookie validation.
1981 *
1982 * Inputs
1983 * (endpoint, asoc, chunk)
1984 *
1985 * Outputs
1986 * (asoc, reply_msg, msg_up, timers, counters)
1987 *
1988 * The return value is the disposition of the chunk.
1989 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001990sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1991 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 const struct sctp_association *asoc,
1993 const sctp_subtype_t type,
1994 void *arg,
1995 sctp_cmd_seq_t *commands)
1996{
1997 sctp_disposition_t retval;
1998 struct sctp_chunk *chunk = arg;
1999 struct sctp_association *new_asoc;
2000 int error = 0;
2001 char action;
2002 struct sctp_chunk *err_chk_p;
2003
2004 /* Make sure that the chunk has a valid length from the protocol
2005 * perspective. In this case check to make sure we have at least
2006 * enough for the chunk header. Cookie length verification is
2007 * done later.
2008 */
2009 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002010 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 commands);
2012
2013 /* "Decode" the chunk. We have no optional parameters so we
2014 * are in good shape.
2015 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002016 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002017 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2018 sizeof(sctp_chunkhdr_t)))
2019 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2022 * of a duplicate COOKIE ECHO match the Verification Tags of the
2023 * current association, consider the State Cookie valid even if
2024 * the lifespan is exceeded.
2025 */
2026 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2027 &err_chk_p);
2028
2029 /* FIXME:
2030 * If the re-build failed, what is the proper error path
2031 * from here?
2032 *
2033 * [We should abort the association. --piggy]
2034 */
2035 if (!new_asoc) {
2036 /* FIXME: Several errors are possible. A bad cookie should
2037 * be silently discarded, but think about logging it too.
2038 */
2039 switch (error) {
2040 case -SCTP_IERROR_NOMEM:
2041 goto nomem;
2042
2043 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002044 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002046 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 case -SCTP_IERROR_BAD_SIG:
2048 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002049 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
2053 /* Compare the tie_tag in cookie with the verification tag of
2054 * current association.
2055 */
2056 action = sctp_tietags_compare(new_asoc, asoc);
2057
2058 switch (action) {
2059 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002060 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 new_asoc);
2062 break;
2063
2064 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002065 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 new_asoc);
2067 break;
2068
2069 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002070 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 new_asoc);
2072 break;
2073
2074 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002075 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 new_asoc);
2077 break;
2078
2079 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002080 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002085 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2087
Max Matveevd5ccd492011-08-29 21:02:24 +00002088 /* Restore association pointer to provide SCTP command interpeter
2089 * with a valid context in case it needs to manipulate
2090 * the queues */
2091 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2092 SCTP_ASOC((struct sctp_association *)asoc));
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 return retval;
2095
2096nomem:
2097 return SCTP_DISPOSITION_NOMEM;
2098}
2099
2100/*
2101 * Process an ABORT. (SHUTDOWN-PENDING state)
2102 *
2103 * See sctp_sf_do_9_1_abort().
2104 */
2105sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002106 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 const struct sctp_endpoint *ep,
2108 const struct sctp_association *asoc,
2109 const sctp_subtype_t type,
2110 void *arg,
2111 sctp_cmd_seq_t *commands)
2112{
2113 struct sctp_chunk *chunk = arg;
2114
2115 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002116 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 /* Make sure that the ABORT chunk has a valid length.
2119 * Since this is an ABORT chunk, we have to discard it
2120 * because of the following text:
2121 * RFC 2960, Section 3.3.7
2122 * If an endpoint receives an ABORT with a format error or for an
2123 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002124 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 * as we do not know its true length. So, to be safe, discard the
2126 * packet.
2127 */
2128 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002129 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Vlad Yasevich75205f42007-12-20 14:12:59 -08002131 /* ADD-IP: Special case for ABORT chunks
2132 * F4) One special consideration is that ABORT Chunks arriving
2133 * destined to the IP address being deleted MUST be
2134 * ignored (see Section 5.3.1 for further details).
2135 */
2136 if (SCTP_ADDR_DEL ==
2137 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002138 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002139
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002140 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
2143/*
2144 * Process an ABORT. (SHUTDOWN-SENT state)
2145 *
2146 * See sctp_sf_do_9_1_abort().
2147 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002148sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2149 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 const struct sctp_association *asoc,
2151 const sctp_subtype_t type,
2152 void *arg,
2153 sctp_cmd_seq_t *commands)
2154{
2155 struct sctp_chunk *chunk = arg;
2156
2157 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002158 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /* Make sure that the ABORT chunk has a valid length.
2161 * Since this is an ABORT chunk, we have to discard it
2162 * because of the following text:
2163 * RFC 2960, Section 3.3.7
2164 * If an endpoint receives an ABORT with a format error or for an
2165 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002166 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 * as we do not know its true length. So, to be safe, discard the
2168 * packet.
2169 */
2170 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002171 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Vlad Yasevich75205f42007-12-20 14:12:59 -08002173 /* ADD-IP: Special case for ABORT chunks
2174 * F4) One special consideration is that ABORT Chunks arriving
2175 * destined to the IP address being deleted MUST be
2176 * ignored (see Section 5.3.1 for further details).
2177 */
2178 if (SCTP_ADDR_DEL ==
2179 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002180 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /* Stop the T2-shutdown timer. */
2183 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2184 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2185
2186 /* Stop the T5-shutdown guard timer. */
2187 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2188 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2189
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002190 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192
2193/*
2194 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2195 *
2196 * See sctp_sf_do_9_1_abort().
2197 */
2198sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002199 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 const struct sctp_endpoint *ep,
2201 const struct sctp_association *asoc,
2202 const sctp_subtype_t type,
2203 void *arg,
2204 sctp_cmd_seq_t *commands)
2205{
2206 /* The same T2 timer, so we should be able to use
2207 * common function with the SHUTDOWN-SENT state.
2208 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002209 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212/*
2213 * Handle an Error received in COOKIE_ECHOED state.
2214 *
2215 * Only handle the error type of stale COOKIE Error, the other errors will
2216 * be ignored.
2217 *
2218 * Inputs
2219 * (endpoint, asoc, chunk)
2220 *
2221 * Outputs
2222 * (asoc, reply_msg, msg_up, timers, counters)
2223 *
2224 * The return value is the disposition of the chunk.
2225 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002226sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2227 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 const struct sctp_association *asoc,
2229 const sctp_subtype_t type,
2230 void *arg,
2231 sctp_cmd_seq_t *commands)
2232{
2233 struct sctp_chunk *chunk = arg;
2234 sctp_errhdr_t *err;
2235
2236 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002237 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
2239 /* Make sure that the ERROR chunk has a valid length.
2240 * The parameter walking depends on this as well.
2241 */
2242 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002243 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 commands);
2245
2246 /* Process the error here */
2247 /* FUTURE FIXME: When PR-SCTP related and other optional
2248 * parms are emitted, this will have to change to handle multiple
2249 * errors.
2250 */
2251 sctp_walk_errors(err, chunk->chunk_hdr) {
2252 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002253 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 arg, commands);
2255 }
2256
2257 /* It is possible to have malformed error causes, and that
2258 * will cause us to end the walk early. However, since
2259 * we are discarding the packet, there should be no adverse
2260 * affects.
2261 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002262 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263}
2264
2265/*
2266 * Handle a Stale COOKIE Error
2267 *
2268 * Section: 5.2.6 Handle Stale COOKIE Error
2269 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2270 * one of the following three alternatives.
2271 * ...
2272 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2273 * Preservative parameter requesting an extension to the lifetime of
2274 * the State Cookie. When calculating the time extension, an
2275 * implementation SHOULD use the RTT information measured based on the
2276 * previous COOKIE ECHO / ERROR exchange, and should add no more
2277 * than 1 second beyond the measured RTT, due to long State Cookie
2278 * lifetimes making the endpoint more subject to a replay attack.
2279 *
2280 * Verification Tag: Not explicit, but safe to ignore.
2281 *
2282 * Inputs
2283 * (endpoint, asoc, chunk)
2284 *
2285 * Outputs
2286 * (asoc, reply_msg, msg_up, timers, counters)
2287 *
2288 * The return value is the disposition of the chunk.
2289 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002290static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2291 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 const struct sctp_association *asoc,
2293 const sctp_subtype_t type,
2294 void *arg,
2295 sctp_cmd_seq_t *commands)
2296{
2297 struct sctp_chunk *chunk = arg;
2298 time_t stale;
2299 sctp_cookie_preserve_param_t bht;
2300 sctp_errhdr_t *err;
2301 struct sctp_chunk *reply;
2302 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002303 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Vlad Yasevich81845c22006-01-30 15:59:54 -08002305 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002306 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2307 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002309 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return SCTP_DISPOSITION_DELETE_TCB;
2311 }
2312
2313 err = (sctp_errhdr_t *)(chunk->skb->data);
2314
2315 /* When calculating the time extension, an implementation
2316 * SHOULD use the RTT information measured based on the
2317 * previous COOKIE ECHO / ERROR exchange, and should add no
2318 * more than 1 second beyond the measured RTT, due to long
2319 * State Cookie lifetimes making the endpoint more subject to
2320 * a replay attack.
2321 * Measure of Staleness's unit is usec. (1/1000000 sec)
2322 * Suggested Cookie Life-span Increment's unit is msec.
2323 * (1/1000 sec)
2324 * In general, if you use the suggested cookie life, the value
2325 * found in the field of measure of staleness should be doubled
2326 * to give ample time to retransmit the new cookie and thus
2327 * yield a higher probability of success on the reattempt.
2328 */
Al Viro34bcca22006-11-20 17:27:15 -08002329 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 stale = (stale * 2) / 1000;
2331
2332 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2333 bht.param_hdr.length = htons(sizeof(bht));
2334 bht.lifespan_increment = htonl(stale);
2335
2336 /* Build that new INIT chunk. */
2337 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2338 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2339 if (!reply)
2340 goto nomem;
2341
2342 sctp_addto_chunk(reply, sizeof(bht), &bht);
2343
2344 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2345 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2346
2347 /* Stop pending T3-rtx and heartbeat timers */
2348 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2349 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2350
2351 /* Delete non-primary peer ip addresses since we are transitioning
2352 * back to the COOKIE-WAIT state
2353 */
2354 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2355
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002356 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2357 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002359 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 SCTP_TRANSPORT(asoc->peer.primary_path));
2361
2362 /* Cast away the const modifier, as we want to just
2363 * rerun it through as a sideffect.
2364 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002365 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2368 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2369 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2370 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2371 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2372 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2373
2374 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2375
2376 return SCTP_DISPOSITION_CONSUME;
2377
2378nomem:
2379 return SCTP_DISPOSITION_NOMEM;
2380}
2381
2382/*
2383 * Process an ABORT.
2384 *
2385 * Section: 9.1
2386 * After checking the Verification Tag, the receiving endpoint shall
2387 * remove the association from its record, and shall report the
2388 * termination to its upper layer.
2389 *
2390 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2391 * B) Rules for packet carrying ABORT:
2392 *
2393 * - The endpoint shall always fill in the Verification Tag field of the
2394 * outbound packet with the destination endpoint's tag value if it
2395 * is known.
2396 *
2397 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2398 * MUST follow the procedure described in Section 8.4.
2399 *
2400 * - The receiver MUST accept the packet if the Verification Tag
2401 * matches either its own tag, OR the tag of its peer. Otherwise, the
2402 * receiver MUST silently discard the packet and take no further
2403 * action.
2404 *
2405 * Inputs
2406 * (endpoint, asoc, chunk)
2407 *
2408 * Outputs
2409 * (asoc, reply_msg, msg_up, timers, counters)
2410 *
2411 * The return value is the disposition of the chunk.
2412 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002413sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2414 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 const struct sctp_association *asoc,
2416 const sctp_subtype_t type,
2417 void *arg,
2418 sctp_cmd_seq_t *commands)
2419{
2420 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002423 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 /* Make sure that the ABORT chunk has a valid length.
2426 * Since this is an ABORT chunk, we have to discard it
2427 * because of the following text:
2428 * RFC 2960, Section 3.3.7
2429 * If an endpoint receives an ABORT with a format error or for an
2430 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002431 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 * as we do not know its true length. So, to be safe, discard the
2433 * packet.
2434 */
2435 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002436 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Vlad Yasevich75205f42007-12-20 14:12:59 -08002438 /* ADD-IP: Special case for ABORT chunks
2439 * F4) One special consideration is that ABORT Chunks arriving
2440 * destined to the IP address being deleted MUST be
2441 * ignored (see Section 5.3.1 for further details).
2442 */
2443 if (SCTP_ADDR_DEL ==
2444 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002445 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002446
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002447 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002448}
2449
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002450static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2451 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002452 const struct sctp_association *asoc,
2453 const sctp_subtype_t type,
2454 void *arg,
2455 sctp_cmd_seq_t *commands)
2456{
2457 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002458 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002459 __be16 error = SCTP_ERROR_NO_ERROR;
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 /* See if we have an error cause code in the chunk. */
2462 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002463 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2464
2465 sctp_errhdr_t *err;
2466 sctp_walk_errors(err, chunk->chunk_hdr);
2467 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002468 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002473 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002474 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002475 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002476 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2477 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 return SCTP_DISPOSITION_ABORT;
2480}
2481
2482/*
2483 * Process an ABORT. (COOKIE-WAIT state)
2484 *
2485 * See sctp_sf_do_9_1_abort() above.
2486 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002487sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2488 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 const struct sctp_association *asoc,
2490 const sctp_subtype_t type,
2491 void *arg,
2492 sctp_cmd_seq_t *commands)
2493{
2494 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002495 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002496 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002499 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
2501 /* Make sure that the ABORT chunk has a valid length.
2502 * Since this is an ABORT chunk, we have to discard it
2503 * because of the following text:
2504 * RFC 2960, Section 3.3.7
2505 * If an endpoint receives an ABORT with a format error or for an
2506 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002507 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 * as we do not know its true length. So, to be safe, discard the
2509 * packet.
2510 */
2511 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002512 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
2514 /* See if we have an error cause code in the chunk. */
2515 len = ntohs(chunk->chunk_hdr->length);
2516 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2517 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2518
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002519 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002520 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
2523/*
2524 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2525 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002526sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2527 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 const struct sctp_association *asoc,
2529 const sctp_subtype_t type,
2530 void *arg,
2531 sctp_cmd_seq_t *commands)
2532{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002533 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002534 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002535 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/*
2539 * Process an ABORT. (COOKIE-ECHOED state)
2540 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002541sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2542 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 const struct sctp_association *asoc,
2544 const sctp_subtype_t type,
2545 void *arg,
2546 sctp_cmd_seq_t *commands)
2547{
2548 /* There is a single T1 timer, so we should be able to use
2549 * common function with the COOKIE-WAIT state.
2550 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002551 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
2554/*
2555 * Stop T1 timer and abort association with "INIT failed".
2556 *
2557 * This is common code called by several sctp_sf_*_abort() functions above.
2558 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002559static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2560 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002561 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002562 const struct sctp_association *asoc,
2563 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Daniel Borkmannbb333812013-06-28 19:49:40 +02002565 pr_debug("%s: ABORT received (INIT)\n", __func__);
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2568 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002569 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2571 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002572 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 /* CMD_INIT_FAILED will DELETE_TCB. */
2574 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002575 SCTP_PERR(error));
Daniel Borkmannbb333812013-06-28 19:49:40 +02002576
Frank Filz3f7a87d2005-06-20 13:14:57 -07002577 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580/*
2581 * sctp_sf_do_9_2_shut
2582 *
2583 * Section: 9.2
2584 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2585 * - enter the SHUTDOWN-RECEIVED state,
2586 *
2587 * - stop accepting new data from its SCTP user
2588 *
2589 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2590 * that all its outstanding DATA chunks have been received by the
2591 * SHUTDOWN sender.
2592 *
2593 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2594 * send a SHUTDOWN in response to a ULP request. And should discard
2595 * subsequent SHUTDOWN chunks.
2596 *
2597 * If there are still outstanding DATA chunks left, the SHUTDOWN
2598 * receiver shall continue to follow normal data transmission
2599 * procedures defined in Section 6 until all outstanding DATA chunks
2600 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2601 * new data from its SCTP user.
2602 *
2603 * Verification Tag: 8.5 Verification Tag [Normal verification]
2604 *
2605 * Inputs
2606 * (endpoint, asoc, chunk)
2607 *
2608 * Outputs
2609 * (asoc, reply_msg, msg_up, timers, counters)
2610 *
2611 * The return value is the disposition of the chunk.
2612 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002613sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2614 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 const struct sctp_association *asoc,
2616 const sctp_subtype_t type,
2617 void *arg,
2618 sctp_cmd_seq_t *commands)
2619{
2620 struct sctp_chunk *chunk = arg;
2621 sctp_shutdownhdr_t *sdh;
2622 sctp_disposition_t disposition;
2623 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002624 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002627 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 /* Make sure that the SHUTDOWN chunk has a valid length. */
2630 if (!sctp_chunk_length_valid(chunk,
2631 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002632 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 commands);
2634
2635 /* Convert the elaborate header. */
2636 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2637 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2638 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002639 ctsn = ntohl(sdh->cum_tsn_ack);
2640
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002641 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002642 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2643 asoc->ctsn_ack_point);
2644
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002645 return SCTP_DISPOSITION_DISCARD;
2646 }
2647
Wei Yongjundf10eec2008-10-23 01:00:21 -07002648 /* If Cumulative TSN Ack beyond the max tsn currently
2649 * send, terminating the association and respond to the
2650 * sender with an ABORT.
2651 */
2652 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002653 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002655 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2656 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2657 * inform the application that it should cease sending data.
2658 */
2659 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2660 if (!ev) {
2661 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002662 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002663 }
2664 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2665
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2667 * - enter the SHUTDOWN-RECEIVED state,
2668 * - stop accepting new data from its SCTP user
2669 *
2670 * [This is implicit in the new state.]
2671 */
2672 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2673 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2674 disposition = SCTP_DISPOSITION_CONSUME;
2675
2676 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002677 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 arg, commands);
2679 }
2680
2681 if (SCTP_DISPOSITION_NOMEM == disposition)
2682 goto out;
2683
2684 /* - verify, by checking the Cumulative TSN Ack field of the
2685 * chunk, that all its outstanding DATA chunks have been
2686 * received by the SHUTDOWN sender.
2687 */
2688 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002689 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691out:
2692 return disposition;
2693}
2694
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002695/*
2696 * sctp_sf_do_9_2_shut_ctsn
2697 *
2698 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2699 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2700 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2701 * MUST be processed.
2702 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002703sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2704 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002705 const struct sctp_association *asoc,
2706 const sctp_subtype_t type,
2707 void *arg,
2708 sctp_cmd_seq_t *commands)
2709{
2710 struct sctp_chunk *chunk = arg;
2711 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002712 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002713
2714 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002715 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002716
2717 /* Make sure that the SHUTDOWN chunk has a valid length. */
2718 if (!sctp_chunk_length_valid(chunk,
2719 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002720 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002721 commands);
2722
2723 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002724 ctsn = ntohl(sdh->cum_tsn_ack);
2725
2726 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02002727 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2728 asoc->ctsn_ack_point);
2729
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002730 return SCTP_DISPOSITION_DISCARD;
2731 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002732
2733 /* If Cumulative TSN Ack beyond the max tsn currently
2734 * send, terminating the association and respond to the
2735 * sender with an ABORT.
2736 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002737 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002738 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002739
2740 /* verify, by checking the Cumulative TSN Ack field of the
2741 * chunk, that all its outstanding DATA chunks have been
2742 * received by the SHUTDOWN sender.
2743 */
2744 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2745 SCTP_BE32(sdh->cum_tsn_ack));
2746
2747 return SCTP_DISPOSITION_CONSUME;
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/* RFC 2960 9.2
2751 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2752 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2753 * transport addresses (either in the IP addresses or in the INIT chunk)
2754 * that belong to this association, it should discard the INIT chunk and
2755 * retransmit the SHUTDOWN ACK chunk.
2756 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002757sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2758 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 const struct sctp_association *asoc,
2760 const sctp_subtype_t type,
2761 void *arg,
2762 sctp_cmd_seq_t *commands)
2763{
2764 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2765 struct sctp_chunk *reply;
2766
Vlad Yasevichece25df2007-09-07 16:30:54 -04002767 /* Make sure that the chunk has a valid length */
2768 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002769 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002770 commands);
2771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /* Since we are not going to really process this INIT, there
2773 * is no point in verifying chunk boundries. Just generate
2774 * the SHUTDOWN ACK.
2775 */
2776 reply = sctp_make_shutdown_ack(asoc, chunk);
2777 if (NULL == reply)
2778 goto nomem;
2779
2780 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2781 * the T2-SHUTDOWN timer.
2782 */
2783 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2784
2785 /* and restart the T2-shutdown timer. */
2786 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2787 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2788
2789 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2790
2791 return SCTP_DISPOSITION_CONSUME;
2792nomem:
2793 return SCTP_DISPOSITION_NOMEM;
2794}
2795
2796/*
2797 * sctp_sf_do_ecn_cwr
2798 *
2799 * Section: Appendix A: Explicit Congestion Notification
2800 *
2801 * CWR:
2802 *
2803 * RFC 2481 details a specific bit for a sender to send in the header of
2804 * its next outbound TCP segment to indicate to its peer that it has
2805 * reduced its congestion window. This is termed the CWR bit. For
2806 * SCTP the same indication is made by including the CWR chunk.
2807 * This chunk contains one data element, i.e. the TSN number that
2808 * was sent in the ECNE chunk. This element represents the lowest
2809 * TSN number in the datagram that was originally marked with the
2810 * CE bit.
2811 *
2812 * Verification Tag: 8.5 Verification Tag [Normal verification]
2813 * Inputs
2814 * (endpoint, asoc, chunk)
2815 *
2816 * Outputs
2817 * (asoc, reply_msg, msg_up, timers, counters)
2818 *
2819 * The return value is the disposition of the chunk.
2820 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002821sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2822 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 const struct sctp_association *asoc,
2824 const sctp_subtype_t type,
2825 void *arg,
2826 sctp_cmd_seq_t *commands)
2827{
2828 sctp_cwrhdr_t *cwr;
2829 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002830 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002833 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002836 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2840 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2841
Al Viro34bcca22006-11-20 17:27:15 -08002842 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002845 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 /* Stop sending ECNE. */
2847 sctp_add_cmd_sf(commands,
2848 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002849 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851 return SCTP_DISPOSITION_CONSUME;
2852}
2853
2854/*
2855 * sctp_sf_do_ecne
2856 *
2857 * Section: Appendix A: Explicit Congestion Notification
2858 *
2859 * ECN-Echo
2860 *
2861 * RFC 2481 details a specific bit for a receiver to send back in its
2862 * TCP acknowledgements to notify the sender of the Congestion
2863 * Experienced (CE) bit having arrived from the network. For SCTP this
2864 * same indication is made by including the ECNE chunk. This chunk
2865 * contains one data element, i.e. the lowest TSN associated with the IP
2866 * datagram marked with the CE bit.....
2867 *
2868 * Verification Tag: 8.5 Verification Tag [Normal verification]
2869 * Inputs
2870 * (endpoint, asoc, chunk)
2871 *
2872 * Outputs
2873 * (asoc, reply_msg, msg_up, timers, counters)
2874 *
2875 * The return value is the disposition of the chunk.
2876 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002877sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2878 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 const struct sctp_association *asoc,
2880 const sctp_subtype_t type,
2881 void *arg,
2882 sctp_cmd_seq_t *commands)
2883{
2884 sctp_ecnehdr_t *ecne;
2885 struct sctp_chunk *chunk = arg;
2886
2887 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002888 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002891 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 commands);
2893
2894 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2895 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2896
2897 /* If this is a newer ECNE than the last CWR packet we sent out */
2898 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2899 SCTP_U32(ntohl(ecne->lowest_tsn)));
2900
2901 return SCTP_DISPOSITION_CONSUME;
2902}
2903
2904/*
2905 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2906 *
2907 * The SCTP endpoint MUST always acknowledge the reception of each valid
2908 * DATA chunk.
2909 *
2910 * The guidelines on delayed acknowledgement algorithm specified in
2911 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2912 * acknowledgement SHOULD be generated for at least every second packet
2913 * (not every second DATA chunk) received, and SHOULD be generated within
2914 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2915 * situations it may be beneficial for an SCTP transmitter to be more
2916 * conservative than the algorithms detailed in this document allow.
2917 * However, an SCTP transmitter MUST NOT be more aggressive than the
2918 * following algorithms allow.
2919 *
2920 * A SCTP receiver MUST NOT generate more than one SACK for every
2921 * incoming packet, other than to update the offered window as the
2922 * receiving application consumes new data.
2923 *
2924 * Verification Tag: 8.5 Verification Tag [Normal verification]
2925 *
2926 * Inputs
2927 * (endpoint, asoc, chunk)
2928 *
2929 * Outputs
2930 * (asoc, reply_msg, msg_up, timers, counters)
2931 *
2932 * The return value is the disposition of the chunk.
2933 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002934sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2935 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 const struct sctp_association *asoc,
2937 const sctp_subtype_t type,
2938 void *arg,
2939 sctp_cmd_seq_t *commands)
2940{
2941 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002942 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 int error;
2944
2945 if (!sctp_vtag_verify(chunk, asoc)) {
2946 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2947 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002948 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002952 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 commands);
2954
wangweidongcb3f8372013-12-23 12:16:50 +08002955 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 switch (error) {
2957 case SCTP_IERROR_NO_ERROR:
2958 break;
2959 case SCTP_IERROR_HIGH_TSN:
2960 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002961 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 goto discard_noforce;
2963 case SCTP_IERROR_DUP_TSN:
2964 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002965 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 goto discard_force;
2967 case SCTP_IERROR_NO_DATA:
2968 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002969 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002970 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002971 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 default:
2973 BUG();
2974 }
2975
Wei Yongjun6dc76942009-11-23 15:53:53 -05002976 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2977 force = SCTP_FORCE();
2978
Neil Horman9f70f462013-12-10 06:48:15 -05002979 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2981 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2982 }
2983
2984 /* If this is the last chunk in a packet, we need to count it
2985 * toward sack generation. Note that we need to SACK every
2986 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2987 * THEM. We elect to NOT generate SACK's if the chunk fails
2988 * the verification tag test.
2989 *
2990 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2991 *
2992 * The SCTP endpoint MUST always acknowledge the reception of
2993 * each valid DATA chunk.
2994 *
2995 * The guidelines on delayed acknowledgement algorithm
2996 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2997 * Specifically, an acknowledgement SHOULD be generated for at
2998 * least every second packet (not every second DATA chunk)
2999 * received, and SHOULD be generated within 200 ms of the
3000 * arrival of any unacknowledged DATA chunk. In some
3001 * situations it may be beneficial for an SCTP transmitter to
3002 * be more conservative than the algorithms detailed in this
3003 * document allow. However, an SCTP transmitter MUST NOT be
3004 * more aggressive than the following algorithms allow.
3005 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003006 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003007 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return SCTP_DISPOSITION_CONSUME;
3010
3011discard_force:
3012 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3013 *
3014 * When a packet arrives with duplicate DATA chunk(s) and with
3015 * no new DATA chunk(s), the endpoint MUST immediately send a
3016 * SACK with no delay. If a packet arrives with duplicate
3017 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3018 * MAY immediately send a SACK. Normally receipt of duplicate
3019 * DATA chunks will occur when the original SACK chunk was lost
3020 * and the peer's RTO has expired. The duplicate TSN number(s)
3021 * SHOULD be reported in the SACK as duplicate.
3022 */
3023 /* In our case, we split the MAY SACK advice up whether or not
3024 * the last chunk is a duplicate.'
3025 */
3026 if (chunk->end_of_packet)
3027 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3028 return SCTP_DISPOSITION_DISCARD;
3029
3030discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003031 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003032 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return SCTP_DISPOSITION_DISCARD;
3035consume:
3036 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
3039
3040/*
3041 * sctp_sf_eat_data_fast_4_4
3042 *
3043 * Section: 4 (4)
3044 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3045 * DATA chunks without delay.
3046 *
3047 * Verification Tag: 8.5 Verification Tag [Normal verification]
3048 * Inputs
3049 * (endpoint, asoc, chunk)
3050 *
3051 * Outputs
3052 * (asoc, reply_msg, msg_up, timers, counters)
3053 *
3054 * The return value is the disposition of the chunk.
3055 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003056sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3057 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 const struct sctp_association *asoc,
3059 const sctp_subtype_t type,
3060 void *arg,
3061 sctp_cmd_seq_t *commands)
3062{
3063 struct sctp_chunk *chunk = arg;
3064 int error;
3065
3066 if (!sctp_vtag_verify(chunk, asoc)) {
3067 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3068 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003069 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071
3072 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003073 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 commands);
3075
wangweidongcb3f8372013-12-23 12:16:50 +08003076 error = sctp_eat_data(asoc, chunk, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 switch (error) {
3078 case SCTP_IERROR_NO_ERROR:
3079 case SCTP_IERROR_HIGH_TSN:
3080 case SCTP_IERROR_DUP_TSN:
3081 case SCTP_IERROR_IGNORE_TSN:
3082 case SCTP_IERROR_BAD_STREAM:
3083 break;
3084 case SCTP_IERROR_NO_DATA:
3085 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003086 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003087 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003088 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 default:
3090 BUG();
3091 }
3092
3093 /* Go a head and force a SACK, since we are shutting down. */
3094
3095 /* Implementor's Guide.
3096 *
3097 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3098 * respond to each received packet containing one or more DATA chunk(s)
3099 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3100 */
3101 if (chunk->end_of_packet) {
3102 /* We must delay the chunk creation since the cumulative
3103 * TSN has not been updated yet.
3104 */
3105 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3106 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3107 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3108 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3109 }
3110
3111consume:
3112 return SCTP_DISPOSITION_CONSUME;
3113}
3114
3115/*
3116 * Section: 6.2 Processing a Received SACK
3117 * D) Any time a SACK arrives, the endpoint performs the following:
3118 *
3119 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3120 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3121 * increasing, a SACK whose Cumulative TSN Ack is less than the
3122 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3123 *
3124 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3125 * of bytes still outstanding after processing the Cumulative TSN Ack
3126 * and the Gap Ack Blocks.
3127 *
3128 * iii) If the SACK is missing a TSN that was previously
3129 * acknowledged via a Gap Ack Block (e.g., the data receiver
3130 * reneged on the data), then mark the corresponding DATA chunk
3131 * as available for retransmit: Mark it as missing for fast
3132 * retransmit as described in Section 7.2.4 and if no retransmit
3133 * timer is running for the destination address to which the DATA
3134 * chunk was originally transmitted, then T3-rtx is started for
3135 * that destination address.
3136 *
3137 * Verification Tag: 8.5 Verification Tag [Normal verification]
3138 *
3139 * Inputs
3140 * (endpoint, asoc, chunk)
3141 *
3142 * Outputs
3143 * (asoc, reply_msg, msg_up, timers, counters)
3144 *
3145 * The return value is the disposition of the chunk.
3146 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003147sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3148 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 const struct sctp_association *asoc,
3150 const sctp_subtype_t type,
3151 void *arg,
3152 sctp_cmd_seq_t *commands)
3153{
3154 struct sctp_chunk *chunk = arg;
3155 sctp_sackhdr_t *sackh;
3156 __u32 ctsn;
3157
3158 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003159 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* Make sure that the SACK chunk has a valid length. */
3162 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003163 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 commands);
3165
3166 /* Pull the SACK chunk from the data buffer */
3167 sackh = sctp_sm_pull_sack(chunk);
3168 /* Was this a bogus SACK? */
3169 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 chunk->subh.sack_hdr = sackh;
3172 ctsn = ntohl(sackh->cum_tsn_ack);
3173
3174 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3175 * Ack Point, then drop the SACK. Since Cumulative TSN
3176 * Ack is monotonically increasing, a SACK whose
3177 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3178 * Point indicates an out-of-order SACK.
3179 */
3180 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02003181 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3182 asoc->ctsn_ack_point);
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return SCTP_DISPOSITION_DISCARD;
3185 }
3186
Wei Yongjunaecedea2007-08-02 16:57:44 +08003187 /* If Cumulative TSN Ack beyond the max tsn currently
3188 * send, terminating the association and respond to the
3189 * sender with an ABORT.
3190 */
3191 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003192 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003195 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196
3197 /* Note: We do the rest of the work on the PROCESS_SACK
3198 * sideeffect.
3199 */
3200 return SCTP_DISPOSITION_CONSUME;
3201}
3202
3203/*
3204 * Generate an ABORT in response to a packet.
3205 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003206 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003208 * 8) The receiver should respond to the sender of the OOTB packet with
3209 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3210 * MUST fill in the Verification Tag field of the outbound packet
3211 * with the value found in the Verification Tag field of the OOTB
3212 * packet and set the T-bit in the Chunk Flags to indicate that the
3213 * Verification Tag is reflected. After sending this ABORT, the
3214 * receiver of the OOTB packet shall discard the OOTB packet and take
3215 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 *
3217 * Verification Tag:
3218 *
3219 * The return value is the disposition of the chunk.
3220*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003221static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3222 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 const struct sctp_association *asoc,
3224 const sctp_subtype_t type,
3225 void *arg,
3226 sctp_cmd_seq_t *commands)
3227{
3228 struct sctp_packet *packet = NULL;
3229 struct sctp_chunk *chunk = arg;
3230 struct sctp_chunk *abort;
3231
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003232 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 if (packet) {
3235 /* Make an ABORT. The T bit will be set if the asoc
3236 * is NULL.
3237 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003238 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 if (!abort) {
3240 sctp_ootb_pkt_free(packet);
3241 return SCTP_DISPOSITION_NOMEM;
3242 }
3243
Jerome Forissier047a2422005-04-28 11:58:43 -07003244 /* Reflect vtag if T-Bit is set */
3245 if (sctp_test_T_bit(abort))
3246 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3247
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 /* Set the skb to the belonging sock for accounting. */
3249 abort->skb->sk = ep->base.sk;
3250
3251 sctp_packet_append_chunk(packet, abort);
3252
3253 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3254 SCTP_PACKET(packet));
3255
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003256 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003258 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return SCTP_DISPOSITION_CONSUME;
3260 }
3261
3262 return SCTP_DISPOSITION_NOMEM;
3263}
3264
3265/*
3266 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3267 * event as ULP notification for each cause included in the chunk.
3268 *
3269 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3270 *
3271 * The return value is the disposition of the chunk.
3272*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003273sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3274 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 const struct sctp_association *asoc,
3276 const sctp_subtype_t type,
3277 void *arg,
3278 sctp_cmd_seq_t *commands)
3279{
3280 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003281 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003284 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 /* Make sure that the ERROR chunk has a valid length. */
3287 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003288 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003290 sctp_walk_errors(err, chunk->chunk_hdr);
3291 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003292 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003293 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Wei Yongjun3df26782009-03-02 06:46:51 +00003295 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3296 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299}
3300
3301/*
3302 * Process an inbound SHUTDOWN ACK.
3303 *
3304 * From Section 9.2:
3305 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3306 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3307 * peer, and remove all record of the association.
3308 *
3309 * The return value is the disposition.
3310 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003311sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3312 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 const struct sctp_association *asoc,
3314 const sctp_subtype_t type,
3315 void *arg,
3316 sctp_cmd_seq_t *commands)
3317{
3318 struct sctp_chunk *chunk = arg;
3319 struct sctp_chunk *reply;
3320 struct sctp_ulpevent *ev;
3321
3322 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003323 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
3325 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3326 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003327 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /* 10.2 H) SHUTDOWN COMPLETE notification
3330 *
3331 * When SCTP completes the shutdown procedures (section 9.2) this
3332 * notification is passed to the upper layer.
3333 */
3334 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003335 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (!ev)
3337 goto nomem;
3338
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003339 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3340 reply = sctp_make_shutdown_complete(asoc, chunk);
3341 if (!reply)
3342 goto nomem_chunk;
3343
3344 /* Do all the commands now (after allocation), so that we
3345 * have consistent state if memory allocation failes
3346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3348
3349 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3350 * stop the T2-shutdown timer,
3351 */
3352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3353 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3354
3355 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3356 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3359 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003360 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3361 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3363
3364 /* ...and remove all record of the association. */
3365 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3366 return SCTP_DISPOSITION_DELETE_TCB;
3367
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003368nomem_chunk:
3369 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370nomem:
3371 return SCTP_DISPOSITION_NOMEM;
3372}
3373
3374/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003375 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3376 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3378 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3379 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3380 * packet must fill in the Verification Tag field of the outbound
3381 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003382 * set the T-bit in the Chunk Flags to indicate that the Verification
3383 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 *
3385 * 8) The receiver should respond to the sender of the OOTB packet with
3386 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3387 * MUST fill in the Verification Tag field of the outbound packet
3388 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003389 * packet and set the T-bit in the Chunk Flags to indicate that the
3390 * Verification Tag is reflected. After sending this ABORT, the
3391 * receiver of the OOTB packet shall discard the OOTB packet and take
3392 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003394sctp_disposition_t sctp_sf_ootb(struct net *net,
3395 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 const struct sctp_association *asoc,
3397 const sctp_subtype_t type,
3398 void *arg,
3399 sctp_cmd_seq_t *commands)
3400{
3401 struct sctp_chunk *chunk = arg;
3402 struct sk_buff *skb = chunk->skb;
3403 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003404 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 __u8 *ch_end;
3406 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003407 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003409 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
3411 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3412 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003413 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003415 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003416 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417
Vlad Yasevichece25df2007-09-07 16:30:54 -04003418 /* Now that we know we at least have a chunk header,
3419 * do things that are type appropriate.
3420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3422 ootb_shut_ack = 1;
3423
3424 /* RFC 2960, Section 3.3.7
3425 * Moreover, under any circumstances, an endpoint that
3426 * receives an ABORT MUST NOT respond to that ABORT by
3427 * sending an ABORT of its own.
3428 */
3429 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003430 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003431
Shan Wei85c5ed42011-04-19 21:30:01 +00003432 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3433 * or a COOKIE ACK the SCTP Packet should be silently
3434 * discarded.
3435 */
3436
3437 if (SCTP_CID_COOKIE_ACK == ch->type)
3438 ootb_cookie_ack = 1;
3439
3440 if (SCTP_CID_ERROR == ch->type) {
3441 sctp_walk_errors(err, ch) {
3442 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3443 ootb_cookie_ack = 1;
3444 break;
3445 }
3446 }
3447 }
3448
Vlad Yasevichece25df2007-09-07 16:30:54 -04003449 /* Report violation if chunk len overflows */
3450 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3451 if (ch_end > skb_tail_pointer(skb))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003452 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003453 commands);
3454
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003456 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003459 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003460 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003463 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464}
3465
3466/*
3467 * Handle an "Out of the blue" SHUTDOWN ACK.
3468 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003469 * Section: 8.4 5, sctpimpguide 2.41.
3470 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003472 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3473 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3474 * packet must fill in the Verification Tag field of the outbound
3475 * packet with the Verification Tag received in the SHUTDOWN ACK and
3476 * set the T-bit in the Chunk Flags to indicate that the Verification
3477 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 *
3479 * Inputs
3480 * (endpoint, asoc, type, arg, commands)
3481 *
3482 * Outputs
3483 * (sctp_disposition_t)
3484 *
3485 * The return value is the disposition of the chunk.
3486 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003487static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3488 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 const struct sctp_association *asoc,
3490 const sctp_subtype_t type,
3491 void *arg,
3492 sctp_cmd_seq_t *commands)
3493{
3494 struct sctp_packet *packet = NULL;
3495 struct sctp_chunk *chunk = arg;
3496 struct sctp_chunk *shut;
3497
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003498 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
3500 if (packet) {
3501 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003502 * The T bit will be set if the asoc is NULL.
3503 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 shut = sctp_make_shutdown_complete(asoc, chunk);
3505 if (!shut) {
3506 sctp_ootb_pkt_free(packet);
3507 return SCTP_DISPOSITION_NOMEM;
3508 }
3509
Jerome Forissier047a2422005-04-28 11:58:43 -07003510 /* Reflect vtag if T-Bit is set */
3511 if (sctp_test_T_bit(shut))
3512 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3513
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 /* Set the skb to the belonging sock for accounting. */
3515 shut->skb->sk = ep->base.sk;
3516
3517 sctp_packet_append_chunk(packet, shut);
3518
3519 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3520 SCTP_PACKET(packet));
3521
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003522 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
3524 /* If the chunk length is invalid, we don't want to process
3525 * the reset of the packet.
3526 */
3527 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003528 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003530 /* We need to discard the rest of the packet to prevent
3531 * potential bomming attacks from additional bundled chunks.
3532 * This is documented in SCTP Threats ID.
3533 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003534 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 }
3536
3537 return SCTP_DISPOSITION_NOMEM;
3538}
3539
3540/*
3541 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3542 *
3543 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3544 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3545 * procedures in section 8.4 SHOULD be followed, in other words it
3546 * should be treated as an Out Of The Blue packet.
3547 * [This means that we do NOT check the Verification Tag on these
3548 * chunks. --piggy ]
3549 *
3550 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003551sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3552 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 const struct sctp_association *asoc,
3554 const sctp_subtype_t type,
3555 void *arg,
3556 sctp_cmd_seq_t *commands)
3557{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003558 struct sctp_chunk *chunk = arg;
3559
3560 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3561 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003562 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003563 commands);
3564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 /* Although we do have an association in this case, it corresponds
3566 * to a restarted association. So the packet is treated as an OOTB
3567 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3568 * called with a NULL association.
3569 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003570 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003571
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003572 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573}
3574
3575/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003576sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3577 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 const struct sctp_association *asoc,
3579 const sctp_subtype_t type, void *arg,
3580 sctp_cmd_seq_t *commands)
3581{
3582 struct sctp_chunk *chunk = arg;
3583 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003584 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003586 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003588 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589
3590 if (!sctp_vtag_verify(chunk, asoc)) {
3591 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3592 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003593 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 }
3595
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003596 /* ADD-IP: Section 4.1.1
3597 * This chunk MUST be sent in an authenticated way by using
3598 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3599 * is received unauthenticated it MUST be silently discarded as
3600 * described in [I-D.ietf-tsvwg-sctp-auth].
3601 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003602 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003603 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3606 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003607 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 commands);
3609
3610 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3611 serial = ntohl(hdr->serial);
3612
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003613 addr_param = (union sctp_addr_param *)hdr->params;
3614 length = ntohs(addr_param->p.length);
3615 if (length < sizeof(sctp_paramhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003616 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003617 (void *)addr_param, commands);
3618
3619 /* Verify the ASCONF chunk before processing it. */
3620 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003621 (sctp_paramhdr_t *)((void *)addr_param + length),
3622 (void *)chunk->chunk_end,
3623 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003624 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003625 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003626
Vlad Yasevicha08de642007-12-20 14:11:47 -08003627 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003629 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 */
3631 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003632 /* If this is the first instance of ASCONF in the packet,
3633 * we can clean our old ASCONF-ACKs.
3634 */
3635 if (!chunk->has_asconf)
3636 sctp_assoc_clean_asconf_ack_cache(asoc);
3637
3638 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3639 * expected, process the ASCONF as described below and after
3640 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3641 * the response packet and cache a copy of it (in the event it
3642 * later needs to be retransmitted).
3643 *
3644 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 */
3646 asconf_ack = sctp_process_asconf((struct sctp_association *)
3647 asoc, chunk);
3648 if (!asconf_ack)
3649 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003650 } else if (serial < asoc->peer.addip_serial + 1) {
3651 /* ADDIP 5.2 E2)
3652 * If the value found in the Sequence Number is less than the
3653 * ('Peer- Sequence-Number' + 1), simply skip to the next
3654 * ASCONF, and include in the outbound response packet
3655 * any previously cached ASCONF-ACK response that was
3656 * sent and saved that matches the Sequence Number of the
3657 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3658 * Chunk exists. This will occur when an older ASCONF
3659 * arrives out of order. In such a case, the receiver
3660 * should skip the ASCONF Chunk and not include ASCONF-ACK
3661 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003663 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3664 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003666
3667 /* Reset the transport so that we select the correct one
3668 * this time around. This is to make sure that we don't
3669 * accidentally use a stale transport that's been removed.
3670 */
3671 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003673 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003675 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return SCTP_DISPOSITION_DISCARD;
3677 }
3678
Vlad Yasevicha08de642007-12-20 14:11:47 -08003679 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3680 * containing the ASCONF-ACK Chunks MUST be the source address of
3681 * the SCTP packet that held the ASCONF Chunks.
3682 *
3683 * To do this properly, we'll set the destination address of the chunk
3684 * and at the transmit time, will try look up the transport to use.
3685 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003686 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003688 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003690 if (asoc->new_transport) {
wangweidongf7010e62013-12-23 12:16:52 +08003691 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
Michio Honda6af29cc2011-06-16 17:14:34 +09003692 ((struct sctp_association *)asoc)->new_transport = NULL;
3693 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003694
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return SCTP_DISPOSITION_CONSUME;
3696}
3697
3698/*
3699 * ADDIP Section 4.3 General rules for address manipulation
3700 * When building TLV parameters for the ASCONF Chunk that will add or
3701 * delete IP addresses the D0 to D13 rules should be applied:
3702 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003703sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3704 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003706 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 sctp_cmd_seq_t *commands)
3708{
3709 struct sctp_chunk *asconf_ack = arg;
3710 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3711 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003712 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 sctp_addiphdr_t *addip_hdr;
3714 __u32 sent_serial, rcvd_serial;
3715
3716 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3717 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3718 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003719 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003722 /* ADD-IP, Section 4.1.2:
3723 * This chunk MUST be sent in an authenticated way by using
3724 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3725 * is received unauthenticated it MUST be silently discarded as
3726 * described in [I-D.ietf-tsvwg-sctp-auth].
3727 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003728 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003729 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* Make sure that the ADDIP chunk has a valid length. */
3732 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003733 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 commands);
3735
3736 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3737 rcvd_serial = ntohl(addip_hdr->serial);
3738
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003739 /* Verify the ASCONF-ACK chunk before processing it. */
3740 if (!sctp_verify_asconf(asoc,
3741 (sctp_paramhdr_t *)addip_hdr->params,
3742 (void *)asconf_ack->chunk_end,
3743 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003744 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003745 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 if (last_asconf) {
3748 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3749 sent_serial = ntohl(addip_hdr->serial);
3750 } else {
3751 sent_serial = asoc->addip_serial - 1;
3752 }
3753
3754 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3755 * equal to the next serial number to be used but no ASCONF chunk is
3756 * outstanding the endpoint MUST ABORT the association. Note that a
3757 * sequence number is greater than if it is no more than 2^^31-1
3758 * larger than the current sequence number (using serial arithmetic).
3759 */
3760 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3761 !(asoc->addip_last_asconf)) {
3762 abort = sctp_make_abort(asoc, asconf_ack,
3763 sizeof(sctp_errhdr_t));
3764 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003765 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3767 SCTP_CHUNK(abort));
3768 }
3769 /* We are going to ABORT, so we might as well stop
3770 * processing the rest of the chunks in the packet.
3771 */
3772 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3773 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
wangweidongcb3f8372013-12-23 12:16:50 +08003774 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003775 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003776 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003778 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003779 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3780 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 return SCTP_DISPOSITION_ABORT;
3782 }
3783
3784 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3785 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3786 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3787
3788 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003789 asconf_ack)) {
3790 /* Successfully processed ASCONF_ACK. We can
3791 * release the next asconf if we have one.
3792 */
3793 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3794 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 abort = sctp_make_abort(asoc, asconf_ack,
3799 sizeof(sctp_errhdr_t));
3800 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003801 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3803 SCTP_CHUNK(abort));
3804 }
3805 /* We are going to ABORT, so we might as well stop
3806 * processing the rest of the chunks in the packet.
3807 */
wangweidongcb3f8372013-12-23 12:16:50 +08003808 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003809 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003810 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003812 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003813 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3814 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 return SCTP_DISPOSITION_ABORT;
3816 }
3817
3818 return SCTP_DISPOSITION_DISCARD;
3819}
3820
3821/*
3822 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3823 *
3824 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3825 * its cumulative TSN point to the value carried in the FORWARD TSN
3826 * chunk, and then MUST further advance its cumulative TSN point locally
3827 * if possible.
3828 * After the above processing, the data receiver MUST stop reporting any
3829 * missing TSNs earlier than or equal to the new cumulative TSN point.
3830 *
3831 * Verification Tag: 8.5 Verification Tag [Normal verification]
3832 *
3833 * The return value is the disposition of the chunk.
3834 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003835sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3836 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 const struct sctp_association *asoc,
3838 const sctp_subtype_t type,
3839 void *arg,
3840 sctp_cmd_seq_t *commands)
3841{
3842 struct sctp_chunk *chunk = arg;
3843 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003844 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 __u16 len;
3846 __u32 tsn;
3847
3848 if (!sctp_vtag_verify(chunk, asoc)) {
3849 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3850 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003851 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853
3854 /* Make sure that the FORWARD_TSN chunk has valid length. */
3855 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003856 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 commands);
3858
3859 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3860 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3861 len = ntohs(chunk->chunk_hdr->length);
3862 len -= sizeof(struct sctp_chunkhdr);
3863 skb_pull(chunk->skb, len);
3864
3865 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003866 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 /* The TSN is too high--silently discard the chunk and count on it
3869 * getting retransmitted later.
3870 */
3871 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3872 goto discard_noforce;
3873
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003874 /* Silently discard the chunk if stream-id is not valid */
3875 sctp_walk_fwdtsn(skip, chunk) {
3876 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3877 goto discard_noforce;
3878 }
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3881 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003882 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 /* Count this as receiving DATA. */
Neil Horman9f70f462013-12-10 06:48:15 -05003886 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3888 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3889 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003892 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 */
3894 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 return SCTP_DISPOSITION_CONSUME;
3897
3898discard_noforce:
3899 return SCTP_DISPOSITION_DISCARD;
3900}
3901
3902sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003903 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 const struct sctp_endpoint *ep,
3905 const struct sctp_association *asoc,
3906 const sctp_subtype_t type,
3907 void *arg,
3908 sctp_cmd_seq_t *commands)
3909{
3910 struct sctp_chunk *chunk = arg;
3911 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003912 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 __u16 len;
3914 __u32 tsn;
3915
3916 if (!sctp_vtag_verify(chunk, asoc)) {
3917 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3918 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003919 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 }
3921
3922 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3923 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003924 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 commands);
3926
3927 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3928 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3929 len = ntohs(chunk->chunk_hdr->length);
3930 len -= sizeof(struct sctp_chunkhdr);
3931 skb_pull(chunk->skb, len);
3932
3933 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02003934 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935
3936 /* The TSN is too high--silently discard the chunk and count on it
3937 * getting retransmitted later.
3938 */
3939 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3940 goto gen_shutdown;
3941
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003942 /* Silently discard the chunk if stream-id is not valid */
3943 sctp_walk_fwdtsn(skip, chunk) {
3944 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3945 goto gen_shutdown;
3946 }
3947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3949 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003950 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 /* Go a head and force a SACK, since we are shutting down. */
3954gen_shutdown:
3955 /* Implementor's Guide.
3956 *
3957 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3958 * respond to each received packet containing one or more DATA chunk(s)
3959 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3960 */
3961 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3962 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3963 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3964 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3965
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003966 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967}
3968
3969/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003970 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003971 *
3972 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3973 * Identifier field. If this algorithm was not specified by the
3974 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3975 * during association setup, the AUTH chunk and all chunks after it MUST
3976 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3977 * defined in Section 4.1.
3978 *
3979 * If an endpoint with no shared key receives a Shared Key Identifier
3980 * other than 0, it MUST silently discard all authenticated chunks. If
3981 * the endpoint has at least one endpoint pair shared key for the peer,
3982 * it MUST use the key specified by the Shared Key Identifier if a
3983 * key has been configured for that Shared Key Identifier. If no
3984 * endpoint pair shared key has been configured for that Shared Key
3985 * Identifier, all authenticated chunks MUST be silently discarded.
3986 *
3987 * Verification Tag: 8.5 Verification Tag [Normal verification]
3988 *
3989 * The return value is the disposition of the chunk.
3990 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003991static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3992 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003993 const struct sctp_association *asoc,
3994 const sctp_subtype_t type,
3995 struct sctp_chunk *chunk)
3996{
3997 struct sctp_authhdr *auth_hdr;
3998 struct sctp_hmac *hmac;
3999 unsigned int sig_len;
4000 __u16 key_id;
4001 __u8 *save_digest;
4002 __u8 *digest;
4003
4004 /* Pull in the auth header, so we can do some more verification */
4005 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4006 chunk->subh.auth_hdr = auth_hdr;
4007 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4008
Masanari Iida02582e92012-08-22 19:11:26 +09004009 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004010 * chunk.
4011 */
4012 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4013 return SCTP_IERROR_AUTH_BAD_HMAC;
4014
4015 /* Make sure that the provided shared key identifier has been
4016 * configured
4017 */
4018 key_id = ntohs(auth_hdr->shkey_id);
4019 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4020 return SCTP_IERROR_AUTH_BAD_KEYID;
4021
4022
4023 /* Make sure that the length of the signature matches what
4024 * we expect.
4025 */
4026 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4027 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4028 if (sig_len != hmac->hmac_len)
4029 return SCTP_IERROR_PROTO_VIOLATION;
4030
4031 /* Now that we've done validation checks, we can compute and
4032 * verify the hmac. The steps involved are:
4033 * 1. Save the digest from the chunk.
4034 * 2. Zero out the digest in the chunk.
4035 * 3. Compute the new digest
4036 * 4. Compare saved and new digests.
4037 */
4038 digest = auth_hdr->hmac;
4039 skb_pull(chunk->skb, sig_len);
4040
4041 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4042 if (!save_digest)
4043 goto nomem;
4044
4045 memset(digest, 0, sig_len);
4046
4047 sctp_auth_calculate_hmac(asoc, chunk->skb,
4048 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4049 GFP_ATOMIC);
4050
4051 /* Discard the packet if the digests do not match */
4052 if (memcmp(save_digest, digest, sig_len)) {
4053 kfree(save_digest);
4054 return SCTP_IERROR_BAD_SIG;
4055 }
4056
4057 kfree(save_digest);
4058 chunk->auth = 1;
4059
4060 return SCTP_IERROR_NO_ERROR;
4061nomem:
4062 return SCTP_IERROR_NOMEM;
4063}
4064
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004065sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4066 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004067 const struct sctp_association *asoc,
4068 const sctp_subtype_t type,
4069 void *arg,
4070 sctp_cmd_seq_t *commands)
4071{
4072 struct sctp_authhdr *auth_hdr;
4073 struct sctp_chunk *chunk = arg;
4074 struct sctp_chunk *err_chunk;
4075 sctp_ierror_t error;
4076
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004077 /* Make sure that the peer has AUTH capable */
4078 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004079 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004080
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004081 if (!sctp_vtag_verify(chunk, asoc)) {
4082 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4083 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004084 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004085 }
4086
4087 /* Make sure that the AUTH chunk has valid length. */
4088 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004089 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004090 commands);
4091
4092 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004093 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004094 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004095 case SCTP_IERROR_AUTH_BAD_HMAC:
4096 /* Generate the ERROR chunk and discard the rest
4097 * of the packet
4098 */
4099 err_chunk = sctp_make_op_error(asoc, chunk,
4100 SCTP_ERROR_UNSUP_HMAC,
4101 &auth_hdr->hmac_id,
4102 sizeof(__u16), 0);
4103 if (err_chunk) {
4104 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4105 SCTP_CHUNK(err_chunk));
4106 }
4107 /* Fall Through */
4108 case SCTP_IERROR_AUTH_BAD_KEYID:
4109 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004110 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004111
4112 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004113 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004114 commands);
4115
4116 case SCTP_IERROR_NOMEM:
4117 return SCTP_DISPOSITION_NOMEM;
4118
4119 default: /* Prevent gcc warnings */
4120 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004121 }
4122
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004123 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4124 struct sctp_ulpevent *ev;
4125
4126 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4127 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4128
4129 if (!ev)
4130 return -ENOMEM;
4131
4132 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4133 SCTP_ULPEVENT(ev));
4134 }
4135
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004136 return SCTP_DISPOSITION_CONSUME;
4137}
4138
4139/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 * Process an unknown chunk.
4141 *
4142 * Section: 3.2. Also, 2.1 in the implementor's guide.
4143 *
4144 * Chunk Types are encoded such that the highest-order two bits specify
4145 * the action that must be taken if the processing endpoint does not
4146 * recognize the Chunk Type.
4147 *
4148 * 00 - Stop processing this SCTP packet and discard it, do not process
4149 * any further chunks within it.
4150 *
4151 * 01 - Stop processing this SCTP packet and discard it, do not process
4152 * any further chunks within it, and report the unrecognized
4153 * chunk in an 'Unrecognized Chunk Type'.
4154 *
4155 * 10 - Skip this chunk and continue processing.
4156 *
4157 * 11 - Skip this chunk and continue processing, but report in an ERROR
4158 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4159 *
4160 * The return value is the disposition of the chunk.
4161 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004162sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4163 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 const struct sctp_association *asoc,
4165 const sctp_subtype_t type,
4166 void *arg,
4167 sctp_cmd_seq_t *commands)
4168{
4169 struct sctp_chunk *unk_chunk = arg;
4170 struct sctp_chunk *err_chunk;
4171 sctp_chunkhdr_t *hdr;
4172
Daniel Borkmannbb333812013-06-28 19:49:40 +02004173 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004176 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
4178 /* Make sure that the chunk has a valid length.
4179 * Since we don't know the chunk type, we use a general
4180 * chunkhdr structure to make a comparison.
4181 */
4182 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004183 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 commands);
4185
4186 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4187 case SCTP_CID_ACTION_DISCARD:
4188 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004189 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 break;
4191 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 /* Generate an ERROR chunk as response. */
4193 hdr = unk_chunk->chunk_hdr;
4194 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4195 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004196 WORD_ROUND(ntohs(hdr->length)),
4197 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 if (err_chunk) {
4199 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4200 SCTP_CHUNK(err_chunk));
4201 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004202
4203 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004204 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 return SCTP_DISPOSITION_CONSUME;
4206 break;
4207 case SCTP_CID_ACTION_SKIP:
4208 /* Skip the chunk. */
4209 return SCTP_DISPOSITION_DISCARD;
4210 break;
4211 case SCTP_CID_ACTION_SKIP_ERR:
4212 /* Generate an ERROR chunk as response. */
4213 hdr = unk_chunk->chunk_hdr;
4214 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4215 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004216 WORD_ROUND(ntohs(hdr->length)),
4217 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (err_chunk) {
4219 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4220 SCTP_CHUNK(err_chunk));
4221 }
4222 /* Skip the chunk. */
4223 return SCTP_DISPOSITION_CONSUME;
4224 break;
4225 default:
4226 break;
4227 }
4228
4229 return SCTP_DISPOSITION_DISCARD;
4230}
4231
4232/*
4233 * Discard the chunk.
4234 *
4235 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4236 * [Too numerous to mention...]
4237 * Verification Tag: No verification needed.
4238 * Inputs
4239 * (endpoint, asoc, chunk)
4240 *
4241 * Outputs
4242 * (asoc, reply_msg, msg_up, timers, counters)
4243 *
4244 * The return value is the disposition of the chunk.
4245 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004246sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4247 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 const struct sctp_association *asoc,
4249 const sctp_subtype_t type,
4250 void *arg,
4251 sctp_cmd_seq_t *commands)
4252{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004253 struct sctp_chunk *chunk = arg;
4254
4255 /* Make sure that the chunk has a valid length.
4256 * Since we don't know the chunk type, we use a general
4257 * chunkhdr structure to make a comparison.
4258 */
4259 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004260 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004261 commands);
4262
Daniel Borkmannbb333812013-06-28 19:49:40 +02004263 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return SCTP_DISPOSITION_DISCARD;
4266}
4267
4268/*
4269 * Discard the whole packet.
4270 *
4271 * Section: 8.4 2)
4272 *
4273 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4274 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 *
4276 * Verification Tag: No verification necessary
4277 *
4278 * Inputs
4279 * (endpoint, asoc, chunk)
4280 *
4281 * Outputs
4282 * (asoc, reply_msg, msg_up, timers, counters)
4283 *
4284 * The return value is the disposition of the chunk.
4285 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004286sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4287 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 const struct sctp_association *asoc,
4289 const sctp_subtype_t type,
4290 void *arg,
4291 sctp_cmd_seq_t *commands)
4292{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004293 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4295
4296 return SCTP_DISPOSITION_CONSUME;
4297}
4298
4299
4300/*
4301 * The other end is violating protocol.
4302 *
4303 * Section: Not specified
4304 * Verification Tag: Not specified
4305 * Inputs
4306 * (endpoint, asoc, chunk)
4307 *
4308 * Outputs
4309 * (asoc, reply_msg, msg_up, timers, counters)
4310 *
4311 * We simply tag the chunk as a violation. The state machine will log
4312 * the violation and continue.
4313 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004314sctp_disposition_t sctp_sf_violation(struct net *net,
4315 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 const struct sctp_association *asoc,
4317 const sctp_subtype_t type,
4318 void *arg,
4319 sctp_cmd_seq_t *commands)
4320{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004321 struct sctp_chunk *chunk = arg;
4322
4323 /* Make sure that the chunk has a valid length. */
4324 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004325 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004326 commands);
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 return SCTP_DISPOSITION_VIOLATION;
4329}
4330
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004332 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004334static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004335 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004336 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004339 sctp_cmd_seq_t *commands,
4340 const __u8 *payload,
4341 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004343 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 struct sctp_chunk *chunk = arg;
4345 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004347 /* SCTP-AUTH, Section 6.3:
4348 * It should be noted that if the receiver wants to tear
4349 * down an association in an authenticated way only, the
4350 * handling of malformed packets should not result in
4351 * tearing down the association.
4352 *
4353 * This means that if we only want to abort associations
4354 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004355 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004356 * was malformed.
4357 */
4358 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4359 goto discard;
4360
Jesper Juhl9abed242007-11-11 23:57:49 +01004361 /* Make the abort chunk. */
4362 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4363 if (!abort)
4364 goto nomem;
4365
Vlad Yasevichece25df2007-09-07 16:30:54 -04004366 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004367 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4368 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4369 !asoc->peer.i.init_tag) {
4370 sctp_initack_chunk_t *initack;
4371
4372 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4373 if (!sctp_chunk_length_valid(chunk,
4374 sizeof(sctp_initack_chunk_t)))
4375 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4376 else {
4377 unsigned int inittag;
4378
4379 inittag = ntohl(initack->init_hdr.init_tag);
4380 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4381 SCTP_U32(inittag));
4382 }
4383 }
4384
Vlad Yasevichece25df2007-09-07 16:30:54 -04004385 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004386 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Vlad Yasevichece25df2007-09-07 16:30:54 -04004388 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4389 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4390 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4391 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4392 SCTP_ERROR(ECONNREFUSED));
4393 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4394 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4395 } else {
4396 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4397 SCTP_ERROR(ECONNABORTED));
4398 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4399 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004400 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004403 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004404
4405 if (!packet)
4406 goto nomem_pkt;
4407
4408 if (sctp_test_T_bit(abort))
4409 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4410
4411 abort->skb->sk = ep->base.sk;
4412
4413 sctp_packet_append_chunk(packet, abort);
4414
4415 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4416 SCTP_PACKET(packet));
4417
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004418 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 }
4420
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004421 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004422
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004423discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004424 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 return SCTP_DISPOSITION_ABORT;
4426
Vlad Yasevichece25df2007-09-07 16:30:54 -04004427nomem_pkt:
4428 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429nomem:
4430 return SCTP_DISPOSITION_NOMEM;
4431}
4432
Wei Yongjunaecedea2007-08-02 16:57:44 +08004433/*
4434 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004435 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004436 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004437 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004438 *
4439 * We inform the other end by sending an ABORT with a Protocol Violation
4440 * error code.
4441 *
4442 * Section: Not specified
4443 * Verification Tag: Nothing to do
4444 * Inputs
4445 * (endpoint, asoc, chunk)
4446 *
4447 * Outputs
4448 * (reply_msg, msg_up, counters)
4449 *
4450 * Generate an ABORT chunk and terminate the association.
4451 */
4452static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004453 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004454 const struct sctp_endpoint *ep,
4455 const struct sctp_association *asoc,
4456 const sctp_subtype_t type,
4457 void *arg,
4458 sctp_cmd_seq_t *commands)
4459{
wangweidongcb3f8372013-12-23 12:16:50 +08004460 static const char err_str[] = "The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004461
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004462 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004463 sizeof(err_str));
4464}
4465
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004466/*
4467 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004468 * If the length is smaller than the minimum length of a given parameter,
4469 * or accumulated length in multi parameters exceeds the end of the chunk,
4470 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004471 */
4472static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004473 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004474 const struct sctp_endpoint *ep,
4475 const struct sctp_association *asoc,
4476 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004477 void *arg, void *ext,
4478 sctp_cmd_seq_t *commands)
4479{
4480 struct sctp_chunk *chunk = arg;
4481 struct sctp_paramhdr *param = ext;
4482 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004483
Wei Yongjunba016672008-09-30 05:32:24 -07004484 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4485 goto discard;
4486
4487 /* Make the abort chunk. */
4488 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4489 if (!abort)
4490 goto nomem;
4491
4492 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004493 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004494
4495 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4496 SCTP_ERROR(ECONNABORTED));
4497 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4498 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004499 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4500 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004501
4502discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004503 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004504 return SCTP_DISPOSITION_ABORT;
4505nomem:
4506 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004507}
4508
Wei Yongjunaecedea2007-08-02 16:57:44 +08004509/* Handle a protocol violation when the peer trying to advance the
4510 * cumulative tsn ack to a point beyond the max tsn currently sent.
4511 *
4512 * We inform the other end by sending an ABORT with a Protocol Violation
4513 * error code.
4514 */
4515static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004516 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004517 const struct sctp_endpoint *ep,
4518 const struct sctp_association *asoc,
4519 const sctp_subtype_t type,
4520 void *arg,
4521 sctp_cmd_seq_t *commands)
4522{
wangweidongcb3f8372013-12-23 12:16:50 +08004523 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004524
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004525 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004526 sizeof(err_str));
4527}
4528
Vlad Yasevichece25df2007-09-07 16:30:54 -04004529/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004530 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004531 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004532 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004533 * on the path and we may not want to continue this communication.
4534 */
4535static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004536 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004537 const struct sctp_endpoint *ep,
4538 const struct sctp_association *asoc,
4539 const sctp_subtype_t type,
4540 void *arg,
4541 sctp_cmd_seq_t *commands)
4542{
wangweidongcb3f8372013-12-23 12:16:50 +08004543 static const char err_str[] = "The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004544
4545 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004546 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004547
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004548 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004549 sizeof(err_str));
4550}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551/***************************************************************************
4552 * These are the state functions for handling primitive (Section 10) events.
4553 ***************************************************************************/
4554/*
4555 * sctp_sf_do_prm_asoc
4556 *
4557 * Section: 10.1 ULP-to-SCTP
4558 * B) Associate
4559 *
4560 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4561 * outbound stream count)
4562 * -> association id [,destination transport addr list] [,outbound stream
4563 * count]
4564 *
4565 * This primitive allows the upper layer to initiate an association to a
4566 * specific peer endpoint.
4567 *
4568 * The peer endpoint shall be specified by one of the transport addresses
4569 * which defines the endpoint (see Section 1.4). If the local SCTP
4570 * instance has not been initialized, the ASSOCIATE is considered an
4571 * error.
4572 * [This is not relevant for the kernel implementation since we do all
4573 * initialization at boot time. It we hadn't initialized we wouldn't
4574 * get anywhere near this code.]
4575 *
4576 * An association id, which is a local handle to the SCTP association,
4577 * will be returned on successful establishment of the association. If
4578 * SCTP is not able to open an SCTP association with the peer endpoint,
4579 * an error is returned.
4580 * [In the kernel implementation, the struct sctp_association needs to
4581 * be created BEFORE causing this primitive to run.]
4582 *
4583 * Other association parameters may be returned, including the
4584 * complete destination transport addresses of the peer as well as the
4585 * outbound stream count of the local endpoint. One of the transport
4586 * address from the returned destination addresses will be selected by
4587 * the local endpoint as default primary path for sending SCTP packets
4588 * to this peer. The returned "destination transport addr list" can
4589 * be used by the ULP to change the default primary path or to force
4590 * sending a packet to a specific transport address. [All of this
4591 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4592 * function.]
4593 *
4594 * Mandatory attributes:
4595 *
4596 * o local SCTP instance name - obtained from the INITIALIZE operation.
4597 * [This is the argument asoc.]
4598 * o destination transport addr - specified as one of the transport
4599 * addresses of the peer endpoint with which the association is to be
4600 * established.
4601 * [This is asoc->peer.active_path.]
4602 * o outbound stream count - the number of outbound streams the ULP
4603 * would like to open towards this peer endpoint.
4604 * [BUG: This is not currently implemented.]
4605 * Optional attributes:
4606 *
4607 * None.
4608 *
4609 * The return value is a disposition.
4610 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004611sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4612 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 const struct sctp_association *asoc,
4614 const sctp_subtype_t type,
4615 void *arg,
4616 sctp_cmd_seq_t *commands)
4617{
4618 struct sctp_chunk *repl;
wangweidong26ac8e52013-12-23 12:16:51 +08004619 struct sctp_association *my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
4621 /* The comment below says that we enter COOKIE-WAIT AFTER
4622 * sending the INIT, but that doesn't actually work in our
4623 * implementation...
4624 */
4625 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4626 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4627
4628 /* RFC 2960 5.1 Normal Establishment of an Association
4629 *
4630 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4631 * must provide its Verification Tag (Tag_A) in the Initiate
4632 * Tag field. Tag_A SHOULD be a random number in the range of
4633 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4634 */
4635
4636 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4637 if (!repl)
4638 goto nomem;
4639
Daniel Borkmann405426f2013-06-14 18:24:05 +02004640 /* Choose transport for INIT. */
4641 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4642 SCTP_CHUNK(repl));
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 /* Cast away the const modifier, as we want to just
4645 * rerun it through as a sideffect.
4646 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004647 my_asoc = (struct sctp_association *)asoc;
4648 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 /* After sending the INIT, "A" starts the T1-init timer and
4651 * enters the COOKIE-WAIT state.
4652 */
4653 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4654 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4655 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4656 return SCTP_DISPOSITION_CONSUME;
4657
4658nomem:
4659 return SCTP_DISPOSITION_NOMEM;
4660}
4661
4662/*
4663 * Process the SEND primitive.
4664 *
4665 * Section: 10.1 ULP-to-SCTP
4666 * E) Send
4667 *
4668 * Format: SEND(association id, buffer address, byte count [,context]
4669 * [,stream id] [,life time] [,destination transport address]
4670 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4671 * -> result
4672 *
4673 * This is the main method to send user data via SCTP.
4674 *
4675 * Mandatory attributes:
4676 *
4677 * o association id - local handle to the SCTP association
4678 *
4679 * o buffer address - the location where the user message to be
4680 * transmitted is stored;
4681 *
4682 * o byte count - The size of the user data in number of bytes;
4683 *
4684 * Optional attributes:
4685 *
4686 * o context - an optional 32 bit integer that will be carried in the
4687 * sending failure notification to the ULP if the transportation of
4688 * this User Message fails.
4689 *
4690 * o stream id - to indicate which stream to send the data on. If not
4691 * specified, stream 0 will be used.
4692 *
4693 * o life time - specifies the life time of the user data. The user data
4694 * will not be sent by SCTP after the life time expires. This
4695 * parameter can be used to avoid efforts to transmit stale
4696 * user messages. SCTP notifies the ULP if the data cannot be
4697 * initiated to transport (i.e. sent to the destination via SCTP's
4698 * send primitive) within the life time variable. However, the
4699 * user data will be transmitted if SCTP has attempted to transmit a
4700 * chunk before the life time expired.
4701 *
4702 * o destination transport address - specified as one of the destination
4703 * transport addresses of the peer endpoint to which this packet
4704 * should be sent. Whenever possible, SCTP should use this destination
4705 * transport address for sending the packets, instead of the current
4706 * primary path.
4707 *
4708 * o unorder flag - this flag, if present, indicates that the user
4709 * would like the data delivered in an unordered fashion to the peer
4710 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4711 * message).
4712 *
4713 * o no-bundle flag - instructs SCTP not to bundle this user data with
4714 * other outbound DATA chunks. SCTP MAY still bundle even when
4715 * this flag is present, when faced with network congestion.
4716 *
4717 * o payload protocol-id - A 32 bit unsigned integer that is to be
4718 * passed to the peer indicating the type of payload protocol data
4719 * being transmitted. This value is passed as opaque data by SCTP.
4720 *
4721 * The return value is the disposition.
4722 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004723sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4724 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 const struct sctp_association *asoc,
4726 const sctp_subtype_t type,
4727 void *arg,
4728 sctp_cmd_seq_t *commands)
4729{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004730 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004732 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 return SCTP_DISPOSITION_CONSUME;
4734}
4735
4736/*
4737 * Process the SHUTDOWN primitive.
4738 *
4739 * Section: 10.1:
4740 * C) Shutdown
4741 *
4742 * Format: SHUTDOWN(association id)
4743 * -> result
4744 *
4745 * Gracefully closes an association. Any locally queued user data
4746 * will be delivered to the peer. The association will be terminated only
4747 * after the peer acknowledges all the SCTP packets sent. A success code
4748 * will be returned on successful termination of the association. If
4749 * attempting to terminate the association results in a failure, an error
4750 * code shall be returned.
4751 *
4752 * Mandatory attributes:
4753 *
4754 * o association id - local handle to the SCTP association
4755 *
4756 * Optional attributes:
4757 *
4758 * None.
4759 *
4760 * The return value is the disposition.
4761 */
4762sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004763 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 const struct sctp_endpoint *ep,
4765 const struct sctp_association *asoc,
4766 const sctp_subtype_t type,
4767 void *arg,
4768 sctp_cmd_seq_t *commands)
4769{
4770 int disposition;
4771
4772 /* From 9.2 Shutdown of an Association
4773 * Upon receipt of the SHUTDOWN primitive from its upper
4774 * layer, the endpoint enters SHUTDOWN-PENDING state and
4775 * remains there until all outstanding data has been
4776 * acknowledged by its peer. The endpoint accepts no new data
4777 * from its upper layer, but retransmits data to the far end
4778 * if necessary to fill gaps.
4779 */
4780 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4781 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4782
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 disposition = SCTP_DISPOSITION_CONSUME;
4784 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004785 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786 arg, commands);
4787 }
4788 return disposition;
4789}
4790
4791/*
4792 * Process the ABORT primitive.
4793 *
4794 * Section: 10.1:
4795 * C) Abort
4796 *
4797 * Format: Abort(association id [, cause code])
4798 * -> result
4799 *
4800 * Ungracefully closes an association. Any locally queued user data
4801 * will be discarded and an ABORT chunk is sent to the peer. A success code
4802 * will be returned on successful abortion of the association. If
4803 * attempting to abort the association results in a failure, an error
4804 * code shall be returned.
4805 *
4806 * Mandatory attributes:
4807 *
4808 * o association id - local handle to the SCTP association
4809 *
4810 * Optional attributes:
4811 *
4812 * o cause code - reason of the abort to be passed to the peer
4813 *
4814 * None.
4815 *
4816 * The return value is the disposition.
4817 */
4818sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004819 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 const struct sctp_endpoint *ep,
4821 const struct sctp_association *asoc,
4822 const sctp_subtype_t type,
4823 void *arg,
4824 sctp_cmd_seq_t *commands)
4825{
4826 /* From 9.1 Abort of an Association
4827 * Upon receipt of the ABORT primitive from its upper
4828 * layer, the endpoint enters CLOSED state and
4829 * discard all outstanding data has been
4830 * acknowledged by its peer. The endpoint accepts no new data
4831 * from its upper layer, but retransmits data to the far end
4832 * if necessary to fill gaps.
4833 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004834 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 sctp_disposition_t retval;
4836
4837 retval = SCTP_DISPOSITION_CONSUME;
4838
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004839 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 /* Even if we can't send the ABORT due to low memory delete the
4842 * TCB. This is a departure from our typical NOMEM handling.
4843 */
4844
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004845 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4846 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 /* Delete the established association. */
4848 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004849 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004851 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4852 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853
4854 return retval;
4855}
4856
4857/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004858sctp_disposition_t sctp_sf_error_closed(struct net *net,
4859 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 const struct sctp_association *asoc,
4861 const sctp_subtype_t type,
4862 void *arg,
4863 sctp_cmd_seq_t *commands)
4864{
4865 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4866 return SCTP_DISPOSITION_CONSUME;
4867}
4868
4869/* We tried an illegal operation on an association which is shutting
4870 * down.
4871 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004872sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4873 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 const struct sctp_association *asoc,
4875 const sctp_subtype_t type,
4876 void *arg,
4877 sctp_cmd_seq_t *commands)
4878{
4879 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4880 SCTP_ERROR(-ESHUTDOWN));
4881 return SCTP_DISPOSITION_CONSUME;
4882}
4883
4884/*
4885 * sctp_cookie_wait_prm_shutdown
4886 *
4887 * Section: 4 Note: 2
4888 * Verification Tag:
4889 * Inputs
4890 * (endpoint, asoc)
4891 *
4892 * The RFC does not explicitly address this issue, but is the route through the
4893 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4894 *
4895 * Outputs
4896 * (timers)
4897 */
4898sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004899 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 const struct sctp_endpoint *ep,
4901 const struct sctp_association *asoc,
4902 const sctp_subtype_t type,
4903 void *arg,
4904 sctp_cmd_seq_t *commands)
4905{
4906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4907 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4908
4909 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4910 SCTP_STATE(SCTP_STATE_CLOSED));
4911
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004912 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913
4914 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4915
4916 return SCTP_DISPOSITION_DELETE_TCB;
4917}
4918
4919/*
4920 * sctp_cookie_echoed_prm_shutdown
4921 *
4922 * Section: 4 Note: 2
4923 * Verification Tag:
4924 * Inputs
4925 * (endpoint, asoc)
4926 *
4927 * The RFC does not explcitly address this issue, but is the route through the
4928 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4929 *
4930 * Outputs
4931 * (timers)
4932 */
4933sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004934 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 const struct sctp_endpoint *ep,
4936 const struct sctp_association *asoc,
4937 const sctp_subtype_t type,
4938 void *arg, sctp_cmd_seq_t *commands)
4939{
4940 /* There is a single T1 timer, so we should be able to use
4941 * common function with the COOKIE-WAIT state.
4942 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004943 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944}
4945
4946/*
4947 * sctp_sf_cookie_wait_prm_abort
4948 *
4949 * Section: 4 Note: 2
4950 * Verification Tag:
4951 * Inputs
4952 * (endpoint, asoc)
4953 *
4954 * The RFC does not explicitly address this issue, but is the route through the
4955 * state table when someone issues an abort while in COOKIE_WAIT state.
4956 *
4957 * Outputs
4958 * (timers)
4959 */
4960sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004961 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 const struct sctp_endpoint *ep,
4963 const struct sctp_association *asoc,
4964 const sctp_subtype_t type,
4965 void *arg,
4966 sctp_cmd_seq_t *commands)
4967{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004968 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 sctp_disposition_t retval;
4970
4971 /* Stop T1-init timer */
4972 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4973 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4974 retval = SCTP_DISPOSITION_CONSUME;
4975
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004976 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
4978 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4979 SCTP_STATE(SCTP_STATE_CLOSED));
4980
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004981 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
4983 /* Even if we can't send the ABORT due to low memory delete the
4984 * TCB. This is a departure from our typical NOMEM handling.
4985 */
4986
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004987 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4988 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 /* Delete the established association. */
4990 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004991 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
4993 return retval;
4994}
4995
4996/*
4997 * sctp_sf_cookie_echoed_prm_abort
4998 *
4999 * Section: 4 Note: 3
5000 * Verification Tag:
5001 * Inputs
5002 * (endpoint, asoc)
5003 *
5004 * The RFC does not explcitly address this issue, but is the route through the
5005 * state table when someone issues an abort while in COOKIE_ECHOED state.
5006 *
5007 * Outputs
5008 * (timers)
5009 */
5010sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005011 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 const struct sctp_endpoint *ep,
5013 const struct sctp_association *asoc,
5014 const sctp_subtype_t type,
5015 void *arg,
5016 sctp_cmd_seq_t *commands)
5017{
5018 /* There is a single T1 timer, so we should be able to use
5019 * common function with the COOKIE-WAIT state.
5020 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005021 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
5024/*
5025 * sctp_sf_shutdown_pending_prm_abort
5026 *
5027 * Inputs
5028 * (endpoint, asoc)
5029 *
5030 * The RFC does not explicitly address this issue, but is the route through the
5031 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5032 *
5033 * Outputs
5034 * (timers)
5035 */
5036sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005037 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 const struct sctp_endpoint *ep,
5039 const struct sctp_association *asoc,
5040 const sctp_subtype_t type,
5041 void *arg,
5042 sctp_cmd_seq_t *commands)
5043{
5044 /* Stop the T5-shutdown guard timer. */
5045 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5046 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5047
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005048 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
5051/*
5052 * sctp_sf_shutdown_sent_prm_abort
5053 *
5054 * Inputs
5055 * (endpoint, asoc)
5056 *
5057 * The RFC does not explicitly address this issue, but is the route through the
5058 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5059 *
5060 * Outputs
5061 * (timers)
5062 */
5063sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005064 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 const struct sctp_endpoint *ep,
5066 const struct sctp_association *asoc,
5067 const sctp_subtype_t type,
5068 void *arg,
5069 sctp_cmd_seq_t *commands)
5070{
5071 /* Stop the T2-shutdown timer. */
5072 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5073 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5074
5075 /* Stop the T5-shutdown guard timer. */
5076 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5077 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5078
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005079 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080}
5081
5082/*
5083 * sctp_sf_cookie_echoed_prm_abort
5084 *
5085 * Inputs
5086 * (endpoint, asoc)
5087 *
5088 * The RFC does not explcitly address this issue, but is the route through the
5089 * state table when someone issues an abort while in COOKIE_ECHOED state.
5090 *
5091 * Outputs
5092 * (timers)
5093 */
5094sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005095 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 const struct sctp_endpoint *ep,
5097 const struct sctp_association *asoc,
5098 const sctp_subtype_t type,
5099 void *arg,
5100 sctp_cmd_seq_t *commands)
5101{
5102 /* The same T2 timer, so we should be able to use
5103 * common function with the SHUTDOWN-SENT state.
5104 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005105 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
5108/*
5109 * Process the REQUESTHEARTBEAT primitive
5110 *
5111 * 10.1 ULP-to-SCTP
5112 * J) Request Heartbeat
5113 *
5114 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5115 *
5116 * -> result
5117 *
5118 * Instructs the local endpoint to perform a HeartBeat on the specified
5119 * destination transport address of the given association. The returned
5120 * result should indicate whether the transmission of the HEARTBEAT
5121 * chunk to the destination address is successful.
5122 *
5123 * Mandatory attributes:
5124 *
5125 * o association id - local handle to the SCTP association
5126 *
5127 * o destination transport address - the transport address of the
5128 * association on which a heartbeat should be issued.
5129 */
5130sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005131 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 const struct sctp_endpoint *ep,
5133 const struct sctp_association *asoc,
5134 const sctp_subtype_t type,
5135 void *arg,
5136 sctp_cmd_seq_t *commands)
5137{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005138 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5139 (struct sctp_transport *)arg, commands))
5140 return SCTP_DISPOSITION_NOMEM;
5141
5142 /*
5143 * RFC 2960 (bis), section 8.3
5144 *
5145 * D) Request an on-demand HEARTBEAT on a specific destination
5146 * transport address of a given association.
5147 *
5148 * The endpoint should increment the respective error counter of
5149 * the destination transport address each time a HEARTBEAT is sent
5150 * to that address and not acknowledged within one RTO.
5151 *
5152 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005153 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005154 SCTP_TRANSPORT(arg));
5155 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156}
5157
5158/*
5159 * ADDIP Section 4.1 ASCONF Chunk Procedures
5160 * When an endpoint has an ASCONF signaled change to be sent to the
5161 * remote endpoint it should do A1 to A9
5162 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005163sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5164 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 const struct sctp_association *asoc,
5166 const sctp_subtype_t type,
5167 void *arg,
5168 sctp_cmd_seq_t *commands)
5169{
5170 struct sctp_chunk *chunk = arg;
5171
5172 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5173 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5174 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5175 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5176 return SCTP_DISPOSITION_CONSUME;
5177}
5178
5179/*
5180 * Ignore the primitive event
5181 *
5182 * The return value is the disposition of the primitive.
5183 */
5184sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005185 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 const struct sctp_endpoint *ep,
5187 const struct sctp_association *asoc,
5188 const sctp_subtype_t type,
5189 void *arg,
5190 sctp_cmd_seq_t *commands)
5191{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005192 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5193 type.primitive);
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 return SCTP_DISPOSITION_DISCARD;
5196}
5197
5198/***************************************************************************
5199 * These are the state functions for the OTHER events.
5200 ***************************************************************************/
5201
5202/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005203 * When the SCTP stack has no more user data to send or retransmit, this
5204 * notification is given to the user. Also, at the time when a user app
5205 * subscribes to this event, if there is no data to be sent or
5206 * retransmit, the stack will immediately send up this notification.
5207 */
5208sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005209 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005210 const struct sctp_endpoint *ep,
5211 const struct sctp_association *asoc,
5212 const sctp_subtype_t type,
5213 void *arg,
5214 sctp_cmd_seq_t *commands)
5215{
5216 struct sctp_ulpevent *event;
5217
5218 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5219 if (!event)
5220 return SCTP_DISPOSITION_NOMEM;
5221
5222 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5223
5224 return SCTP_DISPOSITION_CONSUME;
5225}
5226
5227/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 * Start the shutdown negotiation.
5229 *
5230 * From Section 9.2:
5231 * Once all its outstanding data has been acknowledged, the endpoint
5232 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5233 * TSN Ack field the last sequential TSN it has received from the peer.
5234 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5235 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5236 * with the updated last sequential TSN received from its peer.
5237 *
5238 * The return value is the disposition.
5239 */
5240sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005241 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 const struct sctp_endpoint *ep,
5243 const struct sctp_association *asoc,
5244 const sctp_subtype_t type,
5245 void *arg,
5246 sctp_cmd_seq_t *commands)
5247{
5248 struct sctp_chunk *reply;
5249
5250 /* Once all its outstanding data has been acknowledged, the
5251 * endpoint shall send a SHUTDOWN chunk to its peer including
5252 * in the Cumulative TSN Ack field the last sequential TSN it
5253 * has received from the peer.
5254 */
5255 reply = sctp_make_shutdown(asoc, NULL);
5256 if (!reply)
5257 goto nomem;
5258
5259 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5260 * T2-shutdown timer.
5261 */
5262 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5263
5264 /* It shall then start the T2-shutdown timer */
5265 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5266 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5267
Wei Yongjun536428a2008-09-05 08:55:26 +08005268 /* RFC 4960 Section 9.2
5269 * The sender of the SHUTDOWN MAY also start an overall guard timer
5270 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5271 */
Thomas Graff8d96052011-07-07 00:28:35 +00005272 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005273 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5274
Neil Horman9f70f462013-12-10 06:48:15 -05005275 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5277 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5278
5279 /* and enter the SHUTDOWN-SENT state. */
5280 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5281 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5282
5283 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5284 *
5285 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005286 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287 */
5288 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5289
5290 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5291
5292 return SCTP_DISPOSITION_CONSUME;
5293
5294nomem:
5295 return SCTP_DISPOSITION_NOMEM;
5296}
5297
5298/*
5299 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5300 *
5301 * From Section 9.2:
5302 *
5303 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5304 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5305 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5306 * endpoint must re-send the SHUTDOWN ACK.
5307 *
5308 * The return value is the disposition.
5309 */
5310sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005311 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 const struct sctp_endpoint *ep,
5313 const struct sctp_association *asoc,
5314 const sctp_subtype_t type,
5315 void *arg,
5316 sctp_cmd_seq_t *commands)
5317{
5318 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5319 struct sctp_chunk *reply;
5320
5321 /* There are 2 ways of getting here:
5322 * 1) called in response to a SHUTDOWN chunk
5323 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5324 *
5325 * For the case (2), the arg parameter is set to NULL. We need
5326 * to check that we have a chunk before accessing it's fields.
5327 */
5328 if (chunk) {
5329 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005330 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331
5332 /* Make sure that the SHUTDOWN chunk has a valid length. */
5333 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005334 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 commands);
5336 }
5337
5338 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5339 * shall send a SHUTDOWN ACK ...
5340 */
5341 reply = sctp_make_shutdown_ack(asoc, chunk);
5342 if (!reply)
5343 goto nomem;
5344
5345 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5346 * the T2-shutdown timer.
5347 */
5348 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5349
5350 /* and start/restart a T2-shutdown timer of its own, */
5351 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5352 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5353
Neil Horman9f70f462013-12-10 06:48:15 -05005354 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5356 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5357
5358 /* Enter the SHUTDOWN-ACK-SENT state. */
5359 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5360 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5361
5362 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5363 *
5364 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005365 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 */
5367 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5368
5369 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5370
5371 return SCTP_DISPOSITION_CONSUME;
5372
5373nomem:
5374 return SCTP_DISPOSITION_NOMEM;
5375}
5376
5377/*
5378 * Ignore the event defined as other
5379 *
5380 * The return value is the disposition of the event.
5381 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005382sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5383 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 const struct sctp_association *asoc,
5385 const sctp_subtype_t type,
5386 void *arg,
5387 sctp_cmd_seq_t *commands)
5388{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005389 pr_debug("%s: the event other type:%d is ignored\n",
5390 __func__, type.other);
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 return SCTP_DISPOSITION_DISCARD;
5393}
5394
5395/************************************************************
5396 * These are the state functions for handling timeout events.
5397 ************************************************************/
5398
5399/*
5400 * RTX Timeout
5401 *
5402 * Section: 6.3.3 Handle T3-rtx Expiration
5403 *
5404 * Whenever the retransmission timer T3-rtx expires for a destination
5405 * address, do the following:
5406 * [See below]
5407 *
5408 * The return value is the disposition of the chunk.
5409 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005410sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5411 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 const struct sctp_association *asoc,
5413 const sctp_subtype_t type,
5414 void *arg,
5415 sctp_cmd_seq_t *commands)
5416{
5417 struct sctp_transport *transport = arg;
5418
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005419 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 if (asoc->overall_error_count >= asoc->max_retrans) {
Thomas Graff8d96052011-07-07 00:28:35 +00005422 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5423 /*
5424 * We are here likely because the receiver had its rwnd
5425 * closed for a while and we have not been able to
5426 * transmit the locally queued data within the maximum
5427 * retransmission attempts limit. Start the T5
5428 * shutdown guard timer to give the receiver one last
5429 * chance and some additional time to recover before
5430 * aborting.
5431 */
5432 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5433 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5434 } else {
5435 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5436 SCTP_ERROR(ETIMEDOUT));
5437 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5438 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5439 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005440 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5441 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005442 return SCTP_DISPOSITION_DELETE_TCB;
5443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 }
5445
5446 /* E1) For the destination address for which the timer
5447 * expires, adjust its ssthresh with rules defined in Section
5448 * 7.2.3 and set the cwnd <- MTU.
5449 */
5450
5451 /* E2) For the destination address for which the timer
5452 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5453 * maximum value discussed in rule C7 above (RTO.max) may be
5454 * used to provide an upper bound to this doubling operation.
5455 */
5456
5457 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5458 * outstanding DATA chunks for the address for which the
5459 * T3-rtx has expired will fit into a single packet, subject
5460 * to the MTU constraint for the path corresponding to the
5461 * destination transport address to which the retransmission
5462 * is being sent (this may be different from the address for
5463 * which the timer expires [see Section 6.4]). Call this
5464 * value K. Bundle and retransmit those K DATA chunks in a
5465 * single packet to the destination endpoint.
5466 *
5467 * Note: Any DATA chunks that were sent to the address for
5468 * which the T3-rtx timer expired but did not fit in one MTU
5469 * (rule E3 above), should be marked for retransmission and
5470 * sent as soon as cwnd allows (normally when a SACK arrives).
5471 */
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 /* Do some failure management (Section 8.2). */
5474 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5475
Vlad Yasevich1845a572007-02-21 02:06:19 -08005476 /* NB: Rules E4 and F1 are implicit in R1. */
5477 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5478
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 return SCTP_DISPOSITION_CONSUME;
5480}
5481
5482/*
5483 * Generate delayed SACK on timeout
5484 *
5485 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5486 *
5487 * The guidelines on delayed acknowledgement algorithm specified in
5488 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5489 * acknowledgement SHOULD be generated for at least every second packet
5490 * (not every second DATA chunk) received, and SHOULD be generated
5491 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5492 * some situations it may be beneficial for an SCTP transmitter to be
5493 * more conservative than the algorithms detailed in this document
5494 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5495 * the following algorithms allow.
5496 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005497sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5498 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 const struct sctp_association *asoc,
5500 const sctp_subtype_t type,
5501 void *arg,
5502 sctp_cmd_seq_t *commands)
5503{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005504 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5506 return SCTP_DISPOSITION_CONSUME;
5507}
5508
5509/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005510 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 *
5512 * Section: 4 Note: 2
5513 * Verification Tag:
5514 * Inputs
5515 * (endpoint, asoc)
5516 *
5517 * RFC 2960 Section 4 Notes
5518 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5519 * and re-start the T1-init timer without changing state. This MUST
5520 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5521 * endpoint MUST abort the initialization process and report the
5522 * error to SCTP user.
5523 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005524 * Outputs
5525 * (timers, events)
5526 *
5527 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005528sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5529 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005530 const struct sctp_association *asoc,
5531 const sctp_subtype_t type,
5532 void *arg,
5533 sctp_cmd_seq_t *commands)
5534{
5535 struct sctp_chunk *repl = NULL;
5536 struct sctp_bind_addr *bp;
5537 int attempts = asoc->init_err_counter + 1;
5538
Daniel Borkmannbb333812013-06-28 19:49:40 +02005539 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5540
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005541 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005542
Vlad Yasevich81845c22006-01-30 15:59:54 -08005543 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005544 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5545 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5546 if (!repl)
5547 return SCTP_DISPOSITION_NOMEM;
5548
5549 /* Choose transport for INIT. */
5550 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5551 SCTP_CHUNK(repl));
5552
5553 /* Issue a sideeffect to do the needed accounting. */
5554 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5555 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5556
5557 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5558 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02005559 pr_debug("%s: giving up on INIT, attempts:%d "
5560 "max_init_attempts:%d\n", __func__, attempts,
5561 asoc->max_init_attempts);
5562
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005563 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5564 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005565 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005566 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005567 return SCTP_DISPOSITION_DELETE_TCB;
5568 }
5569
5570 return SCTP_DISPOSITION_CONSUME;
5571}
5572
5573/*
5574 * sctp_sf_t1_cookie_timer_expire
5575 *
5576 * Section: 4 Note: 2
5577 * Verification Tag:
5578 * Inputs
5579 * (endpoint, asoc)
5580 *
5581 * RFC 2960 Section 4 Notes
5582 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 * COOKIE ECHO and re-start the T1-cookie timer without changing
5584 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5585 * After that, the endpoint MUST abort the initialization process and
5586 * report the error to SCTP user.
5587 *
5588 * Outputs
5589 * (timers, events)
5590 *
5591 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005592sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5593 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 const struct sctp_association *asoc,
5595 const sctp_subtype_t type,
5596 void *arg,
5597 sctp_cmd_seq_t *commands)
5598{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005599 struct sctp_chunk *repl = NULL;
5600 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Daniel Borkmannbb333812013-06-28 19:49:40 +02005602 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5603
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005604 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
Vlad Yasevich81845c22006-01-30 15:59:54 -08005606 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005607 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005609 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005611 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5612 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005614 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5615 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5616
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5618 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005619 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5620 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005622 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 return SCTP_DISPOSITION_DELETE_TCB;
5624 }
5625
5626 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627}
5628
5629/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5630 * with the updated last sequential TSN received from its peer.
5631 *
5632 * An endpoint should limit the number of retransmissions of the
5633 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5634 * If this threshold is exceeded the endpoint should destroy the TCB and
5635 * MUST report the peer endpoint unreachable to the upper layer (and
5636 * thus the association enters the CLOSED state). The reception of any
5637 * packet from its peer (i.e. as the peer sends all of its queued DATA
5638 * chunks) should clear the endpoint's retransmission count and restart
5639 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5640 * all of its queued DATA chunks that have not yet been sent.
5641 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005642sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5643 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 const struct sctp_association *asoc,
5645 const sctp_subtype_t type,
5646 void *arg,
5647 sctp_cmd_seq_t *commands)
5648{
5649 struct sctp_chunk *reply = NULL;
5650
Daniel Borkmannbb333812013-06-28 19:49:40 +02005651 pr_debug("%s: timer T2 expired\n", __func__);
5652
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005653 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005654
Neil Horman58fbbed2008-02-29 11:40:56 -08005655 ((struct sctp_association *)asoc)->shutdown_retries++;
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005658 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5659 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5661 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005662 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005663 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5664 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 return SCTP_DISPOSITION_DELETE_TCB;
5666 }
5667
5668 switch (asoc->state) {
5669 case SCTP_STATE_SHUTDOWN_SENT:
5670 reply = sctp_make_shutdown(asoc, NULL);
5671 break;
5672
5673 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5674 reply = sctp_make_shutdown_ack(asoc, NULL);
5675 break;
5676
5677 default:
5678 BUG();
5679 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 if (!reply)
5683 goto nomem;
5684
Wei Yongjun6345b192009-04-26 23:13:35 +08005685 /* Do some failure management (Section 8.2).
5686 * If we remove the transport an SHUTDOWN was last sent to, don't
5687 * do failure management.
5688 */
5689 if (asoc->shutdown_last_sent_to)
5690 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5691 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5694 * the T2-shutdown timer.
5695 */
5696 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5697
5698 /* Restart the T2-shutdown timer. */
5699 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5700 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5701 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5702 return SCTP_DISPOSITION_CONSUME;
5703
5704nomem:
5705 return SCTP_DISPOSITION_NOMEM;
5706}
5707
5708/*
5709 * ADDIP Section 4.1 ASCONF CHunk Procedures
5710 * If the T4 RTO timer expires the endpoint should do B1 to B5
5711 */
5712sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005713 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 const struct sctp_endpoint *ep,
5715 const struct sctp_association *asoc,
5716 const sctp_subtype_t type,
5717 void *arg,
5718 sctp_cmd_seq_t *commands)
5719{
5720 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5721 struct sctp_transport *transport = chunk->transport;
5722
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005723 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005724
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5726 * detection on the appropriate destination address as defined in
5727 * RFC2960 [5] section 8.1 and 8.2.
5728 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005729 if (transport)
5730 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5731 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732
5733 /* Reconfig T4 timer and transport. */
5734 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5735
5736 /* ADDIP 4.1 B2) Increment the association error counters and perform
5737 * endpoint failure detection on the association as defined in
5738 * RFC2960 [5] section 8.1 and 8.2.
5739 * association error counter is incremented in SCTP_CMD_STRIKE.
5740 */
5741 if (asoc->overall_error_count >= asoc->max_retrans) {
5742 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5743 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005744 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5745 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005747 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005748 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5749 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 return SCTP_DISPOSITION_ABORT;
5751 }
5752
5753 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5754 * the ASCONF chunk was sent by doubling the RTO timer value.
5755 * This is done in SCTP_CMD_STRIKE.
5756 */
5757
5758 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5759 * choose an alternate destination address (please refer to RFC2960
5760 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005761 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 * ASCONF sent.
5763 */
5764 sctp_chunk_hold(asoc->addip_last_asconf);
5765 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5766 SCTP_CHUNK(asoc->addip_last_asconf));
5767
5768 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5769 * destination is selected, then the RTO used will be that of the new
5770 * destination address.
5771 */
5772 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5773 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5774
5775 return SCTP_DISPOSITION_CONSUME;
5776}
5777
5778/* sctpimpguide-05 Section 2.12.2
5779 * The sender of the SHUTDOWN MAY also start an overall guard timer
5780 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5781 * At the expiration of this timer the sender SHOULD abort the association
5782 * by sending an ABORT chunk.
5783 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005784sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5785 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 const struct sctp_association *asoc,
5787 const sctp_subtype_t type,
5788 void *arg,
5789 sctp_cmd_seq_t *commands)
5790{
5791 struct sctp_chunk *reply = NULL;
5792
Daniel Borkmannbb333812013-06-28 19:49:40 +02005793 pr_debug("%s: timer T5 expired\n", __func__);
5794
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005795 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 reply = sctp_make_abort(asoc, NULL, 0);
5798 if (!reply)
5799 goto nomem;
5800
5801 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005802 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5803 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005805 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005807 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5808 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 return SCTP_DISPOSITION_DELETE_TCB;
5811nomem:
5812 return SCTP_DISPOSITION_NOMEM;
5813}
5814
5815/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5816 * the association is automatically closed by starting the shutdown process.
5817 * The work that needs to be done is same as when SHUTDOWN is initiated by
5818 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5819 */
5820sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005821 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 const struct sctp_endpoint *ep,
5823 const struct sctp_association *asoc,
5824 const sctp_subtype_t type,
5825 void *arg,
5826 sctp_cmd_seq_t *commands)
5827{
5828 int disposition;
5829
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005830 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005831
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 /* From 9.2 Shutdown of an Association
5833 * Upon receipt of the SHUTDOWN primitive from its upper
5834 * layer, the endpoint enters SHUTDOWN-PENDING state and
5835 * remains there until all outstanding data has been
5836 * acknowledged by its peer. The endpoint accepts no new data
5837 * from its upper layer, but retransmits data to the far end
5838 * if necessary to fill gaps.
5839 */
5840 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5841 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5842
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 disposition = SCTP_DISPOSITION_CONSUME;
5844 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005845 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 arg, commands);
5847 }
5848 return disposition;
5849}
5850
5851/*****************************************************************************
5852 * These are sa state functions which could apply to all types of events.
5853 ****************************************************************************/
5854
5855/*
5856 * This table entry is not implemented.
5857 *
5858 * Inputs
5859 * (endpoint, asoc, chunk)
5860 *
5861 * The return value is the disposition of the chunk.
5862 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005863sctp_disposition_t sctp_sf_not_impl(struct net *net,
5864 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 const struct sctp_association *asoc,
5866 const sctp_subtype_t type,
5867 void *arg,
5868 sctp_cmd_seq_t *commands)
5869{
5870 return SCTP_DISPOSITION_NOT_IMPL;
5871}
5872
5873/*
5874 * This table entry represents a bug.
5875 *
5876 * Inputs
5877 * (endpoint, asoc, chunk)
5878 *
5879 * The return value is the disposition of the chunk.
5880 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005881sctp_disposition_t sctp_sf_bug(struct net *net,
5882 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 const struct sctp_association *asoc,
5884 const sctp_subtype_t type,
5885 void *arg,
5886 sctp_cmd_seq_t *commands)
5887{
5888 return SCTP_DISPOSITION_BUG;
5889}
5890
5891/*
5892 * This table entry represents the firing of a timer in the wrong state.
5893 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5894 * when the association is in the wrong state. This event should
5895 * be ignored, so as to prevent any rearming of the timer.
5896 *
5897 * Inputs
5898 * (endpoint, asoc, chunk)
5899 *
5900 * The return value is the disposition of the chunk.
5901 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005902sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5903 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 const struct sctp_association *asoc,
5905 const sctp_subtype_t type,
5906 void *arg,
5907 sctp_cmd_seq_t *commands)
5908{
Daniel Borkmannbb333812013-06-28 19:49:40 +02005909 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5910
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 return SCTP_DISPOSITION_CONSUME;
5912}
5913
5914/********************************************************************
5915 * 2nd Level Abstractions
5916 ********************************************************************/
5917
5918/* Pull the SACK chunk based on the SACK header. */
5919static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5920{
5921 struct sctp_sackhdr *sack;
5922 unsigned int len;
5923 __u16 num_blocks;
5924 __u16 num_dup_tsns;
5925
5926 /* Protect ourselves from reading too far into
5927 * the skb from a bogus sender.
5928 */
5929 sack = (struct sctp_sackhdr *) chunk->skb->data;
5930
5931 num_blocks = ntohs(sack->num_gap_ack_blocks);
5932 num_dup_tsns = ntohs(sack->num_dup_tsns);
5933 len = sizeof(struct sctp_sackhdr);
5934 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5935 if (len > chunk->skb->len)
5936 return NULL;
5937
5938 skb_pull(chunk->skb, len);
5939
5940 return sack;
5941}
5942
5943/* Create an ABORT packet to be sent as a response, with the specified
5944 * error causes.
5945 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005946static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5947 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 const struct sctp_association *asoc,
5949 struct sctp_chunk *chunk,
5950 const void *payload,
5951 size_t paylen)
5952{
5953 struct sctp_packet *packet;
5954 struct sctp_chunk *abort;
5955
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005956 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957
5958 if (packet) {
5959 /* Make an ABORT.
5960 * The T bit will be set if the asoc is NULL.
5961 */
5962 abort = sctp_make_abort(asoc, chunk, paylen);
5963 if (!abort) {
5964 sctp_ootb_pkt_free(packet);
5965 return NULL;
5966 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005967
5968 /* Reflect vtag if T-Bit is set */
5969 if (sctp_test_T_bit(abort))
5970 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 /* Add specified error causes, i.e., payload, to the
5973 * end of the chunk.
5974 */
5975 sctp_addto_chunk(abort, paylen, payload);
5976
5977 /* Set the skb to the belonging sock for accounting. */
5978 abort->skb->sk = ep->base.sk;
5979
5980 sctp_packet_append_chunk(packet, abort);
5981
5982 }
5983
5984 return packet;
5985}
5986
5987/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005988static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5989 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 const struct sctp_chunk *chunk)
5991{
5992 struct sctp_packet *packet;
5993 struct sctp_transport *transport;
5994 __u16 sport;
5995 __u16 dport;
5996 __u32 vtag;
5997
5998 /* Get the source and destination port from the inbound packet. */
5999 sport = ntohs(chunk->sctp_hdr->dest);
6000 dport = ntohs(chunk->sctp_hdr->source);
6001
6002 /* The V-tag is going to be the same as the inbound packet if no
6003 * association exists, otherwise, use the peer's vtag.
6004 */
6005 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006006 /* Special case the INIT-ACK as there is no peer's vtag
6007 * yet.
6008 */
wangweidongcb3f8372013-12-23 12:16:50 +08006009 switch (chunk->chunk_hdr->type) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08006010 case SCTP_CID_INIT_ACK:
6011 {
6012 sctp_initack_chunk_t *initack;
6013
6014 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6015 vtag = ntohl(initack->init_hdr.init_tag);
6016 break;
6017 }
6018 default:
6019 vtag = asoc->peer.i.init_tag;
6020 break;
6021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 } else {
6023 /* Special case the INIT and stale COOKIE_ECHO as there is no
6024 * vtag yet.
6025 */
wangweidongcb3f8372013-12-23 12:16:50 +08006026 switch (chunk->chunk_hdr->type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 case SCTP_CID_INIT:
6028 {
6029 sctp_init_chunk_t *init;
6030
6031 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6032 vtag = ntohl(init->init_hdr.init_tag);
6033 break;
6034 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006035 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 vtag = ntohl(chunk->sctp_hdr->vtag);
6037 break;
6038 }
6039 }
6040
6041 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006042 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 if (!transport)
6044 goto nomem;
6045
6046 /* Cache a route for the transport with the chunk's destination as
6047 * the source address.
6048 */
Al Viro16b0a032006-11-20 17:13:38 -08006049 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006050 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6053 packet = sctp_packet_config(packet, vtag, 0);
6054
6055 return packet;
6056
6057nomem:
6058 return NULL;
6059}
6060
6061/* Free the packet allocated earlier for responding in the OOTB condition. */
6062void sctp_ootb_pkt_free(struct sctp_packet *packet)
6063{
6064 sctp_transport_free(packet->transport);
6065}
6066
6067/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006068static void sctp_send_stale_cookie_err(struct net *net,
6069 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 const struct sctp_association *asoc,
6071 const struct sctp_chunk *chunk,
6072 sctp_cmd_seq_t *commands,
6073 struct sctp_chunk *err_chunk)
6074{
6075 struct sctp_packet *packet;
6076
6077 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006078 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 if (packet) {
6080 struct sctp_signed_cookie *cookie;
6081
6082 /* Override the OOTB vtag from the cookie. */
6083 cookie = chunk->subh.cookie_hdr;
6084 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 /* Set the skb to the belonging sock for accounting. */
6087 err_chunk->skb->sk = ep->base.sk;
6088 sctp_packet_append_chunk(packet, err_chunk);
6089 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6090 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006091 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 } else
6093 sctp_chunk_free (err_chunk);
6094 }
6095}
6096
6097
6098/* Process a data chunk */
6099static int sctp_eat_data(const struct sctp_association *asoc,
6100 struct sctp_chunk *chunk,
6101 sctp_cmd_seq_t *commands)
6102{
6103 sctp_datahdr_t *data_hdr;
6104 struct sctp_chunk *err;
6105 size_t datalen;
6106 sctp_verb_t deliver;
6107 int tmp;
6108 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006109 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006110 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006111 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006112 u16 ssn;
6113 u16 sid;
6114 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6117 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6118
6119 tsn = ntohl(data_hdr->tsn);
Daniel Borkmannbb333812013-06-28 19:49:40 +02006120 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121
6122 /* ASSERT: Now skb->data is really the user data. */
6123
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124 /* Process ECN based congestion.
6125 *
6126 * Since the chunk structure is reused for all chunks within
6127 * a packet, we use ecn_ce_done to track if we've already
6128 * done CE processing for this packet.
6129 *
6130 * We need to do ECN processing even if we plan to discard the
6131 * chunk later.
6132 */
6133
6134 if (!chunk->ecn_ce_done) {
6135 struct sctp_af *af;
6136 chunk->ecn_ce_done = 1;
6137
6138 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006139 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140
6141 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6142 /* Do real work as sideffect. */
6143 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6144 SCTP_U32(tsn));
6145 }
6146 }
6147
6148 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6149 if (tmp < 0) {
6150 /* The TSN is too high--silently discard the chunk and
6151 * count on it getting retransmitted later.
6152 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006153 if (chunk->asoc)
6154 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155 return SCTP_IERROR_HIGH_TSN;
6156 } else if (tmp > 0) {
6157 /* This is a duplicate. Record it. */
6158 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6159 return SCTP_IERROR_DUP_TSN;
6160 }
6161
6162 /* This is a new TSN. */
6163
6164 /* Discard if there is no room in the receive window.
6165 * Actually, allow a little bit of overflow (up to a MTU).
6166 */
6167 datalen = ntohs(chunk->chunk_hdr->length);
6168 datalen -= sizeof(sctp_data_chunk_t);
6169
6170 deliver = SCTP_CMD_CHUNK_ULP;
6171
6172 /* Think about partial delivery. */
6173 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6174
6175 /* Even if we don't accept this chunk there is
6176 * memory pressure.
6177 */
6178 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6179 }
6180
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006181 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 * seems a bit troublesome in that frag_point varies based on
6183 * PMTU. In cases, such as loopback, this might be a rather
6184 * large spill over.
6185 */
Matija Glavinic Pecoticef2820a2014-02-14 14:51:18 +01006186 if ((!chunk->data_accepted) && (!asoc->rwnd ||
Neil Horman4d93df02007-08-15 16:07:44 -07006187 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188
6189 /* If this is the next TSN, consider reneging to make
6190 * room. Note: Playing nice with a confused sender. A
6191 * malicious sender can still eat up all our buffer
6192 * space and in the future we may want to detect and
6193 * do more drastic reneging.
6194 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006195 if (sctp_tsnmap_has_gap(map) &&
6196 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006197 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 deliver = SCTP_CMD_RENEGE;
6199 } else {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006200 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6201 __func__, tsn, datalen, asoc->rwnd);
6202
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 return SCTP_IERROR_IGNORE_TSN;
6204 }
6205 }
6206
6207 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006208 * Also try to renege to limit our memory usage in the event that
6209 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006210 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006211 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6212 * memory usage too much
6213 */
6214 if (*sk->sk_prot_creator->memory_pressure) {
6215 if (sctp_tsnmap_has_gap(map) &&
wangweidongf7010e62013-12-23 12:16:52 +08006216 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Daniel Borkmannbb333812013-06-28 19:49:40 +02006217 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6218 __func__, tsn);
Neil Horman4d93df02007-08-15 16:07:44 -07006219 deliver = SCTP_CMD_RENEGE;
6220 }
6221 }
6222
6223 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 * Section 3.3.10.9 No User Data (9)
6225 *
6226 * Cause of error
6227 * ---------------
6228 * No User Data: This error cause is returned to the originator of a
6229 * DATA chunk if a received DATA chunk has no user data.
6230 */
6231 if (unlikely(0 == datalen)) {
6232 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6233 if (err) {
6234 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6235 SCTP_CHUNK(err));
6236 }
6237 /* We are going to ABORT, so we might as well stop
6238 * processing the rest of the chunks in the packet.
6239 */
wangweidongcb3f8372013-12-23 12:16:50 +08006240 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006241 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6242 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006244 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006245 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6246 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247 return SCTP_IERROR_NO_DATA;
6248 }
6249
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006250 chunk->data_accepted = 1;
6251
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6253 * if we renege and the chunk arrives again.
6254 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006255 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006256 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006257 if (chunk->asoc)
6258 chunk->asoc->stats.iuodchunks++;
6259 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006260 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006261 if (chunk->asoc)
6262 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006263 ordered = 1;
6264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
6266 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6267 *
6268 * If an endpoint receive a DATA chunk with an invalid stream
6269 * identifier, it shall acknowledge the reception of the DATA chunk
6270 * following the normal procedure, immediately send an ERROR chunk
6271 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6272 * and discard the DATA chunk.
6273 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006274 sid = ntohs(data_hdr->stream);
6275 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006276 /* Mark tsn as received even though we drop it */
6277 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6278
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6280 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006281 sizeof(data_hdr->stream),
6282 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 if (err)
6284 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6285 SCTP_CHUNK(err));
6286 return SCTP_IERROR_BAD_STREAM;
6287 }
6288
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006289 /* Check to see if the SSN is possible for this TSN.
6290 * The biggest gap we can record is 4K wide. Since SSNs wrap
6291 * at an unsigned short, there is no way that an SSN can
6292 * wrap and for a valid TSN. We can simply check if the current
6293 * SSN is smaller then the next expected one. If it is, it wrapped
6294 * and is invalid.
6295 */
6296 ssn = ntohs(data_hdr->ssn);
6297 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6298 return SCTP_IERROR_PROTO_VIOLATION;
6299 }
6300
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 /* Send the data up to the user. Note: Schedule the
6302 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6303 * chunk needs the updated rwnd.
6304 */
6305 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6306
6307 return SCTP_IERROR_NO_ERROR;
6308}