blob: 6eb26403de6a7d9b9b0103ed8fc58a9534dfec04 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
28 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 *
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
35 *
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
Joe Perches145ce502010-08-24 13:21:08 +000053#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/types.h>
56#include <linux/kernel.h>
57#include <linux/ip.h>
58#include <linux/ipv6.h>
59#include <linux/net.h>
60#include <linux/inet.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/sock.h>
63#include <net/inet_ecn.h>
64#include <linux/skbuff.h>
65#include <net/sctp/sctp.h>
66#include <net/sctp/sm.h>
67#include <net/sctp/structs.h>
68
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000069static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
70 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 const void *payload,
74 size_t paylen);
75static int sctp_eat_data(const struct sctp_association *asoc,
76 struct sctp_chunk *chunk,
77 sctp_cmd_seq_t *commands);
Eric W. Biederman2ce955032012-08-06 08:43:06 +000078static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
79 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 const struct sctp_chunk *chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000081static void sctp_send_stale_cookie_err(struct net *net,
82 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 const struct sctp_association *asoc,
84 const struct sctp_chunk *chunk,
85 sctp_cmd_seq_t *commands,
86 struct sctp_chunk *err_chunk);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000087static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
88 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 const struct sctp_association *asoc,
90 const sctp_subtype_t type,
91 void *arg,
92 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000093static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
94 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 const struct sctp_association *asoc,
96 const sctp_subtype_t type,
97 void *arg,
98 sctp_cmd_seq_t *commands);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +000099static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
100 const struct sctp_endpoint *ep,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400101 const struct sctp_association *asoc,
102 const sctp_subtype_t type,
103 void *arg,
104 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
106
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000107static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
108 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800109 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700110 const struct sctp_association *asoc,
111 struct sctp_transport *transport);
112
Wei Yongjunaecedea2007-08-02 16:57:44 +0800113static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000114 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400115 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800116 const struct sctp_association *asoc,
117 void *arg,
118 sctp_cmd_seq_t *commands,
119 const __u8 *payload,
120 const size_t paylen);
121
Adrian Bunk52c1da32005-06-23 22:05:33 -0700122static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000123 struct net *net,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700124 const struct sctp_endpoint *ep,
125 const struct sctp_association *asoc,
126 const sctp_subtype_t type,
127 void *arg,
128 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800130static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000131 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800132 const struct sctp_endpoint *ep,
133 const struct sctp_association *asoc,
134 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700135 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800136 sctp_cmd_seq_t *commands);
137
Wei Yongjunaecedea2007-08-02 16:57:44 +0800138static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000139 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800140 const struct sctp_endpoint *ep,
141 const struct sctp_association *asoc,
142 const sctp_subtype_t type,
143 void *arg,
144 sctp_cmd_seq_t *commands);
145
Vlad Yasevichece25df2007-09-07 16:30:54 -0400146static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000147 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400148 const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 void *arg,
152 sctp_cmd_seq_t *commands);
153
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000154static sctp_ierror_t sctp_sf_authenticate(struct net *net,
155 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700156 const struct sctp_association *asoc,
157 const sctp_subtype_t type,
158 struct sctp_chunk *chunk);
159
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000160static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
161 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -0800162 const struct sctp_association *asoc,
163 const sctp_subtype_t type,
164 void *arg,
165 sctp_cmd_seq_t *commands);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* Small helper function that checks if the chunk length
168 * is of the appropriate length. The 'required_length' argument
169 * is set to be the size of a specific chunk we are testing.
170 * Return Values: 1 = Valid length
171 * 0 = Invalid length
172 *
173 */
174static inline int
175sctp_chunk_length_valid(struct sctp_chunk *chunk,
176 __u16 required_length)
177{
178 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
179
180 if (unlikely(chunk_length < required_length))
181 return 0;
182
183 return 1;
184}
185
186/**********************************************************
187 * These are the state functions for handling chunk events.
188 **********************************************************/
189
190/*
191 * Process the final SHUTDOWN COMPLETE.
192 *
193 * Section: 4 (C) (diagram), 9.2
194 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
195 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
196 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
197 * should stop the T2-shutdown timer and remove all knowledge of the
198 * association (and thus the association enters the CLOSED state).
199 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700200 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * C) Rules for packet carrying SHUTDOWN COMPLETE:
202 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700203 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
204 * if the Verification Tag field of the packet matches its own tag and
205 * the T bit is not set
206 * OR
207 * it is set to its peer's tag and the T bit is set in the Chunk
208 * Flags.
209 * Otherwise, the receiver MUST silently discard the packet
210 * and take no further action. An endpoint MUST ignore the
211 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 *
213 * Inputs
214 * (endpoint, asoc, chunk)
215 *
216 * Outputs
217 * (asoc, reply_msg, msg_up, timers, counters)
218 *
219 * The return value is the disposition of the chunk.
220 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000221sctp_disposition_t sctp_sf_do_4_C(struct net *net,
222 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 const struct sctp_association *asoc,
224 const sctp_subtype_t type,
225 void *arg,
226 sctp_cmd_seq_t *commands)
227{
228 struct sctp_chunk *chunk = arg;
229 struct sctp_ulpevent *ev;
230
Vlad Yasevichece25df2007-09-07 16:30:54 -0400231 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000232 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 /* RFC 2960 6.10 Bundling
235 *
236 * An endpoint MUST NOT bundle INIT, INIT ACK or
237 * SHUTDOWN COMPLETE with any other chunks.
238 */
239 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000240 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Vlad Yasevichece25df2007-09-07 16:30:54 -0400242 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
243 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000244 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400245 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 /* RFC 2960 10.2 SCTP-to-ULP
248 *
249 * H) SHUTDOWN COMPLETE notification
250 *
251 * When SCTP completes the shutdown procedures (section 9.2) this
252 * notification is passed to the upper layer.
253 */
254 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700255 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700256 if (ev)
257 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900258 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
261 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
262 * not the chunk should be discarded. If the endpoint is in
263 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
264 * T2-shutdown timer and remove all knowledge of the
265 * association (and thus the association enters the CLOSED
266 * state).
267 */
268 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
269 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
270
271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
272 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
273
274 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
275 SCTP_STATE(SCTP_STATE_CLOSED));
276
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000277 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
278 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
281
282 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285/*
286 * Respond to a normal INIT chunk.
287 * We are the side that is being asked for an association.
288 *
289 * Section: 5.1 Normal Establishment of an Association, B
290 * B) "Z" shall respond immediately with an INIT ACK chunk. The
291 * destination IP address of the INIT ACK MUST be set to the source
292 * IP address of the INIT to which this INIT ACK is responding. In
293 * the response, besides filling in other parameters, "Z" must set the
294 * Verification Tag field to Tag_A, and also provide its own
295 * Verification Tag (Tag_Z) in the Initiate Tag field.
296 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900297 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 *
299 * Inputs
300 * (endpoint, asoc, chunk)
301 *
302 * Outputs
303 * (asoc, reply_msg, msg_up, timers, counters)
304 *
305 * The return value is the disposition of the chunk.
306 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000307sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
308 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 const struct sctp_association *asoc,
310 const sctp_subtype_t type,
311 void *arg,
312 sctp_cmd_seq_t *commands)
313{
314 struct sctp_chunk *chunk = arg;
315 struct sctp_chunk *repl;
316 struct sctp_association *new_asoc;
317 struct sctp_chunk *err_chunk;
318 struct sctp_packet *packet;
319 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int len;
321
322 /* 6.10 Bundling
323 * An endpoint MUST NOT bundle INIT, INIT ACK or
324 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900325 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * IG Section 2.11.2
327 * Furthermore, we require that the receiver of an INIT chunk MUST
328 * enforce these rules by silently discarding an arriving packet
329 * with an INIT chunk that is bundled with other chunks.
330 */
331 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000332 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* If the packet is an OOTB packet which is temporarily on the
335 * control endpoint, respond with an ABORT.
336 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +0000337 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000338 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000339 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900343 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
345 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000346 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 /* Make sure that the INIT chunk has a valid length.
349 * Normally, this would cause an ABORT with a Protocol Violation
350 * error, but since we don't have an association, we'll
351 * just discard the packet.
352 */
353 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000354 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Vlad Yasevichbec96402009-07-30 18:08:28 -0400356 /* If the INIT is coming toward a closing socket, we'll send back
357 * and ABORT. Essentially, this catches the race of INIT being
358 * backloged to the socket at the same time as the user isses close().
359 * Since the socket and all its associations are going away, we
360 * can treat this OOTB
361 */
362 if (sctp_sstate(ep->base.sk, CLOSING))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000363 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevichbec96402009-07-30 18:08:28 -0400364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 /* Verify the INIT chunk before processing it. */
366 err_chunk = NULL;
Vlad Yaseviche5eae4a2014-04-17 17:26:50 +0200367 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
369 &err_chunk)) {
370 /* This chunk contains fatal error. It is to be discarded.
371 * Send an ABORT, with causes if there is any.
372 */
373 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000374 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 (__u8 *)(err_chunk->chunk_hdr) +
376 sizeof(sctp_chunkhdr_t),
377 ntohs(err_chunk->chunk_hdr->length) -
378 sizeof(sctp_chunkhdr_t));
379
380 sctp_chunk_free(err_chunk);
381
382 if (packet) {
383 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
384 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000385 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return SCTP_DISPOSITION_CONSUME;
387 } else {
388 return SCTP_DISPOSITION_NOMEM;
389 }
390 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000391 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 commands);
393 }
394 }
395
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900396 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
398
399 /* Tag the variable length parameters. */
400 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
401
402 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
403 if (!new_asoc)
404 goto nomem;
405
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000406 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
407 sctp_scope(sctp_source(chunk)),
408 GFP_ATOMIC) < 0)
409 goto nomem_init;
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000412 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 (sctp_init_chunk_t *)chunk->chunk_hdr,
414 GFP_ATOMIC))
415 goto nomem_init;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
418
419 /* If there are errors need to be reported for unknown parameters,
420 * make sure to reserve enough room in the INIT ACK for them.
421 */
422 len = 0;
423 if (err_chunk)
424 len = ntohs(err_chunk->chunk_hdr->length) -
425 sizeof(sctp_chunkhdr_t);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
428 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700429 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* If there are errors need to be reported for unknown parameters,
432 * include them in the outgoing INIT ACK as "Unrecognized parameter"
433 * parameter.
434 */
435 if (err_chunk) {
436 /* Get the "Unrecognized parameter" parameter(s) out of the
437 * ERROR chunk generated by sctp_verify_init(). Since the
438 * error cause code for "unknown parameter" and the
439 * "Unrecognized parameter" type is the same, we can
440 * construct the parameters in INIT ACK by copying the
441 * ERROR causes over.
442 */
443 unk_param = (sctp_unrecognized_param_t *)
444 ((__u8 *)(err_chunk->chunk_hdr) +
445 sizeof(sctp_chunkhdr_t));
446 /* Replace the cause code with the "Unrecognized parameter"
447 * parameter type.
448 */
449 sctp_addto_chunk(repl, len, unk_param);
450 sctp_chunk_free(err_chunk);
451 }
452
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700453 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
456
457 /*
458 * Note: After sending out INIT ACK with the State Cookie parameter,
459 * "Z" MUST NOT allocate any resources, nor keep any states for the
460 * new association. Otherwise, "Z" will be vulnerable to resource
461 * attacks.
462 */
463 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
464
465 return SCTP_DISPOSITION_DELETE_TCB;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467nomem_init:
468 sctp_association_free(new_asoc);
469nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700470 if (err_chunk)
471 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return SCTP_DISPOSITION_NOMEM;
473}
474
475/*
476 * Respond to a normal INIT ACK chunk.
477 * We are the side that is initiating the association.
478 *
479 * Section: 5.1 Normal Establishment of an Association, C
480 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
481 * timer and leave COOKIE-WAIT state. "A" shall then send the State
482 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
483 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
484 *
485 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
486 * DATA chunks, but it MUST be the first chunk in the packet and
487 * until the COOKIE ACK is returned the sender MUST NOT send any
488 * other packets to the peer.
489 *
490 * Verification Tag: 3.3.3
491 * If the value of the Initiate Tag in a received INIT ACK chunk is
492 * found to be 0, the receiver MUST treat it as an error and close the
493 * association by transmitting an ABORT.
494 *
495 * Inputs
496 * (endpoint, asoc, chunk)
497 *
498 * Outputs
499 * (asoc, reply_msg, msg_up, timers, counters)
500 *
501 * The return value is the disposition of the chunk.
502 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000503sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
504 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 const struct sctp_association *asoc,
506 const sctp_subtype_t type,
507 void *arg,
508 sctp_cmd_seq_t *commands)
509{
510 struct sctp_chunk *chunk = arg;
511 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 struct sctp_chunk *err_chunk;
513 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000516 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* 6.10 Bundling
519 * An endpoint MUST NOT bundle INIT, INIT ACK or
520 * SHUTDOWN COMPLETE with any other chunks.
521 */
522 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000523 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Vlad Yasevichece25df2007-09-07 16:30:54 -0400525 /* Make sure that the INIT-ACK chunk has a valid length */
526 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000527 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -0400528 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /* Grab the INIT header. */
530 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 /* Verify the INIT chunk before processing it. */
533 err_chunk = NULL;
Vlad Yaseviche5eae4a2014-04-17 17:26:50 +0200534 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
536 &err_chunk)) {
537
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800538 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800541 * Send an ABORT, with causes. If there are no causes,
542 * then there wasn't enough memory. Just terminate
543 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000546 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 (__u8 *)(err_chunk->chunk_hdr) +
548 sizeof(sctp_chunkhdr_t),
549 ntohs(err_chunk->chunk_hdr->length) -
550 sizeof(sctp_chunkhdr_t));
551
552 sctp_chunk_free(err_chunk);
553
554 if (packet) {
555 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
556 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000557 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700558 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700561
562 /* SCTP-AUTH, Section 6.3:
563 * It should be noted that if the receiver wants to tear
564 * down an association in an authenticated way only, the
565 * handling of malformed packets should not result in
566 * tearing down the association.
567 *
568 * This means that if we only want to abort associations
569 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300570 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700571 * was malformed.
572 */
573 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000574 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700575
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000576 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
577 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700578 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 /* Tag the variable length parameters. Note that we never
582 * convert the parameters in an INIT chunk.
583 */
584 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
585
586 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
587
588 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
589 SCTP_PEER_INIT(initchunk));
590
Frank Filz3f7a87d2005-06-20 13:14:57 -0700591 /* Reset init error count upon receipt of INIT-ACK. */
592 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* 5.1 C) "A" shall stop the T1-init timer and leave
595 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
596 * timer, and enter the COOKIE-ECHOED state.
597 */
598 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
599 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
600 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
601 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
602 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
603 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
604
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700605 /* SCTP-AUTH: genereate the assocition shared keys so that
606 * we can potentially signe the COOKIE-ECHO.
607 */
608 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* 5.1 C) "A" shall then send the State Cookie received in the
611 * INIT ACK chunk in a COOKIE ECHO chunk, ...
612 */
613 /* If there is any errors to report, send the ERROR chunk generated
614 * for unknown parameters as well.
615 */
616 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
617 SCTP_CHUNK(err_chunk));
618
619 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622/*
623 * Respond to a normal COOKIE ECHO chunk.
624 * We are the side that is being asked for an association.
625 *
626 * Section: 5.1 Normal Establishment of an Association, D
627 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
628 * with a COOKIE ACK chunk after building a TCB and moving to
629 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
630 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
631 * chunk MUST be the first chunk in the packet.
632 *
633 * IMPLEMENTATION NOTE: An implementation may choose to send the
634 * Communication Up notification to the SCTP user upon reception
635 * of a valid COOKIE ECHO chunk.
636 *
637 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
638 * D) Rules for packet carrying a COOKIE ECHO
639 *
640 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
641 * Initial Tag received in the INIT ACK.
642 *
643 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
644 *
645 * Inputs
646 * (endpoint, asoc, chunk)
647 *
648 * Outputs
649 * (asoc, reply_msg, msg_up, timers, counters)
650 *
651 * The return value is the disposition of the chunk.
652 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000653sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
654 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 const struct sctp_association *asoc,
656 const sctp_subtype_t type, void *arg,
657 sctp_cmd_seq_t *commands)
658{
659 struct sctp_chunk *chunk = arg;
660 struct sctp_association *new_asoc;
661 sctp_init_chunk_t *peer_init;
662 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700663 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int error = 0;
665 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900666 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 /* If the packet is an OOTB packet which is temporarily on the
669 * control endpoint, respond with an ABORT.
670 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +0000671 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000672 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000673 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* Make sure that the COOKIE_ECHO chunk has a valid length.
677 * In this case, we check that we have enough for at least a
678 * chunk header. More detailed verification is done
679 * in sctp_unpack_cookie().
680 */
681 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000682 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Vlad Yasevich609ee462007-08-31 03:10:59 +0900684 /* If the endpoint is not listening or if the number of associations
685 * on the TCP-style socket exceed the max backlog, respond with an
686 * ABORT.
687 */
688 sk = ep->base.sk;
689 if (!sctp_sstate(sk, LISTENING) ||
690 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000691 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Vlad Yasevich609ee462007-08-31 03:10:59 +0900692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /* "Decode" the chunk. We have no optional parameters so we
694 * are in good shape.
695 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900696 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700698 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
699 sizeof(sctp_chunkhdr_t)))
700 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
703 * "Z" will reply with a COOKIE ACK chunk after building a TCB
704 * and moving to the ESTABLISHED state.
705 */
706 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
707 &err_chk_p);
708
709 /* FIXME:
710 * If the re-build failed, what is the proper error path
711 * from here?
712 *
713 * [We should abort the association. --piggy]
714 */
715 if (!new_asoc) {
716 /* FIXME: Several errors are possible. A bad cookie should
717 * be silently discarded, but think about logging it too.
718 */
719 switch (error) {
720 case -SCTP_IERROR_NOMEM:
721 goto nomem;
722
723 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000724 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000726 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 case -SCTP_IERROR_BAD_SIG:
729 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000730 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700735 /* Delay state machine commands until later.
736 *
737 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 * the sctp_unpack_cookie() already.
739 */
740 /* This is a brand-new association, so these are not yet side
741 * effects--it is safe to run them here.
742 */
743 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
744
Wei Yongjunde6becd2011-04-19 21:30:51 +0000745 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800746 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 peer_init, GFP_ATOMIC))
748 goto nomem_init;
749
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700750 /* SCTP-AUTH: Now that we've populate required fields in
751 * sctp_process_init, set up the assocaition shared keys as
752 * necessary so that we can potentially authenticate the ACK
753 */
754 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
755 if (error)
756 goto nomem_init;
757
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700758 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
759 * is supposed to be authenticated and we have to do delayed
760 * authentication. We've just recreated the association using
761 * the information in the cookie and now it's much easier to
762 * do the authentication.
763 */
764 if (chunk->auth_chunk) {
765 struct sctp_chunk auth;
766 sctp_ierror_t ret;
767
Daniel Borkmann892b46a2014-03-03 17:23:04 +0100768 /* Make sure that we and the peer are AUTH capable */
769 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
Daniel Borkmann892b46a2014-03-03 17:23:04 +0100770 sctp_association_free(new_asoc);
771 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
772 }
773
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700774 /* set-up our fake chunk so that we can process it */
775 auth.skb = chunk->auth_chunk;
776 auth.asoc = chunk->asoc;
777 auth.sctp_hdr = chunk->sctp_hdr;
778 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
779 sizeof(sctp_chunkhdr_t));
780 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
781 auth.transport = chunk->transport;
782
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000783 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700784 if (ret != SCTP_IERROR_NO_ERROR) {
785 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000786 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700787 }
788 }
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 repl = sctp_make_cookie_ack(new_asoc, chunk);
791 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700792 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* RFC 2960 5.1 Normal Establishment of an Association
795 *
796 * D) IMPLEMENTATION NOTE: An implementation may choose to
797 * send the Communication Up notification to the SCTP user
798 * upon reception of a valid COOKIE ECHO chunk.
799 */
800 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
801 new_asoc->c.sinit_num_ostreams,
802 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700803 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!ev)
805 goto nomem_ev;
806
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900807 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800808 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800810 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800812 if (new_asoc->peer.adaptation_ind) {
813 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700815 if (!ai_ev)
816 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700819 /* Add all the state machine commands now since we've created
820 * everything. This way we don't introduce memory corruptions
821 * during side-effect processing and correclty count established
822 * associations.
823 */
824 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
825 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
826 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000827 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
828 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700829 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
830
831 if (new_asoc->autoclose)
832 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
833 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
834
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700835 /* This will send the COOKIE ACK */
836 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
837
838 /* Queue the ASSOC_CHANGE event */
839 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
840
841 /* Send up the Adaptation Layer Indication event */
842 if (ai_ev)
843 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
844 SCTP_ULPEVENT(ai_ev));
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return SCTP_DISPOSITION_CONSUME;
847
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700848nomem_aiev:
849 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850nomem_ev:
851 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852nomem_init:
853 sctp_association_free(new_asoc);
854nomem:
855 return SCTP_DISPOSITION_NOMEM;
856}
857
858/*
859 * Respond to a normal COOKIE ACK chunk.
860 * We are the side that is being asked for an association.
861 *
862 * RFC 2960 5.1 Normal Establishment of an Association
863 *
864 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
865 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
866 * timer. It may also notify its ULP about the successful
867 * establishment of the association with a Communication Up
868 * notification (see Section 10).
869 *
870 * Verification Tag:
871 * Inputs
872 * (endpoint, asoc, chunk)
873 *
874 * Outputs
875 * (asoc, reply_msg, msg_up, timers, counters)
876 *
877 * The return value is the disposition of the chunk.
878 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000879sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
880 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 const struct sctp_association *asoc,
882 const sctp_subtype_t type, void *arg,
883 sctp_cmd_seq_t *commands)
884{
885 struct sctp_chunk *chunk = arg;
886 struct sctp_ulpevent *ev;
887
888 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000889 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /* Verify that the chunk length for the COOKIE-ACK is OK.
892 * If we don't do this, any bundled chunks may be junked.
893 */
894 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000895 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 commands);
897
898 /* Reset init error count upon receipt of COOKIE-ACK,
899 * to avoid problems with the managemement of this
900 * counter in stale cookie situations when a transition back
901 * from the COOKIE-ECHOED state to the COOKIE-WAIT
902 * state is performed.
903 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700904 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* RFC 2960 5.1 Normal Establishment of an Association
907 *
908 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
909 * from the COOKIE-ECHOED state to the ESTABLISHED state,
910 * stopping the T1-cookie timer.
911 */
912 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
913 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
914 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
915 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000916 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
917 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
919 if (asoc->autoclose)
920 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
921 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* It may also notify its ULP about the successful
924 * establishment of the association with a Communication Up
925 * notification (see Section 10).
926 */
927 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
928 0, asoc->c.sinit_num_ostreams,
929 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700930 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 if (!ev)
933 goto nomem;
934
935 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
936
937 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800938 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800940 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800942 if (asoc->peer.adaptation_ind) {
943 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (!ev)
945 goto nomem;
946
947 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
948 SCTP_ULPEVENT(ev));
949 }
950
951 return SCTP_DISPOSITION_CONSUME;
952nomem:
953 return SCTP_DISPOSITION_NOMEM;
954}
955
956/* Generate and sendout a heartbeat packet. */
957static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
958 const struct sctp_association *asoc,
959 const sctp_subtype_t type,
960 void *arg,
961 sctp_cmd_seq_t *commands)
962{
963 struct sctp_transport *transport = (struct sctp_transport *) arg;
964 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000967 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 if (!reply)
969 return SCTP_DISPOSITION_NOMEM;
970
971 /* Set rto_pending indicating that an RTT measurement
972 * is started with this heartbeat chunk.
973 */
974 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
975 SCTP_TRANSPORT(transport));
976
977 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
978 return SCTP_DISPOSITION_CONSUME;
979}
980
981/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000982sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
983 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 const struct sctp_association *asoc,
985 const sctp_subtype_t type,
986 void *arg,
987 sctp_cmd_seq_t *commands)
988{
989 struct sctp_transport *transport = (struct sctp_transport *) arg;
990
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400991 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700992 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
993 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
995 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800996 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000997 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
998 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return SCTP_DISPOSITION_DELETE_TCB;
1000 }
1001
1002 /* Section 3.3.5.
1003 * The Sender-specific Heartbeat Info field should normally include
1004 * information about the sender's current time when this HEARTBEAT
1005 * chunk is sent and the destination transport address to which this
1006 * HEARTBEAT is sent (see Section 8.3).
1007 */
1008
Frank Filz52ccb8e2005-12-22 11:36:46 -08001009 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (SCTP_DISPOSITION_NOMEM ==
1011 sctp_sf_heartbeat(ep, asoc, type, arg,
1012 commands))
1013 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 /* Set transport error counter and association error counter
1016 * when sending heartbeat.
1017 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001018 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 SCTP_TRANSPORT(transport));
1020 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001021 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1022 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1024 SCTP_TRANSPORT(transport));
1025
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001026 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/*
1030 * Process an heartbeat request.
1031 *
1032 * Section: 8.3 Path Heartbeat
1033 * The receiver of the HEARTBEAT should immediately respond with a
1034 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1035 * from the received HEARTBEAT chunk.
1036 *
1037 * Verification Tag: 8.5 Verification Tag [Normal verification]
1038 * When receiving an SCTP packet, the endpoint MUST ensure that the
1039 * value in the Verification Tag field of the received SCTP packet
1040 * matches its own Tag. If the received Verification Tag value does not
1041 * match the receiver's own tag value, the receiver shall silently
1042 * discard the packet and shall not process it any further except for
1043 * those cases listed in Section 8.5.1 below.
1044 *
1045 * Inputs
1046 * (endpoint, asoc, chunk)
1047 *
1048 * Outputs
1049 * (asoc, reply_msg, msg_up, timers, counters)
1050 *
1051 * The return value is the disposition of the chunk.
1052 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001053sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1054 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 const struct sctp_association *asoc,
1056 const sctp_subtype_t type,
1057 void *arg,
1058 sctp_cmd_seq_t *commands)
1059{
Thomas Graf06a31e22012-11-30 02:16:27 +00001060 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 struct sctp_chunk *chunk = arg;
1062 struct sctp_chunk *reply;
1063 size_t paylen = 0;
1064
1065 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001066 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /* Make sure that the HEARTBEAT chunk has a valid length. */
1069 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001070 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 commands);
1072
1073 /* 8.3 The receiver of the HEARTBEAT should immediately
1074 * respond with a HEARTBEAT ACK that contains the Heartbeat
1075 * Information field copied from the received HEARTBEAT chunk.
1076 */
1077 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001078 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001080
1081 if (ntohs(param_hdr->length) > paylen)
1082 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1083 param_hdr, commands);
1084
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001085 if (!pskb_pull(chunk->skb, paylen))
1086 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Thomas Graf06a31e22012-11-30 02:16:27 +00001088 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (!reply)
1090 goto nomem;
1091
1092 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1093 return SCTP_DISPOSITION_CONSUME;
1094
1095nomem:
1096 return SCTP_DISPOSITION_NOMEM;
1097}
1098
1099/*
1100 * Process the returning HEARTBEAT ACK.
1101 *
1102 * Section: 8.3 Path Heartbeat
1103 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1104 * should clear the error counter of the destination transport
1105 * address to which the HEARTBEAT was sent, and mark the destination
1106 * transport address as active if it is not so marked. The endpoint may
1107 * optionally report to the upper layer when an inactive destination
1108 * address is marked as active due to the reception of the latest
1109 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1110 * clear the association overall error count as well (as defined
1111 * in section 8.1).
1112 *
1113 * The receiver of the HEARTBEAT ACK should also perform an RTT
1114 * measurement for that destination transport address using the time
1115 * value carried in the HEARTBEAT ACK chunk.
1116 *
1117 * Verification Tag: 8.5 Verification Tag [Normal verification]
1118 *
1119 * Inputs
1120 * (endpoint, asoc, chunk)
1121 *
1122 * Outputs
1123 * (asoc, reply_msg, msg_up, timers, counters)
1124 *
1125 * The return value is the disposition of the chunk.
1126 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001127sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1128 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 const struct sctp_association *asoc,
1130 const sctp_subtype_t type,
1131 void *arg,
1132 sctp_cmd_seq_t *commands)
1133{
1134 struct sctp_chunk *chunk = arg;
1135 union sctp_addr from_addr;
1136 struct sctp_transport *link;
1137 sctp_sender_hb_info_t *hbinfo;
1138 unsigned long max_interval;
1139
1140 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001141 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001144 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1145 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001146 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 commands);
1148
1149 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001150 /* Make sure that the length of the parameter is what we expect */
1151 if (ntohs(hbinfo->param_hdr.length) !=
1152 sizeof(sctp_sender_hb_info_t)) {
1153 return SCTP_DISPOSITION_DISCARD;
1154 }
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001157 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001160 if (unlikely(!link)) {
1161 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001162 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1163 __func__,
1164 asoc,
1165 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001166 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001167 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1168 __func__,
1169 asoc,
1170 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return SCTP_DISPOSITION_DISCARD;
1173 }
1174
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001175 /* Validate the 64-bit random nonce. */
1176 if (hbinfo->hb_nonce != link->hb_nonce)
1177 return SCTP_DISPOSITION_DISCARD;
1178
Frank Filz52ccb8e2005-12-22 11:36:46 -08001179 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /* Check if the timestamp looks valid. */
1182 if (time_after(hbinfo->sent_at, jiffies) ||
1183 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001184 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001186 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return SCTP_DISPOSITION_DISCARD;
1188 }
1189
1190 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1191 * the HEARTBEAT should clear the error counter of the
1192 * destination transport address to which the HEARTBEAT was
1193 * sent and mark the destination transport address as active if
1194 * it is not so marked.
1195 */
1196 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1197
1198 return SCTP_DISPOSITION_CONSUME;
1199}
1200
1201/* Helper function to send out an abort for the restart
1202 * condition.
1203 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001204static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct sctp_chunk *init,
1206 sctp_cmd_seq_t *commands)
1207{
1208 int len;
1209 struct sctp_packet *pkt;
1210 union sctp_addr_param *addrparm;
1211 struct sctp_errhdr *errhdr;
1212 struct sctp_endpoint *ep;
1213 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1214 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1215
1216 /* Build the error on the stack. We are way to malloc crazy
1217 * throughout the code today.
1218 */
1219 errhdr = (struct sctp_errhdr *)buffer;
1220 addrparm = (union sctp_addr_param *)errhdr->variable;
1221
1222 /* Copy into a parm format. */
1223 len = af->to_addr_param(ssa, addrparm);
1224 len += sizeof(sctp_errhdr_t);
1225
1226 errhdr->cause = SCTP_ERROR_RESTART;
1227 errhdr->length = htons(len);
1228
1229 /* Assign to the control socket. */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001230 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /* Association is NULL since this may be a restart attack and we
1233 * want to send back the attacker's vtag.
1234 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001235 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 if (!pkt)
1238 goto out;
1239 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1240
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001241 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 /* Discard the rest of the inbound packet. */
1244 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1245
1246out:
1247 /* Even if there is no memory, treat as a failure so
1248 * the packet will get dropped.
1249 */
1250 return 0;
1251}
1252
Joe Perches123031c2010-09-08 11:04:21 +00001253static bool list_has_sctp_addr(const struct list_head *list,
1254 union sctp_addr *ipaddr)
1255{
1256 struct sctp_transport *addr;
1257
1258 list_for_each_entry(addr, list, transports) {
1259 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1260 return true;
1261 }
1262
1263 return false;
1264}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265/* A restart is occurring, check to make sure no new addresses
1266 * are being added as we may be under a takeover attack.
1267 */
1268static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1269 const struct sctp_association *asoc,
1270 struct sctp_chunk *init,
1271 sctp_cmd_seq_t *commands)
1272{
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001273 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001274 struct sctp_transport *new_addr;
1275 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Joe Perches123031c2010-09-08 11:04:21 +00001277 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 * ...
1279 * Before responding the endpoint MUST check to see if the
1280 * unexpected INIT adds new addresses to the association. If new
1281 * addresses are added to the association, the endpoint MUST respond
1282 * with an ABORT..
1283 */
1284
1285 /* Search through all current addresses and make sure
1286 * we aren't adding any new ones.
1287 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001288 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001289 transports) {
1290 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1291 &new_addr->ipaddr)) {
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001292 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001293 commands);
1294 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 break;
Joe Perches123031c2010-09-08 11:04:21 +00001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
1299 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303/* Populate the verification/tie tags based on overlapping INIT
1304 * scenario.
1305 *
1306 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1307 */
1308static void sctp_tietags_populate(struct sctp_association *new_asoc,
1309 const struct sctp_association *asoc)
1310{
1311 switch (asoc->state) {
1312
1313 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1314
1315 case SCTP_STATE_COOKIE_WAIT:
1316 new_asoc->c.my_vtag = asoc->c.my_vtag;
1317 new_asoc->c.my_ttag = asoc->c.my_vtag;
1318 new_asoc->c.peer_ttag = 0;
1319 break;
1320
1321 case SCTP_STATE_COOKIE_ECHOED:
1322 new_asoc->c.my_vtag = asoc->c.my_vtag;
1323 new_asoc->c.my_ttag = asoc->c.my_vtag;
1324 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1325 break;
1326
1327 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1328 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1329 */
1330 default:
1331 new_asoc->c.my_ttag = asoc->c.my_vtag;
1332 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1333 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /* Other parameters for the endpoint SHOULD be copied from the
1337 * existing parameters of the association (e.g. number of
1338 * outbound streams) into the INIT ACK and cookie.
1339 */
1340 new_asoc->rwnd = asoc->rwnd;
1341 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1342 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1343 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1344}
1345
1346/*
1347 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1348 * handling action.
1349 *
1350 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1351 *
1352 * Returns value representing action to be taken. These action values
1353 * correspond to Action/Description values in RFC 2960, Table 2.
1354 */
1355static char sctp_tietags_compare(struct sctp_association *new_asoc,
1356 const struct sctp_association *asoc)
1357{
1358 /* In this case, the peer may have restarted. */
1359 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1360 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1361 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1362 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1363 return 'A';
1364
1365 /* Collision case B. */
1366 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1367 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1368 (0 == asoc->c.peer_vtag))) {
1369 return 'B';
1370 }
1371
1372 /* Collision case D. */
1373 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1374 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1375 return 'D';
1376
1377 /* Collision case C. */
1378 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1379 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1380 (0 == new_asoc->c.my_ttag) &&
1381 (0 == new_asoc->c.peer_ttag))
1382 return 'C';
1383
1384 /* No match to any of the special cases; discard this packet. */
1385 return 'E';
1386}
1387
1388/* Common helper routine for both duplicate and simulataneous INIT
1389 * chunk handling.
1390 */
1391static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001392 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 const struct sctp_endpoint *ep,
1394 const struct sctp_association *asoc,
1395 const sctp_subtype_t type,
1396 void *arg, sctp_cmd_seq_t *commands)
1397{
1398 sctp_disposition_t retval;
1399 struct sctp_chunk *chunk = arg;
1400 struct sctp_chunk *repl;
1401 struct sctp_association *new_asoc;
1402 struct sctp_chunk *err_chunk;
1403 struct sctp_packet *packet;
1404 sctp_unrecognized_param_t *unk_param;
1405 int len;
1406
1407 /* 6.10 Bundling
1408 * An endpoint MUST NOT bundle INIT, INIT ACK or
1409 * SHUTDOWN COMPLETE with any other chunks.
1410 *
1411 * IG Section 2.11.2
1412 * Furthermore, we require that the receiver of an INIT chunk MUST
1413 * enforce these rules by silently discarding an arriving packet
1414 * with an INIT chunk that is bundled with other chunks.
1415 */
1416 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001417 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001420 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
1422 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001423 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /* Make sure that the INIT chunk has a valid length.
1426 * In this case, we generate a protocol violation since we have
1427 * an association established.
1428 */
1429 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001430 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 commands);
1432 /* Grab the INIT header. */
1433 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1434
1435 /* Tag the variable length parameters. */
1436 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1437
1438 /* Verify the INIT chunk before processing it. */
1439 err_chunk = NULL;
Vlad Yaseviche5eae4a2014-04-17 17:26:50 +02001440 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1442 &err_chunk)) {
1443 /* This chunk contains fatal error. It is to be discarded.
1444 * Send an ABORT, with causes if there is any.
1445 */
1446 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001447 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 (__u8 *)(err_chunk->chunk_hdr) +
1449 sizeof(sctp_chunkhdr_t),
1450 ntohs(err_chunk->chunk_hdr->length) -
1451 sizeof(sctp_chunkhdr_t));
1452
1453 if (packet) {
1454 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1455 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001456 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 retval = SCTP_DISPOSITION_CONSUME;
1458 } else {
1459 retval = SCTP_DISPOSITION_NOMEM;
1460 }
1461 goto cleanup;
1462 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001463 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 commands);
1465 }
1466 }
1467
1468 /*
1469 * Other parameters for the endpoint SHOULD be copied from the
1470 * existing parameters of the association (e.g. number of
1471 * outbound streams) into the INIT ACK and cookie.
1472 * FIXME: We are copying parameters from the endpoint not the
1473 * association.
1474 */
1475 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1476 if (!new_asoc)
1477 goto nomem;
1478
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001479 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1480 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1481 goto nomem;
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* In the outbound INIT ACK the endpoint MUST copy its current
1484 * Verification Tag and Peers Verification tag into a reserved
1485 * place (local tie-tag and per tie-tag) within the state cookie.
1486 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001487 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001489 GFP_ATOMIC))
1490 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /* Make sure no new addresses are being added during the
1493 * restart. Do not do this check for COOKIE-WAIT state,
1494 * since there are no peer addresses to check against.
1495 * Upon return an ABORT will have been sent if needed.
1496 */
1497 if (!sctp_state(asoc, COOKIE_WAIT)) {
1498 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1499 commands)) {
1500 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001501 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
1503 }
1504
1505 sctp_tietags_populate(new_asoc, asoc);
1506
1507 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1508
1509 /* If there are errors need to be reported for unknown parameters,
1510 * make sure to reserve enough room in the INIT ACK for them.
1511 */
1512 len = 0;
1513 if (err_chunk) {
1514 len = ntohs(err_chunk->chunk_hdr->length) -
1515 sizeof(sctp_chunkhdr_t);
1516 }
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1519 if (!repl)
1520 goto nomem;
1521
1522 /* If there are errors need to be reported for unknown parameters,
1523 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1524 * parameter.
1525 */
1526 if (err_chunk) {
1527 /* Get the "Unrecognized parameter" parameter(s) out of the
1528 * ERROR chunk generated by sctp_verify_init(). Since the
1529 * error cause code for "unknown parameter" and the
1530 * "Unrecognized parameter" type is the same, we can
1531 * construct the parameters in INIT ACK by copying the
1532 * ERROR causes over.
1533 */
1534 unk_param = (sctp_unrecognized_param_t *)
1535 ((__u8 *)(err_chunk->chunk_hdr) +
1536 sizeof(sctp_chunkhdr_t));
1537 /* Replace the cause code with the "Unrecognized parameter"
1538 * parameter type.
1539 */
1540 sctp_addto_chunk(repl, len, unk_param);
1541 }
1542
1543 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1544 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1545
1546 /*
1547 * Note: After sending out INIT ACK with the State Cookie parameter,
1548 * "Z" MUST NOT allocate any resources for this new association.
1549 * Otherwise, "Z" will be vulnerable to resource attacks.
1550 */
1551 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1552 retval = SCTP_DISPOSITION_CONSUME;
1553
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001554 return retval;
1555
1556nomem:
1557 retval = SCTP_DISPOSITION_NOMEM;
1558nomem_retval:
1559 if (new_asoc)
1560 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561cleanup:
1562 if (err_chunk)
1563 sctp_chunk_free(err_chunk);
1564 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001568 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 * This means we started an INIT and then we got an INIT request from
1570 * our peer.
1571 *
1572 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1573 * This usually indicates an initialization collision, i.e., each
1574 * endpoint is attempting, at about the same time, to establish an
1575 * association with the other endpoint.
1576 *
1577 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1578 * endpoint MUST respond with an INIT ACK using the same parameters it
1579 * sent in its original INIT chunk (including its Verification Tag,
1580 * unchanged). These original parameters are combined with those from the
1581 * newly received INIT chunk. The endpoint shall also generate a State
1582 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1583 * INIT to calculate the State Cookie.
1584 *
1585 * After that, the endpoint MUST NOT change its state, the T1-init
1586 * timer shall be left running and the corresponding TCB MUST NOT be
1587 * destroyed. The normal procedures for handling State Cookies when
1588 * a TCB exists will resolve the duplicate INITs to a single association.
1589 *
1590 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1591 * its Tie-Tags with the Tag information of itself and its peer (see
1592 * section 5.2.2 for a description of the Tie-Tags).
1593 *
1594 * Verification Tag: Not explicit, but an INIT can not have a valid
1595 * verification tag, so we skip the check.
1596 *
1597 * Inputs
1598 * (endpoint, asoc, chunk)
1599 *
1600 * Outputs
1601 * (asoc, reply_msg, msg_up, timers, counters)
1602 *
1603 * The return value is the disposition of the chunk.
1604 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001605sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1606 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 const struct sctp_association *asoc,
1608 const sctp_subtype_t type,
1609 void *arg,
1610 sctp_cmd_seq_t *commands)
1611{
1612 /* Call helper to do the real work for both simulataneous and
1613 * duplicate INIT chunk handling.
1614 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001615 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
1618/*
1619 * Handle duplicated INIT messages. These are usually delayed
1620 * restransmissions.
1621 *
1622 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1623 * COOKIE-ECHOED and COOKIE-WAIT
1624 *
1625 * Unless otherwise stated, upon reception of an unexpected INIT for
1626 * this association, the endpoint shall generate an INIT ACK with a
1627 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1628 * current Verification Tag and peer's Verification Tag into a reserved
1629 * place within the state cookie. We shall refer to these locations as
1630 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1631 * containing this INIT ACK MUST carry a Verification Tag value equal to
1632 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1633 * MUST contain a new Initiation Tag (randomly generated see Section
1634 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1635 * existing parameters of the association (e.g. number of outbound
1636 * streams) into the INIT ACK and cookie.
1637 *
1638 * After sending out the INIT ACK, the endpoint shall take no further
1639 * actions, i.e., the existing association, including its current state,
1640 * and the corresponding TCB MUST NOT be changed.
1641 *
1642 * Note: Only when a TCB exists and the association is not in a COOKIE-
1643 * WAIT state are the Tie-Tags populated. For a normal association INIT
1644 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1645 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1646 * State Cookie are populated as specified in section 5.2.1.
1647 *
1648 * Verification Tag: Not specified, but an INIT has no way of knowing
1649 * what the verification tag could be, so we ignore it.
1650 *
1651 * Inputs
1652 * (endpoint, asoc, chunk)
1653 *
1654 * Outputs
1655 * (asoc, reply_msg, msg_up, timers, counters)
1656 *
1657 * The return value is the disposition of the chunk.
1658 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001659sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1660 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 const struct sctp_association *asoc,
1662 const sctp_subtype_t type,
1663 void *arg,
1664 sctp_cmd_seq_t *commands)
1665{
1666 /* Call helper to do the real work for both simulataneous and
1667 * duplicate INIT chunk handling.
1668 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001669 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
1672
Vlad Yasevich610ab732007-01-15 19:18:30 -08001673/*
1674 * Unexpected INIT-ACK handler.
1675 *
1676 * Section 5.2.3
1677 * If an INIT ACK received by an endpoint in any state other than the
1678 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1679 * An unexpected INIT ACK usually indicates the processing of an old or
1680 * duplicated INIT chunk.
1681*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001682sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1683 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001684 const struct sctp_association *asoc,
1685 const sctp_subtype_t type,
1686 void *arg, sctp_cmd_seq_t *commands)
1687{
1688 /* Per the above section, we'll discard the chunk if we have an
1689 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1690 */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00001691 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001692 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001693 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001694 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1698 *
1699 * Section 5.2.4
1700 * A) In this case, the peer may have restarted.
1701 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001702static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1703 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 const struct sctp_association *asoc,
1705 struct sctp_chunk *chunk,
1706 sctp_cmd_seq_t *commands,
1707 struct sctp_association *new_asoc)
1708{
1709 sctp_init_chunk_t *peer_init;
1710 struct sctp_ulpevent *ev;
1711 struct sctp_chunk *repl;
1712 struct sctp_chunk *err;
1713 sctp_disposition_t disposition;
1714
1715 /* new_asoc is a brand-new association, so these are not yet
1716 * side effects--it is safe to run them here.
1717 */
1718 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1719
Wei Yongjunde6becd2011-04-19 21:30:51 +00001720 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 GFP_ATOMIC))
1722 goto nomem;
1723
1724 /* Make sure no new addresses are being added during the
1725 * restart. Though this is a pretty complicated attack
1726 * since you'd have to get inside the cookie.
1727 */
1728 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1729 return SCTP_DISPOSITION_CONSUME;
1730 }
1731
1732 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1733 * the peer has restarted (Action A), it MUST NOT setup a new
1734 * association but instead resend the SHUTDOWN ACK and send an ERROR
1735 * chunk with a "Cookie Received while Shutting Down" error cause to
1736 * its peer.
1737 */
1738 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001739 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1741 chunk, commands);
1742 if (SCTP_DISPOSITION_NOMEM == disposition)
1743 goto nomem;
1744
1745 err = sctp_make_op_error(asoc, chunk,
1746 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001747 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (err)
1749 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1750 SCTP_CHUNK(err));
1751
1752 return SCTP_DISPOSITION_CONSUME;
1753 }
1754
Wei Yongjuna000c012011-05-29 23:23:36 +00001755 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1756 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001758 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1759 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1760 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1762
Wei Yongjuna000c012011-05-29 23:23:36 +00001763 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1764 * and ASCONF-ACK cache.
1765 */
1766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1768 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 repl = sctp_make_cookie_ack(new_asoc, chunk);
1771 if (!repl)
1772 goto nomem;
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 /* Report association restart to upper layer. */
1775 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1776 new_asoc->c.sinit_num_ostreams,
1777 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001778 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (!ev)
1780 goto nomem_ev;
1781
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001782 /* Update the content of current association. */
1783 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
Xufeng Zhang9839ff02013-01-23 16:44:34 +00001785 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1786 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1787 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return SCTP_DISPOSITION_CONSUME;
1789
1790nomem_ev:
1791 sctp_chunk_free(repl);
1792nomem:
1793 return SCTP_DISPOSITION_NOMEM;
1794}
1795
1796/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1797 *
1798 * Section 5.2.4
1799 * B) In this case, both sides may be attempting to start an association
1800 * at about the same time but the peer endpoint started its INIT
1801 * after responding to the local endpoint's INIT
1802 */
1803/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001804static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1805 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 const struct sctp_association *asoc,
1807 struct sctp_chunk *chunk,
1808 sctp_cmd_seq_t *commands,
1809 struct sctp_association *new_asoc)
1810{
1811 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct sctp_chunk *repl;
1813
1814 /* new_asoc is a brand-new association, so these are not yet
1815 * side effects--it is safe to run them here.
1816 */
1817 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001818 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 GFP_ATOMIC))
1820 goto nomem;
1821
1822 /* Update the content of current association. */
1823 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1824 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1825 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001826 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1828
1829 repl = sctp_make_cookie_ack(new_asoc, chunk);
1830 if (!repl)
1831 goto nomem;
1832
1833 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 /* RFC 2960 5.1 Normal Establishment of an Association
1836 *
1837 * D) IMPLEMENTATION NOTE: An implementation may choose to
1838 * send the Communication Up notification to the SCTP user
1839 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001840 *
1841 * Sadly, this needs to be implemented as a side-effect, because
1842 * we are not guaranteed to have set the association id of the real
1843 * association and so these notifications need to be delayed until
1844 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Vlad Yasevich07d93962007-05-04 13:55:27 -07001847 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001850 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001852 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001853 *
1854 * This also needs to be done as a side effect for the same reason as
1855 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001857 if (asoc->peer.adaptation_ind)
1858 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 return SCTP_DISPOSITION_CONSUME;
1861
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862nomem:
1863 return SCTP_DISPOSITION_NOMEM;
1864}
1865
1866/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1867 *
1868 * Section 5.2.4
1869 * C) In this case, the local endpoint's cookie has arrived late.
1870 * Before it arrived, the local endpoint sent an INIT and received an
1871 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1872 * but a new tag of its own.
1873 */
1874/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001875static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1876 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 const struct sctp_association *asoc,
1878 struct sctp_chunk *chunk,
1879 sctp_cmd_seq_t *commands,
1880 struct sctp_association *new_asoc)
1881{
1882 /* The cookie should be silently discarded.
1883 * The endpoint SHOULD NOT change states and should leave
1884 * any timers running.
1885 */
1886 return SCTP_DISPOSITION_DISCARD;
1887}
1888
1889/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1890 *
1891 * Section 5.2.4
1892 *
1893 * D) When both local and remote tags match the endpoint should always
1894 * enter the ESTABLISHED state, if it has not already done so.
1895 */
1896/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001897static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1898 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 const struct sctp_association *asoc,
1900 struct sctp_chunk *chunk,
1901 sctp_cmd_seq_t *commands,
1902 struct sctp_association *new_asoc)
1903{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001904 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 struct sctp_chunk *repl;
1906
1907 /* Clarification from Implementor's Guide:
1908 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001909 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1910 * It should stop any cookie timer that may be running and send
1911 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
1913
1914 /* Don't accidentally move back into established state. */
1915 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1916 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1917 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1918 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1919 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001920 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1922 SCTP_NULL());
1923
1924 /* RFC 2960 5.1 Normal Establishment of an Association
1925 *
1926 * D) IMPLEMENTATION NOTE: An implementation may choose
1927 * to send the Communication Up notification to the
1928 * SCTP user upon reception of a valid COOKIE
1929 * ECHO chunk.
1930 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001931 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001933 asoc->c.sinit_num_ostreams,
1934 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001935 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 if (!ev)
1937 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001940 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001942 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001944 if (asoc->peer.adaptation_ind) {
1945 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001947 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto nomem;
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 repl = sctp_make_cookie_ack(new_asoc, chunk);
1954 if (!repl)
1955 goto nomem;
1956
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001957 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1958
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001959 if (ev)
1960 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1961 SCTP_ULPEVENT(ev));
1962 if (ai_ev)
1963 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1964 SCTP_ULPEVENT(ai_ev));
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return SCTP_DISPOSITION_CONSUME;
1967
1968nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001969 if (ai_ev)
1970 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (ev)
1972 sctp_ulpevent_free(ev);
1973 return SCTP_DISPOSITION_NOMEM;
1974}
1975
1976/*
1977 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1978 * chunk was retransmitted and then delayed in the network.
1979 *
1980 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1981 *
1982 * Verification Tag: None. Do cookie validation.
1983 *
1984 * Inputs
1985 * (endpoint, asoc, chunk)
1986 *
1987 * Outputs
1988 * (asoc, reply_msg, msg_up, timers, counters)
1989 *
1990 * The return value is the disposition of the chunk.
1991 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001992sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1993 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 const struct sctp_association *asoc,
1995 const sctp_subtype_t type,
1996 void *arg,
1997 sctp_cmd_seq_t *commands)
1998{
1999 sctp_disposition_t retval;
2000 struct sctp_chunk *chunk = arg;
2001 struct sctp_association *new_asoc;
2002 int error = 0;
2003 char action;
2004 struct sctp_chunk *err_chk_p;
2005
2006 /* Make sure that the chunk has a valid length from the protocol
2007 * perspective. In this case check to make sure we have at least
2008 * enough for the chunk header. Cookie length verification is
2009 * done later.
2010 */
2011 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002012 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 commands);
2014
2015 /* "Decode" the chunk. We have no optional parameters so we
2016 * are in good shape.
2017 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002018 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002019 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2020 sizeof(sctp_chunkhdr_t)))
2021 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2024 * of a duplicate COOKIE ECHO match the Verification Tags of the
2025 * current association, consider the State Cookie valid even if
2026 * the lifespan is exceeded.
2027 */
2028 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2029 &err_chk_p);
2030
2031 /* FIXME:
2032 * If the re-build failed, what is the proper error path
2033 * from here?
2034 *
2035 * [We should abort the association. --piggy]
2036 */
2037 if (!new_asoc) {
2038 /* FIXME: Several errors are possible. A bad cookie should
2039 * be silently discarded, but think about logging it too.
2040 */
2041 switch (error) {
2042 case -SCTP_IERROR_NOMEM:
2043 goto nomem;
2044
2045 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002046 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002048 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 case -SCTP_IERROR_BAD_SIG:
2050 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002051 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
2055 /* Compare the tie_tag in cookie with the verification tag of
2056 * current association.
2057 */
2058 action = sctp_tietags_compare(new_asoc, asoc);
2059
2060 switch (action) {
2061 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002062 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 new_asoc);
2064 break;
2065
2066 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002067 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 new_asoc);
2069 break;
2070
2071 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002072 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 new_asoc);
2074 break;
2075
2076 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002077 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 new_asoc);
2079 break;
2080
2081 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002082 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /* Delete the tempory new association. */
Vlad Yasevichf2815632013-03-12 15:53:23 +00002087 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2089
Max Matveevd5ccd492011-08-29 21:02:24 +00002090 /* Restore association pointer to provide SCTP command interpeter
2091 * with a valid context in case it needs to manipulate
2092 * the queues */
2093 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2094 SCTP_ASOC((struct sctp_association *)asoc));
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return retval;
2097
2098nomem:
2099 return SCTP_DISPOSITION_NOMEM;
2100}
2101
2102/*
2103 * Process an ABORT. (SHUTDOWN-PENDING state)
2104 *
2105 * See sctp_sf_do_9_1_abort().
2106 */
2107sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002108 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 const struct sctp_endpoint *ep,
2110 const struct sctp_association *asoc,
2111 const sctp_subtype_t type,
2112 void *arg,
2113 sctp_cmd_seq_t *commands)
2114{
2115 struct sctp_chunk *chunk = arg;
2116
2117 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002118 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 /* Make sure that the ABORT chunk has a valid length.
2121 * Since this is an ABORT chunk, we have to discard it
2122 * because of the following text:
2123 * RFC 2960, Section 3.3.7
2124 * If an endpoint receives an ABORT with a format error or for an
2125 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002126 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 * as we do not know its true length. So, to be safe, discard the
2128 * packet.
2129 */
2130 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002131 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Vlad Yasevich75205f42007-12-20 14:12:59 -08002133 /* ADD-IP: Special case for ABORT chunks
2134 * F4) One special consideration is that ABORT Chunks arriving
2135 * destined to the IP address being deleted MUST be
2136 * ignored (see Section 5.3.1 for further details).
2137 */
2138 if (SCTP_ADDR_DEL ==
2139 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002140 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002141
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002142 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145/*
2146 * Process an ABORT. (SHUTDOWN-SENT state)
2147 *
2148 * See sctp_sf_do_9_1_abort().
2149 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002150sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2151 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 const struct sctp_association *asoc,
2153 const sctp_subtype_t type,
2154 void *arg,
2155 sctp_cmd_seq_t *commands)
2156{
2157 struct sctp_chunk *chunk = arg;
2158
2159 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002160 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* Make sure that the ABORT chunk has a valid length.
2163 * Since this is an ABORT chunk, we have to discard it
2164 * because of the following text:
2165 * RFC 2960, Section 3.3.7
2166 * If an endpoint receives an ABORT with a format error or for an
2167 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002168 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 * as we do not know its true length. So, to be safe, discard the
2170 * packet.
2171 */
2172 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002173 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Vlad Yasevich75205f42007-12-20 14:12:59 -08002175 /* ADD-IP: Special case for ABORT chunks
2176 * F4) One special consideration is that ABORT Chunks arriving
2177 * destined to the IP address being deleted MUST be
2178 * ignored (see Section 5.3.1 for further details).
2179 */
2180 if (SCTP_ADDR_DEL ==
2181 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002182 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /* Stop the T2-shutdown timer. */
2185 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2186 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2187
2188 /* Stop the T5-shutdown guard timer. */
2189 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2190 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2191
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002192 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195/*
2196 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2197 *
2198 * See sctp_sf_do_9_1_abort().
2199 */
2200sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002201 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 const struct sctp_endpoint *ep,
2203 const struct sctp_association *asoc,
2204 const sctp_subtype_t type,
2205 void *arg,
2206 sctp_cmd_seq_t *commands)
2207{
2208 /* The same T2 timer, so we should be able to use
2209 * common function with the SHUTDOWN-SENT state.
2210 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002211 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214/*
2215 * Handle an Error received in COOKIE_ECHOED state.
2216 *
2217 * Only handle the error type of stale COOKIE Error, the other errors will
2218 * be ignored.
2219 *
2220 * Inputs
2221 * (endpoint, asoc, chunk)
2222 *
2223 * Outputs
2224 * (asoc, reply_msg, msg_up, timers, counters)
2225 *
2226 * The return value is the disposition of the chunk.
2227 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002228sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2229 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 const struct sctp_association *asoc,
2231 const sctp_subtype_t type,
2232 void *arg,
2233 sctp_cmd_seq_t *commands)
2234{
2235 struct sctp_chunk *chunk = arg;
2236 sctp_errhdr_t *err;
2237
2238 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002239 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /* Make sure that the ERROR chunk has a valid length.
2242 * The parameter walking depends on this as well.
2243 */
2244 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002245 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 commands);
2247
2248 /* Process the error here */
2249 /* FUTURE FIXME: When PR-SCTP related and other optional
2250 * parms are emitted, this will have to change to handle multiple
2251 * errors.
2252 */
2253 sctp_walk_errors(err, chunk->chunk_hdr) {
2254 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002255 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 arg, commands);
2257 }
2258
2259 /* It is possible to have malformed error causes, and that
2260 * will cause us to end the walk early. However, since
2261 * we are discarding the packet, there should be no adverse
2262 * affects.
2263 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002264 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267/*
2268 * Handle a Stale COOKIE Error
2269 *
2270 * Section: 5.2.6 Handle Stale COOKIE Error
2271 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2272 * one of the following three alternatives.
2273 * ...
2274 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2275 * Preservative parameter requesting an extension to the lifetime of
2276 * the State Cookie. When calculating the time extension, an
2277 * implementation SHOULD use the RTT information measured based on the
2278 * previous COOKIE ECHO / ERROR exchange, and should add no more
2279 * than 1 second beyond the measured RTT, due to long State Cookie
2280 * lifetimes making the endpoint more subject to a replay attack.
2281 *
2282 * Verification Tag: Not explicit, but safe to ignore.
2283 *
2284 * Inputs
2285 * (endpoint, asoc, chunk)
2286 *
2287 * Outputs
2288 * (asoc, reply_msg, msg_up, timers, counters)
2289 *
2290 * The return value is the disposition of the chunk.
2291 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002292static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2293 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 const struct sctp_association *asoc,
2295 const sctp_subtype_t type,
2296 void *arg,
2297 sctp_cmd_seq_t *commands)
2298{
2299 struct sctp_chunk *chunk = arg;
2300 time_t stale;
2301 sctp_cookie_preserve_param_t bht;
2302 sctp_errhdr_t *err;
2303 struct sctp_chunk *reply;
2304 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002305 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Vlad Yasevich81845c212006-01-30 15:59:54 -08002307 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002308 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2309 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002311 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return SCTP_DISPOSITION_DELETE_TCB;
2313 }
2314
2315 err = (sctp_errhdr_t *)(chunk->skb->data);
2316
2317 /* When calculating the time extension, an implementation
2318 * SHOULD use the RTT information measured based on the
2319 * previous COOKIE ECHO / ERROR exchange, and should add no
2320 * more than 1 second beyond the measured RTT, due to long
2321 * State Cookie lifetimes making the endpoint more subject to
2322 * a replay attack.
2323 * Measure of Staleness's unit is usec. (1/1000000 sec)
2324 * Suggested Cookie Life-span Increment's unit is msec.
2325 * (1/1000 sec)
2326 * In general, if you use the suggested cookie life, the value
2327 * found in the field of measure of staleness should be doubled
2328 * to give ample time to retransmit the new cookie and thus
2329 * yield a higher probability of success on the reattempt.
2330 */
Al Viro34bcca22006-11-20 17:27:15 -08002331 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 stale = (stale * 2) / 1000;
2333
2334 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2335 bht.param_hdr.length = htons(sizeof(bht));
2336 bht.lifespan_increment = htonl(stale);
2337
2338 /* Build that new INIT chunk. */
2339 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2340 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2341 if (!reply)
2342 goto nomem;
2343
2344 sctp_addto_chunk(reply, sizeof(bht), &bht);
2345
2346 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2347 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2348
2349 /* Stop pending T3-rtx and heartbeat timers */
2350 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2351 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2352
2353 /* Delete non-primary peer ip addresses since we are transitioning
2354 * back to the COOKIE-WAIT state
2355 */
2356 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2357
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002358 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2359 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002361 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 SCTP_TRANSPORT(asoc->peer.primary_path));
2363
2364 /* Cast away the const modifier, as we want to just
2365 * rerun it through as a sideffect.
2366 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002367 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2370 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2371 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2372 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2373 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2374 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2375
2376 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2377
2378 return SCTP_DISPOSITION_CONSUME;
2379
2380nomem:
2381 return SCTP_DISPOSITION_NOMEM;
2382}
2383
2384/*
2385 * Process an ABORT.
2386 *
2387 * Section: 9.1
2388 * After checking the Verification Tag, the receiving endpoint shall
2389 * remove the association from its record, and shall report the
2390 * termination to its upper layer.
2391 *
2392 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2393 * B) Rules for packet carrying ABORT:
2394 *
2395 * - The endpoint shall always fill in the Verification Tag field of the
2396 * outbound packet with the destination endpoint's tag value if it
2397 * is known.
2398 *
2399 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2400 * MUST follow the procedure described in Section 8.4.
2401 *
2402 * - The receiver MUST accept the packet if the Verification Tag
2403 * matches either its own tag, OR the tag of its peer. Otherwise, the
2404 * receiver MUST silently discard the packet and take no further
2405 * action.
2406 *
2407 * Inputs
2408 * (endpoint, asoc, chunk)
2409 *
2410 * Outputs
2411 * (asoc, reply_msg, msg_up, timers, counters)
2412 *
2413 * The return value is the disposition of the chunk.
2414 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002415sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2416 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 const struct sctp_association *asoc,
2418 const sctp_subtype_t type,
2419 void *arg,
2420 sctp_cmd_seq_t *commands)
2421{
2422 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
2424 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002425 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 /* Make sure that the ABORT chunk has a valid length.
2428 * Since this is an ABORT chunk, we have to discard it
2429 * because of the following text:
2430 * RFC 2960, Section 3.3.7
2431 * If an endpoint receives an ABORT with a format error or for an
2432 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002433 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 * as we do not know its true length. So, to be safe, discard the
2435 * packet.
2436 */
2437 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002438 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Vlad Yasevich75205f42007-12-20 14:12:59 -08002440 /* ADD-IP: Special case for ABORT chunks
2441 * F4) One special consideration is that ABORT Chunks arriving
2442 * destined to the IP address being deleted MUST be
2443 * ignored (see Section 5.3.1 for further details).
2444 */
2445 if (SCTP_ADDR_DEL ==
2446 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002447 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002448
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002449 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002450}
2451
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002452static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2453 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002454 const struct sctp_association *asoc,
2455 const sctp_subtype_t type,
2456 void *arg,
2457 sctp_cmd_seq_t *commands)
2458{
2459 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002460 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002461 __be16 error = SCTP_ERROR_NO_ERROR;
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* See if we have an error cause code in the chunk. */
2464 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002465 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2466
2467 sctp_errhdr_t *err;
2468 sctp_walk_errors(err, chunk->chunk_hdr);
2469 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002470 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002475 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002476 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002477 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002478 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2479 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 return SCTP_DISPOSITION_ABORT;
2482}
2483
2484/*
2485 * Process an ABORT. (COOKIE-WAIT state)
2486 *
2487 * See sctp_sf_do_9_1_abort() above.
2488 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002489sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2490 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 const struct sctp_association *asoc,
2492 const sctp_subtype_t type,
2493 void *arg,
2494 sctp_cmd_seq_t *commands)
2495{
2496 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002497 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002498 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002501 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 /* Make sure that the ABORT chunk has a valid length.
2504 * Since this is an ABORT chunk, we have to discard it
2505 * because of the following text:
2506 * RFC 2960, Section 3.3.7
2507 * If an endpoint receives an ABORT with a format error or for an
2508 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002509 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * as we do not know its true length. So, to be safe, discard the
2511 * packet.
2512 */
2513 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002514 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516 /* See if we have an error cause code in the chunk. */
2517 len = ntohs(chunk->chunk_hdr->length);
2518 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2519 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2520
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002521 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002522 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523}
2524
2525/*
2526 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2527 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002528sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2529 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 const struct sctp_association *asoc,
2531 const sctp_subtype_t type,
2532 void *arg,
2533 sctp_cmd_seq_t *commands)
2534{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002535 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002536 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002537 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
2540/*
2541 * Process an ABORT. (COOKIE-ECHOED state)
2542 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002543sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2544 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 const struct sctp_association *asoc,
2546 const sctp_subtype_t type,
2547 void *arg,
2548 sctp_cmd_seq_t *commands)
2549{
2550 /* There is a single T1 timer, so we should be able to use
2551 * common function with the COOKIE-WAIT state.
2552 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002553 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555
2556/*
2557 * Stop T1 timer and abort association with "INIT failed".
2558 *
2559 * This is common code called by several sctp_sf_*_abort() functions above.
2560 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002561static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2562 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002563 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002564 const struct sctp_association *asoc,
2565 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002567 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2569 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002570 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2572 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002573 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 /* CMD_INIT_FAILED will DELETE_TCB. */
2575 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002576 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002577 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580/*
2581 * sctp_sf_do_9_2_shut
2582 *
2583 * Section: 9.2
2584 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2585 * - enter the SHUTDOWN-RECEIVED state,
2586 *
2587 * - stop accepting new data from its SCTP user
2588 *
2589 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2590 * that all its outstanding DATA chunks have been received by the
2591 * SHUTDOWN sender.
2592 *
2593 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2594 * send a SHUTDOWN in response to a ULP request. And should discard
2595 * subsequent SHUTDOWN chunks.
2596 *
2597 * If there are still outstanding DATA chunks left, the SHUTDOWN
2598 * receiver shall continue to follow normal data transmission
2599 * procedures defined in Section 6 until all outstanding DATA chunks
2600 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2601 * new data from its SCTP user.
2602 *
2603 * Verification Tag: 8.5 Verification Tag [Normal verification]
2604 *
2605 * Inputs
2606 * (endpoint, asoc, chunk)
2607 *
2608 * Outputs
2609 * (asoc, reply_msg, msg_up, timers, counters)
2610 *
2611 * The return value is the disposition of the chunk.
2612 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002613sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2614 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 const struct sctp_association *asoc,
2616 const sctp_subtype_t type,
2617 void *arg,
2618 sctp_cmd_seq_t *commands)
2619{
2620 struct sctp_chunk *chunk = arg;
2621 sctp_shutdownhdr_t *sdh;
2622 sctp_disposition_t disposition;
2623 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002624 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002627 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 /* Make sure that the SHUTDOWN chunk has a valid length. */
2630 if (!sctp_chunk_length_valid(chunk,
2631 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002632 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 commands);
2634
2635 /* Convert the elaborate header. */
2636 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2637 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2638 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002639 ctsn = ntohl(sdh->cum_tsn_ack);
2640
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002641 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2642 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2643 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2644 return SCTP_DISPOSITION_DISCARD;
2645 }
2646
Wei Yongjundf10eec2008-10-23 01:00:21 -07002647 /* If Cumulative TSN Ack beyond the max tsn currently
2648 * send, terminating the association and respond to the
2649 * sender with an ABORT.
2650 */
2651 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002652 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002654 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2655 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2656 * inform the application that it should cease sending data.
2657 */
2658 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2659 if (!ev) {
2660 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002661 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002662 }
2663 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2666 * - enter the SHUTDOWN-RECEIVED state,
2667 * - stop accepting new data from its SCTP user
2668 *
2669 * [This is implicit in the new state.]
2670 */
2671 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2672 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2673 disposition = SCTP_DISPOSITION_CONSUME;
2674
2675 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002676 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 arg, commands);
2678 }
2679
2680 if (SCTP_DISPOSITION_NOMEM == disposition)
2681 goto out;
2682
2683 /* - verify, by checking the Cumulative TSN Ack field of the
2684 * chunk, that all its outstanding DATA chunks have been
2685 * received by the SHUTDOWN sender.
2686 */
2687 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002688 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690out:
2691 return disposition;
2692}
2693
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002694/*
2695 * sctp_sf_do_9_2_shut_ctsn
2696 *
2697 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2698 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2699 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2700 * MUST be processed.
2701 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002702sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2703 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002704 const struct sctp_association *asoc,
2705 const sctp_subtype_t type,
2706 void *arg,
2707 sctp_cmd_seq_t *commands)
2708{
2709 struct sctp_chunk *chunk = arg;
2710 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002711 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002712
2713 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002714 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002715
2716 /* Make sure that the SHUTDOWN chunk has a valid length. */
2717 if (!sctp_chunk_length_valid(chunk,
2718 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002719 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002720 commands);
2721
2722 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002723 ctsn = ntohl(sdh->cum_tsn_ack);
2724
2725 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2726 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2727 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2728 return SCTP_DISPOSITION_DISCARD;
2729 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002730
2731 /* If Cumulative TSN Ack beyond the max tsn currently
2732 * send, terminating the association and respond to the
2733 * sender with an ABORT.
2734 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002735 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002736 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002737
2738 /* verify, by checking the Cumulative TSN Ack field of the
2739 * chunk, that all its outstanding DATA chunks have been
2740 * received by the SHUTDOWN sender.
2741 */
2742 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2743 SCTP_BE32(sdh->cum_tsn_ack));
2744
2745 return SCTP_DISPOSITION_CONSUME;
2746}
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/* RFC 2960 9.2
2749 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2750 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2751 * transport addresses (either in the IP addresses or in the INIT chunk)
2752 * that belong to this association, it should discard the INIT chunk and
2753 * retransmit the SHUTDOWN ACK chunk.
2754 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002755sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2756 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 const struct sctp_association *asoc,
2758 const sctp_subtype_t type,
2759 void *arg,
2760 sctp_cmd_seq_t *commands)
2761{
2762 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2763 struct sctp_chunk *reply;
2764
Vlad Yasevichece25df2007-09-07 16:30:54 -04002765 /* Make sure that the chunk has a valid length */
2766 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002767 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002768 commands);
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Since we are not going to really process this INIT, there
2771 * is no point in verifying chunk boundries. Just generate
2772 * the SHUTDOWN ACK.
2773 */
2774 reply = sctp_make_shutdown_ack(asoc, chunk);
2775 if (NULL == reply)
2776 goto nomem;
2777
2778 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2779 * the T2-SHUTDOWN timer.
2780 */
2781 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2782
2783 /* and restart the T2-shutdown timer. */
2784 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2785 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2786
2787 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2788
2789 return SCTP_DISPOSITION_CONSUME;
2790nomem:
2791 return SCTP_DISPOSITION_NOMEM;
2792}
2793
2794/*
2795 * sctp_sf_do_ecn_cwr
2796 *
2797 * Section: Appendix A: Explicit Congestion Notification
2798 *
2799 * CWR:
2800 *
2801 * RFC 2481 details a specific bit for a sender to send in the header of
2802 * its next outbound TCP segment to indicate to its peer that it has
2803 * reduced its congestion window. This is termed the CWR bit. For
2804 * SCTP the same indication is made by including the CWR chunk.
2805 * This chunk contains one data element, i.e. the TSN number that
2806 * was sent in the ECNE chunk. This element represents the lowest
2807 * TSN number in the datagram that was originally marked with the
2808 * CE bit.
2809 *
2810 * Verification Tag: 8.5 Verification Tag [Normal verification]
2811 * Inputs
2812 * (endpoint, asoc, chunk)
2813 *
2814 * Outputs
2815 * (asoc, reply_msg, msg_up, timers, counters)
2816 *
2817 * The return value is the disposition of the chunk.
2818 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002819sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2820 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 const struct sctp_association *asoc,
2822 const sctp_subtype_t type,
2823 void *arg,
2824 sctp_cmd_seq_t *commands)
2825{
2826 sctp_cwrhdr_t *cwr;
2827 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002828 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
2830 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002831 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002834 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2838 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2839
Al Viro34bcca22006-11-20 17:27:15 -08002840 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002843 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 /* Stop sending ECNE. */
2845 sctp_add_cmd_sf(commands,
2846 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002847 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
2849 return SCTP_DISPOSITION_CONSUME;
2850}
2851
2852/*
2853 * sctp_sf_do_ecne
2854 *
2855 * Section: Appendix A: Explicit Congestion Notification
2856 *
2857 * ECN-Echo
2858 *
2859 * RFC 2481 details a specific bit for a receiver to send back in its
2860 * TCP acknowledgements to notify the sender of the Congestion
2861 * Experienced (CE) bit having arrived from the network. For SCTP this
2862 * same indication is made by including the ECNE chunk. This chunk
2863 * contains one data element, i.e. the lowest TSN associated with the IP
2864 * datagram marked with the CE bit.....
2865 *
2866 * Verification Tag: 8.5 Verification Tag [Normal verification]
2867 * Inputs
2868 * (endpoint, asoc, chunk)
2869 *
2870 * Outputs
2871 * (asoc, reply_msg, msg_up, timers, counters)
2872 *
2873 * The return value is the disposition of the chunk.
2874 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002875sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2876 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 const struct sctp_association *asoc,
2878 const sctp_subtype_t type,
2879 void *arg,
2880 sctp_cmd_seq_t *commands)
2881{
2882 sctp_ecnehdr_t *ecne;
2883 struct sctp_chunk *chunk = arg;
2884
2885 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002886 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
2888 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002889 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 commands);
2891
2892 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2893 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2894
2895 /* If this is a newer ECNE than the last CWR packet we sent out */
2896 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2897 SCTP_U32(ntohl(ecne->lowest_tsn)));
2898
2899 return SCTP_DISPOSITION_CONSUME;
2900}
2901
2902/*
2903 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2904 *
2905 * The SCTP endpoint MUST always acknowledge the reception of each valid
2906 * DATA chunk.
2907 *
2908 * The guidelines on delayed acknowledgement algorithm specified in
2909 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2910 * acknowledgement SHOULD be generated for at least every second packet
2911 * (not every second DATA chunk) received, and SHOULD be generated within
2912 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2913 * situations it may be beneficial for an SCTP transmitter to be more
2914 * conservative than the algorithms detailed in this document allow.
2915 * However, an SCTP transmitter MUST NOT be more aggressive than the
2916 * following algorithms allow.
2917 *
2918 * A SCTP receiver MUST NOT generate more than one SACK for every
2919 * incoming packet, other than to update the offered window as the
2920 * receiving application consumes new data.
2921 *
2922 * Verification Tag: 8.5 Verification Tag [Normal verification]
2923 *
2924 * Inputs
2925 * (endpoint, asoc, chunk)
2926 *
2927 * Outputs
2928 * (asoc, reply_msg, msg_up, timers, counters)
2929 *
2930 * The return value is the disposition of the chunk.
2931 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002932sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2933 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 const struct sctp_association *asoc,
2935 const sctp_subtype_t type,
2936 void *arg,
2937 sctp_cmd_seq_t *commands)
2938{
2939 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002940 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 int error;
2942
2943 if (!sctp_vtag_verify(chunk, asoc)) {
2944 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2945 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002946 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002950 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 commands);
2952
2953 error = sctp_eat_data(asoc, chunk, commands );
2954 switch (error) {
2955 case SCTP_IERROR_NO_ERROR:
2956 break;
2957 case SCTP_IERROR_HIGH_TSN:
2958 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002959 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 goto discard_noforce;
2961 case SCTP_IERROR_DUP_TSN:
2962 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002963 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 goto discard_force;
2965 case SCTP_IERROR_NO_DATA:
2966 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002967 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002968 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002969 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 default:
2971 BUG();
2972 }
2973
Wei Yongjun6dc76942009-11-23 15:53:53 -05002974 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2975 force = SCTP_FORCE();
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (asoc->autoclose) {
2978 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2979 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2980 }
2981
2982 /* If this is the last chunk in a packet, we need to count it
2983 * toward sack generation. Note that we need to SACK every
2984 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2985 * THEM. We elect to NOT generate SACK's if the chunk fails
2986 * the verification tag test.
2987 *
2988 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2989 *
2990 * The SCTP endpoint MUST always acknowledge the reception of
2991 * each valid DATA chunk.
2992 *
2993 * The guidelines on delayed acknowledgement algorithm
2994 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2995 * Specifically, an acknowledgement SHOULD be generated for at
2996 * least every second packet (not every second DATA chunk)
2997 * received, and SHOULD be generated within 200 ms of the
2998 * arrival of any unacknowledged DATA chunk. In some
2999 * situations it may be beneficial for an SCTP transmitter to
3000 * be more conservative than the algorithms detailed in this
3001 * document allow. However, an SCTP transmitter MUST NOT be
3002 * more aggressive than the following algorithms allow.
3003 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003004 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003005 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return SCTP_DISPOSITION_CONSUME;
3008
3009discard_force:
3010 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3011 *
3012 * When a packet arrives with duplicate DATA chunk(s) and with
3013 * no new DATA chunk(s), the endpoint MUST immediately send a
3014 * SACK with no delay. If a packet arrives with duplicate
3015 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3016 * MAY immediately send a SACK. Normally receipt of duplicate
3017 * DATA chunks will occur when the original SACK chunk was lost
3018 * and the peer's RTO has expired. The duplicate TSN number(s)
3019 * SHOULD be reported in the SACK as duplicate.
3020 */
3021 /* In our case, we split the MAY SACK advice up whether or not
3022 * the last chunk is a duplicate.'
3023 */
3024 if (chunk->end_of_packet)
3025 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3026 return SCTP_DISPOSITION_DISCARD;
3027
3028discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003029 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003030 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return SCTP_DISPOSITION_DISCARD;
3033consume:
3034 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
3038/*
3039 * sctp_sf_eat_data_fast_4_4
3040 *
3041 * Section: 4 (4)
3042 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3043 * DATA chunks without delay.
3044 *
3045 * Verification Tag: 8.5 Verification Tag [Normal verification]
3046 * Inputs
3047 * (endpoint, asoc, chunk)
3048 *
3049 * Outputs
3050 * (asoc, reply_msg, msg_up, timers, counters)
3051 *
3052 * The return value is the disposition of the chunk.
3053 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003054sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3055 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 const struct sctp_association *asoc,
3057 const sctp_subtype_t type,
3058 void *arg,
3059 sctp_cmd_seq_t *commands)
3060{
3061 struct sctp_chunk *chunk = arg;
3062 int error;
3063
3064 if (!sctp_vtag_verify(chunk, asoc)) {
3065 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3066 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003067 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069
3070 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003071 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 commands);
3073
3074 error = sctp_eat_data(asoc, chunk, commands );
3075 switch (error) {
3076 case SCTP_IERROR_NO_ERROR:
3077 case SCTP_IERROR_HIGH_TSN:
3078 case SCTP_IERROR_DUP_TSN:
3079 case SCTP_IERROR_IGNORE_TSN:
3080 case SCTP_IERROR_BAD_STREAM:
3081 break;
3082 case SCTP_IERROR_NO_DATA:
3083 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003084 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003085 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003086 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 default:
3088 BUG();
3089 }
3090
3091 /* Go a head and force a SACK, since we are shutting down. */
3092
3093 /* Implementor's Guide.
3094 *
3095 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3096 * respond to each received packet containing one or more DATA chunk(s)
3097 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3098 */
3099 if (chunk->end_of_packet) {
3100 /* We must delay the chunk creation since the cumulative
3101 * TSN has not been updated yet.
3102 */
3103 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3104 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3105 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3106 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3107 }
3108
3109consume:
3110 return SCTP_DISPOSITION_CONSUME;
3111}
3112
3113/*
3114 * Section: 6.2 Processing a Received SACK
3115 * D) Any time a SACK arrives, the endpoint performs the following:
3116 *
3117 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3118 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3119 * increasing, a SACK whose Cumulative TSN Ack is less than the
3120 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3121 *
3122 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3123 * of bytes still outstanding after processing the Cumulative TSN Ack
3124 * and the Gap Ack Blocks.
3125 *
3126 * iii) If the SACK is missing a TSN that was previously
3127 * acknowledged via a Gap Ack Block (e.g., the data receiver
3128 * reneged on the data), then mark the corresponding DATA chunk
3129 * as available for retransmit: Mark it as missing for fast
3130 * retransmit as described in Section 7.2.4 and if no retransmit
3131 * timer is running for the destination address to which the DATA
3132 * chunk was originally transmitted, then T3-rtx is started for
3133 * that destination address.
3134 *
3135 * Verification Tag: 8.5 Verification Tag [Normal verification]
3136 *
3137 * Inputs
3138 * (endpoint, asoc, chunk)
3139 *
3140 * Outputs
3141 * (asoc, reply_msg, msg_up, timers, counters)
3142 *
3143 * The return value is the disposition of the chunk.
3144 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003145sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3146 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 const struct sctp_association *asoc,
3148 const sctp_subtype_t type,
3149 void *arg,
3150 sctp_cmd_seq_t *commands)
3151{
3152 struct sctp_chunk *chunk = arg;
3153 sctp_sackhdr_t *sackh;
3154 __u32 ctsn;
3155
3156 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003157 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159 /* Make sure that the SACK chunk has a valid length. */
3160 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003161 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 commands);
3163
3164 /* Pull the SACK chunk from the data buffer */
3165 sackh = sctp_sm_pull_sack(chunk);
3166 /* Was this a bogus SACK? */
3167 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003168 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 chunk->subh.sack_hdr = sackh;
3170 ctsn = ntohl(sackh->cum_tsn_ack);
3171
3172 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3173 * Ack Point, then drop the SACK. Since Cumulative TSN
3174 * Ack is monotonically increasing, a SACK whose
3175 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3176 * Point indicates an out-of-order SACK.
3177 */
3178 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3179 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3180 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3181 return SCTP_DISPOSITION_DISCARD;
3182 }
3183
Wei Yongjunaecedea2007-08-02 16:57:44 +08003184 /* If Cumulative TSN Ack beyond the max tsn currently
3185 * send, terminating the association and respond to the
3186 * sender with an ABORT.
3187 */
3188 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003189 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003192 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 /* Note: We do the rest of the work on the PROCESS_SACK
3195 * sideeffect.
3196 */
3197 return SCTP_DISPOSITION_CONSUME;
3198}
3199
3200/*
3201 * Generate an ABORT in response to a packet.
3202 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003203 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003205 * 8) The receiver should respond to the sender of the OOTB packet with
3206 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3207 * MUST fill in the Verification Tag field of the outbound packet
3208 * with the value found in the Verification Tag field of the OOTB
3209 * packet and set the T-bit in the Chunk Flags to indicate that the
3210 * Verification Tag is reflected. After sending this ABORT, the
3211 * receiver of the OOTB packet shall discard the OOTB packet and take
3212 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 *
3214 * Verification Tag:
3215 *
3216 * The return value is the disposition of the chunk.
3217*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003218static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3219 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 const struct sctp_association *asoc,
3221 const sctp_subtype_t type,
3222 void *arg,
3223 sctp_cmd_seq_t *commands)
3224{
3225 struct sctp_packet *packet = NULL;
3226 struct sctp_chunk *chunk = arg;
3227 struct sctp_chunk *abort;
3228
Eric W. Biederman2ce955032012-08-06 08:43:06 +00003229 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
3231 if (packet) {
3232 /* Make an ABORT. The T bit will be set if the asoc
3233 * is NULL.
3234 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003235 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (!abort) {
3237 sctp_ootb_pkt_free(packet);
3238 return SCTP_DISPOSITION_NOMEM;
3239 }
3240
Jerome Forissier047a2422005-04-28 11:58:43 -07003241 /* Reflect vtag if T-Bit is set */
3242 if (sctp_test_T_bit(abort))
3243 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 /* Set the skb to the belonging sock for accounting. */
3246 abort->skb->sk = ep->base.sk;
3247
3248 sctp_packet_append_chunk(packet, abort);
3249
3250 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3251 SCTP_PACKET(packet));
3252
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003253 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003255 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return SCTP_DISPOSITION_CONSUME;
3257 }
3258
3259 return SCTP_DISPOSITION_NOMEM;
3260}
3261
3262/*
3263 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3264 * event as ULP notification for each cause included in the chunk.
3265 *
3266 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3267 *
3268 * The return value is the disposition of the chunk.
3269*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003270sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3271 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 const struct sctp_association *asoc,
3273 const sctp_subtype_t type,
3274 void *arg,
3275 sctp_cmd_seq_t *commands)
3276{
3277 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003278 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003281 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283 /* Make sure that the ERROR chunk has a valid length. */
3284 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003285 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003287 sctp_walk_errors(err, chunk->chunk_hdr);
3288 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003289 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003290 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
Wei Yongjun3df26782009-03-02 06:46:51 +00003292 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3293 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
3298/*
3299 * Process an inbound SHUTDOWN ACK.
3300 *
3301 * From Section 9.2:
3302 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3303 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3304 * peer, and remove all record of the association.
3305 *
3306 * The return value is the disposition.
3307 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003308sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3309 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 const struct sctp_association *asoc,
3311 const sctp_subtype_t type,
3312 void *arg,
3313 sctp_cmd_seq_t *commands)
3314{
3315 struct sctp_chunk *chunk = arg;
3316 struct sctp_chunk *reply;
3317 struct sctp_ulpevent *ev;
3318
3319 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003320 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
3322 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3323 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003324 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 /* 10.2 H) SHUTDOWN COMPLETE notification
3327 *
3328 * When SCTP completes the shutdown procedures (section 9.2) this
3329 * notification is passed to the upper layer.
3330 */
3331 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003332 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 if (!ev)
3334 goto nomem;
3335
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003336 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3337 reply = sctp_make_shutdown_complete(asoc, chunk);
3338 if (!reply)
3339 goto nomem_chunk;
3340
3341 /* Do all the commands now (after allocation), so that we
3342 * have consistent state if memory allocation failes
3343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3345
3346 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3347 * stop the T2-shutdown timer,
3348 */
3349 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3350 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3351
3352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3353 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3356 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003357 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3358 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3360
3361 /* ...and remove all record of the association. */
3362 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3363 return SCTP_DISPOSITION_DELETE_TCB;
3364
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003365nomem_chunk:
3366 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367nomem:
3368 return SCTP_DISPOSITION_NOMEM;
3369}
3370
3371/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003372 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3373 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3375 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3376 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3377 * packet must fill in the Verification Tag field of the outbound
3378 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003379 * set the T-bit in the Chunk Flags to indicate that the Verification
3380 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 *
3382 * 8) The receiver should respond to the sender of the OOTB packet with
3383 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3384 * MUST fill in the Verification Tag field of the outbound packet
3385 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003386 * packet and set the T-bit in the Chunk Flags to indicate that the
3387 * Verification Tag is reflected. After sending this ABORT, the
3388 * receiver of the OOTB packet shall discard the OOTB packet and take
3389 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003391sctp_disposition_t sctp_sf_ootb(struct net *net,
3392 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 const struct sctp_association *asoc,
3394 const sctp_subtype_t type,
3395 void *arg,
3396 sctp_cmd_seq_t *commands)
3397{
3398 struct sctp_chunk *chunk = arg;
3399 struct sk_buff *skb = chunk->skb;
3400 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003401 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 __u8 *ch_end;
3403 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003404 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003406 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3409 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003410 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003412 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003413 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414
Vlad Yasevichece25df2007-09-07 16:30:54 -04003415 /* Now that we know we at least have a chunk header,
3416 * do things that are type appropriate.
3417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3419 ootb_shut_ack = 1;
3420
3421 /* RFC 2960, Section 3.3.7
3422 * Moreover, under any circumstances, an endpoint that
3423 * receives an ABORT MUST NOT respond to that ABORT by
3424 * sending an ABORT of its own.
3425 */
3426 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003427 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003428
Shan Wei85c5ed42011-04-19 21:30:01 +00003429 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3430 * or a COOKIE ACK the SCTP Packet should be silently
3431 * discarded.
3432 */
3433
3434 if (SCTP_CID_COOKIE_ACK == ch->type)
3435 ootb_cookie_ack = 1;
3436
3437 if (SCTP_CID_ERROR == ch->type) {
3438 sctp_walk_errors(err, ch) {
3439 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3440 ootb_cookie_ack = 1;
3441 break;
3442 }
3443 }
3444 }
3445
Vlad Yasevichece25df2007-09-07 16:30:54 -04003446 /* Report violation if chunk len overflows */
3447 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3448 if (ch_end > skb_tail_pointer(skb))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003449 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003450 commands);
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003453 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
3455 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003456 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003457 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003458 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003460 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
3463/*
3464 * Handle an "Out of the blue" SHUTDOWN ACK.
3465 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003466 * Section: 8.4 5, sctpimpguide 2.41.
3467 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003469 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3470 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3471 * packet must fill in the Verification Tag field of the outbound
3472 * packet with the Verification Tag received in the SHUTDOWN ACK and
3473 * set the T-bit in the Chunk Flags to indicate that the Verification
3474 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 *
3476 * Inputs
3477 * (endpoint, asoc, type, arg, commands)
3478 *
3479 * Outputs
3480 * (sctp_disposition_t)
3481 *
3482 * The return value is the disposition of the chunk.
3483 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003484static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3485 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 const struct sctp_association *asoc,
3487 const sctp_subtype_t type,
3488 void *arg,
3489 sctp_cmd_seq_t *commands)
3490{
3491 struct sctp_packet *packet = NULL;
3492 struct sctp_chunk *chunk = arg;
3493 struct sctp_chunk *shut;
3494
Eric W. Biederman2ce955032012-08-06 08:43:06 +00003495 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 if (packet) {
3498 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003499 * The T bit will be set if the asoc is NULL.
3500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 shut = sctp_make_shutdown_complete(asoc, chunk);
3502 if (!shut) {
3503 sctp_ootb_pkt_free(packet);
3504 return SCTP_DISPOSITION_NOMEM;
3505 }
3506
Jerome Forissier047a2422005-04-28 11:58:43 -07003507 /* Reflect vtag if T-Bit is set */
3508 if (sctp_test_T_bit(shut))
3509 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 /* Set the skb to the belonging sock for accounting. */
3512 shut->skb->sk = ep->base.sk;
3513
3514 sctp_packet_append_chunk(packet, shut);
3515
3516 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3517 SCTP_PACKET(packet));
3518
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003519 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 /* If the chunk length is invalid, we don't want to process
3522 * the reset of the packet.
3523 */
3524 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003525 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003527 /* We need to discard the rest of the packet to prevent
3528 * potential bomming attacks from additional bundled chunks.
3529 * This is documented in SCTP Threats ID.
3530 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003531 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
3533
3534 return SCTP_DISPOSITION_NOMEM;
3535}
3536
3537/*
3538 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3539 *
3540 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3541 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3542 * procedures in section 8.4 SHOULD be followed, in other words it
3543 * should be treated as an Out Of The Blue packet.
3544 * [This means that we do NOT check the Verification Tag on these
3545 * chunks. --piggy ]
3546 *
3547 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003548sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3549 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 const struct sctp_association *asoc,
3551 const sctp_subtype_t type,
3552 void *arg,
3553 sctp_cmd_seq_t *commands)
3554{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003555 struct sctp_chunk *chunk = arg;
3556
3557 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3558 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003559 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003560 commands);
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 /* Although we do have an association in this case, it corresponds
3563 * to a restarted association. So the packet is treated as an OOTB
3564 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3565 * called with a NULL association.
3566 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003567 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003568
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003569 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570}
3571
3572/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003573sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3574 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 const struct sctp_association *asoc,
3576 const sctp_subtype_t type, void *arg,
3577 sctp_cmd_seq_t *commands)
3578{
3579 struct sctp_chunk *chunk = arg;
3580 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003581 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003583 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003585 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 if (!sctp_vtag_verify(chunk, asoc)) {
3588 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3589 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003590 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 }
3592
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003593 /* ADD-IP: Section 4.1.1
3594 * This chunk MUST be sent in an authenticated way by using
3595 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3596 * is received unauthenticated it MUST be silently discarded as
3597 * described in [I-D.ietf-tsvwg-sctp-auth].
3598 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003599 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003600 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3603 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003604 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 commands);
3606
3607 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3608 serial = ntohl(hdr->serial);
3609
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003610 addr_param = (union sctp_addr_param *)hdr->params;
3611 length = ntohs(addr_param->p.length);
3612 if (length < sizeof(sctp_paramhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003613 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003614 (void *)addr_param, commands);
3615
3616 /* Verify the ASCONF chunk before processing it. */
3617 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003618 (sctp_paramhdr_t *)((void *)addr_param + length),
3619 (void *)chunk->chunk_end,
3620 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003621 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003622 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003623
Vlad Yasevicha08de642007-12-20 14:11:47 -08003624 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003626 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 */
3628 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003629 /* If this is the first instance of ASCONF in the packet,
3630 * we can clean our old ASCONF-ACKs.
3631 */
3632 if (!chunk->has_asconf)
3633 sctp_assoc_clean_asconf_ack_cache(asoc);
3634
3635 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3636 * expected, process the ASCONF as described below and after
3637 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3638 * the response packet and cache a copy of it (in the event it
3639 * later needs to be retransmitted).
3640 *
3641 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 */
3643 asconf_ack = sctp_process_asconf((struct sctp_association *)
3644 asoc, chunk);
3645 if (!asconf_ack)
3646 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003647 } else if (serial < asoc->peer.addip_serial + 1) {
3648 /* ADDIP 5.2 E2)
3649 * If the value found in the Sequence Number is less than the
3650 * ('Peer- Sequence-Number' + 1), simply skip to the next
3651 * ASCONF, and include in the outbound response packet
3652 * any previously cached ASCONF-ACK response that was
3653 * sent and saved that matches the Sequence Number of the
3654 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3655 * Chunk exists. This will occur when an older ASCONF
3656 * arrives out of order. In such a case, the receiver
3657 * should skip the ASCONF Chunk and not include ASCONF-ACK
3658 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003660 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3661 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003663
3664 /* Reset the transport so that we select the correct one
3665 * this time around. This is to make sure that we don't
3666 * accidentally use a stale transport that's been removed.
3667 */
3668 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003670 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003672 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return SCTP_DISPOSITION_DISCARD;
3674 }
3675
Vlad Yasevicha08de642007-12-20 14:11:47 -08003676 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3677 * containing the ASCONF-ACK Chunks MUST be the source address of
3678 * the SCTP packet that held the ASCONF Chunks.
3679 *
3680 * To do this properly, we'll set the destination address of the chunk
3681 * and at the transmit time, will try look up the transport to use.
3682 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003683 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003685 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003687 if (asoc->new_transport) {
3688 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3689 commands);
3690 ((struct sctp_association *)asoc)->new_transport = NULL;
3691 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 return SCTP_DISPOSITION_CONSUME;
3694}
3695
3696/*
3697 * ADDIP Section 4.3 General rules for address manipulation
3698 * When building TLV parameters for the ASCONF Chunk that will add or
3699 * delete IP addresses the D0 to D13 rules should be applied:
3700 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003701sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3702 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003704 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 sctp_cmd_seq_t *commands)
3706{
3707 struct sctp_chunk *asconf_ack = arg;
3708 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3709 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003710 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 sctp_addiphdr_t *addip_hdr;
3712 __u32 sent_serial, rcvd_serial;
3713
3714 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3715 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3716 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003717 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
3719
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003720 /* ADD-IP, Section 4.1.2:
3721 * This chunk MUST be sent in an authenticated way by using
3722 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3723 * is received unauthenticated it MUST be silently discarded as
3724 * described in [I-D.ietf-tsvwg-sctp-auth].
3725 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003726 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003727 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003728
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 /* Make sure that the ADDIP chunk has a valid length. */
3730 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003731 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 commands);
3733
3734 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3735 rcvd_serial = ntohl(addip_hdr->serial);
3736
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003737 /* Verify the ASCONF-ACK chunk before processing it. */
3738 if (!sctp_verify_asconf(asoc,
3739 (sctp_paramhdr_t *)addip_hdr->params,
3740 (void *)asconf_ack->chunk_end,
3741 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003742 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003743 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003744
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 if (last_asconf) {
3746 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3747 sent_serial = ntohl(addip_hdr->serial);
3748 } else {
3749 sent_serial = asoc->addip_serial - 1;
3750 }
3751
3752 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3753 * equal to the next serial number to be used but no ASCONF chunk is
3754 * outstanding the endpoint MUST ABORT the association. Note that a
3755 * sequence number is greater than if it is no more than 2^^31-1
3756 * larger than the current sequence number (using serial arithmetic).
3757 */
3758 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3759 !(asoc->addip_last_asconf)) {
3760 abort = sctp_make_abort(asoc, asconf_ack,
3761 sizeof(sctp_errhdr_t));
3762 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003763 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3765 SCTP_CHUNK(abort));
3766 }
3767 /* We are going to ABORT, so we might as well stop
3768 * processing the rest of the chunks in the packet.
3769 */
3770 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3771 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3772 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003773 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003774 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003776 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003777 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3778 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 return SCTP_DISPOSITION_ABORT;
3780 }
3781
3782 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3783 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3784 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3785
3786 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003787 asconf_ack)) {
3788 /* Successfully processed ASCONF_ACK. We can
3789 * release the next asconf if we have one.
3790 */
3791 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3792 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 abort = sctp_make_abort(asoc, asconf_ack,
3797 sizeof(sctp_errhdr_t));
3798 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003799 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3801 SCTP_CHUNK(abort));
3802 }
3803 /* We are going to ABORT, so we might as well stop
3804 * processing the rest of the chunks in the packet.
3805 */
3806 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003807 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003808 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003810 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003811 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3812 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 return SCTP_DISPOSITION_ABORT;
3814 }
3815
3816 return SCTP_DISPOSITION_DISCARD;
3817}
3818
3819/*
3820 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3821 *
3822 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3823 * its cumulative TSN point to the value carried in the FORWARD TSN
3824 * chunk, and then MUST further advance its cumulative TSN point locally
3825 * if possible.
3826 * After the above processing, the data receiver MUST stop reporting any
3827 * missing TSNs earlier than or equal to the new cumulative TSN point.
3828 *
3829 * Verification Tag: 8.5 Verification Tag [Normal verification]
3830 *
3831 * The return value is the disposition of the chunk.
3832 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003833sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3834 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 const struct sctp_association *asoc,
3836 const sctp_subtype_t type,
3837 void *arg,
3838 sctp_cmd_seq_t *commands)
3839{
3840 struct sctp_chunk *chunk = arg;
3841 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003842 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 __u16 len;
3844 __u32 tsn;
3845
3846 if (!sctp_vtag_verify(chunk, asoc)) {
3847 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3848 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003849 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851
3852 /* Make sure that the FORWARD_TSN chunk has valid length. */
3853 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003854 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 commands);
3856
3857 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3858 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3859 len = ntohs(chunk->chunk_hdr->length);
3860 len -= sizeof(struct sctp_chunkhdr);
3861 skb_pull(chunk->skb, len);
3862
3863 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003864 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
3866 /* The TSN is too high--silently discard the chunk and count on it
3867 * getting retransmitted later.
3868 */
3869 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3870 goto discard_noforce;
3871
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003872 /* Silently discard the chunk if stream-id is not valid */
3873 sctp_walk_fwdtsn(skip, chunk) {
3874 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3875 goto discard_noforce;
3876 }
3877
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3879 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003880 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 /* Count this as receiving DATA. */
3884 if (asoc->autoclose) {
3885 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3886 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3887 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003888
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003890 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 */
3892 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
3894 return SCTP_DISPOSITION_CONSUME;
3895
3896discard_noforce:
3897 return SCTP_DISPOSITION_DISCARD;
3898}
3899
3900sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003901 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 const struct sctp_endpoint *ep,
3903 const struct sctp_association *asoc,
3904 const sctp_subtype_t type,
3905 void *arg,
3906 sctp_cmd_seq_t *commands)
3907{
3908 struct sctp_chunk *chunk = arg;
3909 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003910 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 __u16 len;
3912 __u32 tsn;
3913
3914 if (!sctp_vtag_verify(chunk, asoc)) {
3915 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3916 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003917 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
3920 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3921 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003922 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 commands);
3924
3925 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3926 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3927 len = ntohs(chunk->chunk_hdr->length);
3928 len -= sizeof(struct sctp_chunkhdr);
3929 skb_pull(chunk->skb, len);
3930
3931 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003932 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
3934 /* The TSN is too high--silently discard the chunk and count on it
3935 * getting retransmitted later.
3936 */
3937 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3938 goto gen_shutdown;
3939
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003940 /* Silently discard the chunk if stream-id is not valid */
3941 sctp_walk_fwdtsn(skip, chunk) {
3942 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3943 goto gen_shutdown;
3944 }
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3947 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003948 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /* Go a head and force a SACK, since we are shutting down. */
3952gen_shutdown:
3953 /* Implementor's Guide.
3954 *
3955 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3956 * respond to each received packet containing one or more DATA chunk(s)
3957 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3958 */
3959 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3960 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3961 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3962 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3963
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003964 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965}
3966
3967/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003968 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003969 *
3970 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3971 * Identifier field. If this algorithm was not specified by the
3972 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3973 * during association setup, the AUTH chunk and all chunks after it MUST
3974 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3975 * defined in Section 4.1.
3976 *
3977 * If an endpoint with no shared key receives a Shared Key Identifier
3978 * other than 0, it MUST silently discard all authenticated chunks. If
3979 * the endpoint has at least one endpoint pair shared key for the peer,
3980 * it MUST use the key specified by the Shared Key Identifier if a
3981 * key has been configured for that Shared Key Identifier. If no
3982 * endpoint pair shared key has been configured for that Shared Key
3983 * Identifier, all authenticated chunks MUST be silently discarded.
3984 *
3985 * Verification Tag: 8.5 Verification Tag [Normal verification]
3986 *
3987 * The return value is the disposition of the chunk.
3988 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003989static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3990 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003991 const struct sctp_association *asoc,
3992 const sctp_subtype_t type,
3993 struct sctp_chunk *chunk)
3994{
3995 struct sctp_authhdr *auth_hdr;
3996 struct sctp_hmac *hmac;
3997 unsigned int sig_len;
3998 __u16 key_id;
3999 __u8 *save_digest;
4000 __u8 *digest;
4001
4002 /* Pull in the auth header, so we can do some more verification */
4003 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4004 chunk->subh.auth_hdr = auth_hdr;
4005 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4006
Masanari Iida02582e92012-08-22 19:11:26 +09004007 /* Make sure that we support the HMAC algorithm from the auth
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004008 * chunk.
4009 */
4010 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4011 return SCTP_IERROR_AUTH_BAD_HMAC;
4012
4013 /* Make sure that the provided shared key identifier has been
4014 * configured
4015 */
4016 key_id = ntohs(auth_hdr->shkey_id);
4017 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4018 return SCTP_IERROR_AUTH_BAD_KEYID;
4019
4020
4021 /* Make sure that the length of the signature matches what
4022 * we expect.
4023 */
4024 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4025 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4026 if (sig_len != hmac->hmac_len)
4027 return SCTP_IERROR_PROTO_VIOLATION;
4028
4029 /* Now that we've done validation checks, we can compute and
4030 * verify the hmac. The steps involved are:
4031 * 1. Save the digest from the chunk.
4032 * 2. Zero out the digest in the chunk.
4033 * 3. Compute the new digest
4034 * 4. Compare saved and new digests.
4035 */
4036 digest = auth_hdr->hmac;
4037 skb_pull(chunk->skb, sig_len);
4038
4039 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4040 if (!save_digest)
4041 goto nomem;
4042
4043 memset(digest, 0, sig_len);
4044
4045 sctp_auth_calculate_hmac(asoc, chunk->skb,
4046 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4047 GFP_ATOMIC);
4048
4049 /* Discard the packet if the digests do not match */
4050 if (memcmp(save_digest, digest, sig_len)) {
4051 kfree(save_digest);
4052 return SCTP_IERROR_BAD_SIG;
4053 }
4054
4055 kfree(save_digest);
4056 chunk->auth = 1;
4057
4058 return SCTP_IERROR_NO_ERROR;
4059nomem:
4060 return SCTP_IERROR_NOMEM;
4061}
4062
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004063sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4064 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004065 const struct sctp_association *asoc,
4066 const sctp_subtype_t type,
4067 void *arg,
4068 sctp_cmd_seq_t *commands)
4069{
4070 struct sctp_authhdr *auth_hdr;
4071 struct sctp_chunk *chunk = arg;
4072 struct sctp_chunk *err_chunk;
4073 sctp_ierror_t error;
4074
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004075 /* Make sure that the peer has AUTH capable */
4076 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004077 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004078
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004079 if (!sctp_vtag_verify(chunk, asoc)) {
4080 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4081 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004082 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004083 }
4084
4085 /* Make sure that the AUTH chunk has valid length. */
4086 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004087 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004088 commands);
4089
4090 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004091 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004092 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004093 case SCTP_IERROR_AUTH_BAD_HMAC:
4094 /* Generate the ERROR chunk and discard the rest
4095 * of the packet
4096 */
4097 err_chunk = sctp_make_op_error(asoc, chunk,
4098 SCTP_ERROR_UNSUP_HMAC,
4099 &auth_hdr->hmac_id,
4100 sizeof(__u16), 0);
4101 if (err_chunk) {
4102 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4103 SCTP_CHUNK(err_chunk));
4104 }
4105 /* Fall Through */
4106 case SCTP_IERROR_AUTH_BAD_KEYID:
4107 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004108 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004109
4110 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004111 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004112 commands);
4113
4114 case SCTP_IERROR_NOMEM:
4115 return SCTP_DISPOSITION_NOMEM;
4116
4117 default: /* Prevent gcc warnings */
4118 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004119 }
4120
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004121 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4122 struct sctp_ulpevent *ev;
4123
4124 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4125 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4126
4127 if (!ev)
4128 return -ENOMEM;
4129
4130 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4131 SCTP_ULPEVENT(ev));
4132 }
4133
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004134 return SCTP_DISPOSITION_CONSUME;
4135}
4136
4137/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 * Process an unknown chunk.
4139 *
4140 * Section: 3.2. Also, 2.1 in the implementor's guide.
4141 *
4142 * Chunk Types are encoded such that the highest-order two bits specify
4143 * the action that must be taken if the processing endpoint does not
4144 * recognize the Chunk Type.
4145 *
4146 * 00 - Stop processing this SCTP packet and discard it, do not process
4147 * any further chunks within it.
4148 *
4149 * 01 - Stop processing this SCTP packet and discard it, do not process
4150 * any further chunks within it, and report the unrecognized
4151 * chunk in an 'Unrecognized Chunk Type'.
4152 *
4153 * 10 - Skip this chunk and continue processing.
4154 *
4155 * 11 - Skip this chunk and continue processing, but report in an ERROR
4156 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4157 *
4158 * The return value is the disposition of the chunk.
4159 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004160sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4161 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 const struct sctp_association *asoc,
4163 const sctp_subtype_t type,
4164 void *arg,
4165 sctp_cmd_seq_t *commands)
4166{
4167 struct sctp_chunk *unk_chunk = arg;
4168 struct sctp_chunk *err_chunk;
4169 sctp_chunkhdr_t *hdr;
4170
4171 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4172
4173 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004174 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
4176 /* Make sure that the chunk has a valid length.
4177 * Since we don't know the chunk type, we use a general
4178 * chunkhdr structure to make a comparison.
4179 */
4180 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004181 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 commands);
4183
4184 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4185 case SCTP_CID_ACTION_DISCARD:
4186 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004187 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 break;
4189 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 /* Generate an ERROR chunk as response. */
4191 hdr = unk_chunk->chunk_hdr;
4192 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4193 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004194 WORD_ROUND(ntohs(hdr->length)),
4195 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 if (err_chunk) {
4197 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4198 SCTP_CHUNK(err_chunk));
4199 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004200
4201 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004202 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 return SCTP_DISPOSITION_CONSUME;
4204 break;
4205 case SCTP_CID_ACTION_SKIP:
4206 /* Skip the chunk. */
4207 return SCTP_DISPOSITION_DISCARD;
4208 break;
4209 case SCTP_CID_ACTION_SKIP_ERR:
4210 /* Generate an ERROR chunk as response. */
4211 hdr = unk_chunk->chunk_hdr;
4212 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4213 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004214 WORD_ROUND(ntohs(hdr->length)),
4215 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 if (err_chunk) {
4217 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4218 SCTP_CHUNK(err_chunk));
4219 }
4220 /* Skip the chunk. */
4221 return SCTP_DISPOSITION_CONSUME;
4222 break;
4223 default:
4224 break;
4225 }
4226
4227 return SCTP_DISPOSITION_DISCARD;
4228}
4229
4230/*
4231 * Discard the chunk.
4232 *
4233 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4234 * [Too numerous to mention...]
4235 * Verification Tag: No verification needed.
4236 * Inputs
4237 * (endpoint, asoc, chunk)
4238 *
4239 * Outputs
4240 * (asoc, reply_msg, msg_up, timers, counters)
4241 *
4242 * The return value is the disposition of the chunk.
4243 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004244sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4245 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 const struct sctp_association *asoc,
4247 const sctp_subtype_t type,
4248 void *arg,
4249 sctp_cmd_seq_t *commands)
4250{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004251 struct sctp_chunk *chunk = arg;
4252
4253 /* Make sure that the chunk has a valid length.
4254 * Since we don't know the chunk type, we use a general
4255 * chunkhdr structure to make a comparison.
4256 */
4257 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004258 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004259 commands);
4260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4262 return SCTP_DISPOSITION_DISCARD;
4263}
4264
4265/*
4266 * Discard the whole packet.
4267 *
4268 * Section: 8.4 2)
4269 *
4270 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4271 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 *
4273 * Verification Tag: No verification necessary
4274 *
4275 * Inputs
4276 * (endpoint, asoc, chunk)
4277 *
4278 * Outputs
4279 * (asoc, reply_msg, msg_up, timers, counters)
4280 *
4281 * The return value is the disposition of the chunk.
4282 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004283sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4284 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 const struct sctp_association *asoc,
4286 const sctp_subtype_t type,
4287 void *arg,
4288 sctp_cmd_seq_t *commands)
4289{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004290 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4292
4293 return SCTP_DISPOSITION_CONSUME;
4294}
4295
4296
4297/*
4298 * The other end is violating protocol.
4299 *
4300 * Section: Not specified
4301 * Verification Tag: Not specified
4302 * Inputs
4303 * (endpoint, asoc, chunk)
4304 *
4305 * Outputs
4306 * (asoc, reply_msg, msg_up, timers, counters)
4307 *
4308 * We simply tag the chunk as a violation. The state machine will log
4309 * the violation and continue.
4310 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004311sctp_disposition_t sctp_sf_violation(struct net *net,
4312 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 const struct sctp_association *asoc,
4314 const sctp_subtype_t type,
4315 void *arg,
4316 sctp_cmd_seq_t *commands)
4317{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004318 struct sctp_chunk *chunk = arg;
4319
4320 /* Make sure that the chunk has a valid length. */
4321 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004322 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004323 commands);
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return SCTP_DISPOSITION_VIOLATION;
4326}
4327
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004329 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004331static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004332 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004333 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004336 sctp_cmd_seq_t *commands,
4337 const __u8 *payload,
4338 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004340 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 struct sctp_chunk *chunk = arg;
4342 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004344 /* SCTP-AUTH, Section 6.3:
4345 * It should be noted that if the receiver wants to tear
4346 * down an association in an authenticated way only, the
4347 * handling of malformed packets should not result in
4348 * tearing down the association.
4349 *
4350 * This means that if we only want to abort associations
4351 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004352 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004353 * was malformed.
4354 */
4355 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4356 goto discard;
4357
Jesper Juhl9abed242007-11-11 23:57:49 +01004358 /* Make the abort chunk. */
4359 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4360 if (!abort)
4361 goto nomem;
4362
Vlad Yasevichece25df2007-09-07 16:30:54 -04004363 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004364 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4365 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4366 !asoc->peer.i.init_tag) {
4367 sctp_initack_chunk_t *initack;
4368
4369 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4370 if (!sctp_chunk_length_valid(chunk,
4371 sizeof(sctp_initack_chunk_t)))
4372 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4373 else {
4374 unsigned int inittag;
4375
4376 inittag = ntohl(initack->init_hdr.init_tag);
4377 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4378 SCTP_U32(inittag));
4379 }
4380 }
4381
Vlad Yasevichece25df2007-09-07 16:30:54 -04004382 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004383 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Vlad Yasevichece25df2007-09-07 16:30:54 -04004385 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4386 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4387 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4388 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4389 SCTP_ERROR(ECONNREFUSED));
4390 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4391 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4392 } else {
4393 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4394 SCTP_ERROR(ECONNABORTED));
4395 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4396 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004397 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 } else {
Eric W. Biederman2ce955032012-08-06 08:43:06 +00004400 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004401
4402 if (!packet)
4403 goto nomem_pkt;
4404
4405 if (sctp_test_T_bit(abort))
4406 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4407
4408 abort->skb->sk = ep->base.sk;
4409
4410 sctp_packet_append_chunk(packet, abort);
4411
4412 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4413 SCTP_PACKET(packet));
4414
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004415 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 }
4417
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004418 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004419
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004420discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004421 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 return SCTP_DISPOSITION_ABORT;
4423
Vlad Yasevichece25df2007-09-07 16:30:54 -04004424nomem_pkt:
4425 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426nomem:
4427 return SCTP_DISPOSITION_NOMEM;
4428}
4429
Wei Yongjunaecedea2007-08-02 16:57:44 +08004430/*
4431 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004432 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004433 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004434 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004435 *
4436 * We inform the other end by sending an ABORT with a Protocol Violation
4437 * error code.
4438 *
4439 * Section: Not specified
4440 * Verification Tag: Nothing to do
4441 * Inputs
4442 * (endpoint, asoc, chunk)
4443 *
4444 * Outputs
4445 * (reply_msg, msg_up, counters)
4446 *
4447 * Generate an ABORT chunk and terminate the association.
4448 */
4449static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004450 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004451 const struct sctp_endpoint *ep,
4452 const struct sctp_association *asoc,
4453 const sctp_subtype_t type,
4454 void *arg,
4455 sctp_cmd_seq_t *commands)
4456{
Florian Westphal24448442008-03-23 22:46:34 -07004457 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004458
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004459 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004460 sizeof(err_str));
4461}
4462
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004463/*
4464 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004465 * If the length is smaller than the minimum length of a given parameter,
4466 * or accumulated length in multi parameters exceeds the end of the chunk,
4467 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004468 */
4469static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004470 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004471 const struct sctp_endpoint *ep,
4472 const struct sctp_association *asoc,
4473 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004474 void *arg, void *ext,
4475 sctp_cmd_seq_t *commands)
4476{
4477 struct sctp_chunk *chunk = arg;
4478 struct sctp_paramhdr *param = ext;
4479 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004480
Wei Yongjunba016672008-09-30 05:32:24 -07004481 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4482 goto discard;
4483
4484 /* Make the abort chunk. */
4485 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4486 if (!abort)
4487 goto nomem;
4488
4489 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004490 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004491
4492 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4493 SCTP_ERROR(ECONNABORTED));
4494 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4495 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004496 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4497 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004498
4499discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004500 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004501 return SCTP_DISPOSITION_ABORT;
4502nomem:
4503 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004504}
4505
Wei Yongjunaecedea2007-08-02 16:57:44 +08004506/* Handle a protocol violation when the peer trying to advance the
4507 * cumulative tsn ack to a point beyond the max tsn currently sent.
4508 *
4509 * We inform the other end by sending an ABORT with a Protocol Violation
4510 * error code.
4511 */
4512static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004513 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004514 const struct sctp_endpoint *ep,
4515 const struct sctp_association *asoc,
4516 const sctp_subtype_t type,
4517 void *arg,
4518 sctp_cmd_seq_t *commands)
4519{
Florian Westphal24448442008-03-23 22:46:34 -07004520 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004521
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004522 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004523 sizeof(err_str));
4524}
4525
Vlad Yasevichece25df2007-09-07 16:30:54 -04004526/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004527 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004528 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004529 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004530 * on the path and we may not want to continue this communication.
4531 */
4532static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004533 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004534 const struct sctp_endpoint *ep,
4535 const struct sctp_association *asoc,
4536 const sctp_subtype_t type,
4537 void *arg,
4538 sctp_cmd_seq_t *commands)
4539{
Florian Westphal24448442008-03-23 22:46:34 -07004540 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004541
4542 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004543 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004544
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004545 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004546 sizeof(err_str));
4547}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548/***************************************************************************
4549 * These are the state functions for handling primitive (Section 10) events.
4550 ***************************************************************************/
4551/*
4552 * sctp_sf_do_prm_asoc
4553 *
4554 * Section: 10.1 ULP-to-SCTP
4555 * B) Associate
4556 *
4557 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4558 * outbound stream count)
4559 * -> association id [,destination transport addr list] [,outbound stream
4560 * count]
4561 *
4562 * This primitive allows the upper layer to initiate an association to a
4563 * specific peer endpoint.
4564 *
4565 * The peer endpoint shall be specified by one of the transport addresses
4566 * which defines the endpoint (see Section 1.4). If the local SCTP
4567 * instance has not been initialized, the ASSOCIATE is considered an
4568 * error.
4569 * [This is not relevant for the kernel implementation since we do all
4570 * initialization at boot time. It we hadn't initialized we wouldn't
4571 * get anywhere near this code.]
4572 *
4573 * An association id, which is a local handle to the SCTP association,
4574 * will be returned on successful establishment of the association. If
4575 * SCTP is not able to open an SCTP association with the peer endpoint,
4576 * an error is returned.
4577 * [In the kernel implementation, the struct sctp_association needs to
4578 * be created BEFORE causing this primitive to run.]
4579 *
4580 * Other association parameters may be returned, including the
4581 * complete destination transport addresses of the peer as well as the
4582 * outbound stream count of the local endpoint. One of the transport
4583 * address from the returned destination addresses will be selected by
4584 * the local endpoint as default primary path for sending SCTP packets
4585 * to this peer. The returned "destination transport addr list" can
4586 * be used by the ULP to change the default primary path or to force
4587 * sending a packet to a specific transport address. [All of this
4588 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4589 * function.]
4590 *
4591 * Mandatory attributes:
4592 *
4593 * o local SCTP instance name - obtained from the INITIALIZE operation.
4594 * [This is the argument asoc.]
4595 * o destination transport addr - specified as one of the transport
4596 * addresses of the peer endpoint with which the association is to be
4597 * established.
4598 * [This is asoc->peer.active_path.]
4599 * o outbound stream count - the number of outbound streams the ULP
4600 * would like to open towards this peer endpoint.
4601 * [BUG: This is not currently implemented.]
4602 * Optional attributes:
4603 *
4604 * None.
4605 *
4606 * The return value is a disposition.
4607 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004608sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4609 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 const struct sctp_association *asoc,
4611 const sctp_subtype_t type,
4612 void *arg,
4613 sctp_cmd_seq_t *commands)
4614{
4615 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004616 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 /* The comment below says that we enter COOKIE-WAIT AFTER
4619 * sending the INIT, but that doesn't actually work in our
4620 * implementation...
4621 */
4622 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4623 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4624
4625 /* RFC 2960 5.1 Normal Establishment of an Association
4626 *
4627 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4628 * must provide its Verification Tag (Tag_A) in the Initiate
4629 * Tag field. Tag_A SHOULD be a random number in the range of
4630 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4631 */
4632
4633 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4634 if (!repl)
4635 goto nomem;
4636
4637 /* 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
Frank Filz3f7a87d2005-06-20 13:14:57 -07004643 /* Choose transport for INIT. */
4644 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4645 SCTP_CHUNK(repl));
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 /* After sending the INIT, "A" starts the T1-init timer and
4648 * enters the COOKIE-WAIT state.
4649 */
4650 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4651 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4652 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4653 return SCTP_DISPOSITION_CONSUME;
4654
4655nomem:
4656 return SCTP_DISPOSITION_NOMEM;
4657}
4658
4659/*
4660 * Process the SEND primitive.
4661 *
4662 * Section: 10.1 ULP-to-SCTP
4663 * E) Send
4664 *
4665 * Format: SEND(association id, buffer address, byte count [,context]
4666 * [,stream id] [,life time] [,destination transport address]
4667 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4668 * -> result
4669 *
4670 * This is the main method to send user data via SCTP.
4671 *
4672 * Mandatory attributes:
4673 *
4674 * o association id - local handle to the SCTP association
4675 *
4676 * o buffer address - the location where the user message to be
4677 * transmitted is stored;
4678 *
4679 * o byte count - The size of the user data in number of bytes;
4680 *
4681 * Optional attributes:
4682 *
4683 * o context - an optional 32 bit integer that will be carried in the
4684 * sending failure notification to the ULP if the transportation of
4685 * this User Message fails.
4686 *
4687 * o stream id - to indicate which stream to send the data on. If not
4688 * specified, stream 0 will be used.
4689 *
4690 * o life time - specifies the life time of the user data. The user data
4691 * will not be sent by SCTP after the life time expires. This
4692 * parameter can be used to avoid efforts to transmit stale
4693 * user messages. SCTP notifies the ULP if the data cannot be
4694 * initiated to transport (i.e. sent to the destination via SCTP's
4695 * send primitive) within the life time variable. However, the
4696 * user data will be transmitted if SCTP has attempted to transmit a
4697 * chunk before the life time expired.
4698 *
4699 * o destination transport address - specified as one of the destination
4700 * transport addresses of the peer endpoint to which this packet
4701 * should be sent. Whenever possible, SCTP should use this destination
4702 * transport address for sending the packets, instead of the current
4703 * primary path.
4704 *
4705 * o unorder flag - this flag, if present, indicates that the user
4706 * would like the data delivered in an unordered fashion to the peer
4707 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4708 * message).
4709 *
4710 * o no-bundle flag - instructs SCTP not to bundle this user data with
4711 * other outbound DATA chunks. SCTP MAY still bundle even when
4712 * this flag is present, when faced with network congestion.
4713 *
4714 * o payload protocol-id - A 32 bit unsigned integer that is to be
4715 * passed to the peer indicating the type of payload protocol data
4716 * being transmitted. This value is passed as opaque data by SCTP.
4717 *
4718 * The return value is the disposition.
4719 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004720sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4721 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 const struct sctp_association *asoc,
4723 const sctp_subtype_t type,
4724 void *arg,
4725 sctp_cmd_seq_t *commands)
4726{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004727 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004729 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 return SCTP_DISPOSITION_CONSUME;
4731}
4732
4733/*
4734 * Process the SHUTDOWN primitive.
4735 *
4736 * Section: 10.1:
4737 * C) Shutdown
4738 *
4739 * Format: SHUTDOWN(association id)
4740 * -> result
4741 *
4742 * Gracefully closes an association. Any locally queued user data
4743 * will be delivered to the peer. The association will be terminated only
4744 * after the peer acknowledges all the SCTP packets sent. A success code
4745 * will be returned on successful termination of the association. If
4746 * attempting to terminate the association results in a failure, an error
4747 * code shall be returned.
4748 *
4749 * Mandatory attributes:
4750 *
4751 * o association id - local handle to the SCTP association
4752 *
4753 * Optional attributes:
4754 *
4755 * None.
4756 *
4757 * The return value is the disposition.
4758 */
4759sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004760 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 const struct sctp_endpoint *ep,
4762 const struct sctp_association *asoc,
4763 const sctp_subtype_t type,
4764 void *arg,
4765 sctp_cmd_seq_t *commands)
4766{
4767 int disposition;
4768
4769 /* From 9.2 Shutdown of an Association
4770 * Upon receipt of the SHUTDOWN primitive from its upper
4771 * layer, the endpoint enters SHUTDOWN-PENDING state and
4772 * remains there until all outstanding data has been
4773 * acknowledged by its peer. The endpoint accepts no new data
4774 * from its upper layer, but retransmits data to the far end
4775 * if necessary to fill gaps.
4776 */
4777 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4778 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4779
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 disposition = SCTP_DISPOSITION_CONSUME;
4781 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004782 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 arg, commands);
4784 }
4785 return disposition;
4786}
4787
4788/*
4789 * Process the ABORT primitive.
4790 *
4791 * Section: 10.1:
4792 * C) Abort
4793 *
4794 * Format: Abort(association id [, cause code])
4795 * -> result
4796 *
4797 * Ungracefully closes an association. Any locally queued user data
4798 * will be discarded and an ABORT chunk is sent to the peer. A success code
4799 * will be returned on successful abortion of the association. If
4800 * attempting to abort the association results in a failure, an error
4801 * code shall be returned.
4802 *
4803 * Mandatory attributes:
4804 *
4805 * o association id - local handle to the SCTP association
4806 *
4807 * Optional attributes:
4808 *
4809 * o cause code - reason of the abort to be passed to the peer
4810 *
4811 * None.
4812 *
4813 * The return value is the disposition.
4814 */
4815sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004816 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 const struct sctp_endpoint *ep,
4818 const struct sctp_association *asoc,
4819 const sctp_subtype_t type,
4820 void *arg,
4821 sctp_cmd_seq_t *commands)
4822{
4823 /* From 9.1 Abort of an Association
4824 * Upon receipt of the ABORT primitive from its upper
4825 * layer, the endpoint enters CLOSED state and
4826 * discard all outstanding data has been
4827 * acknowledged by its peer. The endpoint accepts no new data
4828 * from its upper layer, but retransmits data to the far end
4829 * if necessary to fill gaps.
4830 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004831 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 sctp_disposition_t retval;
4833
4834 retval = SCTP_DISPOSITION_CONSUME;
4835
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004836 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
4838 /* Even if we can't send the ABORT due to low memory delete the
4839 * TCB. This is a departure from our typical NOMEM handling.
4840 */
4841
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004842 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4843 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 /* Delete the established association. */
4845 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004846 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004848 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4849 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
4851 return retval;
4852}
4853
4854/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004855sctp_disposition_t sctp_sf_error_closed(struct net *net,
4856 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 const struct sctp_association *asoc,
4858 const sctp_subtype_t type,
4859 void *arg,
4860 sctp_cmd_seq_t *commands)
4861{
4862 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4863 return SCTP_DISPOSITION_CONSUME;
4864}
4865
4866/* We tried an illegal operation on an association which is shutting
4867 * down.
4868 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004869sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4870 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 const struct sctp_association *asoc,
4872 const sctp_subtype_t type,
4873 void *arg,
4874 sctp_cmd_seq_t *commands)
4875{
4876 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4877 SCTP_ERROR(-ESHUTDOWN));
4878 return SCTP_DISPOSITION_CONSUME;
4879}
4880
4881/*
4882 * sctp_cookie_wait_prm_shutdown
4883 *
4884 * Section: 4 Note: 2
4885 * Verification Tag:
4886 * Inputs
4887 * (endpoint, asoc)
4888 *
4889 * The RFC does not explicitly address this issue, but is the route through the
4890 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4891 *
4892 * Outputs
4893 * (timers)
4894 */
4895sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004896 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 const struct sctp_endpoint *ep,
4898 const struct sctp_association *asoc,
4899 const sctp_subtype_t type,
4900 void *arg,
4901 sctp_cmd_seq_t *commands)
4902{
4903 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4904 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4905
4906 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4907 SCTP_STATE(SCTP_STATE_CLOSED));
4908
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004909 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
4911 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4912
4913 return SCTP_DISPOSITION_DELETE_TCB;
4914}
4915
4916/*
4917 * sctp_cookie_echoed_prm_shutdown
4918 *
4919 * Section: 4 Note: 2
4920 * Verification Tag:
4921 * Inputs
4922 * (endpoint, asoc)
4923 *
4924 * The RFC does not explcitly address this issue, but is the route through the
4925 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4926 *
4927 * Outputs
4928 * (timers)
4929 */
4930sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004931 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 const struct sctp_endpoint *ep,
4933 const struct sctp_association *asoc,
4934 const sctp_subtype_t type,
4935 void *arg, sctp_cmd_seq_t *commands)
4936{
4937 /* There is a single T1 timer, so we should be able to use
4938 * common function with the COOKIE-WAIT state.
4939 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004940 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941}
4942
4943/*
4944 * sctp_sf_cookie_wait_prm_abort
4945 *
4946 * Section: 4 Note: 2
4947 * Verification Tag:
4948 * Inputs
4949 * (endpoint, asoc)
4950 *
4951 * The RFC does not explicitly address this issue, but is the route through the
4952 * state table when someone issues an abort while in COOKIE_WAIT state.
4953 *
4954 * Outputs
4955 * (timers)
4956 */
4957sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004958 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 const struct sctp_endpoint *ep,
4960 const struct sctp_association *asoc,
4961 const sctp_subtype_t type,
4962 void *arg,
4963 sctp_cmd_seq_t *commands)
4964{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004965 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 sctp_disposition_t retval;
4967
4968 /* Stop T1-init timer */
4969 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4970 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4971 retval = SCTP_DISPOSITION_CONSUME;
4972
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004973 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974
4975 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4976 SCTP_STATE(SCTP_STATE_CLOSED));
4977
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004978 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 /* Even if we can't send the ABORT due to low memory delete the
4981 * TCB. This is a departure from our typical NOMEM handling.
4982 */
4983
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004984 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4985 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 /* Delete the established association. */
4987 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004988 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
4990 return retval;
4991}
4992
4993/*
4994 * sctp_sf_cookie_echoed_prm_abort
4995 *
4996 * Section: 4 Note: 3
4997 * Verification Tag:
4998 * Inputs
4999 * (endpoint, asoc)
5000 *
5001 * The RFC does not explcitly address this issue, but is the route through the
5002 * state table when someone issues an abort while in COOKIE_ECHOED state.
5003 *
5004 * Outputs
5005 * (timers)
5006 */
5007sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005008 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 const struct sctp_endpoint *ep,
5010 const struct sctp_association *asoc,
5011 const sctp_subtype_t type,
5012 void *arg,
5013 sctp_cmd_seq_t *commands)
5014{
5015 /* There is a single T1 timer, so we should be able to use
5016 * common function with the COOKIE-WAIT state.
5017 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005018 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019}
5020
5021/*
5022 * sctp_sf_shutdown_pending_prm_abort
5023 *
5024 * Inputs
5025 * (endpoint, asoc)
5026 *
5027 * The RFC does not explicitly address this issue, but is the route through the
5028 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5029 *
5030 * Outputs
5031 * (timers)
5032 */
5033sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005034 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 const struct sctp_endpoint *ep,
5036 const struct sctp_association *asoc,
5037 const sctp_subtype_t type,
5038 void *arg,
5039 sctp_cmd_seq_t *commands)
5040{
5041 /* Stop the T5-shutdown guard timer. */
5042 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5043 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5044
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005045 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046}
5047
5048/*
5049 * sctp_sf_shutdown_sent_prm_abort
5050 *
5051 * Inputs
5052 * (endpoint, asoc)
5053 *
5054 * The RFC does not explicitly address this issue, but is the route through the
5055 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5056 *
5057 * Outputs
5058 * (timers)
5059 */
5060sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005061 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 const struct sctp_endpoint *ep,
5063 const struct sctp_association *asoc,
5064 const sctp_subtype_t type,
5065 void *arg,
5066 sctp_cmd_seq_t *commands)
5067{
5068 /* Stop the T2-shutdown timer. */
5069 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5070 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5071
5072 /* Stop the T5-shutdown guard timer. */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5074 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5075
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005076 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077}
5078
5079/*
5080 * sctp_sf_cookie_echoed_prm_abort
5081 *
5082 * Inputs
5083 * (endpoint, asoc)
5084 *
5085 * The RFC does not explcitly address this issue, but is the route through the
5086 * state table when someone issues an abort while in COOKIE_ECHOED state.
5087 *
5088 * Outputs
5089 * (timers)
5090 */
5091sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005092 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 const struct sctp_endpoint *ep,
5094 const struct sctp_association *asoc,
5095 const sctp_subtype_t type,
5096 void *arg,
5097 sctp_cmd_seq_t *commands)
5098{
5099 /* The same T2 timer, so we should be able to use
5100 * common function with the SHUTDOWN-SENT state.
5101 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005102 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
5105/*
5106 * Process the REQUESTHEARTBEAT primitive
5107 *
5108 * 10.1 ULP-to-SCTP
5109 * J) Request Heartbeat
5110 *
5111 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5112 *
5113 * -> result
5114 *
5115 * Instructs the local endpoint to perform a HeartBeat on the specified
5116 * destination transport address of the given association. The returned
5117 * result should indicate whether the transmission of the HEARTBEAT
5118 * chunk to the destination address is successful.
5119 *
5120 * Mandatory attributes:
5121 *
5122 * o association id - local handle to the SCTP association
5123 *
5124 * o destination transport address - the transport address of the
5125 * association on which a heartbeat should be issued.
5126 */
5127sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005128 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 const struct sctp_endpoint *ep,
5130 const struct sctp_association *asoc,
5131 const sctp_subtype_t type,
5132 void *arg,
5133 sctp_cmd_seq_t *commands)
5134{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005135 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5136 (struct sctp_transport *)arg, commands))
5137 return SCTP_DISPOSITION_NOMEM;
5138
5139 /*
5140 * RFC 2960 (bis), section 8.3
5141 *
5142 * D) Request an on-demand HEARTBEAT on a specific destination
5143 * transport address of a given association.
5144 *
5145 * The endpoint should increment the respective error counter of
5146 * the destination transport address each time a HEARTBEAT is sent
5147 * to that address and not acknowledged within one RTO.
5148 *
5149 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005150 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005151 SCTP_TRANSPORT(arg));
5152 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153}
5154
5155/*
5156 * ADDIP Section 4.1 ASCONF Chunk Procedures
5157 * When an endpoint has an ASCONF signaled change to be sent to the
5158 * remote endpoint it should do A1 to A9
5159 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005160sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5161 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 const struct sctp_association *asoc,
5163 const sctp_subtype_t type,
5164 void *arg,
5165 sctp_cmd_seq_t *commands)
5166{
5167 struct sctp_chunk *chunk = arg;
5168
5169 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5170 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5171 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5172 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5173 return SCTP_DISPOSITION_CONSUME;
5174}
5175
5176/*
5177 * Ignore the primitive event
5178 *
5179 * The return value is the disposition of the primitive.
5180 */
5181sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005182 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 const struct sctp_endpoint *ep,
5184 const struct sctp_association *asoc,
5185 const sctp_subtype_t type,
5186 void *arg,
5187 sctp_cmd_seq_t *commands)
5188{
5189 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5190 return SCTP_DISPOSITION_DISCARD;
5191}
5192
5193/***************************************************************************
5194 * These are the state functions for the OTHER events.
5195 ***************************************************************************/
5196
5197/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005198 * When the SCTP stack has no more user data to send or retransmit, this
5199 * notification is given to the user. Also, at the time when a user app
5200 * subscribes to this event, if there is no data to be sent or
5201 * retransmit, the stack will immediately send up this notification.
5202 */
5203sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005204 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005205 const struct sctp_endpoint *ep,
5206 const struct sctp_association *asoc,
5207 const sctp_subtype_t type,
5208 void *arg,
5209 sctp_cmd_seq_t *commands)
5210{
5211 struct sctp_ulpevent *event;
5212
5213 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5214 if (!event)
5215 return SCTP_DISPOSITION_NOMEM;
5216
5217 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5218
5219 return SCTP_DISPOSITION_CONSUME;
5220}
5221
5222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 * Start the shutdown negotiation.
5224 *
5225 * From Section 9.2:
5226 * Once all its outstanding data has been acknowledged, the endpoint
5227 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5228 * TSN Ack field the last sequential TSN it has received from the peer.
5229 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5230 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5231 * with the updated last sequential TSN received from its peer.
5232 *
5233 * The return value is the disposition.
5234 */
5235sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005236 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 const struct sctp_endpoint *ep,
5238 const struct sctp_association *asoc,
5239 const sctp_subtype_t type,
5240 void *arg,
5241 sctp_cmd_seq_t *commands)
5242{
5243 struct sctp_chunk *reply;
5244
5245 /* Once all its outstanding data has been acknowledged, the
5246 * endpoint shall send a SHUTDOWN chunk to its peer including
5247 * in the Cumulative TSN Ack field the last sequential TSN it
5248 * has received from the peer.
5249 */
5250 reply = sctp_make_shutdown(asoc, NULL);
5251 if (!reply)
5252 goto nomem;
5253
5254 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5255 * T2-shutdown timer.
5256 */
5257 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5258
5259 /* It shall then start the T2-shutdown timer */
5260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5261 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5262
Wei Yongjun536428a2008-09-05 08:55:26 +08005263 /* RFC 4960 Section 9.2
5264 * The sender of the SHUTDOWN MAY also start an overall guard timer
5265 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5266 */
Thomas Graff8d96052011-07-07 00:28:35 +00005267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5269
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (asoc->autoclose)
5271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5272 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5273
5274 /* and enter the SHUTDOWN-SENT state. */
5275 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5276 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5277
5278 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5279 *
5280 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005281 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 */
5283 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5284
5285 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5286
5287 return SCTP_DISPOSITION_CONSUME;
5288
5289nomem:
5290 return SCTP_DISPOSITION_NOMEM;
5291}
5292
5293/*
5294 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5295 *
5296 * From Section 9.2:
5297 *
5298 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5299 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5300 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5301 * endpoint must re-send the SHUTDOWN ACK.
5302 *
5303 * The return value is the disposition.
5304 */
5305sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005306 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 const struct sctp_endpoint *ep,
5308 const struct sctp_association *asoc,
5309 const sctp_subtype_t type,
5310 void *arg,
5311 sctp_cmd_seq_t *commands)
5312{
5313 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5314 struct sctp_chunk *reply;
5315
5316 /* There are 2 ways of getting here:
5317 * 1) called in response to a SHUTDOWN chunk
5318 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5319 *
5320 * For the case (2), the arg parameter is set to NULL. We need
5321 * to check that we have a chunk before accessing it's fields.
5322 */
5323 if (chunk) {
5324 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005325 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 /* Make sure that the SHUTDOWN chunk has a valid length. */
5328 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005329 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 commands);
5331 }
5332
5333 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5334 * shall send a SHUTDOWN ACK ...
5335 */
5336 reply = sctp_make_shutdown_ack(asoc, chunk);
5337 if (!reply)
5338 goto nomem;
5339
5340 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5341 * the T2-shutdown timer.
5342 */
5343 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5344
5345 /* and start/restart a T2-shutdown timer of its own, */
5346 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5347 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5348
5349 if (asoc->autoclose)
5350 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5351 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5352
5353 /* Enter the SHUTDOWN-ACK-SENT state. */
5354 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5355 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5356
5357 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5358 *
5359 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005360 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 */
5362 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5363
5364 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5365
5366 return SCTP_DISPOSITION_CONSUME;
5367
5368nomem:
5369 return SCTP_DISPOSITION_NOMEM;
5370}
5371
5372/*
5373 * Ignore the event defined as other
5374 *
5375 * The return value is the disposition of the event.
5376 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005377sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5378 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 const struct sctp_association *asoc,
5380 const sctp_subtype_t type,
5381 void *arg,
5382 sctp_cmd_seq_t *commands)
5383{
5384 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5385 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
5532 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005533 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005534
Vlad Yasevich81845c212006-01-30 15:59:54 -08005535 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005536 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5537 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5538 if (!repl)
5539 return SCTP_DISPOSITION_NOMEM;
5540
5541 /* Choose transport for INIT. */
5542 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5543 SCTP_CHUNK(repl));
5544
5545 /* Issue a sideeffect to do the needed accounting. */
5546 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5547 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5548
5549 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5550 } else {
5551 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5552 " max_init_attempts: %d\n",
5553 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005554 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5555 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005556 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005557 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005558 return SCTP_DISPOSITION_DELETE_TCB;
5559 }
5560
5561 return SCTP_DISPOSITION_CONSUME;
5562}
5563
5564/*
5565 * sctp_sf_t1_cookie_timer_expire
5566 *
5567 * Section: 4 Note: 2
5568 * Verification Tag:
5569 * Inputs
5570 * (endpoint, asoc)
5571 *
5572 * RFC 2960 Section 4 Notes
5573 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 * COOKIE ECHO and re-start the T1-cookie timer without changing
5575 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5576 * After that, the endpoint MUST abort the initialization process and
5577 * report the error to SCTP user.
5578 *
5579 * Outputs
5580 * (timers, events)
5581 *
5582 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005583sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5584 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 const struct sctp_association *asoc,
5586 const sctp_subtype_t type,
5587 void *arg,
5588 sctp_cmd_seq_t *commands)
5589{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005590 struct sctp_chunk *repl = NULL;
5591 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Frank Filz3f7a87d2005-06-20 13:14:57 -07005593 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005594 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595
Vlad Yasevich81845c212006-01-30 15:59:54 -08005596 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005597 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005599 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005601 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5602 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005604 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5608 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005609 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5610 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005612 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 return SCTP_DISPOSITION_DELETE_TCB;
5614 }
5615
5616 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617}
5618
5619/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5620 * with the updated last sequential TSN received from its peer.
5621 *
5622 * An endpoint should limit the number of retransmissions of the
5623 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5624 * If this threshold is exceeded the endpoint should destroy the TCB and
5625 * MUST report the peer endpoint unreachable to the upper layer (and
5626 * thus the association enters the CLOSED state). The reception of any
5627 * packet from its peer (i.e. as the peer sends all of its queued DATA
5628 * chunks) should clear the endpoint's retransmission count and restart
5629 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5630 * all of its queued DATA chunks that have not yet been sent.
5631 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005632sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5633 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 const struct sctp_association *asoc,
5635 const sctp_subtype_t type,
5636 void *arg,
5637 sctp_cmd_seq_t *commands)
5638{
5639 struct sctp_chunk *reply = NULL;
5640
5641 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005642 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005643
Neil Horman58fbbed2008-02-29 11:40:56 -08005644 ((struct sctp_association *)asoc)->shutdown_retries++;
5645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005647 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5648 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5650 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005651 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005652 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5653 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 return SCTP_DISPOSITION_DELETE_TCB;
5655 }
5656
5657 switch (asoc->state) {
5658 case SCTP_STATE_SHUTDOWN_SENT:
5659 reply = sctp_make_shutdown(asoc, NULL);
5660 break;
5661
5662 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5663 reply = sctp_make_shutdown_ack(asoc, NULL);
5664 break;
5665
5666 default:
5667 BUG();
5668 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670
5671 if (!reply)
5672 goto nomem;
5673
Wei Yongjun6345b192009-04-26 23:13:35 +08005674 /* Do some failure management (Section 8.2).
5675 * If we remove the transport an SHUTDOWN was last sent to, don't
5676 * do failure management.
5677 */
5678 if (asoc->shutdown_last_sent_to)
5679 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5680 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681
5682 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5683 * the T2-shutdown timer.
5684 */
5685 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5686
5687 /* Restart the T2-shutdown timer. */
5688 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5689 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5690 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5691 return SCTP_DISPOSITION_CONSUME;
5692
5693nomem:
5694 return SCTP_DISPOSITION_NOMEM;
5695}
5696
5697/*
5698 * ADDIP Section 4.1 ASCONF CHunk Procedures
5699 * If the T4 RTO timer expires the endpoint should do B1 to B5
5700 */
5701sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005702 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 const struct sctp_endpoint *ep,
5704 const struct sctp_association *asoc,
5705 const sctp_subtype_t type,
5706 void *arg,
5707 sctp_cmd_seq_t *commands)
5708{
5709 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5710 struct sctp_transport *transport = chunk->transport;
5711
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005712 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005713
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5715 * detection on the appropriate destination address as defined in
5716 * RFC2960 [5] section 8.1 and 8.2.
5717 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005718 if (transport)
5719 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5720 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721
5722 /* Reconfig T4 timer and transport. */
5723 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5724
5725 /* ADDIP 4.1 B2) Increment the association error counters and perform
5726 * endpoint failure detection on the association as defined in
5727 * RFC2960 [5] section 8.1 and 8.2.
5728 * association error counter is incremented in SCTP_CMD_STRIKE.
5729 */
5730 if (asoc->overall_error_count >= asoc->max_retrans) {
5731 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5732 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005733 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5734 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005736 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005737 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5738 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 return SCTP_DISPOSITION_ABORT;
5740 }
5741
5742 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5743 * the ASCONF chunk was sent by doubling the RTO timer value.
5744 * This is done in SCTP_CMD_STRIKE.
5745 */
5746
5747 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5748 * choose an alternate destination address (please refer to RFC2960
5749 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005750 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 * ASCONF sent.
5752 */
5753 sctp_chunk_hold(asoc->addip_last_asconf);
5754 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5755 SCTP_CHUNK(asoc->addip_last_asconf));
5756
5757 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5758 * destination is selected, then the RTO used will be that of the new
5759 * destination address.
5760 */
5761 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5762 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5763
5764 return SCTP_DISPOSITION_CONSUME;
5765}
5766
5767/* sctpimpguide-05 Section 2.12.2
5768 * The sender of the SHUTDOWN MAY also start an overall guard timer
5769 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5770 * At the expiration of this timer the sender SHOULD abort the association
5771 * by sending an ABORT chunk.
5772 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005773sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5774 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 const struct sctp_association *asoc,
5776 const sctp_subtype_t type,
5777 void *arg,
5778 sctp_cmd_seq_t *commands)
5779{
5780 struct sctp_chunk *reply = NULL;
5781
5782 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005783 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
5785 reply = sctp_make_abort(asoc, NULL, 0);
5786 if (!reply)
5787 goto nomem;
5788
5789 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005790 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5791 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005793 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005795 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5796 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005797
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 return SCTP_DISPOSITION_DELETE_TCB;
5799nomem:
5800 return SCTP_DISPOSITION_NOMEM;
5801}
5802
5803/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5804 * the association is automatically closed by starting the shutdown process.
5805 * The work that needs to be done is same as when SHUTDOWN is initiated by
5806 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5807 */
5808sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005809 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 const struct sctp_endpoint *ep,
5811 const struct sctp_association *asoc,
5812 const sctp_subtype_t type,
5813 void *arg,
5814 sctp_cmd_seq_t *commands)
5815{
5816 int disposition;
5817
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005818 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 /* From 9.2 Shutdown of an Association
5821 * Upon receipt of the SHUTDOWN primitive from its upper
5822 * layer, the endpoint enters SHUTDOWN-PENDING state and
5823 * remains there until all outstanding data has been
5824 * acknowledged by its peer. The endpoint accepts no new data
5825 * from its upper layer, but retransmits data to the far end
5826 * if necessary to fill gaps.
5827 */
5828 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5829 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 disposition = SCTP_DISPOSITION_CONSUME;
5832 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005833 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 arg, commands);
5835 }
5836 return disposition;
5837}
5838
5839/*****************************************************************************
5840 * These are sa state functions which could apply to all types of events.
5841 ****************************************************************************/
5842
5843/*
5844 * This table entry is not implemented.
5845 *
5846 * Inputs
5847 * (endpoint, asoc, chunk)
5848 *
5849 * The return value is the disposition of the chunk.
5850 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005851sctp_disposition_t sctp_sf_not_impl(struct net *net,
5852 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 const struct sctp_association *asoc,
5854 const sctp_subtype_t type,
5855 void *arg,
5856 sctp_cmd_seq_t *commands)
5857{
5858 return SCTP_DISPOSITION_NOT_IMPL;
5859}
5860
5861/*
5862 * This table entry represents a bug.
5863 *
5864 * Inputs
5865 * (endpoint, asoc, chunk)
5866 *
5867 * The return value is the disposition of the chunk.
5868 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005869sctp_disposition_t sctp_sf_bug(struct net *net,
5870 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 const struct sctp_association *asoc,
5872 const sctp_subtype_t type,
5873 void *arg,
5874 sctp_cmd_seq_t *commands)
5875{
5876 return SCTP_DISPOSITION_BUG;
5877}
5878
5879/*
5880 * This table entry represents the firing of a timer in the wrong state.
5881 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5882 * when the association is in the wrong state. This event should
5883 * be ignored, so as to prevent any rearming of the timer.
5884 *
5885 * Inputs
5886 * (endpoint, asoc, chunk)
5887 *
5888 * The return value is the disposition of the chunk.
5889 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005890sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5891 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 const struct sctp_association *asoc,
5893 const sctp_subtype_t type,
5894 void *arg,
5895 sctp_cmd_seq_t *commands)
5896{
5897 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5898 return SCTP_DISPOSITION_CONSUME;
5899}
5900
5901/********************************************************************
5902 * 2nd Level Abstractions
5903 ********************************************************************/
5904
5905/* Pull the SACK chunk based on the SACK header. */
5906static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5907{
5908 struct sctp_sackhdr *sack;
5909 unsigned int len;
5910 __u16 num_blocks;
5911 __u16 num_dup_tsns;
5912
5913 /* Protect ourselves from reading too far into
5914 * the skb from a bogus sender.
5915 */
5916 sack = (struct sctp_sackhdr *) chunk->skb->data;
5917
5918 num_blocks = ntohs(sack->num_gap_ack_blocks);
5919 num_dup_tsns = ntohs(sack->num_dup_tsns);
5920 len = sizeof(struct sctp_sackhdr);
5921 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5922 if (len > chunk->skb->len)
5923 return NULL;
5924
5925 skb_pull(chunk->skb, len);
5926
5927 return sack;
5928}
5929
5930/* Create an ABORT packet to be sent as a response, with the specified
5931 * error causes.
5932 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005933static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5934 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 const struct sctp_association *asoc,
5936 struct sctp_chunk *chunk,
5937 const void *payload,
5938 size_t paylen)
5939{
5940 struct sctp_packet *packet;
5941 struct sctp_chunk *abort;
5942
Eric W. Biederman2ce955032012-08-06 08:43:06 +00005943 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
5945 if (packet) {
5946 /* Make an ABORT.
5947 * The T bit will be set if the asoc is NULL.
5948 */
5949 abort = sctp_make_abort(asoc, chunk, paylen);
5950 if (!abort) {
5951 sctp_ootb_pkt_free(packet);
5952 return NULL;
5953 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005954
5955 /* Reflect vtag if T-Bit is set */
5956 if (sctp_test_T_bit(abort))
5957 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 /* Add specified error causes, i.e., payload, to the
5960 * end of the chunk.
5961 */
5962 sctp_addto_chunk(abort, paylen, payload);
5963
5964 /* Set the skb to the belonging sock for accounting. */
5965 abort->skb->sk = ep->base.sk;
5966
5967 sctp_packet_append_chunk(packet, abort);
5968
5969 }
5970
5971 return packet;
5972}
5973
5974/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce955032012-08-06 08:43:06 +00005975static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5976 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 const struct sctp_chunk *chunk)
5978{
5979 struct sctp_packet *packet;
5980 struct sctp_transport *transport;
5981 __u16 sport;
5982 __u16 dport;
5983 __u32 vtag;
5984
5985 /* Get the source and destination port from the inbound packet. */
5986 sport = ntohs(chunk->sctp_hdr->dest);
5987 dport = ntohs(chunk->sctp_hdr->source);
5988
5989 /* The V-tag is going to be the same as the inbound packet if no
5990 * association exists, otherwise, use the peer's vtag.
5991 */
5992 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005993 /* Special case the INIT-ACK as there is no peer's vtag
5994 * yet.
5995 */
5996 switch(chunk->chunk_hdr->type) {
5997 case SCTP_CID_INIT_ACK:
5998 {
5999 sctp_initack_chunk_t *initack;
6000
6001 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6002 vtag = ntohl(initack->init_hdr.init_tag);
6003 break;
6004 }
6005 default:
6006 vtag = asoc->peer.i.init_tag;
6007 break;
6008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 } else {
6010 /* Special case the INIT and stale COOKIE_ECHO as there is no
6011 * vtag yet.
6012 */
6013 switch(chunk->chunk_hdr->type) {
6014 case SCTP_CID_INIT:
6015 {
6016 sctp_init_chunk_t *init;
6017
6018 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6019 vtag = ntohl(init->init_hdr.init_tag);
6020 break;
6021 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006022 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 vtag = ntohl(chunk->sctp_hdr->vtag);
6024 break;
6025 }
6026 }
6027
6028 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006029 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 if (!transport)
6031 goto nomem;
6032
6033 /* Cache a route for the transport with the chunk's destination as
6034 * the source address.
6035 */
Al Viro16b0a032006-11-20 17:13:38 -08006036 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce955032012-08-06 08:43:06 +00006037 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038
6039 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6040 packet = sctp_packet_config(packet, vtag, 0);
6041
6042 return packet;
6043
6044nomem:
6045 return NULL;
6046}
6047
6048/* Free the packet allocated earlier for responding in the OOTB condition. */
6049void sctp_ootb_pkt_free(struct sctp_packet *packet)
6050{
6051 sctp_transport_free(packet->transport);
6052}
6053
6054/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006055static void sctp_send_stale_cookie_err(struct net *net,
6056 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 const struct sctp_association *asoc,
6058 const struct sctp_chunk *chunk,
6059 sctp_cmd_seq_t *commands,
6060 struct sctp_chunk *err_chunk)
6061{
6062 struct sctp_packet *packet;
6063
6064 if (err_chunk) {
Eric W. Biederman2ce955032012-08-06 08:43:06 +00006065 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 if (packet) {
6067 struct sctp_signed_cookie *cookie;
6068
6069 /* Override the OOTB vtag from the cookie. */
6070 cookie = chunk->subh.cookie_hdr;
6071 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006072
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 /* Set the skb to the belonging sock for accounting. */
6074 err_chunk->skb->sk = ep->base.sk;
6075 sctp_packet_append_chunk(packet, err_chunk);
6076 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6077 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006078 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 } else
6080 sctp_chunk_free (err_chunk);
6081 }
6082}
6083
6084
6085/* Process a data chunk */
6086static int sctp_eat_data(const struct sctp_association *asoc,
6087 struct sctp_chunk *chunk,
6088 sctp_cmd_seq_t *commands)
6089{
6090 sctp_datahdr_t *data_hdr;
6091 struct sctp_chunk *err;
6092 size_t datalen;
6093 sctp_verb_t deliver;
6094 int tmp;
6095 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006096 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006097 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006098 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006099 u16 ssn;
6100 u16 sid;
6101 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102
6103 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6104 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6105
6106 tsn = ntohl(data_hdr->tsn);
6107 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
6108
6109 /* ASSERT: Now skb->data is really the user data. */
6110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 /* Process ECN based congestion.
6112 *
6113 * Since the chunk structure is reused for all chunks within
6114 * a packet, we use ecn_ce_done to track if we've already
6115 * done CE processing for this packet.
6116 *
6117 * We need to do ECN processing even if we plan to discard the
6118 * chunk later.
6119 */
6120
6121 if (!chunk->ecn_ce_done) {
6122 struct sctp_af *af;
6123 chunk->ecn_ce_done = 1;
6124
6125 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006126 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127
6128 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6129 /* Do real work as sideffect. */
6130 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6131 SCTP_U32(tsn));
6132 }
6133 }
6134
6135 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6136 if (tmp < 0) {
6137 /* The TSN is too high--silently discard the chunk and
6138 * count on it getting retransmitted later.
6139 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006140 if (chunk->asoc)
6141 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 return SCTP_IERROR_HIGH_TSN;
6143 } else if (tmp > 0) {
6144 /* This is a duplicate. Record it. */
6145 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6146 return SCTP_IERROR_DUP_TSN;
6147 }
6148
6149 /* This is a new TSN. */
6150
6151 /* Discard if there is no room in the receive window.
6152 * Actually, allow a little bit of overflow (up to a MTU).
6153 */
6154 datalen = ntohs(chunk->chunk_hdr->length);
6155 datalen -= sizeof(sctp_data_chunk_t);
6156
6157 deliver = SCTP_CMD_CHUNK_ULP;
6158
6159 /* Think about partial delivery. */
6160 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6161
6162 /* Even if we don't accept this chunk there is
6163 * memory pressure.
6164 */
6165 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6166 }
6167
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006168 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 * seems a bit troublesome in that frag_point varies based on
6170 * PMTU. In cases, such as loopback, this might be a rather
6171 * large spill over.
6172 */
Neil Horman4d93df02007-08-15 16:07:44 -07006173 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6174 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
6176 /* If this is the next TSN, consider reneging to make
6177 * room. Note: Playing nice with a confused sender. A
6178 * malicious sender can still eat up all our buffer
6179 * space and in the future we may want to detect and
6180 * do more drastic reneging.
6181 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006182 if (sctp_tsnmap_has_gap(map) &&
6183 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6185 deliver = SCTP_CMD_RENEGE;
6186 } else {
6187 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6188 "rwnd: %d\n", tsn, datalen,
6189 asoc->rwnd);
6190 return SCTP_IERROR_IGNORE_TSN;
6191 }
6192 }
6193
6194 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006195 * Also try to renege to limit our memory usage in the event that
6196 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006197 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006198 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6199 * memory usage too much
6200 */
6201 if (*sk->sk_prot_creator->memory_pressure) {
6202 if (sctp_tsnmap_has_gap(map) &&
6203 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6204 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6205 deliver = SCTP_CMD_RENEGE;
6206 }
6207 }
6208
6209 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 * Section 3.3.10.9 No User Data (9)
6211 *
6212 * Cause of error
6213 * ---------------
6214 * No User Data: This error cause is returned to the originator of a
6215 * DATA chunk if a received DATA chunk has no user data.
6216 */
6217 if (unlikely(0 == datalen)) {
6218 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6219 if (err) {
6220 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6221 SCTP_CHUNK(err));
6222 }
6223 /* We are going to ABORT, so we might as well stop
6224 * processing the rest of the chunks in the packet.
6225 */
6226 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006227 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6228 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006230 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006231 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6232 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 return SCTP_IERROR_NO_DATA;
6234 }
6235
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006236 chunk->data_accepted = 1;
6237
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6239 * if we renege and the chunk arrives again.
6240 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006241 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006242 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006243 if (chunk->asoc)
6244 chunk->asoc->stats.iuodchunks++;
6245 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006246 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006247 if (chunk->asoc)
6248 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006249 ordered = 1;
6250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
6252 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6253 *
6254 * If an endpoint receive a DATA chunk with an invalid stream
6255 * identifier, it shall acknowledge the reception of the DATA chunk
6256 * following the normal procedure, immediately send an ERROR chunk
6257 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6258 * and discard the DATA chunk.
6259 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006260 sid = ntohs(data_hdr->stream);
6261 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006262 /* Mark tsn as received even though we drop it */
6263 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6264
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6266 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006267 sizeof(data_hdr->stream),
6268 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 if (err)
6270 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6271 SCTP_CHUNK(err));
6272 return SCTP_IERROR_BAD_STREAM;
6273 }
6274
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006275 /* Check to see if the SSN is possible for this TSN.
6276 * The biggest gap we can record is 4K wide. Since SSNs wrap
6277 * at an unsigned short, there is no way that an SSN can
6278 * wrap and for a valid TSN. We can simply check if the current
6279 * SSN is smaller then the next expected one. If it is, it wrapped
6280 * and is invalid.
6281 */
6282 ssn = ntohs(data_hdr->ssn);
6283 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6284 return SCTP_IERROR_PROTO_VIOLATION;
6285 }
6286
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 /* Send the data up to the user. Note: Schedule the
6288 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6289 * chunk needs the updated rwnd.
6290 */
6291 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6292
6293 return SCTP_IERROR_NO_ERROR;
6294}