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