blob: ebcd1eedb1151d050d51c5a5043e720f4797a33a [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. Biederman2ce95502012-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. Biederman2ce95502012-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;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +0000367 if (!sctp_verify_init(net, 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;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +0000534 if (!sctp_verify_init(net, 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. Biederman2ce95502012-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
768 /* set-up our fake chunk so that we can process it */
769 auth.skb = chunk->auth_chunk;
770 auth.asoc = chunk->asoc;
771 auth.sctp_hdr = chunk->sctp_hdr;
772 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
773 sizeof(sctp_chunkhdr_t));
774 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
775 auth.transport = chunk->transport;
776
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000777 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700778
779 /* We can now safely free the auth_chunk clone */
780 kfree_skb(chunk->auth_chunk);
781
782 if (ret != SCTP_IERROR_NO_ERROR) {
783 sctp_association_free(new_asoc);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000784 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700785 }
786 }
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 repl = sctp_make_cookie_ack(new_asoc, chunk);
789 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700790 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 /* RFC 2960 5.1 Normal Establishment of an Association
793 *
794 * D) IMPLEMENTATION NOTE: An implementation may choose to
795 * send the Communication Up notification to the SCTP user
796 * upon reception of a valid COOKIE ECHO chunk.
797 */
798 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
799 new_asoc->c.sinit_num_ostreams,
800 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700801 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!ev)
803 goto nomem_ev;
804
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900805 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800806 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800808 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800810 if (new_asoc->peer.adaptation_ind) {
811 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700813 if (!ai_ev)
814 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700817 /* Add all the state machine commands now since we've created
818 * everything. This way we don't introduce memory corruptions
819 * during side-effect processing and correclty count established
820 * associations.
821 */
822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
823 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
824 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000825 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
826 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700827 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
828
829 if (new_asoc->autoclose)
830 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
831 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
832
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700833 /* This will send the COOKIE ACK */
834 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
835
836 /* Queue the ASSOC_CHANGE event */
837 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
838
839 /* Send up the Adaptation Layer Indication event */
840 if (ai_ev)
841 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
842 SCTP_ULPEVENT(ai_ev));
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return SCTP_DISPOSITION_CONSUME;
845
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700846nomem_aiev:
847 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848nomem_ev:
849 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850nomem_init:
851 sctp_association_free(new_asoc);
852nomem:
853 return SCTP_DISPOSITION_NOMEM;
854}
855
856/*
857 * Respond to a normal COOKIE ACK chunk.
858 * We are the side that is being asked for an association.
859 *
860 * RFC 2960 5.1 Normal Establishment of an Association
861 *
862 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
863 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
864 * timer. It may also notify its ULP about the successful
865 * establishment of the association with a Communication Up
866 * notification (see Section 10).
867 *
868 * Verification Tag:
869 * Inputs
870 * (endpoint, asoc, chunk)
871 *
872 * Outputs
873 * (asoc, reply_msg, msg_up, timers, counters)
874 *
875 * The return value is the disposition of the chunk.
876 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000877sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
878 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 const struct sctp_association *asoc,
880 const sctp_subtype_t type, void *arg,
881 sctp_cmd_seq_t *commands)
882{
883 struct sctp_chunk *chunk = arg;
884 struct sctp_ulpevent *ev;
885
886 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000887 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* Verify that the chunk length for the COOKIE-ACK is OK.
890 * If we don't do this, any bundled chunks may be junked.
891 */
892 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000893 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 commands);
895
896 /* Reset init error count upon receipt of COOKIE-ACK,
897 * to avoid problems with the managemement of this
898 * counter in stale cookie situations when a transition back
899 * from the COOKIE-ECHOED state to the COOKIE-WAIT
900 * state is performed.
901 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700902 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* RFC 2960 5.1 Normal Establishment of an Association
905 *
906 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
907 * from the COOKIE-ECHOED state to the ESTABLISHED state,
908 * stopping the T1-cookie timer.
909 */
910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
911 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
912 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
913 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000914 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
915 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
917 if (asoc->autoclose)
918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
919 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 /* It may also notify its ULP about the successful
922 * establishment of the association with a Communication Up
923 * notification (see Section 10).
924 */
925 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
926 0, asoc->c.sinit_num_ostreams,
927 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700928 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 if (!ev)
931 goto nomem;
932
933 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
934
935 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800936 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800938 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800940 if (asoc->peer.adaptation_ind) {
941 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 if (!ev)
943 goto nomem;
944
945 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
946 SCTP_ULPEVENT(ev));
947 }
948
949 return SCTP_DISPOSITION_CONSUME;
950nomem:
951 return SCTP_DISPOSITION_NOMEM;
952}
953
954/* Generate and sendout a heartbeat packet. */
955static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
956 const struct sctp_association *asoc,
957 const sctp_subtype_t type,
958 void *arg,
959 sctp_cmd_seq_t *commands)
960{
961 struct sctp_transport *transport = (struct sctp_transport *) arg;
962 struct sctp_chunk *reply;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* Send a heartbeat to our peer. */
Wei Yongjun92c73af2011-04-19 21:31:47 +0000965 reply = sctp_make_heartbeat(asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!reply)
967 return SCTP_DISPOSITION_NOMEM;
968
969 /* Set rto_pending indicating that an RTT measurement
970 * is started with this heartbeat chunk.
971 */
972 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
973 SCTP_TRANSPORT(transport));
974
975 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
976 return SCTP_DISPOSITION_CONSUME;
977}
978
979/* Generate a HEARTBEAT packet on the given transport. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +0000980sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
981 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 const struct sctp_association *asoc,
983 const sctp_subtype_t type,
984 void *arg,
985 sctp_cmd_seq_t *commands)
986{
987 struct sctp_transport *transport = (struct sctp_transport *) arg;
988
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400989 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700990 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
991 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
993 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800994 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +0000995 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
996 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return SCTP_DISPOSITION_DELETE_TCB;
998 }
999
1000 /* Section 3.3.5.
1001 * The Sender-specific Heartbeat Info field should normally include
1002 * information about the sender's current time when this HEARTBEAT
1003 * chunk is sent and the destination transport address to which this
1004 * HEARTBEAT is sent (see Section 8.3).
1005 */
1006
Frank Filz52ccb8e2005-12-22 11:36:46 -08001007 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (SCTP_DISPOSITION_NOMEM ==
1009 sctp_sf_heartbeat(ep, asoc, type, arg,
1010 commands))
1011 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 /* Set transport error counter and association error counter
1014 * when sending heartbeat.
1015 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001016 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 SCTP_TRANSPORT(transport));
1018 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001019 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1020 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1022 SCTP_TRANSPORT(transport));
1023
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001024 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
1028 * Process an heartbeat request.
1029 *
1030 * Section: 8.3 Path Heartbeat
1031 * The receiver of the HEARTBEAT should immediately respond with a
1032 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1033 * from the received HEARTBEAT chunk.
1034 *
1035 * Verification Tag: 8.5 Verification Tag [Normal verification]
1036 * When receiving an SCTP packet, the endpoint MUST ensure that the
1037 * value in the Verification Tag field of the received SCTP packet
1038 * matches its own Tag. If the received Verification Tag value does not
1039 * match the receiver's own tag value, the receiver shall silently
1040 * discard the packet and shall not process it any further except for
1041 * those cases listed in Section 8.5.1 below.
1042 *
1043 * Inputs
1044 * (endpoint, asoc, chunk)
1045 *
1046 * Outputs
1047 * (asoc, reply_msg, msg_up, timers, counters)
1048 *
1049 * The return value is the disposition of the chunk.
1050 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001051sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1052 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 const struct sctp_association *asoc,
1054 const sctp_subtype_t type,
1055 void *arg,
1056 sctp_cmd_seq_t *commands)
1057{
Thomas Graf06a31e22012-11-30 02:16:27 +00001058 sctp_paramhdr_t *param_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 struct sctp_chunk *chunk = arg;
1060 struct sctp_chunk *reply;
1061 size_t paylen = 0;
1062
1063 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001064 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 /* Make sure that the HEARTBEAT chunk has a valid length. */
1067 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001068 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 commands);
1070
1071 /* 8.3 The receiver of the HEARTBEAT should immediately
1072 * respond with a HEARTBEAT ACK that contains the Heartbeat
1073 * Information field copied from the received HEARTBEAT chunk.
1074 */
1075 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
Thomas Graf06a31e22012-11-30 02:16:27 +00001076 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Thomas Graf06a31e22012-11-30 02:16:27 +00001078
1079 if (ntohs(param_hdr->length) > paylen)
1080 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1081 param_hdr, commands);
1082
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001083 if (!pskb_pull(chunk->skb, paylen))
1084 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Thomas Graf06a31e22012-11-30 02:16:27 +00001086 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (!reply)
1088 goto nomem;
1089
1090 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1091 return SCTP_DISPOSITION_CONSUME;
1092
1093nomem:
1094 return SCTP_DISPOSITION_NOMEM;
1095}
1096
1097/*
1098 * Process the returning HEARTBEAT ACK.
1099 *
1100 * Section: 8.3 Path Heartbeat
1101 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1102 * should clear the error counter of the destination transport
1103 * address to which the HEARTBEAT was sent, and mark the destination
1104 * transport address as active if it is not so marked. The endpoint may
1105 * optionally report to the upper layer when an inactive destination
1106 * address is marked as active due to the reception of the latest
1107 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1108 * clear the association overall error count as well (as defined
1109 * in section 8.1).
1110 *
1111 * The receiver of the HEARTBEAT ACK should also perform an RTT
1112 * measurement for that destination transport address using the time
1113 * value carried in the HEARTBEAT ACK chunk.
1114 *
1115 * Verification Tag: 8.5 Verification Tag [Normal verification]
1116 *
1117 * Inputs
1118 * (endpoint, asoc, chunk)
1119 *
1120 * Outputs
1121 * (asoc, reply_msg, msg_up, timers, counters)
1122 *
1123 * The return value is the disposition of the chunk.
1124 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001125sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1126 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 const struct sctp_association *asoc,
1128 const sctp_subtype_t type,
1129 void *arg,
1130 sctp_cmd_seq_t *commands)
1131{
1132 struct sctp_chunk *chunk = arg;
1133 union sctp_addr from_addr;
1134 struct sctp_transport *link;
1135 sctp_sender_hb_info_t *hbinfo;
1136 unsigned long max_interval;
1137
1138 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001139 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001142 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1143 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001144 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 commands);
1146
1147 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001148 /* Make sure that the length of the parameter is what we expect */
1149 if (ntohs(hbinfo->param_hdr.length) !=
1150 sizeof(sctp_sender_hb_info_t)) {
1151 return SCTP_DISPOSITION_DISCARD;
1152 }
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001155 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001158 if (unlikely(!link)) {
1159 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001160 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1161 __func__,
1162 asoc,
1163 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001164 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001165 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1166 __func__,
1167 asoc,
1168 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 return SCTP_DISPOSITION_DISCARD;
1171 }
1172
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001173 /* Validate the 64-bit random nonce. */
1174 if (hbinfo->hb_nonce != link->hb_nonce)
1175 return SCTP_DISPOSITION_DISCARD;
1176
Frank Filz52ccb8e2005-12-22 11:36:46 -08001177 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* Check if the timestamp looks valid. */
1180 if (time_after(hbinfo->sent_at, jiffies) ||
1181 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001182 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001184 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return SCTP_DISPOSITION_DISCARD;
1186 }
1187
1188 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1189 * the HEARTBEAT should clear the error counter of the
1190 * destination transport address to which the HEARTBEAT was
1191 * sent and mark the destination transport address as active if
1192 * it is not so marked.
1193 */
1194 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1195
1196 return SCTP_DISPOSITION_CONSUME;
1197}
1198
1199/* Helper function to send out an abort for the restart
1200 * condition.
1201 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001202static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct sctp_chunk *init,
1204 sctp_cmd_seq_t *commands)
1205{
1206 int len;
1207 struct sctp_packet *pkt;
1208 union sctp_addr_param *addrparm;
1209 struct sctp_errhdr *errhdr;
1210 struct sctp_endpoint *ep;
1211 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1212 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1213
1214 /* Build the error on the stack. We are way to malloc crazy
1215 * throughout the code today.
1216 */
1217 errhdr = (struct sctp_errhdr *)buffer;
1218 addrparm = (union sctp_addr_param *)errhdr->variable;
1219
1220 /* Copy into a parm format. */
1221 len = af->to_addr_param(ssa, addrparm);
1222 len += sizeof(sctp_errhdr_t);
1223
1224 errhdr->cause = SCTP_ERROR_RESTART;
1225 errhdr->length = htons(len);
1226
1227 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001228 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* Association is NULL since this may be a restart attack and we
1231 * want to send back the attacker's vtag.
1232 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001233 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 if (!pkt)
1236 goto out;
1237 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1238
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001239 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /* Discard the rest of the inbound packet. */
1242 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1243
1244out:
1245 /* Even if there is no memory, treat as a failure so
1246 * the packet will get dropped.
1247 */
1248 return 0;
1249}
1250
Joe Perches123031c2010-09-08 11:04:21 +00001251static bool list_has_sctp_addr(const struct list_head *list,
1252 union sctp_addr *ipaddr)
1253{
1254 struct sctp_transport *addr;
1255
1256 list_for_each_entry(addr, list, transports) {
1257 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1258 return true;
1259 }
1260
1261 return false;
1262}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263/* A restart is occurring, check to make sure no new addresses
1264 * are being added as we may be under a takeover attack.
1265 */
1266static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1267 const struct sctp_association *asoc,
1268 struct sctp_chunk *init,
1269 sctp_cmd_seq_t *commands)
1270{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001271 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001272 struct sctp_transport *new_addr;
1273 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Joe Perches123031c2010-09-08 11:04:21 +00001275 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 * ...
1277 * Before responding the endpoint MUST check to see if the
1278 * unexpected INIT adds new addresses to the association. If new
1279 * addresses are added to the association, the endpoint MUST respond
1280 * with an ABORT..
1281 */
1282
1283 /* Search through all current addresses and make sure
1284 * we aren't adding any new ones.
1285 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001286 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001287 transports) {
1288 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1289 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001290 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001291 commands);
1292 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
Joe Perches123031c2010-09-08 11:04:21 +00001294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
1297 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301/* Populate the verification/tie tags based on overlapping INIT
1302 * scenario.
1303 *
1304 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1305 */
1306static void sctp_tietags_populate(struct sctp_association *new_asoc,
1307 const struct sctp_association *asoc)
1308{
1309 switch (asoc->state) {
1310
1311 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1312
1313 case SCTP_STATE_COOKIE_WAIT:
1314 new_asoc->c.my_vtag = asoc->c.my_vtag;
1315 new_asoc->c.my_ttag = asoc->c.my_vtag;
1316 new_asoc->c.peer_ttag = 0;
1317 break;
1318
1319 case SCTP_STATE_COOKIE_ECHOED:
1320 new_asoc->c.my_vtag = asoc->c.my_vtag;
1321 new_asoc->c.my_ttag = asoc->c.my_vtag;
1322 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1323 break;
1324
1325 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1326 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1327 */
1328 default:
1329 new_asoc->c.my_ttag = asoc->c.my_vtag;
1330 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1331 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* Other parameters for the endpoint SHOULD be copied from the
1335 * existing parameters of the association (e.g. number of
1336 * outbound streams) into the INIT ACK and cookie.
1337 */
1338 new_asoc->rwnd = asoc->rwnd;
1339 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1340 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1341 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1342}
1343
1344/*
1345 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1346 * handling action.
1347 *
1348 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1349 *
1350 * Returns value representing action to be taken. These action values
1351 * correspond to Action/Description values in RFC 2960, Table 2.
1352 */
1353static char sctp_tietags_compare(struct sctp_association *new_asoc,
1354 const struct sctp_association *asoc)
1355{
1356 /* In this case, the peer may have restarted. */
1357 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1358 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1359 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1360 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1361 return 'A';
1362
1363 /* Collision case B. */
1364 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1365 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1366 (0 == asoc->c.peer_vtag))) {
1367 return 'B';
1368 }
1369
1370 /* Collision case D. */
1371 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1372 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1373 return 'D';
1374
1375 /* Collision case C. */
1376 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1377 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1378 (0 == new_asoc->c.my_ttag) &&
1379 (0 == new_asoc->c.peer_ttag))
1380 return 'C';
1381
1382 /* No match to any of the special cases; discard this packet. */
1383 return 'E';
1384}
1385
1386/* Common helper routine for both duplicate and simulataneous INIT
1387 * chunk handling.
1388 */
1389static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001390 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 const struct sctp_endpoint *ep,
1392 const struct sctp_association *asoc,
1393 const sctp_subtype_t type,
1394 void *arg, sctp_cmd_seq_t *commands)
1395{
1396 sctp_disposition_t retval;
1397 struct sctp_chunk *chunk = arg;
1398 struct sctp_chunk *repl;
1399 struct sctp_association *new_asoc;
1400 struct sctp_chunk *err_chunk;
1401 struct sctp_packet *packet;
1402 sctp_unrecognized_param_t *unk_param;
1403 int len;
1404
1405 /* 6.10 Bundling
1406 * An endpoint MUST NOT bundle INIT, INIT ACK or
1407 * SHUTDOWN COMPLETE with any other chunks.
1408 *
1409 * IG Section 2.11.2
1410 * Furthermore, we require that the receiver of an INIT chunk MUST
1411 * enforce these rules by silently discarding an arriving packet
1412 * with an INIT chunk that is bundled with other chunks.
1413 */
1414 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001415 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001418 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
1420 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001421 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* Make sure that the INIT chunk has a valid length.
1424 * In this case, we generate a protocol violation since we have
1425 * an association established.
1426 */
1427 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001428 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 commands);
1430 /* Grab the INIT header. */
1431 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1432
1433 /* Tag the variable length parameters. */
1434 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1435
1436 /* Verify the INIT chunk before processing it. */
1437 err_chunk = NULL;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00001438 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1440 &err_chunk)) {
1441 /* This chunk contains fatal error. It is to be discarded.
1442 * Send an ABORT, with causes if there is any.
1443 */
1444 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001445 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 (__u8 *)(err_chunk->chunk_hdr) +
1447 sizeof(sctp_chunkhdr_t),
1448 ntohs(err_chunk->chunk_hdr->length) -
1449 sizeof(sctp_chunkhdr_t));
1450
1451 if (packet) {
1452 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1453 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001454 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 retval = SCTP_DISPOSITION_CONSUME;
1456 } else {
1457 retval = SCTP_DISPOSITION_NOMEM;
1458 }
1459 goto cleanup;
1460 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001461 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 commands);
1463 }
1464 }
1465
1466 /*
1467 * Other parameters for the endpoint SHOULD be copied from the
1468 * existing parameters of the association (e.g. number of
1469 * outbound streams) into the INIT ACK and cookie.
1470 * FIXME: We are copying parameters from the endpoint not the
1471 * association.
1472 */
1473 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1474 if (!new_asoc)
1475 goto nomem;
1476
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001477 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1478 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1479 goto nomem;
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 /* In the outbound INIT ACK the endpoint MUST copy its current
1482 * Verification Tag and Peers Verification tag into a reserved
1483 * place (local tie-tag and per tie-tag) within the state cookie.
1484 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001485 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001487 GFP_ATOMIC))
1488 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
1490 /* Make sure no new addresses are being added during the
1491 * restart. Do not do this check for COOKIE-WAIT state,
1492 * since there are no peer addresses to check against.
1493 * Upon return an ABORT will have been sent if needed.
1494 */
1495 if (!sctp_state(asoc, COOKIE_WAIT)) {
1496 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1497 commands)) {
1498 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001499 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 }
1501 }
1502
1503 sctp_tietags_populate(new_asoc, asoc);
1504
1505 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1506
1507 /* If there are errors need to be reported for unknown parameters,
1508 * make sure to reserve enough room in the INIT ACK for them.
1509 */
1510 len = 0;
1511 if (err_chunk) {
1512 len = ntohs(err_chunk->chunk_hdr->length) -
1513 sizeof(sctp_chunkhdr_t);
1514 }
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1517 if (!repl)
1518 goto nomem;
1519
1520 /* If there are errors need to be reported for unknown parameters,
1521 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1522 * parameter.
1523 */
1524 if (err_chunk) {
1525 /* Get the "Unrecognized parameter" parameter(s) out of the
1526 * ERROR chunk generated by sctp_verify_init(). Since the
1527 * error cause code for "unknown parameter" and the
1528 * "Unrecognized parameter" type is the same, we can
1529 * construct the parameters in INIT ACK by copying the
1530 * ERROR causes over.
1531 */
1532 unk_param = (sctp_unrecognized_param_t *)
1533 ((__u8 *)(err_chunk->chunk_hdr) +
1534 sizeof(sctp_chunkhdr_t));
1535 /* Replace the cause code with the "Unrecognized parameter"
1536 * parameter type.
1537 */
1538 sctp_addto_chunk(repl, len, unk_param);
1539 }
1540
1541 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1542 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1543
1544 /*
1545 * Note: After sending out INIT ACK with the State Cookie parameter,
1546 * "Z" MUST NOT allocate any resources for this new association.
1547 * Otherwise, "Z" will be vulnerable to resource attacks.
1548 */
1549 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1550 retval = SCTP_DISPOSITION_CONSUME;
1551
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001552 return retval;
1553
1554nomem:
1555 retval = SCTP_DISPOSITION_NOMEM;
1556nomem_retval:
1557 if (new_asoc)
1558 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559cleanup:
1560 if (err_chunk)
1561 sctp_chunk_free(err_chunk);
1562 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001566 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * This means we started an INIT and then we got an INIT request from
1568 * our peer.
1569 *
1570 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1571 * This usually indicates an initialization collision, i.e., each
1572 * endpoint is attempting, at about the same time, to establish an
1573 * association with the other endpoint.
1574 *
1575 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1576 * endpoint MUST respond with an INIT ACK using the same parameters it
1577 * sent in its original INIT chunk (including its Verification Tag,
1578 * unchanged). These original parameters are combined with those from the
1579 * newly received INIT chunk. The endpoint shall also generate a State
1580 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1581 * INIT to calculate the State Cookie.
1582 *
1583 * After that, the endpoint MUST NOT change its state, the T1-init
1584 * timer shall be left running and the corresponding TCB MUST NOT be
1585 * destroyed. The normal procedures for handling State Cookies when
1586 * a TCB exists will resolve the duplicate INITs to a single association.
1587 *
1588 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1589 * its Tie-Tags with the Tag information of itself and its peer (see
1590 * section 5.2.2 for a description of the Tie-Tags).
1591 *
1592 * Verification Tag: Not explicit, but an INIT can not have a valid
1593 * verification tag, so we skip the check.
1594 *
1595 * Inputs
1596 * (endpoint, asoc, chunk)
1597 *
1598 * Outputs
1599 * (asoc, reply_msg, msg_up, timers, counters)
1600 *
1601 * The return value is the disposition of the chunk.
1602 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001603sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1604 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 const struct sctp_association *asoc,
1606 const sctp_subtype_t type,
1607 void *arg,
1608 sctp_cmd_seq_t *commands)
1609{
1610 /* Call helper to do the real work for both simulataneous and
1611 * duplicate INIT chunk handling.
1612 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001613 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
1616/*
1617 * Handle duplicated INIT messages. These are usually delayed
1618 * restransmissions.
1619 *
1620 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1621 * COOKIE-ECHOED and COOKIE-WAIT
1622 *
1623 * Unless otherwise stated, upon reception of an unexpected INIT for
1624 * this association, the endpoint shall generate an INIT ACK with a
1625 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1626 * current Verification Tag and peer's Verification Tag into a reserved
1627 * place within the state cookie. We shall refer to these locations as
1628 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1629 * containing this INIT ACK MUST carry a Verification Tag value equal to
1630 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1631 * MUST contain a new Initiation Tag (randomly generated see Section
1632 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1633 * existing parameters of the association (e.g. number of outbound
1634 * streams) into the INIT ACK and cookie.
1635 *
1636 * After sending out the INIT ACK, the endpoint shall take no further
1637 * actions, i.e., the existing association, including its current state,
1638 * and the corresponding TCB MUST NOT be changed.
1639 *
1640 * Note: Only when a TCB exists and the association is not in a COOKIE-
1641 * WAIT state are the Tie-Tags populated. For a normal association INIT
1642 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1643 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1644 * State Cookie are populated as specified in section 5.2.1.
1645 *
1646 * Verification Tag: Not specified, but an INIT has no way of knowing
1647 * what the verification tag could be, so we ignore it.
1648 *
1649 * Inputs
1650 * (endpoint, asoc, chunk)
1651 *
1652 * Outputs
1653 * (asoc, reply_msg, msg_up, timers, counters)
1654 *
1655 * The return value is the disposition of the chunk.
1656 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001657sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1658 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 const struct sctp_association *asoc,
1660 const sctp_subtype_t type,
1661 void *arg,
1662 sctp_cmd_seq_t *commands)
1663{
1664 /* Call helper to do the real work for both simulataneous and
1665 * duplicate INIT chunk handling.
1666 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001667 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
1670
Vlad Yasevich610ab732007-01-15 19:18:30 -08001671/*
1672 * Unexpected INIT-ACK handler.
1673 *
1674 * Section 5.2.3
1675 * If an INIT ACK received by an endpoint in any state other than the
1676 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1677 * An unexpected INIT ACK usually indicates the processing of an old or
1678 * duplicated INIT chunk.
1679*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001680sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1681 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001682 const struct sctp_association *asoc,
1683 const sctp_subtype_t type,
1684 void *arg, sctp_cmd_seq_t *commands)
1685{
1686 /* Per the above section, we'll discard the chunk if we have an
1687 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1688 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001689 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001690 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001691 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001692 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001693}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1696 *
1697 * Section 5.2.4
1698 * A) In this case, the peer may have restarted.
1699 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001700static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1701 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 const struct sctp_association *asoc,
1703 struct sctp_chunk *chunk,
1704 sctp_cmd_seq_t *commands,
1705 struct sctp_association *new_asoc)
1706{
1707 sctp_init_chunk_t *peer_init;
1708 struct sctp_ulpevent *ev;
1709 struct sctp_chunk *repl;
1710 struct sctp_chunk *err;
1711 sctp_disposition_t disposition;
1712
1713 /* new_asoc is a brand-new association, so these are not yet
1714 * side effects--it is safe to run them here.
1715 */
1716 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1717
Wei Yongjunde6becd2011-04-19 21:30:51 +00001718 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 GFP_ATOMIC))
1720 goto nomem;
1721
1722 /* Make sure no new addresses are being added during the
1723 * restart. Though this is a pretty complicated attack
1724 * since you'd have to get inside the cookie.
1725 */
1726 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1727 return SCTP_DISPOSITION_CONSUME;
1728 }
1729
1730 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1731 * the peer has restarted (Action A), it MUST NOT setup a new
1732 * association but instead resend the SHUTDOWN ACK and send an ERROR
1733 * chunk with a "Cookie Received while Shutting Down" error cause to
1734 * its peer.
1735 */
1736 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001737 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1739 chunk, commands);
1740 if (SCTP_DISPOSITION_NOMEM == disposition)
1741 goto nomem;
1742
1743 err = sctp_make_op_error(asoc, chunk,
1744 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001745 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 if (err)
1747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1748 SCTP_CHUNK(err));
1749
1750 return SCTP_DISPOSITION_CONSUME;
1751 }
1752
Wei Yongjuna000c012011-05-29 23:23:36 +00001753 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1754 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001756 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1757 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1758 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1760
Wei Yongjuna000c012011-05-29 23:23:36 +00001761 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1762 * and ASCONF-ACK cache.
1763 */
1764 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1765 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1766 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 repl = sctp_make_cookie_ack(new_asoc, chunk);
1769 if (!repl)
1770 goto nomem;
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* Report association restart to upper layer. */
1773 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1774 new_asoc->c.sinit_num_ostreams,
1775 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001776 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (!ev)
1778 goto nomem_ev;
1779
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001780 /* Update the content of current association. */
1781 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1782 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1784 return SCTP_DISPOSITION_CONSUME;
1785
1786nomem_ev:
1787 sctp_chunk_free(repl);
1788nomem:
1789 return SCTP_DISPOSITION_NOMEM;
1790}
1791
1792/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1793 *
1794 * Section 5.2.4
1795 * B) In this case, both sides may be attempting to start an association
1796 * at about the same time but the peer endpoint started its INIT
1797 * after responding to the local endpoint's INIT
1798 */
1799/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001800static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1801 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 const struct sctp_association *asoc,
1803 struct sctp_chunk *chunk,
1804 sctp_cmd_seq_t *commands,
1805 struct sctp_association *new_asoc)
1806{
1807 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct sctp_chunk *repl;
1809
1810 /* new_asoc is a brand-new association, so these are not yet
1811 * side effects--it is safe to run them here.
1812 */
1813 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001814 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 GFP_ATOMIC))
1816 goto nomem;
1817
1818 /* Update the content of current association. */
1819 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1820 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1821 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001822 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1824
1825 repl = sctp_make_cookie_ack(new_asoc, chunk);
1826 if (!repl)
1827 goto nomem;
1828
1829 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 /* RFC 2960 5.1 Normal Establishment of an Association
1832 *
1833 * D) IMPLEMENTATION NOTE: An implementation may choose to
1834 * send the Communication Up notification to the SCTP user
1835 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001836 *
1837 * Sadly, this needs to be implemented as a side-effect, because
1838 * we are not guaranteed to have set the association id of the real
1839 * association and so these notifications need to be delayed until
1840 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Vlad Yasevich07d93962007-05-04 13:55:27 -07001843 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001846 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001848 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001849 *
1850 * This also needs to be done as a side effect for the same reason as
1851 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001853 if (asoc->peer.adaptation_ind)
1854 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 return SCTP_DISPOSITION_CONSUME;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858nomem:
1859 return SCTP_DISPOSITION_NOMEM;
1860}
1861
1862/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1863 *
1864 * Section 5.2.4
1865 * C) In this case, the local endpoint's cookie has arrived late.
1866 * Before it arrived, the local endpoint sent an INIT and received an
1867 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1868 * but a new tag of its own.
1869 */
1870/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001871static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1872 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 const struct sctp_association *asoc,
1874 struct sctp_chunk *chunk,
1875 sctp_cmd_seq_t *commands,
1876 struct sctp_association *new_asoc)
1877{
1878 /* The cookie should be silently discarded.
1879 * The endpoint SHOULD NOT change states and should leave
1880 * any timers running.
1881 */
1882 return SCTP_DISPOSITION_DISCARD;
1883}
1884
1885/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1886 *
1887 * Section 5.2.4
1888 *
1889 * D) When both local and remote tags match the endpoint should always
1890 * enter the ESTABLISHED state, if it has not already done so.
1891 */
1892/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001893static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1894 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 const struct sctp_association *asoc,
1896 struct sctp_chunk *chunk,
1897 sctp_cmd_seq_t *commands,
1898 struct sctp_association *new_asoc)
1899{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001900 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct sctp_chunk *repl;
1902
1903 /* Clarification from Implementor's Guide:
1904 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001905 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1906 * It should stop any cookie timer that may be running and send
1907 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 */
1909
1910 /* Don't accidentally move back into established state. */
1911 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1912 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1913 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1914 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1915 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001916 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1918 SCTP_NULL());
1919
1920 /* RFC 2960 5.1 Normal Establishment of an Association
1921 *
1922 * D) IMPLEMENTATION NOTE: An implementation may choose
1923 * to send the Communication Up notification to the
1924 * SCTP user upon reception of a valid COOKIE
1925 * ECHO chunk.
1926 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001927 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001929 asoc->c.sinit_num_ostreams,
1930 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001931 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (!ev)
1933 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001936 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001938 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001940 if (asoc->peer.adaptation_ind) {
1941 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001943 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 goto nomem;
1945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 repl = sctp_make_cookie_ack(new_asoc, chunk);
1950 if (!repl)
1951 goto nomem;
1952
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001953 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1954
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001955 if (ev)
1956 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1957 SCTP_ULPEVENT(ev));
1958 if (ai_ev)
1959 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1960 SCTP_ULPEVENT(ai_ev));
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return SCTP_DISPOSITION_CONSUME;
1963
1964nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001965 if (ai_ev)
1966 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (ev)
1968 sctp_ulpevent_free(ev);
1969 return SCTP_DISPOSITION_NOMEM;
1970}
1971
1972/*
1973 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1974 * chunk was retransmitted and then delayed in the network.
1975 *
1976 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1977 *
1978 * Verification Tag: None. Do cookie validation.
1979 *
1980 * Inputs
1981 * (endpoint, asoc, chunk)
1982 *
1983 * Outputs
1984 * (asoc, reply_msg, msg_up, timers, counters)
1985 *
1986 * The return value is the disposition of the chunk.
1987 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001988sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1989 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 const struct sctp_association *asoc,
1991 const sctp_subtype_t type,
1992 void *arg,
1993 sctp_cmd_seq_t *commands)
1994{
1995 sctp_disposition_t retval;
1996 struct sctp_chunk *chunk = arg;
1997 struct sctp_association *new_asoc;
1998 int error = 0;
1999 char action;
2000 struct sctp_chunk *err_chk_p;
2001
2002 /* Make sure that the chunk has a valid length from the protocol
2003 * perspective. In this case check to make sure we have at least
2004 * enough for the chunk header. Cookie length verification is
2005 * done later.
2006 */
2007 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002008 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 commands);
2010
2011 /* "Decode" the chunk. We have no optional parameters so we
2012 * are in good shape.
2013 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002014 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002015 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2016 sizeof(sctp_chunkhdr_t)))
2017 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2020 * of a duplicate COOKIE ECHO match the Verification Tags of the
2021 * current association, consider the State Cookie valid even if
2022 * the lifespan is exceeded.
2023 */
2024 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2025 &err_chk_p);
2026
2027 /* FIXME:
2028 * If the re-build failed, what is the proper error path
2029 * from here?
2030 *
2031 * [We should abort the association. --piggy]
2032 */
2033 if (!new_asoc) {
2034 /* FIXME: Several errors are possible. A bad cookie should
2035 * be silently discarded, but think about logging it too.
2036 */
2037 switch (error) {
2038 case -SCTP_IERROR_NOMEM:
2039 goto nomem;
2040
2041 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002042 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002044 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 case -SCTP_IERROR_BAD_SIG:
2046 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002047 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050
2051 /* Compare the tie_tag in cookie with the verification tag of
2052 * current association.
2053 */
2054 action = sctp_tietags_compare(new_asoc, asoc);
2055
2056 switch (action) {
2057 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002058 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 new_asoc);
2060 break;
2061
2062 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002063 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 new_asoc);
2065 break;
2066
2067 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002068 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 new_asoc);
2070 break;
2071
2072 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002073 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 new_asoc);
2075 break;
2076
2077 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002078 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 /* Delete the tempory new association. */
2083 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2084 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2085
Max Matveevd5ccd492011-08-29 21:02:24 +00002086 /* Restore association pointer to provide SCTP command interpeter
2087 * with a valid context in case it needs to manipulate
2088 * the queues */
2089 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2090 SCTP_ASOC((struct sctp_association *)asoc));
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return retval;
2093
2094nomem:
2095 return SCTP_DISPOSITION_NOMEM;
2096}
2097
2098/*
2099 * Process an ABORT. (SHUTDOWN-PENDING state)
2100 *
2101 * See sctp_sf_do_9_1_abort().
2102 */
2103sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002104 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 const struct sctp_endpoint *ep,
2106 const struct sctp_association *asoc,
2107 const sctp_subtype_t type,
2108 void *arg,
2109 sctp_cmd_seq_t *commands)
2110{
2111 struct sctp_chunk *chunk = arg;
2112
2113 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002114 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 /* Make sure that the ABORT chunk has a valid length.
2117 * Since this is an ABORT chunk, we have to discard it
2118 * because of the following text:
2119 * RFC 2960, Section 3.3.7
2120 * If an endpoint receives an ABORT with a format error or for an
2121 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002122 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 * as we do not know its true length. So, to be safe, discard the
2124 * packet.
2125 */
2126 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002127 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Vlad Yasevich75205f42007-12-20 14:12:59 -08002129 /* ADD-IP: Special case for ABORT chunks
2130 * F4) One special consideration is that ABORT Chunks arriving
2131 * destined to the IP address being deleted MUST be
2132 * ignored (see Section 5.3.1 for further details).
2133 */
2134 if (SCTP_ADDR_DEL ==
2135 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002136 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002137
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002138 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
2141/*
2142 * Process an ABORT. (SHUTDOWN-SENT state)
2143 *
2144 * See sctp_sf_do_9_1_abort().
2145 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002146sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2147 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 const struct sctp_association *asoc,
2149 const sctp_subtype_t type,
2150 void *arg,
2151 sctp_cmd_seq_t *commands)
2152{
2153 struct sctp_chunk *chunk = arg;
2154
2155 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002156 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* Make sure that the ABORT chunk has a valid length.
2159 * Since this is an ABORT chunk, we have to discard it
2160 * because of the following text:
2161 * RFC 2960, Section 3.3.7
2162 * If an endpoint receives an ABORT with a format error or for an
2163 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002164 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * as we do not know its true length. So, to be safe, discard the
2166 * packet.
2167 */
2168 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002169 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Vlad Yasevich75205f42007-12-20 14:12:59 -08002171 /* ADD-IP: Special case for ABORT chunks
2172 * F4) One special consideration is that ABORT Chunks arriving
2173 * destined to the IP address being deleted MUST be
2174 * ignored (see Section 5.3.1 for further details).
2175 */
2176 if (SCTP_ADDR_DEL ==
2177 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002178 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 /* Stop the T2-shutdown timer. */
2181 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2182 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2183
2184 /* Stop the T5-shutdown guard timer. */
2185 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2186 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2187
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002188 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
2191/*
2192 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2193 *
2194 * See sctp_sf_do_9_1_abort().
2195 */
2196sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002197 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 const struct sctp_endpoint *ep,
2199 const struct sctp_association *asoc,
2200 const sctp_subtype_t type,
2201 void *arg,
2202 sctp_cmd_seq_t *commands)
2203{
2204 /* The same T2 timer, so we should be able to use
2205 * common function with the SHUTDOWN-SENT state.
2206 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002207 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
2210/*
2211 * Handle an Error received in COOKIE_ECHOED state.
2212 *
2213 * Only handle the error type of stale COOKIE Error, the other errors will
2214 * be ignored.
2215 *
2216 * Inputs
2217 * (endpoint, asoc, chunk)
2218 *
2219 * Outputs
2220 * (asoc, reply_msg, msg_up, timers, counters)
2221 *
2222 * The return value is the disposition of the chunk.
2223 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002224sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2225 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 const struct sctp_association *asoc,
2227 const sctp_subtype_t type,
2228 void *arg,
2229 sctp_cmd_seq_t *commands)
2230{
2231 struct sctp_chunk *chunk = arg;
2232 sctp_errhdr_t *err;
2233
2234 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002235 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /* Make sure that the ERROR chunk has a valid length.
2238 * The parameter walking depends on this as well.
2239 */
2240 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002241 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 commands);
2243
2244 /* Process the error here */
2245 /* FUTURE FIXME: When PR-SCTP related and other optional
2246 * parms are emitted, this will have to change to handle multiple
2247 * errors.
2248 */
2249 sctp_walk_errors(err, chunk->chunk_hdr) {
2250 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002251 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 arg, commands);
2253 }
2254
2255 /* It is possible to have malformed error causes, and that
2256 * will cause us to end the walk early. However, since
2257 * we are discarding the packet, there should be no adverse
2258 * affects.
2259 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002260 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
2262
2263/*
2264 * Handle a Stale COOKIE Error
2265 *
2266 * Section: 5.2.6 Handle Stale COOKIE Error
2267 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2268 * one of the following three alternatives.
2269 * ...
2270 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2271 * Preservative parameter requesting an extension to the lifetime of
2272 * the State Cookie. When calculating the time extension, an
2273 * implementation SHOULD use the RTT information measured based on the
2274 * previous COOKIE ECHO / ERROR exchange, and should add no more
2275 * than 1 second beyond the measured RTT, due to long State Cookie
2276 * lifetimes making the endpoint more subject to a replay attack.
2277 *
2278 * Verification Tag: Not explicit, but safe to ignore.
2279 *
2280 * Inputs
2281 * (endpoint, asoc, chunk)
2282 *
2283 * Outputs
2284 * (asoc, reply_msg, msg_up, timers, counters)
2285 *
2286 * The return value is the disposition of the chunk.
2287 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002288static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2289 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 const struct sctp_association *asoc,
2291 const sctp_subtype_t type,
2292 void *arg,
2293 sctp_cmd_seq_t *commands)
2294{
2295 struct sctp_chunk *chunk = arg;
2296 time_t stale;
2297 sctp_cookie_preserve_param_t bht;
2298 sctp_errhdr_t *err;
2299 struct sctp_chunk *reply;
2300 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002301 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Vlad Yasevich81845c22006-01-30 15:59:54 -08002303 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002304 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2305 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002307 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return SCTP_DISPOSITION_DELETE_TCB;
2309 }
2310
2311 err = (sctp_errhdr_t *)(chunk->skb->data);
2312
2313 /* When calculating the time extension, an implementation
2314 * SHOULD use the RTT information measured based on the
2315 * previous COOKIE ECHO / ERROR exchange, and should add no
2316 * more than 1 second beyond the measured RTT, due to long
2317 * State Cookie lifetimes making the endpoint more subject to
2318 * a replay attack.
2319 * Measure of Staleness's unit is usec. (1/1000000 sec)
2320 * Suggested Cookie Life-span Increment's unit is msec.
2321 * (1/1000 sec)
2322 * In general, if you use the suggested cookie life, the value
2323 * found in the field of measure of staleness should be doubled
2324 * to give ample time to retransmit the new cookie and thus
2325 * yield a higher probability of success on the reattempt.
2326 */
Al Viro34bcca22006-11-20 17:27:15 -08002327 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 stale = (stale * 2) / 1000;
2329
2330 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2331 bht.param_hdr.length = htons(sizeof(bht));
2332 bht.lifespan_increment = htonl(stale);
2333
2334 /* Build that new INIT chunk. */
2335 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2336 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2337 if (!reply)
2338 goto nomem;
2339
2340 sctp_addto_chunk(reply, sizeof(bht), &bht);
2341
2342 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2343 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2344
2345 /* Stop pending T3-rtx and heartbeat timers */
2346 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2347 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2348
2349 /* Delete non-primary peer ip addresses since we are transitioning
2350 * back to the COOKIE-WAIT state
2351 */
2352 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2353
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002354 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2355 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002357 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 SCTP_TRANSPORT(asoc->peer.primary_path));
2359
2360 /* Cast away the const modifier, as we want to just
2361 * rerun it through as a sideffect.
2362 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002363 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2366 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2367 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2368 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2369 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2370 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2371
2372 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2373
2374 return SCTP_DISPOSITION_CONSUME;
2375
2376nomem:
2377 return SCTP_DISPOSITION_NOMEM;
2378}
2379
2380/*
2381 * Process an ABORT.
2382 *
2383 * Section: 9.1
2384 * After checking the Verification Tag, the receiving endpoint shall
2385 * remove the association from its record, and shall report the
2386 * termination to its upper layer.
2387 *
2388 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2389 * B) Rules for packet carrying ABORT:
2390 *
2391 * - The endpoint shall always fill in the Verification Tag field of the
2392 * outbound packet with the destination endpoint's tag value if it
2393 * is known.
2394 *
2395 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2396 * MUST follow the procedure described in Section 8.4.
2397 *
2398 * - The receiver MUST accept the packet if the Verification Tag
2399 * matches either its own tag, OR the tag of its peer. Otherwise, the
2400 * receiver MUST silently discard the packet and take no further
2401 * action.
2402 *
2403 * Inputs
2404 * (endpoint, asoc, chunk)
2405 *
2406 * Outputs
2407 * (asoc, reply_msg, msg_up, timers, counters)
2408 *
2409 * The return value is the disposition of the chunk.
2410 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002411sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2412 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 const struct sctp_association *asoc,
2414 const sctp_subtype_t type,
2415 void *arg,
2416 sctp_cmd_seq_t *commands)
2417{
2418 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
2420 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002421 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /* Make sure that the ABORT chunk has a valid length.
2424 * Since this is an ABORT chunk, we have to discard it
2425 * because of the following text:
2426 * RFC 2960, Section 3.3.7
2427 * If an endpoint receives an ABORT with a format error or for an
2428 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002429 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 * as we do not know its true length. So, to be safe, discard the
2431 * packet.
2432 */
2433 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002434 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Vlad Yasevich75205f42007-12-20 14:12:59 -08002436 /* ADD-IP: Special case for ABORT chunks
2437 * F4) One special consideration is that ABORT Chunks arriving
2438 * destined to the IP address being deleted MUST be
2439 * ignored (see Section 5.3.1 for further details).
2440 */
2441 if (SCTP_ADDR_DEL ==
2442 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002443 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002444
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002445 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002446}
2447
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002448static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2449 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002450 const struct sctp_association *asoc,
2451 const sctp_subtype_t type,
2452 void *arg,
2453 sctp_cmd_seq_t *commands)
2454{
2455 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002456 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002457 __be16 error = SCTP_ERROR_NO_ERROR;
2458
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* See if we have an error cause code in the chunk. */
2460 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002461 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2462
2463 sctp_errhdr_t *err;
2464 sctp_walk_errors(err, chunk->chunk_hdr);
2465 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002466 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002471 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002472 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002473 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002474 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2475 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 return SCTP_DISPOSITION_ABORT;
2478}
2479
2480/*
2481 * Process an ABORT. (COOKIE-WAIT state)
2482 *
2483 * See sctp_sf_do_9_1_abort() above.
2484 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002485sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2486 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 const struct sctp_association *asoc,
2488 const sctp_subtype_t type,
2489 void *arg,
2490 sctp_cmd_seq_t *commands)
2491{
2492 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002493 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002494 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
2496 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002497 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /* Make sure that the ABORT chunk has a valid length.
2500 * Since this is an ABORT chunk, we have to discard it
2501 * because of the following text:
2502 * RFC 2960, Section 3.3.7
2503 * If an endpoint receives an ABORT with a format error or for an
2504 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002505 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 * as we do not know its true length. So, to be safe, discard the
2507 * packet.
2508 */
2509 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002510 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 /* See if we have an error cause code in the chunk. */
2513 len = ntohs(chunk->chunk_hdr->length);
2514 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2515 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2516
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002517 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002518 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
2521/*
2522 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2523 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002524sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2525 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 const struct sctp_association *asoc,
2527 const sctp_subtype_t type,
2528 void *arg,
2529 sctp_cmd_seq_t *commands)
2530{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002531 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002532 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002533 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
2536/*
2537 * Process an ABORT. (COOKIE-ECHOED state)
2538 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002539sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2540 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 const struct sctp_association *asoc,
2542 const sctp_subtype_t type,
2543 void *arg,
2544 sctp_cmd_seq_t *commands)
2545{
2546 /* There is a single T1 timer, so we should be able to use
2547 * common function with the COOKIE-WAIT state.
2548 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002549 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
2552/*
2553 * Stop T1 timer and abort association with "INIT failed".
2554 *
2555 * This is common code called by several sctp_sf_*_abort() functions above.
2556 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002557static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2558 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002559 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002560 const struct sctp_association *asoc,
2561 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002563 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2565 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002566 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2568 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002569 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /* CMD_INIT_FAILED will DELETE_TCB. */
2571 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002572 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002573 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
2576/*
2577 * sctp_sf_do_9_2_shut
2578 *
2579 * Section: 9.2
2580 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2581 * - enter the SHUTDOWN-RECEIVED state,
2582 *
2583 * - stop accepting new data from its SCTP user
2584 *
2585 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2586 * that all its outstanding DATA chunks have been received by the
2587 * SHUTDOWN sender.
2588 *
2589 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2590 * send a SHUTDOWN in response to a ULP request. And should discard
2591 * subsequent SHUTDOWN chunks.
2592 *
2593 * If there are still outstanding DATA chunks left, the SHUTDOWN
2594 * receiver shall continue to follow normal data transmission
2595 * procedures defined in Section 6 until all outstanding DATA chunks
2596 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2597 * new data from its SCTP user.
2598 *
2599 * Verification Tag: 8.5 Verification Tag [Normal verification]
2600 *
2601 * Inputs
2602 * (endpoint, asoc, chunk)
2603 *
2604 * Outputs
2605 * (asoc, reply_msg, msg_up, timers, counters)
2606 *
2607 * The return value is the disposition of the chunk.
2608 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002609sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2610 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 const struct sctp_association *asoc,
2612 const sctp_subtype_t type,
2613 void *arg,
2614 sctp_cmd_seq_t *commands)
2615{
2616 struct sctp_chunk *chunk = arg;
2617 sctp_shutdownhdr_t *sdh;
2618 sctp_disposition_t disposition;
2619 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002620 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002623 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
2625 /* Make sure that the SHUTDOWN chunk has a valid length. */
2626 if (!sctp_chunk_length_valid(chunk,
2627 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002628 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 commands);
2630
2631 /* Convert the elaborate header. */
2632 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2633 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2634 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002635 ctsn = ntohl(sdh->cum_tsn_ack);
2636
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002637 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2638 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2639 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2640 return SCTP_DISPOSITION_DISCARD;
2641 }
2642
Wei Yongjundf10eec2008-10-23 01:00:21 -07002643 /* If Cumulative TSN Ack beyond the max tsn currently
2644 * send, terminating the association and respond to the
2645 * sender with an ABORT.
2646 */
2647 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002648 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002650 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2651 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2652 * inform the application that it should cease sending data.
2653 */
2654 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2655 if (!ev) {
2656 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002657 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002658 }
2659 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2662 * - enter the SHUTDOWN-RECEIVED state,
2663 * - stop accepting new data from its SCTP user
2664 *
2665 * [This is implicit in the new state.]
2666 */
2667 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2668 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2669 disposition = SCTP_DISPOSITION_CONSUME;
2670
2671 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002672 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 arg, commands);
2674 }
2675
2676 if (SCTP_DISPOSITION_NOMEM == disposition)
2677 goto out;
2678
2679 /* - verify, by checking the Cumulative TSN Ack field of the
2680 * chunk, that all its outstanding DATA chunks have been
2681 * received by the SHUTDOWN sender.
2682 */
2683 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002684 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686out:
2687 return disposition;
2688}
2689
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002690/*
2691 * sctp_sf_do_9_2_shut_ctsn
2692 *
2693 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2694 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2695 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2696 * MUST be processed.
2697 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002698sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2699 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002700 const struct sctp_association *asoc,
2701 const sctp_subtype_t type,
2702 void *arg,
2703 sctp_cmd_seq_t *commands)
2704{
2705 struct sctp_chunk *chunk = arg;
2706 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002707 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002708
2709 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002710 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002711
2712 /* Make sure that the SHUTDOWN chunk has a valid length. */
2713 if (!sctp_chunk_length_valid(chunk,
2714 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002715 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002716 commands);
2717
2718 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002719 ctsn = ntohl(sdh->cum_tsn_ack);
2720
2721 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2722 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2723 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2724 return SCTP_DISPOSITION_DISCARD;
2725 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002726
2727 /* If Cumulative TSN Ack beyond the max tsn currently
2728 * send, terminating the association and respond to the
2729 * sender with an ABORT.
2730 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002731 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002732 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002733
2734 /* verify, by checking the Cumulative TSN Ack field of the
2735 * chunk, that all its outstanding DATA chunks have been
2736 * received by the SHUTDOWN sender.
2737 */
2738 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2739 SCTP_BE32(sdh->cum_tsn_ack));
2740
2741 return SCTP_DISPOSITION_CONSUME;
2742}
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744/* RFC 2960 9.2
2745 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2746 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2747 * transport addresses (either in the IP addresses or in the INIT chunk)
2748 * that belong to this association, it should discard the INIT chunk and
2749 * retransmit the SHUTDOWN ACK chunk.
2750 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002751sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2752 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 const struct sctp_association *asoc,
2754 const sctp_subtype_t type,
2755 void *arg,
2756 sctp_cmd_seq_t *commands)
2757{
2758 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2759 struct sctp_chunk *reply;
2760
Vlad Yasevichece25df2007-09-07 16:30:54 -04002761 /* Make sure that the chunk has a valid length */
2762 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002763 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002764 commands);
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 /* Since we are not going to really process this INIT, there
2767 * is no point in verifying chunk boundries. Just generate
2768 * the SHUTDOWN ACK.
2769 */
2770 reply = sctp_make_shutdown_ack(asoc, chunk);
2771 if (NULL == reply)
2772 goto nomem;
2773
2774 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2775 * the T2-SHUTDOWN timer.
2776 */
2777 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2778
2779 /* and restart the T2-shutdown timer. */
2780 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2781 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2782
2783 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2784
2785 return SCTP_DISPOSITION_CONSUME;
2786nomem:
2787 return SCTP_DISPOSITION_NOMEM;
2788}
2789
2790/*
2791 * sctp_sf_do_ecn_cwr
2792 *
2793 * Section: Appendix A: Explicit Congestion Notification
2794 *
2795 * CWR:
2796 *
2797 * RFC 2481 details a specific bit for a sender to send in the header of
2798 * its next outbound TCP segment to indicate to its peer that it has
2799 * reduced its congestion window. This is termed the CWR bit. For
2800 * SCTP the same indication is made by including the CWR chunk.
2801 * This chunk contains one data element, i.e. the TSN number that
2802 * was sent in the ECNE chunk. This element represents the lowest
2803 * TSN number in the datagram that was originally marked with the
2804 * CE bit.
2805 *
2806 * Verification Tag: 8.5 Verification Tag [Normal verification]
2807 * Inputs
2808 * (endpoint, asoc, chunk)
2809 *
2810 * Outputs
2811 * (asoc, reply_msg, msg_up, timers, counters)
2812 *
2813 * The return value is the disposition of the chunk.
2814 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002815sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2816 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 const struct sctp_association *asoc,
2818 const sctp_subtype_t type,
2819 void *arg,
2820 sctp_cmd_seq_t *commands)
2821{
2822 sctp_cwrhdr_t *cwr;
2823 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002824 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
2826 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002827 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002830 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2834 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2835
Al Viro34bcca22006-11-20 17:27:15 -08002836 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002839 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 /* Stop sending ECNE. */
2841 sctp_add_cmd_sf(commands,
2842 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002843 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845 return SCTP_DISPOSITION_CONSUME;
2846}
2847
2848/*
2849 * sctp_sf_do_ecne
2850 *
2851 * Section: Appendix A: Explicit Congestion Notification
2852 *
2853 * ECN-Echo
2854 *
2855 * RFC 2481 details a specific bit for a receiver to send back in its
2856 * TCP acknowledgements to notify the sender of the Congestion
2857 * Experienced (CE) bit having arrived from the network. For SCTP this
2858 * same indication is made by including the ECNE chunk. This chunk
2859 * contains one data element, i.e. the lowest TSN associated with the IP
2860 * datagram marked with the CE bit.....
2861 *
2862 * Verification Tag: 8.5 Verification Tag [Normal verification]
2863 * Inputs
2864 * (endpoint, asoc, chunk)
2865 *
2866 * Outputs
2867 * (asoc, reply_msg, msg_up, timers, counters)
2868 *
2869 * The return value is the disposition of the chunk.
2870 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002871sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2872 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 const struct sctp_association *asoc,
2874 const sctp_subtype_t type,
2875 void *arg,
2876 sctp_cmd_seq_t *commands)
2877{
2878 sctp_ecnehdr_t *ecne;
2879 struct sctp_chunk *chunk = arg;
2880
2881 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002882 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002885 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 commands);
2887
2888 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2889 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2890
2891 /* If this is a newer ECNE than the last CWR packet we sent out */
2892 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2893 SCTP_U32(ntohl(ecne->lowest_tsn)));
2894
2895 return SCTP_DISPOSITION_CONSUME;
2896}
2897
2898/*
2899 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2900 *
2901 * The SCTP endpoint MUST always acknowledge the reception of each valid
2902 * DATA chunk.
2903 *
2904 * The guidelines on delayed acknowledgement algorithm specified in
2905 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2906 * acknowledgement SHOULD be generated for at least every second packet
2907 * (not every second DATA chunk) received, and SHOULD be generated within
2908 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2909 * situations it may be beneficial for an SCTP transmitter to be more
2910 * conservative than the algorithms detailed in this document allow.
2911 * However, an SCTP transmitter MUST NOT be more aggressive than the
2912 * following algorithms allow.
2913 *
2914 * A SCTP receiver MUST NOT generate more than one SACK for every
2915 * incoming packet, other than to update the offered window as the
2916 * receiving application consumes new data.
2917 *
2918 * Verification Tag: 8.5 Verification Tag [Normal verification]
2919 *
2920 * Inputs
2921 * (endpoint, asoc, chunk)
2922 *
2923 * Outputs
2924 * (asoc, reply_msg, msg_up, timers, counters)
2925 *
2926 * The return value is the disposition of the chunk.
2927 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002928sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2929 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 const struct sctp_association *asoc,
2931 const sctp_subtype_t type,
2932 void *arg,
2933 sctp_cmd_seq_t *commands)
2934{
2935 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002936 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 int error;
2938
2939 if (!sctp_vtag_verify(chunk, asoc)) {
2940 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2941 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002942 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
2945 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002946 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 commands);
2948
2949 error = sctp_eat_data(asoc, chunk, commands );
2950 switch (error) {
2951 case SCTP_IERROR_NO_ERROR:
2952 break;
2953 case SCTP_IERROR_HIGH_TSN:
2954 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002955 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto discard_noforce;
2957 case SCTP_IERROR_DUP_TSN:
2958 case SCTP_IERROR_IGNORE_TSN:
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_force;
2961 case SCTP_IERROR_NO_DATA:
2962 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002963 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002964 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002965 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 default:
2967 BUG();
2968 }
2969
Wei Yongjun6dc76942009-11-23 15:53:53 -05002970 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2971 force = SCTP_FORCE();
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (asoc->autoclose) {
2974 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2975 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2976 }
2977
2978 /* If this is the last chunk in a packet, we need to count it
2979 * toward sack generation. Note that we need to SACK every
2980 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2981 * THEM. We elect to NOT generate SACK's if the chunk fails
2982 * the verification tag test.
2983 *
2984 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2985 *
2986 * The SCTP endpoint MUST always acknowledge the reception of
2987 * each valid DATA chunk.
2988 *
2989 * The guidelines on delayed acknowledgement algorithm
2990 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2991 * Specifically, an acknowledgement SHOULD be generated for at
2992 * least every second packet (not every second DATA chunk)
2993 * received, and SHOULD be generated within 200 ms of the
2994 * arrival of any unacknowledged DATA chunk. In some
2995 * situations it may be beneficial for an SCTP transmitter to
2996 * be more conservative than the algorithms detailed in this
2997 * document allow. However, an SCTP transmitter MUST NOT be
2998 * more aggressive than the following algorithms allow.
2999 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08003000 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003001 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return SCTP_DISPOSITION_CONSUME;
3004
3005discard_force:
3006 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3007 *
3008 * When a packet arrives with duplicate DATA chunk(s) and with
3009 * no new DATA chunk(s), the endpoint MUST immediately send a
3010 * SACK with no delay. If a packet arrives with duplicate
3011 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3012 * MAY immediately send a SACK. Normally receipt of duplicate
3013 * DATA chunks will occur when the original SACK chunk was lost
3014 * and the peer's RTO has expired. The duplicate TSN number(s)
3015 * SHOULD be reported in the SACK as duplicate.
3016 */
3017 /* In our case, we split the MAY SACK advice up whether or not
3018 * the last chunk is a duplicate.'
3019 */
3020 if (chunk->end_of_packet)
3021 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3022 return SCTP_DISPOSITION_DISCARD;
3023
3024discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003025 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003026 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return SCTP_DISPOSITION_DISCARD;
3029consume:
3030 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003031
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
3034/*
3035 * sctp_sf_eat_data_fast_4_4
3036 *
3037 * Section: 4 (4)
3038 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3039 * DATA chunks without delay.
3040 *
3041 * Verification Tag: 8.5 Verification Tag [Normal verification]
3042 * Inputs
3043 * (endpoint, asoc, chunk)
3044 *
3045 * Outputs
3046 * (asoc, reply_msg, msg_up, timers, counters)
3047 *
3048 * The return value is the disposition of the chunk.
3049 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003050sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3051 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 const struct sctp_association *asoc,
3053 const sctp_subtype_t type,
3054 void *arg,
3055 sctp_cmd_seq_t *commands)
3056{
3057 struct sctp_chunk *chunk = arg;
3058 int error;
3059
3060 if (!sctp_vtag_verify(chunk, asoc)) {
3061 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3062 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003063 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065
3066 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003067 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 commands);
3069
3070 error = sctp_eat_data(asoc, chunk, commands );
3071 switch (error) {
3072 case SCTP_IERROR_NO_ERROR:
3073 case SCTP_IERROR_HIGH_TSN:
3074 case SCTP_IERROR_DUP_TSN:
3075 case SCTP_IERROR_IGNORE_TSN:
3076 case SCTP_IERROR_BAD_STREAM:
3077 break;
3078 case SCTP_IERROR_NO_DATA:
3079 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003080 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003081 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003082 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 default:
3084 BUG();
3085 }
3086
3087 /* Go a head and force a SACK, since we are shutting down. */
3088
3089 /* Implementor's Guide.
3090 *
3091 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3092 * respond to each received packet containing one or more DATA chunk(s)
3093 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3094 */
3095 if (chunk->end_of_packet) {
3096 /* We must delay the chunk creation since the cumulative
3097 * TSN has not been updated yet.
3098 */
3099 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3100 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3101 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3102 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3103 }
3104
3105consume:
3106 return SCTP_DISPOSITION_CONSUME;
3107}
3108
3109/*
3110 * Section: 6.2 Processing a Received SACK
3111 * D) Any time a SACK arrives, the endpoint performs the following:
3112 *
3113 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3114 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3115 * increasing, a SACK whose Cumulative TSN Ack is less than the
3116 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3117 *
3118 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3119 * of bytes still outstanding after processing the Cumulative TSN Ack
3120 * and the Gap Ack Blocks.
3121 *
3122 * iii) If the SACK is missing a TSN that was previously
3123 * acknowledged via a Gap Ack Block (e.g., the data receiver
3124 * reneged on the data), then mark the corresponding DATA chunk
3125 * as available for retransmit: Mark it as missing for fast
3126 * retransmit as described in Section 7.2.4 and if no retransmit
3127 * timer is running for the destination address to which the DATA
3128 * chunk was originally transmitted, then T3-rtx is started for
3129 * that destination address.
3130 *
3131 * Verification Tag: 8.5 Verification Tag [Normal verification]
3132 *
3133 * Inputs
3134 * (endpoint, asoc, chunk)
3135 *
3136 * Outputs
3137 * (asoc, reply_msg, msg_up, timers, counters)
3138 *
3139 * The return value is the disposition of the chunk.
3140 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003141sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3142 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 const struct sctp_association *asoc,
3144 const sctp_subtype_t type,
3145 void *arg,
3146 sctp_cmd_seq_t *commands)
3147{
3148 struct sctp_chunk *chunk = arg;
3149 sctp_sackhdr_t *sackh;
3150 __u32 ctsn;
3151
3152 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003153 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 /* Make sure that the SACK chunk has a valid length. */
3156 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003157 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 commands);
3159
3160 /* Pull the SACK chunk from the data buffer */
3161 sackh = sctp_sm_pull_sack(chunk);
3162 /* Was this a bogus SACK? */
3163 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003164 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 chunk->subh.sack_hdr = sackh;
3166 ctsn = ntohl(sackh->cum_tsn_ack);
3167
3168 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3169 * Ack Point, then drop the SACK. Since Cumulative TSN
3170 * Ack is monotonically increasing, a SACK whose
3171 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3172 * Point indicates an out-of-order SACK.
3173 */
3174 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3175 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3176 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3177 return SCTP_DISPOSITION_DISCARD;
3178 }
3179
Wei Yongjunaecedea2007-08-02 16:57:44 +08003180 /* If Cumulative TSN Ack beyond the max tsn currently
3181 * send, terminating the association and respond to the
3182 * sender with an ABORT.
3183 */
3184 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003185 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /* Return this SACK for further processing. */
Nicolas Dichteledfee032012-10-03 05:43:22 +00003188 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 /* Note: We do the rest of the work on the PROCESS_SACK
3191 * sideeffect.
3192 */
3193 return SCTP_DISPOSITION_CONSUME;
3194}
3195
3196/*
3197 * Generate an ABORT in response to a packet.
3198 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003199 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003201 * 8) The receiver should respond to the sender of the OOTB packet with
3202 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3203 * MUST fill in the Verification Tag field of the outbound packet
3204 * with the value found in the Verification Tag field of the OOTB
3205 * packet and set the T-bit in the Chunk Flags to indicate that the
3206 * Verification Tag is reflected. After sending this ABORT, the
3207 * receiver of the OOTB packet shall discard the OOTB packet and take
3208 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 *
3210 * Verification Tag:
3211 *
3212 * The return value is the disposition of the chunk.
3213*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003214static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3215 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 const struct sctp_association *asoc,
3217 const sctp_subtype_t type,
3218 void *arg,
3219 sctp_cmd_seq_t *commands)
3220{
3221 struct sctp_packet *packet = NULL;
3222 struct sctp_chunk *chunk = arg;
3223 struct sctp_chunk *abort;
3224
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003225 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
3227 if (packet) {
3228 /* Make an ABORT. The T bit will be set if the asoc
3229 * is NULL.
3230 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003231 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (!abort) {
3233 sctp_ootb_pkt_free(packet);
3234 return SCTP_DISPOSITION_NOMEM;
3235 }
3236
Jerome Forissier047a2422005-04-28 11:58:43 -07003237 /* Reflect vtag if T-Bit is set */
3238 if (sctp_test_T_bit(abort))
3239 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 /* Set the skb to the belonging sock for accounting. */
3242 abort->skb->sk = ep->base.sk;
3243
3244 sctp_packet_append_chunk(packet, abort);
3245
3246 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3247 SCTP_PACKET(packet));
3248
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003249 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003251 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return SCTP_DISPOSITION_CONSUME;
3253 }
3254
3255 return SCTP_DISPOSITION_NOMEM;
3256}
3257
3258/*
3259 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3260 * event as ULP notification for each cause included in the chunk.
3261 *
3262 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3263 *
3264 * The return value is the disposition of the chunk.
3265*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003266sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3267 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 const struct sctp_association *asoc,
3269 const sctp_subtype_t type,
3270 void *arg,
3271 sctp_cmd_seq_t *commands)
3272{
3273 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003274 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003277 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
3279 /* Make sure that the ERROR chunk has a valid length. */
3280 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003281 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003283 sctp_walk_errors(err, chunk->chunk_hdr);
3284 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003285 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003286 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Wei Yongjun3df26782009-03-02 06:46:51 +00003288 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3289 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294/*
3295 * Process an inbound SHUTDOWN ACK.
3296 *
3297 * From Section 9.2:
3298 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3299 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3300 * peer, and remove all record of the association.
3301 *
3302 * The return value is the disposition.
3303 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003304sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3305 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 const struct sctp_association *asoc,
3307 const sctp_subtype_t type,
3308 void *arg,
3309 sctp_cmd_seq_t *commands)
3310{
3311 struct sctp_chunk *chunk = arg;
3312 struct sctp_chunk *reply;
3313 struct sctp_ulpevent *ev;
3314
3315 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317
3318 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3319 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003320 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /* 10.2 H) SHUTDOWN COMPLETE notification
3323 *
3324 * When SCTP completes the shutdown procedures (section 9.2) this
3325 * notification is passed to the upper layer.
3326 */
3327 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003328 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (!ev)
3330 goto nomem;
3331
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003332 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3333 reply = sctp_make_shutdown_complete(asoc, chunk);
3334 if (!reply)
3335 goto nomem_chunk;
3336
3337 /* Do all the commands now (after allocation), so that we
3338 * have consistent state if memory allocation failes
3339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3341
3342 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3343 * stop the T2-shutdown timer,
3344 */
3345 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3346 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3347
3348 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3349 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3352 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003353 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3354 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3356
3357 /* ...and remove all record of the association. */
3358 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3359 return SCTP_DISPOSITION_DELETE_TCB;
3360
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003361nomem_chunk:
3362 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363nomem:
3364 return SCTP_DISPOSITION_NOMEM;
3365}
3366
3367/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003368 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3369 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3371 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3372 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3373 * packet must fill in the Verification Tag field of the outbound
3374 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003375 * set the T-bit in the Chunk Flags to indicate that the Verification
3376 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 *
3378 * 8) The receiver should respond to the sender of the OOTB packet with
3379 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3380 * MUST fill in the Verification Tag field of the outbound packet
3381 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003382 * packet and set the T-bit in the Chunk Flags to indicate that the
3383 * Verification Tag is reflected. After sending this ABORT, the
3384 * receiver of the OOTB packet shall discard the OOTB packet and take
3385 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003387sctp_disposition_t sctp_sf_ootb(struct net *net,
3388 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 const struct sctp_association *asoc,
3390 const sctp_subtype_t type,
3391 void *arg,
3392 sctp_cmd_seq_t *commands)
3393{
3394 struct sctp_chunk *chunk = arg;
3395 struct sk_buff *skb = chunk->skb;
3396 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003397 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 __u8 *ch_end;
3399 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003400 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003402 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3405 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003406 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003408 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003409 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Vlad Yasevichece25df2007-09-07 16:30:54 -04003411 /* Now that we know we at least have a chunk header,
3412 * do things that are type appropriate.
3413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3415 ootb_shut_ack = 1;
3416
3417 /* RFC 2960, Section 3.3.7
3418 * Moreover, under any circumstances, an endpoint that
3419 * receives an ABORT MUST NOT respond to that ABORT by
3420 * sending an ABORT of its own.
3421 */
3422 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003423 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003424
Shan Wei85c5ed42011-04-19 21:30:01 +00003425 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3426 * or a COOKIE ACK the SCTP Packet should be silently
3427 * discarded.
3428 */
3429
3430 if (SCTP_CID_COOKIE_ACK == ch->type)
3431 ootb_cookie_ack = 1;
3432
3433 if (SCTP_CID_ERROR == ch->type) {
3434 sctp_walk_errors(err, ch) {
3435 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3436 ootb_cookie_ack = 1;
3437 break;
3438 }
3439 }
3440 }
3441
Vlad Yasevichece25df2007-09-07 16:30:54 -04003442 /* Report violation if chunk len overflows */
3443 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3444 if (ch_end > skb_tail_pointer(skb))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003445 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003446 commands);
3447
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003449 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450
3451 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003452 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003453 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003454 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003456 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
3459/*
3460 * Handle an "Out of the blue" SHUTDOWN ACK.
3461 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003462 * Section: 8.4 5, sctpimpguide 2.41.
3463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003465 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3466 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3467 * packet must fill in the Verification Tag field of the outbound
3468 * packet with the Verification Tag received in the SHUTDOWN ACK and
3469 * set the T-bit in the Chunk Flags to indicate that the Verification
3470 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 *
3472 * Inputs
3473 * (endpoint, asoc, type, arg, commands)
3474 *
3475 * Outputs
3476 * (sctp_disposition_t)
3477 *
3478 * The return value is the disposition of the chunk.
3479 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003480static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3481 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 const struct sctp_association *asoc,
3483 const sctp_subtype_t type,
3484 void *arg,
3485 sctp_cmd_seq_t *commands)
3486{
3487 struct sctp_packet *packet = NULL;
3488 struct sctp_chunk *chunk = arg;
3489 struct sctp_chunk *shut;
3490
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003491 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 if (packet) {
3494 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003495 * The T bit will be set if the asoc is NULL.
3496 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 shut = sctp_make_shutdown_complete(asoc, chunk);
3498 if (!shut) {
3499 sctp_ootb_pkt_free(packet);
3500 return SCTP_DISPOSITION_NOMEM;
3501 }
3502
Jerome Forissier047a2422005-04-28 11:58:43 -07003503 /* Reflect vtag if T-Bit is set */
3504 if (sctp_test_T_bit(shut))
3505 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 /* Set the skb to the belonging sock for accounting. */
3508 shut->skb->sk = ep->base.sk;
3509
3510 sctp_packet_append_chunk(packet, shut);
3511
3512 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3513 SCTP_PACKET(packet));
3514
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003515 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 /* If the chunk length is invalid, we don't want to process
3518 * the reset of the packet.
3519 */
3520 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003521 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003523 /* We need to discard the rest of the packet to prevent
3524 * potential bomming attacks from additional bundled chunks.
3525 * This is documented in SCTP Threats ID.
3526 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003527 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529
3530 return SCTP_DISPOSITION_NOMEM;
3531}
3532
3533/*
3534 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3535 *
3536 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3537 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3538 * procedures in section 8.4 SHOULD be followed, in other words it
3539 * should be treated as an Out Of The Blue packet.
3540 * [This means that we do NOT check the Verification Tag on these
3541 * chunks. --piggy ]
3542 *
3543 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003544sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3545 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 const struct sctp_association *asoc,
3547 const sctp_subtype_t type,
3548 void *arg,
3549 sctp_cmd_seq_t *commands)
3550{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003551 struct sctp_chunk *chunk = arg;
3552
3553 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3554 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003555 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003556 commands);
3557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 /* Although we do have an association in this case, it corresponds
3559 * to a restarted association. So the packet is treated as an OOTB
3560 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3561 * called with a NULL association.
3562 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003563 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003564
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003565 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567
3568/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003569sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3570 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 const struct sctp_association *asoc,
3572 const sctp_subtype_t type, void *arg,
3573 sctp_cmd_seq_t *commands)
3574{
3575 struct sctp_chunk *chunk = arg;
3576 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003577 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003579 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003581 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 if (!sctp_vtag_verify(chunk, asoc)) {
3584 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3585 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003586 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 }
3588
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003589 /* ADD-IP: Section 4.1.1
3590 * This chunk MUST be sent in an authenticated way by using
3591 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3592 * is received unauthenticated it MUST be silently discarded as
3593 * described in [I-D.ietf-tsvwg-sctp-auth].
3594 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003595 if (!net->sctp.addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003596 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3599 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003600 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 commands);
3602
3603 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3604 serial = ntohl(hdr->serial);
3605
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003606 addr_param = (union sctp_addr_param *)hdr->params;
3607 length = ntohs(addr_param->p.length);
3608 if (length < sizeof(sctp_paramhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003609 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003610 (void *)addr_param, commands);
3611
3612 /* Verify the ASCONF chunk before processing it. */
3613 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003614 (sctp_paramhdr_t *)((void *)addr_param + length),
3615 (void *)chunk->chunk_end,
3616 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003617 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003618 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003619
Vlad Yasevicha08de642007-12-20 14:11:47 -08003620 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003622 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 */
3624 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003625 /* If this is the first instance of ASCONF in the packet,
3626 * we can clean our old ASCONF-ACKs.
3627 */
3628 if (!chunk->has_asconf)
3629 sctp_assoc_clean_asconf_ack_cache(asoc);
3630
3631 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3632 * expected, process the ASCONF as described below and after
3633 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3634 * the response packet and cache a copy of it (in the event it
3635 * later needs to be retransmitted).
3636 *
3637 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 */
3639 asconf_ack = sctp_process_asconf((struct sctp_association *)
3640 asoc, chunk);
3641 if (!asconf_ack)
3642 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003643 } else if (serial < asoc->peer.addip_serial + 1) {
3644 /* ADDIP 5.2 E2)
3645 * If the value found in the Sequence Number is less than the
3646 * ('Peer- Sequence-Number' + 1), simply skip to the next
3647 * ASCONF, and include in the outbound response packet
3648 * any previously cached ASCONF-ACK response that was
3649 * sent and saved that matches the Sequence Number of the
3650 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3651 * Chunk exists. This will occur when an older ASCONF
3652 * arrives out of order. In such a case, the receiver
3653 * should skip the ASCONF Chunk and not include ASCONF-ACK
3654 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003656 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3657 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003659
3660 /* Reset the transport so that we select the correct one
3661 * this time around. This is to make sure that we don't
3662 * accidentally use a stale transport that's been removed.
3663 */
3664 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003666 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return SCTP_DISPOSITION_DISCARD;
3670 }
3671
Vlad Yasevicha08de642007-12-20 14:11:47 -08003672 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3673 * containing the ASCONF-ACK Chunks MUST be the source address of
3674 * the SCTP packet that held the ASCONF Chunks.
3675 *
3676 * To do this properly, we'll set the destination address of the chunk
3677 * and at the transmit time, will try look up the transport to use.
3678 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003679 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003681 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003683 if (asoc->new_transport) {
3684 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3685 commands);
3686 ((struct sctp_association *)asoc)->new_transport = NULL;
3687 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return SCTP_DISPOSITION_CONSUME;
3690}
3691
3692/*
3693 * ADDIP Section 4.3 General rules for address manipulation
3694 * When building TLV parameters for the ASCONF Chunk that will add or
3695 * delete IP addresses the D0 to D13 rules should be applied:
3696 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003697sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3698 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003700 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 sctp_cmd_seq_t *commands)
3702{
3703 struct sctp_chunk *asconf_ack = arg;
3704 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3705 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003706 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 sctp_addiphdr_t *addip_hdr;
3708 __u32 sent_serial, rcvd_serial;
3709
3710 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3711 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3712 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003713 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
3715
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003716 /* ADD-IP, Section 4.1.2:
3717 * This chunk MUST be sent in an authenticated way by using
3718 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3719 * is received unauthenticated it MUST be silently discarded as
3720 * described in [I-D.ietf-tsvwg-sctp-auth].
3721 */
Eric W. Biedermane1fc3b12012-08-07 07:29:57 +00003722 if (!net->sctp.addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003723 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 /* Make sure that the ADDIP chunk has a valid length. */
3726 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003727 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 commands);
3729
3730 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3731 rcvd_serial = ntohl(addip_hdr->serial);
3732
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003733 /* Verify the ASCONF-ACK chunk before processing it. */
3734 if (!sctp_verify_asconf(asoc,
3735 (sctp_paramhdr_t *)addip_hdr->params,
3736 (void *)asconf_ack->chunk_end,
3737 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003738 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003739 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 if (last_asconf) {
3742 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3743 sent_serial = ntohl(addip_hdr->serial);
3744 } else {
3745 sent_serial = asoc->addip_serial - 1;
3746 }
3747
3748 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3749 * equal to the next serial number to be used but no ASCONF chunk is
3750 * outstanding the endpoint MUST ABORT the association. Note that a
3751 * sequence number is greater than if it is no more than 2^^31-1
3752 * larger than the current sequence number (using serial arithmetic).
3753 */
3754 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3755 !(asoc->addip_last_asconf)) {
3756 abort = sctp_make_abort(asoc, asconf_ack,
3757 sizeof(sctp_errhdr_t));
3758 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003759 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3761 SCTP_CHUNK(abort));
3762 }
3763 /* We are going to ABORT, so we might as well stop
3764 * processing the rest of the chunks in the packet.
3765 */
3766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3768 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003769 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003770 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003772 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003773 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3774 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return SCTP_DISPOSITION_ABORT;
3776 }
3777
3778 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3779 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3780 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3781
3782 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003783 asconf_ack)) {
3784 /* Successfully processed ASCONF_ACK. We can
3785 * release the next asconf if we have one.
3786 */
3787 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3788 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 abort = sctp_make_abort(asoc, asconf_ack,
3793 sizeof(sctp_errhdr_t));
3794 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003795 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3797 SCTP_CHUNK(abort));
3798 }
3799 /* We are going to ABORT, so we might as well stop
3800 * processing the rest of the chunks in the packet.
3801 */
3802 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003803 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003804 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003806 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003807 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3808 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 return SCTP_DISPOSITION_ABORT;
3810 }
3811
3812 return SCTP_DISPOSITION_DISCARD;
3813}
3814
3815/*
3816 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3817 *
3818 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3819 * its cumulative TSN point to the value carried in the FORWARD TSN
3820 * chunk, and then MUST further advance its cumulative TSN point locally
3821 * if possible.
3822 * After the above processing, the data receiver MUST stop reporting any
3823 * missing TSNs earlier than or equal to the new cumulative TSN point.
3824 *
3825 * Verification Tag: 8.5 Verification Tag [Normal verification]
3826 *
3827 * The return value is the disposition of the chunk.
3828 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003829sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3830 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 const struct sctp_association *asoc,
3832 const sctp_subtype_t type,
3833 void *arg,
3834 sctp_cmd_seq_t *commands)
3835{
3836 struct sctp_chunk *chunk = arg;
3837 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003838 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 __u16 len;
3840 __u32 tsn;
3841
3842 if (!sctp_vtag_verify(chunk, asoc)) {
3843 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3844 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003845 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 }
3847
3848 /* Make sure that the FORWARD_TSN chunk has valid length. */
3849 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003850 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 commands);
3852
3853 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3854 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3855 len = ntohs(chunk->chunk_hdr->length);
3856 len -= sizeof(struct sctp_chunkhdr);
3857 skb_pull(chunk->skb, len);
3858
3859 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003860 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 /* The TSN is too high--silently discard the chunk and count on it
3863 * getting retransmitted later.
3864 */
3865 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3866 goto discard_noforce;
3867
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003868 /* Silently discard the chunk if stream-id is not valid */
3869 sctp_walk_fwdtsn(skip, chunk) {
3870 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3871 goto discard_noforce;
3872 }
3873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3875 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003876 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 /* Count this as receiving DATA. */
3880 if (asoc->autoclose) {
3881 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3882 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3883 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003886 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 */
3888 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 return SCTP_DISPOSITION_CONSUME;
3891
3892discard_noforce:
3893 return SCTP_DISPOSITION_DISCARD;
3894}
3895
3896sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003897 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 const struct sctp_endpoint *ep,
3899 const struct sctp_association *asoc,
3900 const sctp_subtype_t type,
3901 void *arg,
3902 sctp_cmd_seq_t *commands)
3903{
3904 struct sctp_chunk *chunk = arg;
3905 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003906 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 __u16 len;
3908 __u32 tsn;
3909
3910 if (!sctp_vtag_verify(chunk, asoc)) {
3911 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3912 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003913 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 }
3915
3916 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3917 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003918 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 commands);
3920
3921 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3922 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3923 len = ntohs(chunk->chunk_hdr->length);
3924 len -= sizeof(struct sctp_chunkhdr);
3925 skb_pull(chunk->skb, len);
3926
3927 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003928 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
3930 /* The TSN is too high--silently discard the chunk and count on it
3931 * getting retransmitted later.
3932 */
3933 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3934 goto gen_shutdown;
3935
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003936 /* Silently discard the chunk if stream-id is not valid */
3937 sctp_walk_fwdtsn(skip, chunk) {
3938 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3939 goto gen_shutdown;
3940 }
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3943 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003944 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 /* Go a head and force a SACK, since we are shutting down. */
3948gen_shutdown:
3949 /* Implementor's Guide.
3950 *
3951 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3952 * respond to each received packet containing one or more DATA chunk(s)
3953 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3954 */
3955 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3956 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3957 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3958 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3959
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003960 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961}
3962
3963/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003964 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003965 *
3966 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3967 * Identifier field. If this algorithm was not specified by the
3968 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3969 * during association setup, the AUTH chunk and all chunks after it MUST
3970 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3971 * defined in Section 4.1.
3972 *
3973 * If an endpoint with no shared key receives a Shared Key Identifier
3974 * other than 0, it MUST silently discard all authenticated chunks. If
3975 * the endpoint has at least one endpoint pair shared key for the peer,
3976 * it MUST use the key specified by the Shared Key Identifier if a
3977 * key has been configured for that Shared Key Identifier. If no
3978 * endpoint pair shared key has been configured for that Shared Key
3979 * Identifier, all authenticated chunks MUST be silently discarded.
3980 *
3981 * Verification Tag: 8.5 Verification Tag [Normal verification]
3982 *
3983 * The return value is the disposition of the chunk.
3984 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003985static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3986 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003987 const struct sctp_association *asoc,
3988 const sctp_subtype_t type,
3989 struct sctp_chunk *chunk)
3990{
3991 struct sctp_authhdr *auth_hdr;
3992 struct sctp_hmac *hmac;
3993 unsigned int sig_len;
3994 __u16 key_id;
3995 __u8 *save_digest;
3996 __u8 *digest;
3997
3998 /* Pull in the auth header, so we can do some more verification */
3999 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4000 chunk->subh.auth_hdr = auth_hdr;
4001 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4002
4003 /* Make sure that we suport the HMAC algorithm from the auth
4004 * chunk.
4005 */
4006 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4007 return SCTP_IERROR_AUTH_BAD_HMAC;
4008
4009 /* Make sure that the provided shared key identifier has been
4010 * configured
4011 */
4012 key_id = ntohs(auth_hdr->shkey_id);
4013 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4014 return SCTP_IERROR_AUTH_BAD_KEYID;
4015
4016
4017 /* Make sure that the length of the signature matches what
4018 * we expect.
4019 */
4020 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4021 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4022 if (sig_len != hmac->hmac_len)
4023 return SCTP_IERROR_PROTO_VIOLATION;
4024
4025 /* Now that we've done validation checks, we can compute and
4026 * verify the hmac. The steps involved are:
4027 * 1. Save the digest from the chunk.
4028 * 2. Zero out the digest in the chunk.
4029 * 3. Compute the new digest
4030 * 4. Compare saved and new digests.
4031 */
4032 digest = auth_hdr->hmac;
4033 skb_pull(chunk->skb, sig_len);
4034
4035 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4036 if (!save_digest)
4037 goto nomem;
4038
4039 memset(digest, 0, sig_len);
4040
4041 sctp_auth_calculate_hmac(asoc, chunk->skb,
4042 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4043 GFP_ATOMIC);
4044
4045 /* Discard the packet if the digests do not match */
4046 if (memcmp(save_digest, digest, sig_len)) {
4047 kfree(save_digest);
4048 return SCTP_IERROR_BAD_SIG;
4049 }
4050
4051 kfree(save_digest);
4052 chunk->auth = 1;
4053
4054 return SCTP_IERROR_NO_ERROR;
4055nomem:
4056 return SCTP_IERROR_NOMEM;
4057}
4058
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004059sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4060 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004061 const struct sctp_association *asoc,
4062 const sctp_subtype_t type,
4063 void *arg,
4064 sctp_cmd_seq_t *commands)
4065{
4066 struct sctp_authhdr *auth_hdr;
4067 struct sctp_chunk *chunk = arg;
4068 struct sctp_chunk *err_chunk;
4069 sctp_ierror_t error;
4070
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004071 /* Make sure that the peer has AUTH capable */
4072 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004073 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004074
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004075 if (!sctp_vtag_verify(chunk, asoc)) {
4076 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4077 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004078 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004079 }
4080
4081 /* Make sure that the AUTH chunk has valid length. */
4082 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004083 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004084 commands);
4085
4086 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004087 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004088 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004089 case SCTP_IERROR_AUTH_BAD_HMAC:
4090 /* Generate the ERROR chunk and discard the rest
4091 * of the packet
4092 */
4093 err_chunk = sctp_make_op_error(asoc, chunk,
4094 SCTP_ERROR_UNSUP_HMAC,
4095 &auth_hdr->hmac_id,
4096 sizeof(__u16), 0);
4097 if (err_chunk) {
4098 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4099 SCTP_CHUNK(err_chunk));
4100 }
4101 /* Fall Through */
4102 case SCTP_IERROR_AUTH_BAD_KEYID:
4103 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004104 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004105
4106 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004107 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004108 commands);
4109
4110 case SCTP_IERROR_NOMEM:
4111 return SCTP_DISPOSITION_NOMEM;
4112
4113 default: /* Prevent gcc warnings */
4114 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004115 }
4116
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004117 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4118 struct sctp_ulpevent *ev;
4119
4120 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4121 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4122
4123 if (!ev)
4124 return -ENOMEM;
4125
4126 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4127 SCTP_ULPEVENT(ev));
4128 }
4129
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004130 return SCTP_DISPOSITION_CONSUME;
4131}
4132
4133/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 * Process an unknown chunk.
4135 *
4136 * Section: 3.2. Also, 2.1 in the implementor's guide.
4137 *
4138 * Chunk Types are encoded such that the highest-order two bits specify
4139 * the action that must be taken if the processing endpoint does not
4140 * recognize the Chunk Type.
4141 *
4142 * 00 - Stop processing this SCTP packet and discard it, do not process
4143 * any further chunks within it.
4144 *
4145 * 01 - Stop processing this SCTP packet and discard it, do not process
4146 * any further chunks within it, and report the unrecognized
4147 * chunk in an 'Unrecognized Chunk Type'.
4148 *
4149 * 10 - Skip this chunk and continue processing.
4150 *
4151 * 11 - Skip this chunk and continue processing, but report in an ERROR
4152 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4153 *
4154 * The return value is the disposition of the chunk.
4155 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004156sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4157 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 const struct sctp_association *asoc,
4159 const sctp_subtype_t type,
4160 void *arg,
4161 sctp_cmd_seq_t *commands)
4162{
4163 struct sctp_chunk *unk_chunk = arg;
4164 struct sctp_chunk *err_chunk;
4165 sctp_chunkhdr_t *hdr;
4166
4167 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4168
4169 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 /* Make sure that the chunk has a valid length.
4173 * Since we don't know the chunk type, we use a general
4174 * chunkhdr structure to make a comparison.
4175 */
4176 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004177 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 commands);
4179
4180 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4181 case SCTP_CID_ACTION_DISCARD:
4182 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004183 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 break;
4185 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 /* Generate an ERROR chunk as response. */
4187 hdr = unk_chunk->chunk_hdr;
4188 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4189 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004190 WORD_ROUND(ntohs(hdr->length)),
4191 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (err_chunk) {
4193 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4194 SCTP_CHUNK(err_chunk));
4195 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004196
4197 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004198 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return SCTP_DISPOSITION_CONSUME;
4200 break;
4201 case SCTP_CID_ACTION_SKIP:
4202 /* Skip the chunk. */
4203 return SCTP_DISPOSITION_DISCARD;
4204 break;
4205 case SCTP_CID_ACTION_SKIP_ERR:
4206 /* Generate an ERROR chunk as response. */
4207 hdr = unk_chunk->chunk_hdr;
4208 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4209 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004210 WORD_ROUND(ntohs(hdr->length)),
4211 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 if (err_chunk) {
4213 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4214 SCTP_CHUNK(err_chunk));
4215 }
4216 /* Skip the chunk. */
4217 return SCTP_DISPOSITION_CONSUME;
4218 break;
4219 default:
4220 break;
4221 }
4222
4223 return SCTP_DISPOSITION_DISCARD;
4224}
4225
4226/*
4227 * Discard the chunk.
4228 *
4229 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4230 * [Too numerous to mention...]
4231 * Verification Tag: No verification needed.
4232 * Inputs
4233 * (endpoint, asoc, chunk)
4234 *
4235 * Outputs
4236 * (asoc, reply_msg, msg_up, timers, counters)
4237 *
4238 * The return value is the disposition of the chunk.
4239 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004240sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4241 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 const struct sctp_association *asoc,
4243 const sctp_subtype_t type,
4244 void *arg,
4245 sctp_cmd_seq_t *commands)
4246{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004247 struct sctp_chunk *chunk = arg;
4248
4249 /* Make sure that the chunk has a valid length.
4250 * Since we don't know the chunk type, we use a general
4251 * chunkhdr structure to make a comparison.
4252 */
4253 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004254 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004255 commands);
4256
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4258 return SCTP_DISPOSITION_DISCARD;
4259}
4260
4261/*
4262 * Discard the whole packet.
4263 *
4264 * Section: 8.4 2)
4265 *
4266 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4267 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 *
4269 * Verification Tag: No verification necessary
4270 *
4271 * Inputs
4272 * (endpoint, asoc, chunk)
4273 *
4274 * Outputs
4275 * (asoc, reply_msg, msg_up, timers, counters)
4276 *
4277 * The return value is the disposition of the chunk.
4278 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004279sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4280 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 const struct sctp_association *asoc,
4282 const sctp_subtype_t type,
4283 void *arg,
4284 sctp_cmd_seq_t *commands)
4285{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004286 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4288
4289 return SCTP_DISPOSITION_CONSUME;
4290}
4291
4292
4293/*
4294 * The other end is violating protocol.
4295 *
4296 * Section: Not specified
4297 * Verification Tag: Not specified
4298 * Inputs
4299 * (endpoint, asoc, chunk)
4300 *
4301 * Outputs
4302 * (asoc, reply_msg, msg_up, timers, counters)
4303 *
4304 * We simply tag the chunk as a violation. The state machine will log
4305 * the violation and continue.
4306 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004307sctp_disposition_t sctp_sf_violation(struct net *net,
4308 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 const struct sctp_association *asoc,
4310 const sctp_subtype_t type,
4311 void *arg,
4312 sctp_cmd_seq_t *commands)
4313{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004314 struct sctp_chunk *chunk = arg;
4315
4316 /* Make sure that the chunk has a valid length. */
4317 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004318 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004319 commands);
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return SCTP_DISPOSITION_VIOLATION;
4322}
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004325 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004327static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004328 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004329 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004332 sctp_cmd_seq_t *commands,
4333 const __u8 *payload,
4334 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004336 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 struct sctp_chunk *chunk = arg;
4338 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004340 /* SCTP-AUTH, Section 6.3:
4341 * It should be noted that if the receiver wants to tear
4342 * down an association in an authenticated way only, the
4343 * handling of malformed packets should not result in
4344 * tearing down the association.
4345 *
4346 * This means that if we only want to abort associations
4347 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004348 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004349 * was malformed.
4350 */
4351 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4352 goto discard;
4353
Jesper Juhl9abed242007-11-11 23:57:49 +01004354 /* Make the abort chunk. */
4355 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4356 if (!abort)
4357 goto nomem;
4358
Vlad Yasevichece25df2007-09-07 16:30:54 -04004359 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004360 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4361 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4362 !asoc->peer.i.init_tag) {
4363 sctp_initack_chunk_t *initack;
4364
4365 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4366 if (!sctp_chunk_length_valid(chunk,
4367 sizeof(sctp_initack_chunk_t)))
4368 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4369 else {
4370 unsigned int inittag;
4371
4372 inittag = ntohl(initack->init_hdr.init_tag);
4373 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4374 SCTP_U32(inittag));
4375 }
4376 }
4377
Vlad Yasevichece25df2007-09-07 16:30:54 -04004378 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004379 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380
Vlad Yasevichece25df2007-09-07 16:30:54 -04004381 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4382 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4383 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4384 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4385 SCTP_ERROR(ECONNREFUSED));
4386 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4387 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4388 } else {
4389 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4390 SCTP_ERROR(ECONNABORTED));
4391 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4392 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004393 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004396 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004397
4398 if (!packet)
4399 goto nomem_pkt;
4400
4401 if (sctp_test_T_bit(abort))
4402 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4403
4404 abort->skb->sk = ep->base.sk;
4405
4406 sctp_packet_append_chunk(packet, abort);
4407
4408 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4409 SCTP_PACKET(packet));
4410
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004411 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 }
4413
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004414 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004415
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004416discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004417 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return SCTP_DISPOSITION_ABORT;
4419
Vlad Yasevichece25df2007-09-07 16:30:54 -04004420nomem_pkt:
4421 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422nomem:
4423 return SCTP_DISPOSITION_NOMEM;
4424}
4425
Wei Yongjunaecedea2007-08-02 16:57:44 +08004426/*
4427 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004428 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004429 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004430 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004431 *
4432 * We inform the other end by sending an ABORT with a Protocol Violation
4433 * error code.
4434 *
4435 * Section: Not specified
4436 * Verification Tag: Nothing to do
4437 * Inputs
4438 * (endpoint, asoc, chunk)
4439 *
4440 * Outputs
4441 * (reply_msg, msg_up, counters)
4442 *
4443 * Generate an ABORT chunk and terminate the association.
4444 */
4445static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004446 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004447 const struct sctp_endpoint *ep,
4448 const struct sctp_association *asoc,
4449 const sctp_subtype_t type,
4450 void *arg,
4451 sctp_cmd_seq_t *commands)
4452{
Florian Westphal24448442008-03-23 22:46:34 -07004453 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004454
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004455 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004456 sizeof(err_str));
4457}
4458
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004459/*
4460 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004461 * If the length is smaller than the minimum length of a given parameter,
4462 * or accumulated length in multi parameters exceeds the end of the chunk,
4463 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004464 */
4465static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004466 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004467 const struct sctp_endpoint *ep,
4468 const struct sctp_association *asoc,
4469 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004470 void *arg, void *ext,
4471 sctp_cmd_seq_t *commands)
4472{
4473 struct sctp_chunk *chunk = arg;
4474 struct sctp_paramhdr *param = ext;
4475 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004476
Wei Yongjunba016672008-09-30 05:32:24 -07004477 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4478 goto discard;
4479
4480 /* Make the abort chunk. */
4481 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4482 if (!abort)
4483 goto nomem;
4484
4485 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004486 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004487
4488 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4489 SCTP_ERROR(ECONNABORTED));
4490 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4491 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004492 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4493 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004494
4495discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004496 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004497 return SCTP_DISPOSITION_ABORT;
4498nomem:
4499 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004500}
4501
Wei Yongjunaecedea2007-08-02 16:57:44 +08004502/* Handle a protocol violation when the peer trying to advance the
4503 * cumulative tsn ack to a point beyond the max tsn currently sent.
4504 *
4505 * We inform the other end by sending an ABORT with a Protocol Violation
4506 * error code.
4507 */
4508static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004509 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004510 const struct sctp_endpoint *ep,
4511 const struct sctp_association *asoc,
4512 const sctp_subtype_t type,
4513 void *arg,
4514 sctp_cmd_seq_t *commands)
4515{
Florian Westphal24448442008-03-23 22:46:34 -07004516 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004517
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004518 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004519 sizeof(err_str));
4520}
4521
Vlad Yasevichece25df2007-09-07 16:30:54 -04004522/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004523 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004524 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004525 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004526 * on the path and we may not want to continue this communication.
4527 */
4528static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004529 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004530 const struct sctp_endpoint *ep,
4531 const struct sctp_association *asoc,
4532 const sctp_subtype_t type,
4533 void *arg,
4534 sctp_cmd_seq_t *commands)
4535{
Florian Westphal24448442008-03-23 22:46:34 -07004536 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004537
4538 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004539 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004540
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004541 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004542 sizeof(err_str));
4543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544/***************************************************************************
4545 * These are the state functions for handling primitive (Section 10) events.
4546 ***************************************************************************/
4547/*
4548 * sctp_sf_do_prm_asoc
4549 *
4550 * Section: 10.1 ULP-to-SCTP
4551 * B) Associate
4552 *
4553 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4554 * outbound stream count)
4555 * -> association id [,destination transport addr list] [,outbound stream
4556 * count]
4557 *
4558 * This primitive allows the upper layer to initiate an association to a
4559 * specific peer endpoint.
4560 *
4561 * The peer endpoint shall be specified by one of the transport addresses
4562 * which defines the endpoint (see Section 1.4). If the local SCTP
4563 * instance has not been initialized, the ASSOCIATE is considered an
4564 * error.
4565 * [This is not relevant for the kernel implementation since we do all
4566 * initialization at boot time. It we hadn't initialized we wouldn't
4567 * get anywhere near this code.]
4568 *
4569 * An association id, which is a local handle to the SCTP association,
4570 * will be returned on successful establishment of the association. If
4571 * SCTP is not able to open an SCTP association with the peer endpoint,
4572 * an error is returned.
4573 * [In the kernel implementation, the struct sctp_association needs to
4574 * be created BEFORE causing this primitive to run.]
4575 *
4576 * Other association parameters may be returned, including the
4577 * complete destination transport addresses of the peer as well as the
4578 * outbound stream count of the local endpoint. One of the transport
4579 * address from the returned destination addresses will be selected by
4580 * the local endpoint as default primary path for sending SCTP packets
4581 * to this peer. The returned "destination transport addr list" can
4582 * be used by the ULP to change the default primary path or to force
4583 * sending a packet to a specific transport address. [All of this
4584 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4585 * function.]
4586 *
4587 * Mandatory attributes:
4588 *
4589 * o local SCTP instance name - obtained from the INITIALIZE operation.
4590 * [This is the argument asoc.]
4591 * o destination transport addr - specified as one of the transport
4592 * addresses of the peer endpoint with which the association is to be
4593 * established.
4594 * [This is asoc->peer.active_path.]
4595 * o outbound stream count - the number of outbound streams the ULP
4596 * would like to open towards this peer endpoint.
4597 * [BUG: This is not currently implemented.]
4598 * Optional attributes:
4599 *
4600 * None.
4601 *
4602 * The return value is a disposition.
4603 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004604sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4605 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 const struct sctp_association *asoc,
4607 const sctp_subtype_t type,
4608 void *arg,
4609 sctp_cmd_seq_t *commands)
4610{
4611 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004612 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
4614 /* The comment below says that we enter COOKIE-WAIT AFTER
4615 * sending the INIT, but that doesn't actually work in our
4616 * implementation...
4617 */
4618 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4619 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4620
4621 /* RFC 2960 5.1 Normal Establishment of an Association
4622 *
4623 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4624 * must provide its Verification Tag (Tag_A) in the Initiate
4625 * Tag field. Tag_A SHOULD be a random number in the range of
4626 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4627 */
4628
4629 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4630 if (!repl)
4631 goto nomem;
4632
4633 /* Cast away the const modifier, as we want to just
4634 * rerun it through as a sideffect.
4635 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004636 my_asoc = (struct sctp_association *)asoc;
4637 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638
Frank Filz3f7a87d2005-06-20 13:14:57 -07004639 /* Choose transport for INIT. */
4640 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4641 SCTP_CHUNK(repl));
4642
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 /* After sending the INIT, "A" starts the T1-init timer and
4644 * enters the COOKIE-WAIT state.
4645 */
4646 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4647 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4648 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4649 return SCTP_DISPOSITION_CONSUME;
4650
4651nomem:
4652 return SCTP_DISPOSITION_NOMEM;
4653}
4654
4655/*
4656 * Process the SEND primitive.
4657 *
4658 * Section: 10.1 ULP-to-SCTP
4659 * E) Send
4660 *
4661 * Format: SEND(association id, buffer address, byte count [,context]
4662 * [,stream id] [,life time] [,destination transport address]
4663 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4664 * -> result
4665 *
4666 * This is the main method to send user data via SCTP.
4667 *
4668 * Mandatory attributes:
4669 *
4670 * o association id - local handle to the SCTP association
4671 *
4672 * o buffer address - the location where the user message to be
4673 * transmitted is stored;
4674 *
4675 * o byte count - The size of the user data in number of bytes;
4676 *
4677 * Optional attributes:
4678 *
4679 * o context - an optional 32 bit integer that will be carried in the
4680 * sending failure notification to the ULP if the transportation of
4681 * this User Message fails.
4682 *
4683 * o stream id - to indicate which stream to send the data on. If not
4684 * specified, stream 0 will be used.
4685 *
4686 * o life time - specifies the life time of the user data. The user data
4687 * will not be sent by SCTP after the life time expires. This
4688 * parameter can be used to avoid efforts to transmit stale
4689 * user messages. SCTP notifies the ULP if the data cannot be
4690 * initiated to transport (i.e. sent to the destination via SCTP's
4691 * send primitive) within the life time variable. However, the
4692 * user data will be transmitted if SCTP has attempted to transmit a
4693 * chunk before the life time expired.
4694 *
4695 * o destination transport address - specified as one of the destination
4696 * transport addresses of the peer endpoint to which this packet
4697 * should be sent. Whenever possible, SCTP should use this destination
4698 * transport address for sending the packets, instead of the current
4699 * primary path.
4700 *
4701 * o unorder flag - this flag, if present, indicates that the user
4702 * would like the data delivered in an unordered fashion to the peer
4703 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4704 * message).
4705 *
4706 * o no-bundle flag - instructs SCTP not to bundle this user data with
4707 * other outbound DATA chunks. SCTP MAY still bundle even when
4708 * this flag is present, when faced with network congestion.
4709 *
4710 * o payload protocol-id - A 32 bit unsigned integer that is to be
4711 * passed to the peer indicating the type of payload protocol data
4712 * being transmitted. This value is passed as opaque data by SCTP.
4713 *
4714 * The return value is the disposition.
4715 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004716sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4717 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 const struct sctp_association *asoc,
4719 const sctp_subtype_t type,
4720 void *arg,
4721 sctp_cmd_seq_t *commands)
4722{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004723 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004725 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 return SCTP_DISPOSITION_CONSUME;
4727}
4728
4729/*
4730 * Process the SHUTDOWN primitive.
4731 *
4732 * Section: 10.1:
4733 * C) Shutdown
4734 *
4735 * Format: SHUTDOWN(association id)
4736 * -> result
4737 *
4738 * Gracefully closes an association. Any locally queued user data
4739 * will be delivered to the peer. The association will be terminated only
4740 * after the peer acknowledges all the SCTP packets sent. A success code
4741 * will be returned on successful termination of the association. If
4742 * attempting to terminate the association results in a failure, an error
4743 * code shall be returned.
4744 *
4745 * Mandatory attributes:
4746 *
4747 * o association id - local handle to the SCTP association
4748 *
4749 * Optional attributes:
4750 *
4751 * None.
4752 *
4753 * The return value is the disposition.
4754 */
4755sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004756 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 const struct sctp_endpoint *ep,
4758 const struct sctp_association *asoc,
4759 const sctp_subtype_t type,
4760 void *arg,
4761 sctp_cmd_seq_t *commands)
4762{
4763 int disposition;
4764
4765 /* From 9.2 Shutdown of an Association
4766 * Upon receipt of the SHUTDOWN primitive from its upper
4767 * layer, the endpoint enters SHUTDOWN-PENDING state and
4768 * remains there until all outstanding data has been
4769 * acknowledged by its peer. The endpoint accepts no new data
4770 * from its upper layer, but retransmits data to the far end
4771 * if necessary to fill gaps.
4772 */
4773 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4774 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4775
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 disposition = SCTP_DISPOSITION_CONSUME;
4777 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004778 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 arg, commands);
4780 }
4781 return disposition;
4782}
4783
4784/*
4785 * Process the ABORT primitive.
4786 *
4787 * Section: 10.1:
4788 * C) Abort
4789 *
4790 * Format: Abort(association id [, cause code])
4791 * -> result
4792 *
4793 * Ungracefully closes an association. Any locally queued user data
4794 * will be discarded and an ABORT chunk is sent to the peer. A success code
4795 * will be returned on successful abortion of the association. If
4796 * attempting to abort the association results in a failure, an error
4797 * code shall be returned.
4798 *
4799 * Mandatory attributes:
4800 *
4801 * o association id - local handle to the SCTP association
4802 *
4803 * Optional attributes:
4804 *
4805 * o cause code - reason of the abort to be passed to the peer
4806 *
4807 * None.
4808 *
4809 * The return value is the disposition.
4810 */
4811sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004812 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 const struct sctp_endpoint *ep,
4814 const struct sctp_association *asoc,
4815 const sctp_subtype_t type,
4816 void *arg,
4817 sctp_cmd_seq_t *commands)
4818{
4819 /* From 9.1 Abort of an Association
4820 * Upon receipt of the ABORT primitive from its upper
4821 * layer, the endpoint enters CLOSED state and
4822 * discard all outstanding data has been
4823 * acknowledged by its peer. The endpoint accepts no new data
4824 * from its upper layer, but retransmits data to the far end
4825 * if necessary to fill gaps.
4826 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004827 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 sctp_disposition_t retval;
4829
4830 retval = SCTP_DISPOSITION_CONSUME;
4831
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004832 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
4834 /* Even if we can't send the ABORT due to low memory delete the
4835 * TCB. This is a departure from our typical NOMEM handling.
4836 */
4837
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004838 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4839 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 /* Delete the established association. */
4841 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004842 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004844 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4845 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847 return retval;
4848}
4849
4850/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004851sctp_disposition_t sctp_sf_error_closed(struct net *net,
4852 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 const struct sctp_association *asoc,
4854 const sctp_subtype_t type,
4855 void *arg,
4856 sctp_cmd_seq_t *commands)
4857{
4858 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4859 return SCTP_DISPOSITION_CONSUME;
4860}
4861
4862/* We tried an illegal operation on an association which is shutting
4863 * down.
4864 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004865sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4866 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 const struct sctp_association *asoc,
4868 const sctp_subtype_t type,
4869 void *arg,
4870 sctp_cmd_seq_t *commands)
4871{
4872 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4873 SCTP_ERROR(-ESHUTDOWN));
4874 return SCTP_DISPOSITION_CONSUME;
4875}
4876
4877/*
4878 * sctp_cookie_wait_prm_shutdown
4879 *
4880 * Section: 4 Note: 2
4881 * Verification Tag:
4882 * Inputs
4883 * (endpoint, asoc)
4884 *
4885 * The RFC does not explicitly address this issue, but is the route through the
4886 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4887 *
4888 * Outputs
4889 * (timers)
4890 */
4891sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004892 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 const struct sctp_endpoint *ep,
4894 const struct sctp_association *asoc,
4895 const sctp_subtype_t type,
4896 void *arg,
4897 sctp_cmd_seq_t *commands)
4898{
4899 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4900 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4901
4902 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4903 SCTP_STATE(SCTP_STATE_CLOSED));
4904
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004905 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906
4907 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4908
4909 return SCTP_DISPOSITION_DELETE_TCB;
4910}
4911
4912/*
4913 * sctp_cookie_echoed_prm_shutdown
4914 *
4915 * Section: 4 Note: 2
4916 * Verification Tag:
4917 * Inputs
4918 * (endpoint, asoc)
4919 *
4920 * The RFC does not explcitly address this issue, but is the route through the
4921 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4922 *
4923 * Outputs
4924 * (timers)
4925 */
4926sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004927 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 const struct sctp_endpoint *ep,
4929 const struct sctp_association *asoc,
4930 const sctp_subtype_t type,
4931 void *arg, sctp_cmd_seq_t *commands)
4932{
4933 /* There is a single T1 timer, so we should be able to use
4934 * common function with the COOKIE-WAIT state.
4935 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004936 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937}
4938
4939/*
4940 * sctp_sf_cookie_wait_prm_abort
4941 *
4942 * Section: 4 Note: 2
4943 * Verification Tag:
4944 * Inputs
4945 * (endpoint, asoc)
4946 *
4947 * The RFC does not explicitly address this issue, but is the route through the
4948 * state table when someone issues an abort while in COOKIE_WAIT state.
4949 *
4950 * Outputs
4951 * (timers)
4952 */
4953sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004954 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 const struct sctp_endpoint *ep,
4956 const struct sctp_association *asoc,
4957 const sctp_subtype_t type,
4958 void *arg,
4959 sctp_cmd_seq_t *commands)
4960{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004961 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 sctp_disposition_t retval;
4963
4964 /* Stop T1-init timer */
4965 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4966 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4967 retval = SCTP_DISPOSITION_CONSUME;
4968
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004969 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4972 SCTP_STATE(SCTP_STATE_CLOSED));
4973
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004974 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975
4976 /* Even if we can't send the ABORT due to low memory delete the
4977 * TCB. This is a departure from our typical NOMEM handling.
4978 */
4979
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004980 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4981 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 /* Delete the established association. */
4983 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004984 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
4986 return retval;
4987}
4988
4989/*
4990 * sctp_sf_cookie_echoed_prm_abort
4991 *
4992 * Section: 4 Note: 3
4993 * Verification Tag:
4994 * Inputs
4995 * (endpoint, asoc)
4996 *
4997 * The RFC does not explcitly address this issue, but is the route through the
4998 * state table when someone issues an abort while in COOKIE_ECHOED state.
4999 *
5000 * Outputs
5001 * (timers)
5002 */
5003sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005004 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 const struct sctp_endpoint *ep,
5006 const struct sctp_association *asoc,
5007 const sctp_subtype_t type,
5008 void *arg,
5009 sctp_cmd_seq_t *commands)
5010{
5011 /* There is a single T1 timer, so we should be able to use
5012 * common function with the COOKIE-WAIT state.
5013 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005014 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
5016
5017/*
5018 * sctp_sf_shutdown_pending_prm_abort
5019 *
5020 * Inputs
5021 * (endpoint, asoc)
5022 *
5023 * The RFC does not explicitly address this issue, but is the route through the
5024 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5025 *
5026 * Outputs
5027 * (timers)
5028 */
5029sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005030 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 const struct sctp_endpoint *ep,
5032 const struct sctp_association *asoc,
5033 const sctp_subtype_t type,
5034 void *arg,
5035 sctp_cmd_seq_t *commands)
5036{
5037 /* Stop the T5-shutdown guard timer. */
5038 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5039 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5040
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005041 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042}
5043
5044/*
5045 * sctp_sf_shutdown_sent_prm_abort
5046 *
5047 * Inputs
5048 * (endpoint, asoc)
5049 *
5050 * The RFC does not explicitly address this issue, but is the route through the
5051 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5052 *
5053 * Outputs
5054 * (timers)
5055 */
5056sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005057 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 const struct sctp_endpoint *ep,
5059 const struct sctp_association *asoc,
5060 const sctp_subtype_t type,
5061 void *arg,
5062 sctp_cmd_seq_t *commands)
5063{
5064 /* Stop the T2-shutdown timer. */
5065 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5066 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5067
5068 /* Stop the T5-shutdown guard timer. */
5069 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5070 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5071
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005072 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073}
5074
5075/*
5076 * sctp_sf_cookie_echoed_prm_abort
5077 *
5078 * Inputs
5079 * (endpoint, asoc)
5080 *
5081 * The RFC does not explcitly address this issue, but is the route through the
5082 * state table when someone issues an abort while in COOKIE_ECHOED state.
5083 *
5084 * Outputs
5085 * (timers)
5086 */
5087sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005088 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 const struct sctp_endpoint *ep,
5090 const struct sctp_association *asoc,
5091 const sctp_subtype_t type,
5092 void *arg,
5093 sctp_cmd_seq_t *commands)
5094{
5095 /* The same T2 timer, so we should be able to use
5096 * common function with the SHUTDOWN-SENT state.
5097 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005098 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099}
5100
5101/*
5102 * Process the REQUESTHEARTBEAT primitive
5103 *
5104 * 10.1 ULP-to-SCTP
5105 * J) Request Heartbeat
5106 *
5107 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5108 *
5109 * -> result
5110 *
5111 * Instructs the local endpoint to perform a HeartBeat on the specified
5112 * destination transport address of the given association. The returned
5113 * result should indicate whether the transmission of the HEARTBEAT
5114 * chunk to the destination address is successful.
5115 *
5116 * Mandatory attributes:
5117 *
5118 * o association id - local handle to the SCTP association
5119 *
5120 * o destination transport address - the transport address of the
5121 * association on which a heartbeat should be issued.
5122 */
5123sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005124 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 const struct sctp_endpoint *ep,
5126 const struct sctp_association *asoc,
5127 const sctp_subtype_t type,
5128 void *arg,
5129 sctp_cmd_seq_t *commands)
5130{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005131 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5132 (struct sctp_transport *)arg, commands))
5133 return SCTP_DISPOSITION_NOMEM;
5134
5135 /*
5136 * RFC 2960 (bis), section 8.3
5137 *
5138 * D) Request an on-demand HEARTBEAT on a specific destination
5139 * transport address of a given association.
5140 *
5141 * The endpoint should increment the respective error counter of
5142 * the destination transport address each time a HEARTBEAT is sent
5143 * to that address and not acknowledged within one RTO.
5144 *
5145 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005146 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005147 SCTP_TRANSPORT(arg));
5148 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149}
5150
5151/*
5152 * ADDIP Section 4.1 ASCONF Chunk Procedures
5153 * When an endpoint has an ASCONF signaled change to be sent to the
5154 * remote endpoint it should do A1 to A9
5155 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005156sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5157 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 const struct sctp_association *asoc,
5159 const sctp_subtype_t type,
5160 void *arg,
5161 sctp_cmd_seq_t *commands)
5162{
5163 struct sctp_chunk *chunk = arg;
5164
5165 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5166 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5167 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5168 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5169 return SCTP_DISPOSITION_CONSUME;
5170}
5171
5172/*
5173 * Ignore the primitive event
5174 *
5175 * The return value is the disposition of the primitive.
5176 */
5177sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005178 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 const struct sctp_endpoint *ep,
5180 const struct sctp_association *asoc,
5181 const sctp_subtype_t type,
5182 void *arg,
5183 sctp_cmd_seq_t *commands)
5184{
5185 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5186 return SCTP_DISPOSITION_DISCARD;
5187}
5188
5189/***************************************************************************
5190 * These are the state functions for the OTHER events.
5191 ***************************************************************************/
5192
5193/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005194 * When the SCTP stack has no more user data to send or retransmit, this
5195 * notification is given to the user. Also, at the time when a user app
5196 * subscribes to this event, if there is no data to be sent or
5197 * retransmit, the stack will immediately send up this notification.
5198 */
5199sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005200 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005201 const struct sctp_endpoint *ep,
5202 const struct sctp_association *asoc,
5203 const sctp_subtype_t type,
5204 void *arg,
5205 sctp_cmd_seq_t *commands)
5206{
5207 struct sctp_ulpevent *event;
5208
5209 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5210 if (!event)
5211 return SCTP_DISPOSITION_NOMEM;
5212
5213 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5214
5215 return SCTP_DISPOSITION_CONSUME;
5216}
5217
5218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 * Start the shutdown negotiation.
5220 *
5221 * From Section 9.2:
5222 * Once all its outstanding data has been acknowledged, the endpoint
5223 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5224 * TSN Ack field the last sequential TSN it has received from the peer.
5225 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5226 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5227 * with the updated last sequential TSN received from its peer.
5228 *
5229 * The return value is the disposition.
5230 */
5231sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005232 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 const struct sctp_endpoint *ep,
5234 const struct sctp_association *asoc,
5235 const sctp_subtype_t type,
5236 void *arg,
5237 sctp_cmd_seq_t *commands)
5238{
5239 struct sctp_chunk *reply;
5240
5241 /* Once all its outstanding data has been acknowledged, the
5242 * endpoint shall send a SHUTDOWN chunk to its peer including
5243 * in the Cumulative TSN Ack field the last sequential TSN it
5244 * has received from the peer.
5245 */
5246 reply = sctp_make_shutdown(asoc, NULL);
5247 if (!reply)
5248 goto nomem;
5249
5250 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5251 * T2-shutdown timer.
5252 */
5253 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5254
5255 /* It shall then start the T2-shutdown timer */
5256 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5257 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5258
Wei Yongjun536428a2008-09-05 08:55:26 +08005259 /* RFC 4960 Section 9.2
5260 * The sender of the SHUTDOWN MAY also start an overall guard timer
5261 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5262 */
Thomas Graff8d96052011-07-07 00:28:35 +00005263 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005264 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 if (asoc->autoclose)
5267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5268 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5269
5270 /* and enter the SHUTDOWN-SENT state. */
5271 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5272 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5273
5274 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5275 *
5276 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005277 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 */
5279 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5280
5281 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5282
5283 return SCTP_DISPOSITION_CONSUME;
5284
5285nomem:
5286 return SCTP_DISPOSITION_NOMEM;
5287}
5288
5289/*
5290 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5291 *
5292 * From Section 9.2:
5293 *
5294 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5295 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5296 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5297 * endpoint must re-send the SHUTDOWN ACK.
5298 *
5299 * The return value is the disposition.
5300 */
5301sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005302 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 const struct sctp_endpoint *ep,
5304 const struct sctp_association *asoc,
5305 const sctp_subtype_t type,
5306 void *arg,
5307 sctp_cmd_seq_t *commands)
5308{
5309 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5310 struct sctp_chunk *reply;
5311
5312 /* There are 2 ways of getting here:
5313 * 1) called in response to a SHUTDOWN chunk
5314 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5315 *
5316 * For the case (2), the arg parameter is set to NULL. We need
5317 * to check that we have a chunk before accessing it's fields.
5318 */
5319 if (chunk) {
5320 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005321 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322
5323 /* Make sure that the SHUTDOWN chunk has a valid length. */
5324 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005325 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 commands);
5327 }
5328
5329 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5330 * shall send a SHUTDOWN ACK ...
5331 */
5332 reply = sctp_make_shutdown_ack(asoc, chunk);
5333 if (!reply)
5334 goto nomem;
5335
5336 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5337 * the T2-shutdown timer.
5338 */
5339 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5340
5341 /* and start/restart a T2-shutdown timer of its own, */
5342 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5343 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5344
5345 if (asoc->autoclose)
5346 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5347 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5348
5349 /* Enter the SHUTDOWN-ACK-SENT state. */
5350 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5351 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5352
5353 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5354 *
5355 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005356 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 */
5358 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5359
5360 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5361
5362 return SCTP_DISPOSITION_CONSUME;
5363
5364nomem:
5365 return SCTP_DISPOSITION_NOMEM;
5366}
5367
5368/*
5369 * Ignore the event defined as other
5370 *
5371 * The return value is the disposition of the event.
5372 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005373sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5374 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 const struct sctp_association *asoc,
5376 const sctp_subtype_t type,
5377 void *arg,
5378 sctp_cmd_seq_t *commands)
5379{
5380 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5381 return SCTP_DISPOSITION_DISCARD;
5382}
5383
5384/************************************************************
5385 * These are the state functions for handling timeout events.
5386 ************************************************************/
5387
5388/*
5389 * RTX Timeout
5390 *
5391 * Section: 6.3.3 Handle T3-rtx Expiration
5392 *
5393 * Whenever the retransmission timer T3-rtx expires for a destination
5394 * address, do the following:
5395 * [See below]
5396 *
5397 * The return value is the disposition of the chunk.
5398 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005399sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5400 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 const struct sctp_association *asoc,
5402 const sctp_subtype_t type,
5403 void *arg,
5404 sctp_cmd_seq_t *commands)
5405{
5406 struct sctp_transport *transport = arg;
5407
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005408 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 if (asoc->overall_error_count >= asoc->max_retrans) {
Thomas Graff8d96052011-07-07 00:28:35 +00005411 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5412 /*
5413 * We are here likely because the receiver had its rwnd
5414 * closed for a while and we have not been able to
5415 * transmit the locally queued data within the maximum
5416 * retransmission attempts limit. Start the T5
5417 * shutdown guard timer to give the receiver one last
5418 * chance and some additional time to recover before
5419 * aborting.
5420 */
5421 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5422 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5423 } else {
5424 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5425 SCTP_ERROR(ETIMEDOUT));
5426 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5427 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5428 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005429 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5430 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005431 return SCTP_DISPOSITION_DELETE_TCB;
5432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 }
5434
5435 /* E1) For the destination address for which the timer
5436 * expires, adjust its ssthresh with rules defined in Section
5437 * 7.2.3 and set the cwnd <- MTU.
5438 */
5439
5440 /* E2) For the destination address for which the timer
5441 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5442 * maximum value discussed in rule C7 above (RTO.max) may be
5443 * used to provide an upper bound to this doubling operation.
5444 */
5445
5446 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5447 * outstanding DATA chunks for the address for which the
5448 * T3-rtx has expired will fit into a single packet, subject
5449 * to the MTU constraint for the path corresponding to the
5450 * destination transport address to which the retransmission
5451 * is being sent (this may be different from the address for
5452 * which the timer expires [see Section 6.4]). Call this
5453 * value K. Bundle and retransmit those K DATA chunks in a
5454 * single packet to the destination endpoint.
5455 *
5456 * Note: Any DATA chunks that were sent to the address for
5457 * which the T3-rtx timer expired but did not fit in one MTU
5458 * (rule E3 above), should be marked for retransmission and
5459 * sent as soon as cwnd allows (normally when a SACK arrives).
5460 */
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 /* Do some failure management (Section 8.2). */
5463 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5464
Vlad Yasevich1845a572007-02-21 02:06:19 -08005465 /* NB: Rules E4 and F1 are implicit in R1. */
5466 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5467
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 return SCTP_DISPOSITION_CONSUME;
5469}
5470
5471/*
5472 * Generate delayed SACK on timeout
5473 *
5474 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5475 *
5476 * The guidelines on delayed acknowledgement algorithm specified in
5477 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5478 * acknowledgement SHOULD be generated for at least every second packet
5479 * (not every second DATA chunk) received, and SHOULD be generated
5480 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5481 * some situations it may be beneficial for an SCTP transmitter to be
5482 * more conservative than the algorithms detailed in this document
5483 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5484 * the following algorithms allow.
5485 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005486sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5487 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 const struct sctp_association *asoc,
5489 const sctp_subtype_t type,
5490 void *arg,
5491 sctp_cmd_seq_t *commands)
5492{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005493 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5495 return SCTP_DISPOSITION_CONSUME;
5496}
5497
5498/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005499 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 *
5501 * Section: 4 Note: 2
5502 * Verification Tag:
5503 * Inputs
5504 * (endpoint, asoc)
5505 *
5506 * RFC 2960 Section 4 Notes
5507 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5508 * and re-start the T1-init timer without changing state. This MUST
5509 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5510 * endpoint MUST abort the initialization process and report the
5511 * error to SCTP user.
5512 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005513 * Outputs
5514 * (timers, events)
5515 *
5516 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005517sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5518 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005519 const struct sctp_association *asoc,
5520 const sctp_subtype_t type,
5521 void *arg,
5522 sctp_cmd_seq_t *commands)
5523{
5524 struct sctp_chunk *repl = NULL;
5525 struct sctp_bind_addr *bp;
5526 int attempts = asoc->init_err_counter + 1;
5527
5528 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005529 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005530
Vlad Yasevich81845c22006-01-30 15:59:54 -08005531 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005532 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5533 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5534 if (!repl)
5535 return SCTP_DISPOSITION_NOMEM;
5536
5537 /* Choose transport for INIT. */
5538 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5539 SCTP_CHUNK(repl));
5540
5541 /* Issue a sideeffect to do the needed accounting. */
5542 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5543 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5544
5545 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5546 } else {
5547 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5548 " max_init_attempts: %d\n",
5549 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005550 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5551 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005552 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005553 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005554 return SCTP_DISPOSITION_DELETE_TCB;
5555 }
5556
5557 return SCTP_DISPOSITION_CONSUME;
5558}
5559
5560/*
5561 * sctp_sf_t1_cookie_timer_expire
5562 *
5563 * Section: 4 Note: 2
5564 * Verification Tag:
5565 * Inputs
5566 * (endpoint, asoc)
5567 *
5568 * RFC 2960 Section 4 Notes
5569 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 * COOKIE ECHO and re-start the T1-cookie timer without changing
5571 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5572 * After that, the endpoint MUST abort the initialization process and
5573 * report the error to SCTP user.
5574 *
5575 * Outputs
5576 * (timers, events)
5577 *
5578 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005579sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5580 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 const struct sctp_association *asoc,
5582 const sctp_subtype_t type,
5583 void *arg,
5584 sctp_cmd_seq_t *commands)
5585{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005586 struct sctp_chunk *repl = NULL;
5587 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588
Frank Filz3f7a87d2005-06-20 13:14:57 -07005589 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005590 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591
Vlad Yasevich81845c22006-01-30 15:59:54 -08005592 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005593 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005595 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005597 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5598 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005600 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5601 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5602
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5604 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005605 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5606 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005608 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 return SCTP_DISPOSITION_DELETE_TCB;
5610 }
5611
5612 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613}
5614
5615/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5616 * with the updated last sequential TSN received from its peer.
5617 *
5618 * An endpoint should limit the number of retransmissions of the
5619 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5620 * If this threshold is exceeded the endpoint should destroy the TCB and
5621 * MUST report the peer endpoint unreachable to the upper layer (and
5622 * thus the association enters the CLOSED state). The reception of any
5623 * packet from its peer (i.e. as the peer sends all of its queued DATA
5624 * chunks) should clear the endpoint's retransmission count and restart
5625 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5626 * all of its queued DATA chunks that have not yet been sent.
5627 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005628sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5629 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 const struct sctp_association *asoc,
5631 const sctp_subtype_t type,
5632 void *arg,
5633 sctp_cmd_seq_t *commands)
5634{
5635 struct sctp_chunk *reply = NULL;
5636
5637 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005638 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005639
Neil Horman58fbbed2008-02-29 11:40:56 -08005640 ((struct sctp_association *)asoc)->shutdown_retries++;
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005643 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5644 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5646 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005647 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005648 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5649 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 return SCTP_DISPOSITION_DELETE_TCB;
5651 }
5652
5653 switch (asoc->state) {
5654 case SCTP_STATE_SHUTDOWN_SENT:
5655 reply = sctp_make_shutdown(asoc, NULL);
5656 break;
5657
5658 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5659 reply = sctp_make_shutdown_ack(asoc, NULL);
5660 break;
5661
5662 default:
5663 BUG();
5664 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666
5667 if (!reply)
5668 goto nomem;
5669
Wei Yongjun6345b192009-04-26 23:13:35 +08005670 /* Do some failure management (Section 8.2).
5671 * If we remove the transport an SHUTDOWN was last sent to, don't
5672 * do failure management.
5673 */
5674 if (asoc->shutdown_last_sent_to)
5675 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5676 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677
5678 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5679 * the T2-shutdown timer.
5680 */
5681 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5682
5683 /* Restart the T2-shutdown timer. */
5684 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5685 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5686 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5687 return SCTP_DISPOSITION_CONSUME;
5688
5689nomem:
5690 return SCTP_DISPOSITION_NOMEM;
5691}
5692
5693/*
5694 * ADDIP Section 4.1 ASCONF CHunk Procedures
5695 * If the T4 RTO timer expires the endpoint should do B1 to B5
5696 */
5697sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005698 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 const struct sctp_endpoint *ep,
5700 const struct sctp_association *asoc,
5701 const sctp_subtype_t type,
5702 void *arg,
5703 sctp_cmd_seq_t *commands)
5704{
5705 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5706 struct sctp_transport *transport = chunk->transport;
5707
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005708 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5711 * detection on the appropriate destination address as defined in
5712 * RFC2960 [5] section 8.1 and 8.2.
5713 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005714 if (transport)
5715 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5716 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717
5718 /* Reconfig T4 timer and transport. */
5719 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5720
5721 /* ADDIP 4.1 B2) Increment the association error counters and perform
5722 * endpoint failure detection on the association as defined in
5723 * RFC2960 [5] section 8.1 and 8.2.
5724 * association error counter is incremented in SCTP_CMD_STRIKE.
5725 */
5726 if (asoc->overall_error_count >= asoc->max_retrans) {
5727 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5728 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005729 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5730 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005732 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005733 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5734 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 return SCTP_DISPOSITION_ABORT;
5736 }
5737
5738 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5739 * the ASCONF chunk was sent by doubling the RTO timer value.
5740 * This is done in SCTP_CMD_STRIKE.
5741 */
5742
5743 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5744 * choose an alternate destination address (please refer to RFC2960
5745 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005746 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 * ASCONF sent.
5748 */
5749 sctp_chunk_hold(asoc->addip_last_asconf);
5750 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5751 SCTP_CHUNK(asoc->addip_last_asconf));
5752
5753 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5754 * destination is selected, then the RTO used will be that of the new
5755 * destination address.
5756 */
5757 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5758 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5759
5760 return SCTP_DISPOSITION_CONSUME;
5761}
5762
5763/* sctpimpguide-05 Section 2.12.2
5764 * The sender of the SHUTDOWN MAY also start an overall guard timer
5765 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5766 * At the expiration of this timer the sender SHOULD abort the association
5767 * by sending an ABORT chunk.
5768 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005769sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5770 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 const struct sctp_association *asoc,
5772 const sctp_subtype_t type,
5773 void *arg,
5774 sctp_cmd_seq_t *commands)
5775{
5776 struct sctp_chunk *reply = NULL;
5777
5778 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005779 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780
5781 reply = sctp_make_abort(asoc, NULL, 0);
5782 if (!reply)
5783 goto nomem;
5784
5785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005786 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5787 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005789 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005791 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5792 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005793
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 return SCTP_DISPOSITION_DELETE_TCB;
5795nomem:
5796 return SCTP_DISPOSITION_NOMEM;
5797}
5798
5799/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5800 * the association is automatically closed by starting the shutdown process.
5801 * The work that needs to be done is same as when SHUTDOWN is initiated by
5802 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5803 */
5804sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005805 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 const struct sctp_endpoint *ep,
5807 const struct sctp_association *asoc,
5808 const sctp_subtype_t type,
5809 void *arg,
5810 sctp_cmd_seq_t *commands)
5811{
5812 int disposition;
5813
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005814 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005815
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 /* From 9.2 Shutdown of an Association
5817 * Upon receipt of the SHUTDOWN primitive from its upper
5818 * layer, the endpoint enters SHUTDOWN-PENDING state and
5819 * remains there until all outstanding data has been
5820 * acknowledged by its peer. The endpoint accepts no new data
5821 * from its upper layer, but retransmits data to the far end
5822 * if necessary to fill gaps.
5823 */
5824 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5825 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 disposition = SCTP_DISPOSITION_CONSUME;
5828 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005829 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 arg, commands);
5831 }
5832 return disposition;
5833}
5834
5835/*****************************************************************************
5836 * These are sa state functions which could apply to all types of events.
5837 ****************************************************************************/
5838
5839/*
5840 * This table entry is not implemented.
5841 *
5842 * Inputs
5843 * (endpoint, asoc, chunk)
5844 *
5845 * The return value is the disposition of the chunk.
5846 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005847sctp_disposition_t sctp_sf_not_impl(struct net *net,
5848 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 const struct sctp_association *asoc,
5850 const sctp_subtype_t type,
5851 void *arg,
5852 sctp_cmd_seq_t *commands)
5853{
5854 return SCTP_DISPOSITION_NOT_IMPL;
5855}
5856
5857/*
5858 * This table entry represents a bug.
5859 *
5860 * Inputs
5861 * (endpoint, asoc, chunk)
5862 *
5863 * The return value is the disposition of the chunk.
5864 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005865sctp_disposition_t sctp_sf_bug(struct net *net,
5866 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 const struct sctp_association *asoc,
5868 const sctp_subtype_t type,
5869 void *arg,
5870 sctp_cmd_seq_t *commands)
5871{
5872 return SCTP_DISPOSITION_BUG;
5873}
5874
5875/*
5876 * This table entry represents the firing of a timer in the wrong state.
5877 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5878 * when the association is in the wrong state. This event should
5879 * be ignored, so as to prevent any rearming of the timer.
5880 *
5881 * Inputs
5882 * (endpoint, asoc, chunk)
5883 *
5884 * The return value is the disposition of the chunk.
5885 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005886sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5887 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 const struct sctp_association *asoc,
5889 const sctp_subtype_t type,
5890 void *arg,
5891 sctp_cmd_seq_t *commands)
5892{
5893 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5894 return SCTP_DISPOSITION_CONSUME;
5895}
5896
5897/********************************************************************
5898 * 2nd Level Abstractions
5899 ********************************************************************/
5900
5901/* Pull the SACK chunk based on the SACK header. */
5902static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5903{
5904 struct sctp_sackhdr *sack;
5905 unsigned int len;
5906 __u16 num_blocks;
5907 __u16 num_dup_tsns;
5908
5909 /* Protect ourselves from reading too far into
5910 * the skb from a bogus sender.
5911 */
5912 sack = (struct sctp_sackhdr *) chunk->skb->data;
5913
5914 num_blocks = ntohs(sack->num_gap_ack_blocks);
5915 num_dup_tsns = ntohs(sack->num_dup_tsns);
5916 len = sizeof(struct sctp_sackhdr);
5917 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5918 if (len > chunk->skb->len)
5919 return NULL;
5920
5921 skb_pull(chunk->skb, len);
5922
5923 return sack;
5924}
5925
5926/* Create an ABORT packet to be sent as a response, with the specified
5927 * error causes.
5928 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005929static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5930 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 const struct sctp_association *asoc,
5932 struct sctp_chunk *chunk,
5933 const void *payload,
5934 size_t paylen)
5935{
5936 struct sctp_packet *packet;
5937 struct sctp_chunk *abort;
5938
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005939 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 if (packet) {
5942 /* Make an ABORT.
5943 * The T bit will be set if the asoc is NULL.
5944 */
5945 abort = sctp_make_abort(asoc, chunk, paylen);
5946 if (!abort) {
5947 sctp_ootb_pkt_free(packet);
5948 return NULL;
5949 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005950
5951 /* Reflect vtag if T-Bit is set */
5952 if (sctp_test_T_bit(abort))
5953 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 /* Add specified error causes, i.e., payload, to the
5956 * end of the chunk.
5957 */
5958 sctp_addto_chunk(abort, paylen, payload);
5959
5960 /* Set the skb to the belonging sock for accounting. */
5961 abort->skb->sk = ep->base.sk;
5962
5963 sctp_packet_append_chunk(packet, abort);
5964
5965 }
5966
5967 return packet;
5968}
5969
5970/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005971static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5972 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 const struct sctp_chunk *chunk)
5974{
5975 struct sctp_packet *packet;
5976 struct sctp_transport *transport;
5977 __u16 sport;
5978 __u16 dport;
5979 __u32 vtag;
5980
5981 /* Get the source and destination port from the inbound packet. */
5982 sport = ntohs(chunk->sctp_hdr->dest);
5983 dport = ntohs(chunk->sctp_hdr->source);
5984
5985 /* The V-tag is going to be the same as the inbound packet if no
5986 * association exists, otherwise, use the peer's vtag.
5987 */
5988 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005989 /* Special case the INIT-ACK as there is no peer's vtag
5990 * yet.
5991 */
5992 switch(chunk->chunk_hdr->type) {
5993 case SCTP_CID_INIT_ACK:
5994 {
5995 sctp_initack_chunk_t *initack;
5996
5997 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5998 vtag = ntohl(initack->init_hdr.init_tag);
5999 break;
6000 }
6001 default:
6002 vtag = asoc->peer.i.init_tag;
6003 break;
6004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 } else {
6006 /* Special case the INIT and stale COOKIE_ECHO as there is no
6007 * vtag yet.
6008 */
6009 switch(chunk->chunk_hdr->type) {
6010 case SCTP_CID_INIT:
6011 {
6012 sctp_init_chunk_t *init;
6013
6014 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6015 vtag = ntohl(init->init_hdr.init_tag);
6016 break;
6017 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006018 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 vtag = ntohl(chunk->sctp_hdr->vtag);
6020 break;
6021 }
6022 }
6023
6024 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006025 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 if (!transport)
6027 goto nomem;
6028
6029 /* Cache a route for the transport with the chunk's destination as
6030 * the source address.
6031 */
Al Viro16b0a032006-11-20 17:13:38 -08006032 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006033 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
6035 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6036 packet = sctp_packet_config(packet, vtag, 0);
6037
6038 return packet;
6039
6040nomem:
6041 return NULL;
6042}
6043
6044/* Free the packet allocated earlier for responding in the OOTB condition. */
6045void sctp_ootb_pkt_free(struct sctp_packet *packet)
6046{
6047 sctp_transport_free(packet->transport);
6048}
6049
6050/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006051static void sctp_send_stale_cookie_err(struct net *net,
6052 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 const struct sctp_association *asoc,
6054 const struct sctp_chunk *chunk,
6055 sctp_cmd_seq_t *commands,
6056 struct sctp_chunk *err_chunk)
6057{
6058 struct sctp_packet *packet;
6059
6060 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006061 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 if (packet) {
6063 struct sctp_signed_cookie *cookie;
6064
6065 /* Override the OOTB vtag from the cookie. */
6066 cookie = chunk->subh.cookie_hdr;
6067 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006068
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 /* Set the skb to the belonging sock for accounting. */
6070 err_chunk->skb->sk = ep->base.sk;
6071 sctp_packet_append_chunk(packet, err_chunk);
6072 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6073 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006074 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 } else
6076 sctp_chunk_free (err_chunk);
6077 }
6078}
6079
6080
6081/* Process a data chunk */
6082static int sctp_eat_data(const struct sctp_association *asoc,
6083 struct sctp_chunk *chunk,
6084 sctp_cmd_seq_t *commands)
6085{
6086 sctp_datahdr_t *data_hdr;
6087 struct sctp_chunk *err;
6088 size_t datalen;
6089 sctp_verb_t deliver;
6090 int tmp;
6091 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006092 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006093 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006094 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006095 u16 ssn;
6096 u16 sid;
6097 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098
6099 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6100 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6101
6102 tsn = ntohl(data_hdr->tsn);
6103 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
6104
6105 /* ASSERT: Now skb->data is really the user data. */
6106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 /* Process ECN based congestion.
6108 *
6109 * Since the chunk structure is reused for all chunks within
6110 * a packet, we use ecn_ce_done to track if we've already
6111 * done CE processing for this packet.
6112 *
6113 * We need to do ECN processing even if we plan to discard the
6114 * chunk later.
6115 */
6116
6117 if (!chunk->ecn_ce_done) {
6118 struct sctp_af *af;
6119 chunk->ecn_ce_done = 1;
6120
6121 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006122 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123
6124 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6125 /* Do real work as sideffect. */
6126 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6127 SCTP_U32(tsn));
6128 }
6129 }
6130
6131 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6132 if (tmp < 0) {
6133 /* The TSN is too high--silently discard the chunk and
6134 * count on it getting retransmitted later.
6135 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006136 if (chunk->asoc)
6137 chunk->asoc->stats.outofseqtsns++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 return SCTP_IERROR_HIGH_TSN;
6139 } else if (tmp > 0) {
6140 /* This is a duplicate. Record it. */
6141 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6142 return SCTP_IERROR_DUP_TSN;
6143 }
6144
6145 /* This is a new TSN. */
6146
6147 /* Discard if there is no room in the receive window.
6148 * Actually, allow a little bit of overflow (up to a MTU).
6149 */
6150 datalen = ntohs(chunk->chunk_hdr->length);
6151 datalen -= sizeof(sctp_data_chunk_t);
6152
6153 deliver = SCTP_CMD_CHUNK_ULP;
6154
6155 /* Think about partial delivery. */
6156 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6157
6158 /* Even if we don't accept this chunk there is
6159 * memory pressure.
6160 */
6161 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6162 }
6163
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006164 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 * seems a bit troublesome in that frag_point varies based on
6166 * PMTU. In cases, such as loopback, this might be a rather
6167 * large spill over.
6168 */
Neil Horman4d93df02007-08-15 16:07:44 -07006169 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6170 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171
6172 /* If this is the next TSN, consider reneging to make
6173 * room. Note: Playing nice with a confused sender. A
6174 * malicious sender can still eat up all our buffer
6175 * space and in the future we may want to detect and
6176 * do more drastic reneging.
6177 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006178 if (sctp_tsnmap_has_gap(map) &&
6179 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6181 deliver = SCTP_CMD_RENEGE;
6182 } else {
6183 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6184 "rwnd: %d\n", tsn, datalen,
6185 asoc->rwnd);
6186 return SCTP_IERROR_IGNORE_TSN;
6187 }
6188 }
6189
6190 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006191 * Also try to renege to limit our memory usage in the event that
6192 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006193 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006194 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6195 * memory usage too much
6196 */
6197 if (*sk->sk_prot_creator->memory_pressure) {
6198 if (sctp_tsnmap_has_gap(map) &&
6199 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6200 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6201 deliver = SCTP_CMD_RENEGE;
6202 }
6203 }
6204
6205 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 * Section 3.3.10.9 No User Data (9)
6207 *
6208 * Cause of error
6209 * ---------------
6210 * No User Data: This error cause is returned to the originator of a
6211 * DATA chunk if a received DATA chunk has no user data.
6212 */
6213 if (unlikely(0 == datalen)) {
6214 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6215 if (err) {
6216 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6217 SCTP_CHUNK(err));
6218 }
6219 /* We are going to ABORT, so we might as well stop
6220 * processing the rest of the chunks in the packet.
6221 */
6222 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006223 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6224 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006226 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006227 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6228 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 return SCTP_IERROR_NO_DATA;
6230 }
6231
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006232 chunk->data_accepted = 1;
6233
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6235 * if we renege and the chunk arrives again.
6236 */
Michele Baldessari196d6752012-12-01 04:49:42 +00006237 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006238 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006239 if (chunk->asoc)
6240 chunk->asoc->stats.iuodchunks++;
6241 } else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006242 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Michele Baldessari196d6752012-12-01 04:49:42 +00006243 if (chunk->asoc)
6244 chunk->asoc->stats.iodchunks++;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006245 ordered = 1;
6246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006247
6248 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6249 *
6250 * If an endpoint receive a DATA chunk with an invalid stream
6251 * identifier, it shall acknowledge the reception of the DATA chunk
6252 * following the normal procedure, immediately send an ERROR chunk
6253 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6254 * and discard the DATA chunk.
6255 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006256 sid = ntohs(data_hdr->stream);
6257 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006258 /* Mark tsn as received even though we drop it */
6259 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6260
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6262 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006263 sizeof(data_hdr->stream),
6264 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 if (err)
6266 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6267 SCTP_CHUNK(err));
6268 return SCTP_IERROR_BAD_STREAM;
6269 }
6270
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006271 /* Check to see if the SSN is possible for this TSN.
6272 * The biggest gap we can record is 4K wide. Since SSNs wrap
6273 * at an unsigned short, there is no way that an SSN can
6274 * wrap and for a valid TSN. We can simply check if the current
6275 * SSN is smaller then the next expected one. If it is, it wrapped
6276 * and is invalid.
6277 */
6278 ssn = ntohs(data_hdr->ssn);
6279 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6280 return SCTP_IERROR_PROTO_VIOLATION;
6281 }
6282
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 /* Send the data up to the user. Note: Schedule the
6284 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6285 * chunk needs the updated rwnd.
6286 */
6287 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6288
6289 return SCTP_IERROR_NO_ERROR;
6290}