blob: e17ada47afc4da732ca474b587fe4436aa8fd41c [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{
1058 struct sctp_chunk *chunk = arg;
1059 struct sctp_chunk *reply;
1060 size_t paylen = 0;
1061
1062 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001063 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /* Make sure that the HEARTBEAT chunk has a valid length. */
1066 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001067 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 commands);
1069
1070 /* 8.3 The receiver of the HEARTBEAT should immediately
1071 * respond with a HEARTBEAT ACK that contains the Heartbeat
1072 * Information field copied from the received HEARTBEAT chunk.
1073 */
1074 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1075 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001076 if (!pskb_pull(chunk->skb, paylen))
1077 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 reply = sctp_make_heartbeat_ack(asoc, chunk,
1080 chunk->subh.hb_hdr, paylen);
1081 if (!reply)
1082 goto nomem;
1083
1084 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1085 return SCTP_DISPOSITION_CONSUME;
1086
1087nomem:
1088 return SCTP_DISPOSITION_NOMEM;
1089}
1090
1091/*
1092 * Process the returning HEARTBEAT ACK.
1093 *
1094 * Section: 8.3 Path Heartbeat
1095 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1096 * should clear the error counter of the destination transport
1097 * address to which the HEARTBEAT was sent, and mark the destination
1098 * transport address as active if it is not so marked. The endpoint may
1099 * optionally report to the upper layer when an inactive destination
1100 * address is marked as active due to the reception of the latest
1101 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1102 * clear the association overall error count as well (as defined
1103 * in section 8.1).
1104 *
1105 * The receiver of the HEARTBEAT ACK should also perform an RTT
1106 * measurement for that destination transport address using the time
1107 * value carried in the HEARTBEAT ACK chunk.
1108 *
1109 * Verification Tag: 8.5 Verification Tag [Normal verification]
1110 *
1111 * Inputs
1112 * (endpoint, asoc, chunk)
1113 *
1114 * Outputs
1115 * (asoc, reply_msg, msg_up, timers, counters)
1116 *
1117 * The return value is the disposition of the chunk.
1118 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001119sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1120 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 const struct sctp_association *asoc,
1122 const sctp_subtype_t type,
1123 void *arg,
1124 sctp_cmd_seq_t *commands)
1125{
1126 struct sctp_chunk *chunk = arg;
1127 union sctp_addr from_addr;
1128 struct sctp_transport *link;
1129 sctp_sender_hb_info_t *hbinfo;
1130 unsigned long max_interval;
1131
1132 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001133 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001136 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1137 sizeof(sctp_sender_hb_info_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001138 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 commands);
1140
1141 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001142 /* Make sure that the length of the parameter is what we expect */
1143 if (ntohs(hbinfo->param_hdr.length) !=
1144 sizeof(sctp_sender_hb_info_t)) {
1145 return SCTP_DISPOSITION_DISCARD;
1146 }
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001149 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001152 if (unlikely(!link)) {
1153 if (from_addr.sa.sa_family == AF_INET6) {
Joe Perchese87cc472012-05-13 21:56:26 +00001154 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1155 __func__,
1156 asoc,
1157 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001158 } else {
Joe Perchese87cc472012-05-13 21:56:26 +00001159 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1160 __func__,
1161 asoc,
1162 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return SCTP_DISPOSITION_DISCARD;
1165 }
1166
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001167 /* Validate the 64-bit random nonce. */
1168 if (hbinfo->hb_nonce != link->hb_nonce)
1169 return SCTP_DISPOSITION_DISCARD;
1170
Frank Filz52ccb8e2005-12-22 11:36:46 -08001171 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 /* Check if the timestamp looks valid. */
1174 if (time_after(hbinfo->sent_at, jiffies) ||
1175 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001176 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001178 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return SCTP_DISPOSITION_DISCARD;
1180 }
1181
1182 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1183 * the HEARTBEAT should clear the error counter of the
1184 * destination transport address to which the HEARTBEAT was
1185 * sent and mark the destination transport address as active if
1186 * it is not so marked.
1187 */
1188 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1189
1190 return SCTP_DISPOSITION_CONSUME;
1191}
1192
1193/* Helper function to send out an abort for the restart
1194 * condition.
1195 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001196static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 struct sctp_chunk *init,
1198 sctp_cmd_seq_t *commands)
1199{
1200 int len;
1201 struct sctp_packet *pkt;
1202 union sctp_addr_param *addrparm;
1203 struct sctp_errhdr *errhdr;
1204 struct sctp_endpoint *ep;
1205 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1206 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1207
1208 /* Build the error on the stack. We are way to malloc crazy
1209 * throughout the code today.
1210 */
1211 errhdr = (struct sctp_errhdr *)buffer;
1212 addrparm = (union sctp_addr_param *)errhdr->variable;
1213
1214 /* Copy into a parm format. */
1215 len = af->to_addr_param(ssa, addrparm);
1216 len += sizeof(sctp_errhdr_t);
1217
1218 errhdr->cause = SCTP_ERROR_RESTART;
1219 errhdr->length = htons(len);
1220
1221 /* Assign to the control socket. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001222 ep = sctp_sk(net->sctp.ctl_sock)->ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 /* Association is NULL since this may be a restart attack and we
1225 * want to send back the attacker's vtag.
1226 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001227 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (!pkt)
1230 goto out;
1231 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1232
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00001233 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /* Discard the rest of the inbound packet. */
1236 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1237
1238out:
1239 /* Even if there is no memory, treat as a failure so
1240 * the packet will get dropped.
1241 */
1242 return 0;
1243}
1244
Joe Perches123031c2010-09-08 11:04:21 +00001245static bool list_has_sctp_addr(const struct list_head *list,
1246 union sctp_addr *ipaddr)
1247{
1248 struct sctp_transport *addr;
1249
1250 list_for_each_entry(addr, list, transports) {
1251 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1252 return true;
1253 }
1254
1255 return false;
1256}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257/* A restart is occurring, check to make sure no new addresses
1258 * are being added as we may be under a takeover attack.
1259 */
1260static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1261 const struct sctp_association *asoc,
1262 struct sctp_chunk *init,
1263 sctp_cmd_seq_t *commands)
1264{
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001265 struct net *net = sock_net(new_asoc->base.sk);
Joe Perches123031c2010-09-08 11:04:21 +00001266 struct sctp_transport *new_addr;
1267 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Joe Perches123031c2010-09-08 11:04:21 +00001269 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 * ...
1271 * Before responding the endpoint MUST check to see if the
1272 * unexpected INIT adds new addresses to the association. If new
1273 * addresses are added to the association, the endpoint MUST respond
1274 * with an ABORT..
1275 */
1276
1277 /* Search through all current addresses and make sure
1278 * we aren't adding any new ones.
1279 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001280 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001281 transports) {
1282 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1283 &new_addr->ipaddr)) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001284 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
Joe Perches123031c2010-09-08 11:04:21 +00001285 commands);
1286 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
Joe Perches123031c2010-09-08 11:04:21 +00001288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
1291 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001292 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/* Populate the verification/tie tags based on overlapping INIT
1296 * scenario.
1297 *
1298 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1299 */
1300static void sctp_tietags_populate(struct sctp_association *new_asoc,
1301 const struct sctp_association *asoc)
1302{
1303 switch (asoc->state) {
1304
1305 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1306
1307 case SCTP_STATE_COOKIE_WAIT:
1308 new_asoc->c.my_vtag = asoc->c.my_vtag;
1309 new_asoc->c.my_ttag = asoc->c.my_vtag;
1310 new_asoc->c.peer_ttag = 0;
1311 break;
1312
1313 case SCTP_STATE_COOKIE_ECHOED:
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 = asoc->c.peer_vtag;
1317 break;
1318
1319 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1320 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1321 */
1322 default:
1323 new_asoc->c.my_ttag = asoc->c.my_vtag;
1324 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1325 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 /* Other parameters for the endpoint SHOULD be copied from the
1329 * existing parameters of the association (e.g. number of
1330 * outbound streams) into the INIT ACK and cookie.
1331 */
1332 new_asoc->rwnd = asoc->rwnd;
1333 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1334 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1335 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1336}
1337
1338/*
1339 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1340 * handling action.
1341 *
1342 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1343 *
1344 * Returns value representing action to be taken. These action values
1345 * correspond to Action/Description values in RFC 2960, Table 2.
1346 */
1347static char sctp_tietags_compare(struct sctp_association *new_asoc,
1348 const struct sctp_association *asoc)
1349{
1350 /* In this case, the peer may have restarted. */
1351 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1352 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1353 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1354 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1355 return 'A';
1356
1357 /* Collision case B. */
1358 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1359 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1360 (0 == asoc->c.peer_vtag))) {
1361 return 'B';
1362 }
1363
1364 /* Collision case D. */
1365 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1366 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1367 return 'D';
1368
1369 /* Collision case C. */
1370 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1371 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1372 (0 == new_asoc->c.my_ttag) &&
1373 (0 == new_asoc->c.peer_ttag))
1374 return 'C';
1375
1376 /* No match to any of the special cases; discard this packet. */
1377 return 'E';
1378}
1379
1380/* Common helper routine for both duplicate and simulataneous INIT
1381 * chunk handling.
1382 */
1383static sctp_disposition_t sctp_sf_do_unexpected_init(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001384 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 const struct sctp_endpoint *ep,
1386 const struct sctp_association *asoc,
1387 const sctp_subtype_t type,
1388 void *arg, sctp_cmd_seq_t *commands)
1389{
1390 sctp_disposition_t retval;
1391 struct sctp_chunk *chunk = arg;
1392 struct sctp_chunk *repl;
1393 struct sctp_association *new_asoc;
1394 struct sctp_chunk *err_chunk;
1395 struct sctp_packet *packet;
1396 sctp_unrecognized_param_t *unk_param;
1397 int len;
1398
1399 /* 6.10 Bundling
1400 * An endpoint MUST NOT bundle INIT, INIT ACK or
1401 * SHUTDOWN COMPLETE with any other chunks.
1402 *
1403 * IG Section 2.11.2
1404 * Furthermore, we require that the receiver of an INIT chunk MUST
1405 * enforce these rules by silently discarding an arriving packet
1406 * with an INIT chunk that is bundled with other chunks.
1407 */
1408 if (!chunk->singleton)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001409 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001412 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
1414 if (chunk->sctp_hdr->vtag != 0)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001415 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /* Make sure that the INIT chunk has a valid length.
1418 * In this case, we generate a protocol violation since we have
1419 * an association established.
1420 */
1421 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001422 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 commands);
1424 /* Grab the INIT header. */
1425 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1426
1427 /* Tag the variable length parameters. */
1428 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1429
1430 /* Verify the INIT chunk before processing it. */
1431 err_chunk = NULL;
Eric W. Biedermanf53b5b02012-08-07 07:29:08 +00001432 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1434 &err_chunk)) {
1435 /* This chunk contains fatal error. It is to be discarded.
1436 * Send an ABORT, with causes if there is any.
1437 */
1438 if (err_chunk) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001439 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 (__u8 *)(err_chunk->chunk_hdr) +
1441 sizeof(sctp_chunkhdr_t),
1442 ntohs(err_chunk->chunk_hdr->length) -
1443 sizeof(sctp_chunkhdr_t));
1444
1445 if (packet) {
1446 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1447 SCTP_PACKET(packet));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001448 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 retval = SCTP_DISPOSITION_CONSUME;
1450 } else {
1451 retval = SCTP_DISPOSITION_NOMEM;
1452 }
1453 goto cleanup;
1454 } else {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001455 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 commands);
1457 }
1458 }
1459
1460 /*
1461 * Other parameters for the endpoint SHOULD be copied from the
1462 * existing parameters of the association (e.g. number of
1463 * outbound streams) into the INIT ACK and cookie.
1464 * FIXME: We are copying parameters from the endpoint not the
1465 * association.
1466 */
1467 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1468 if (!new_asoc)
1469 goto nomem;
1470
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001471 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1472 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1473 goto nomem;
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 /* In the outbound INIT ACK the endpoint MUST copy its current
1476 * Verification Tag and Peers Verification tag into a reserved
1477 * place (local tie-tag and per tie-tag) within the state cookie.
1478 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001479 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001481 GFP_ATOMIC))
1482 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /* Make sure no new addresses are being added during the
1485 * restart. Do not do this check for COOKIE-WAIT state,
1486 * since there are no peer addresses to check against.
1487 * Upon return an ABORT will have been sent if needed.
1488 */
1489 if (!sctp_state(asoc, COOKIE_WAIT)) {
1490 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1491 commands)) {
1492 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001493 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495 }
1496
1497 sctp_tietags_populate(new_asoc, asoc);
1498
1499 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1500
1501 /* If there are errors need to be reported for unknown parameters,
1502 * make sure to reserve enough room in the INIT ACK for them.
1503 */
1504 len = 0;
1505 if (err_chunk) {
1506 len = ntohs(err_chunk->chunk_hdr->length) -
1507 sizeof(sctp_chunkhdr_t);
1508 }
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1511 if (!repl)
1512 goto nomem;
1513
1514 /* If there are errors need to be reported for unknown parameters,
1515 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1516 * parameter.
1517 */
1518 if (err_chunk) {
1519 /* Get the "Unrecognized parameter" parameter(s) out of the
1520 * ERROR chunk generated by sctp_verify_init(). Since the
1521 * error cause code for "unknown parameter" and the
1522 * "Unrecognized parameter" type is the same, we can
1523 * construct the parameters in INIT ACK by copying the
1524 * ERROR causes over.
1525 */
1526 unk_param = (sctp_unrecognized_param_t *)
1527 ((__u8 *)(err_chunk->chunk_hdr) +
1528 sizeof(sctp_chunkhdr_t));
1529 /* Replace the cause code with the "Unrecognized parameter"
1530 * parameter type.
1531 */
1532 sctp_addto_chunk(repl, len, unk_param);
1533 }
1534
1535 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1536 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1537
1538 /*
1539 * Note: After sending out INIT ACK with the State Cookie parameter,
1540 * "Z" MUST NOT allocate any resources for this new association.
1541 * Otherwise, "Z" will be vulnerable to resource attacks.
1542 */
1543 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1544 retval = SCTP_DISPOSITION_CONSUME;
1545
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001546 return retval;
1547
1548nomem:
1549 retval = SCTP_DISPOSITION_NOMEM;
1550nomem_retval:
1551 if (new_asoc)
1552 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553cleanup:
1554 if (err_chunk)
1555 sctp_chunk_free(err_chunk);
1556 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557}
1558
1559/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001560 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 * This means we started an INIT and then we got an INIT request from
1562 * our peer.
1563 *
1564 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1565 * This usually indicates an initialization collision, i.e., each
1566 * endpoint is attempting, at about the same time, to establish an
1567 * association with the other endpoint.
1568 *
1569 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1570 * endpoint MUST respond with an INIT ACK using the same parameters it
1571 * sent in its original INIT chunk (including its Verification Tag,
1572 * unchanged). These original parameters are combined with those from the
1573 * newly received INIT chunk. The endpoint shall also generate a State
1574 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1575 * INIT to calculate the State Cookie.
1576 *
1577 * After that, the endpoint MUST NOT change its state, the T1-init
1578 * timer shall be left running and the corresponding TCB MUST NOT be
1579 * destroyed. The normal procedures for handling State Cookies when
1580 * a TCB exists will resolve the duplicate INITs to a single association.
1581 *
1582 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1583 * its Tie-Tags with the Tag information of itself and its peer (see
1584 * section 5.2.2 for a description of the Tie-Tags).
1585 *
1586 * Verification Tag: Not explicit, but an INIT can not have a valid
1587 * verification tag, so we skip the check.
1588 *
1589 * Inputs
1590 * (endpoint, asoc, chunk)
1591 *
1592 * Outputs
1593 * (asoc, reply_msg, msg_up, timers, counters)
1594 *
1595 * The return value is the disposition of the chunk.
1596 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001597sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1598 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 const struct sctp_association *asoc,
1600 const sctp_subtype_t type,
1601 void *arg,
1602 sctp_cmd_seq_t *commands)
1603{
1604 /* Call helper to do the real work for both simulataneous and
1605 * duplicate INIT chunk handling.
1606 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001607 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
1610/*
1611 * Handle duplicated INIT messages. These are usually delayed
1612 * restransmissions.
1613 *
1614 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1615 * COOKIE-ECHOED and COOKIE-WAIT
1616 *
1617 * Unless otherwise stated, upon reception of an unexpected INIT for
1618 * this association, the endpoint shall generate an INIT ACK with a
1619 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1620 * current Verification Tag and peer's Verification Tag into a reserved
1621 * place within the state cookie. We shall refer to these locations as
1622 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1623 * containing this INIT ACK MUST carry a Verification Tag value equal to
1624 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1625 * MUST contain a new Initiation Tag (randomly generated see Section
1626 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1627 * existing parameters of the association (e.g. number of outbound
1628 * streams) into the INIT ACK and cookie.
1629 *
1630 * After sending out the INIT ACK, the endpoint shall take no further
1631 * actions, i.e., the existing association, including its current state,
1632 * and the corresponding TCB MUST NOT be changed.
1633 *
1634 * Note: Only when a TCB exists and the association is not in a COOKIE-
1635 * WAIT state are the Tie-Tags populated. For a normal association INIT
1636 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1637 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1638 * State Cookie are populated as specified in section 5.2.1.
1639 *
1640 * Verification Tag: Not specified, but an INIT has no way of knowing
1641 * what the verification tag could be, so we ignore it.
1642 *
1643 * Inputs
1644 * (endpoint, asoc, chunk)
1645 *
1646 * Outputs
1647 * (asoc, reply_msg, msg_up, timers, counters)
1648 *
1649 * The return value is the disposition of the chunk.
1650 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001651sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1652 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 const struct sctp_association *asoc,
1654 const sctp_subtype_t type,
1655 void *arg,
1656 sctp_cmd_seq_t *commands)
1657{
1658 /* Call helper to do the real work for both simulataneous and
1659 * duplicate INIT chunk handling.
1660 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001661 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662}
1663
1664
Vlad Yasevich610ab732007-01-15 19:18:30 -08001665/*
1666 * Unexpected INIT-ACK handler.
1667 *
1668 * Section 5.2.3
1669 * If an INIT ACK received by an endpoint in any state other than the
1670 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1671 * An unexpected INIT ACK usually indicates the processing of an old or
1672 * duplicated INIT chunk.
1673*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001674sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1675 const struct sctp_endpoint *ep,
Vlad Yasevich610ab732007-01-15 19:18:30 -08001676 const struct sctp_association *asoc,
1677 const sctp_subtype_t type,
1678 void *arg, sctp_cmd_seq_t *commands)
1679{
1680 /* Per the above section, we'll discard the chunk if we have an
1681 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1682 */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00001683 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001684 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001685 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001686 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich610ab732007-01-15 19:18:30 -08001687}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1690 *
1691 * Section 5.2.4
1692 * A) In this case, the peer may have restarted.
1693 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001694static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1695 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 const struct sctp_association *asoc,
1697 struct sctp_chunk *chunk,
1698 sctp_cmd_seq_t *commands,
1699 struct sctp_association *new_asoc)
1700{
1701 sctp_init_chunk_t *peer_init;
1702 struct sctp_ulpevent *ev;
1703 struct sctp_chunk *repl;
1704 struct sctp_chunk *err;
1705 sctp_disposition_t disposition;
1706
1707 /* new_asoc is a brand-new association, so these are not yet
1708 * side effects--it is safe to run them here.
1709 */
1710 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1711
Wei Yongjunde6becd2011-04-19 21:30:51 +00001712 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 GFP_ATOMIC))
1714 goto nomem;
1715
1716 /* Make sure no new addresses are being added during the
1717 * restart. Though this is a pretty complicated attack
1718 * since you'd have to get inside the cookie.
1719 */
1720 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1721 return SCTP_DISPOSITION_CONSUME;
1722 }
1723
1724 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1725 * the peer has restarted (Action A), it MUST NOT setup a new
1726 * association but instead resend the SHUTDOWN ACK and send an ERROR
1727 * chunk with a "Cookie Received while Shutting Down" error cause to
1728 * its peer.
1729 */
1730 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001731 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1733 chunk, commands);
1734 if (SCTP_DISPOSITION_NOMEM == disposition)
1735 goto nomem;
1736
1737 err = sctp_make_op_error(asoc, chunk,
1738 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001739 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (err)
1741 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1742 SCTP_CHUNK(err));
1743
1744 return SCTP_DISPOSITION_CONSUME;
1745 }
1746
Wei Yongjuna000c012011-05-29 23:23:36 +00001747 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1748 * data. Consider the optional choice of resending of this data.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 */
Wei Yongjuna000c012011-05-29 23:23:36 +00001750 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1751 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1752 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1754
Wei Yongjuna000c012011-05-29 23:23:36 +00001755 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1756 * and ASCONF-ACK cache.
1757 */
1758 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1759 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1760 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 repl = sctp_make_cookie_ack(new_asoc, chunk);
1763 if (!repl)
1764 goto nomem;
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* Report association restart to upper layer. */
1767 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1768 new_asoc->c.sinit_num_ostreams,
1769 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001770 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (!ev)
1772 goto nomem_ev;
1773
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001774 /* Update the content of current association. */
1775 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1776 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1778 return SCTP_DISPOSITION_CONSUME;
1779
1780nomem_ev:
1781 sctp_chunk_free(repl);
1782nomem:
1783 return SCTP_DISPOSITION_NOMEM;
1784}
1785
1786/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1787 *
1788 * Section 5.2.4
1789 * B) In this case, both sides may be attempting to start an association
1790 * at about the same time but the peer endpoint started its INIT
1791 * after responding to the local endpoint's INIT
1792 */
1793/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001794static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1795 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 const struct sctp_association *asoc,
1797 struct sctp_chunk *chunk,
1798 sctp_cmd_seq_t *commands,
1799 struct sctp_association *new_asoc)
1800{
1801 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 struct sctp_chunk *repl;
1803
1804 /* new_asoc is a brand-new association, so these are not yet
1805 * side effects--it is safe to run them here.
1806 */
1807 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001808 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 GFP_ATOMIC))
1810 goto nomem;
1811
1812 /* Update the content of current association. */
1813 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1814 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1815 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001816 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1818
1819 repl = sctp_make_cookie_ack(new_asoc, chunk);
1820 if (!repl)
1821 goto nomem;
1822
1823 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825 /* RFC 2960 5.1 Normal Establishment of an Association
1826 *
1827 * D) IMPLEMENTATION NOTE: An implementation may choose to
1828 * send the Communication Up notification to the SCTP user
1829 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001830 *
1831 * Sadly, this needs to be implemented as a side-effect, because
1832 * we are not guaranteed to have set the association id of the real
1833 * association and so these notifications need to be delayed until
1834 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Vlad Yasevich07d93962007-05-04 13:55:27 -07001837 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001840 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001842 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001843 *
1844 * This also needs to be done as a side effect for the same reason as
1845 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001847 if (asoc->peer.adaptation_ind)
1848 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 return SCTP_DISPOSITION_CONSUME;
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852nomem:
1853 return SCTP_DISPOSITION_NOMEM;
1854}
1855
1856/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1857 *
1858 * Section 5.2.4
1859 * C) In this case, the local endpoint's cookie has arrived late.
1860 * Before it arrived, the local endpoint sent an INIT and received an
1861 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1862 * but a new tag of its own.
1863 */
1864/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001865static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1866 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 const struct sctp_association *asoc,
1868 struct sctp_chunk *chunk,
1869 sctp_cmd_seq_t *commands,
1870 struct sctp_association *new_asoc)
1871{
1872 /* The cookie should be silently discarded.
1873 * The endpoint SHOULD NOT change states and should leave
1874 * any timers running.
1875 */
1876 return SCTP_DISPOSITION_DISCARD;
1877}
1878
1879/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1880 *
1881 * Section 5.2.4
1882 *
1883 * D) When both local and remote tags match the endpoint should always
1884 * enter the ESTABLISHED state, if it has not already done so.
1885 */
1886/* This case represents an initialization collision. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001887static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1888 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 const struct sctp_association *asoc,
1890 struct sctp_chunk *chunk,
1891 sctp_cmd_seq_t *commands,
1892 struct sctp_association *new_asoc)
1893{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001894 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct sctp_chunk *repl;
1896
1897 /* Clarification from Implementor's Guide:
1898 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001899 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1900 * It should stop any cookie timer that may be running and send
1901 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
1903
1904 /* Don't accidentally move back into established state. */
1905 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1907 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1908 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1909 SCTP_STATE(SCTP_STATE_ESTABLISHED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001910 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1912 SCTP_NULL());
1913
1914 /* RFC 2960 5.1 Normal Establishment of an Association
1915 *
1916 * D) IMPLEMENTATION NOTE: An implementation may choose
1917 * to send the Communication Up notification to the
1918 * SCTP user upon reception of a valid COOKIE
1919 * ECHO chunk.
1920 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001921 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001923 asoc->c.sinit_num_ostreams,
1924 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001925 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 if (!ev)
1927 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001930 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001932 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001934 if (asoc->peer.adaptation_ind) {
1935 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001937 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 goto nomem;
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
1941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 repl = sctp_make_cookie_ack(new_asoc, chunk);
1944 if (!repl)
1945 goto nomem;
1946
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001947 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1948
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001949 if (ev)
1950 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1951 SCTP_ULPEVENT(ev));
1952 if (ai_ev)
1953 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1954 SCTP_ULPEVENT(ai_ev));
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return SCTP_DISPOSITION_CONSUME;
1957
1958nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001959 if (ai_ev)
1960 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (ev)
1962 sctp_ulpevent_free(ev);
1963 return SCTP_DISPOSITION_NOMEM;
1964}
1965
1966/*
1967 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1968 * chunk was retransmitted and then delayed in the network.
1969 *
1970 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1971 *
1972 * Verification Tag: None. Do cookie validation.
1973 *
1974 * Inputs
1975 * (endpoint, asoc, chunk)
1976 *
1977 * Outputs
1978 * (asoc, reply_msg, msg_up, timers, counters)
1979 *
1980 * The return value is the disposition of the chunk.
1981 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00001982sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1983 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 const struct sctp_association *asoc,
1985 const sctp_subtype_t type,
1986 void *arg,
1987 sctp_cmd_seq_t *commands)
1988{
1989 sctp_disposition_t retval;
1990 struct sctp_chunk *chunk = arg;
1991 struct sctp_association *new_asoc;
1992 int error = 0;
1993 char action;
1994 struct sctp_chunk *err_chk_p;
1995
1996 /* Make sure that the chunk has a valid length from the protocol
1997 * perspective. In this case check to make sure we have at least
1998 * enough for the chunk header. Cookie length verification is
1999 * done later.
2000 */
2001 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002002 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 commands);
2004
2005 /* "Decode" the chunk. We have no optional parameters so we
2006 * are in good shape.
2007 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002008 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07002009 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2010 sizeof(sctp_chunkhdr_t)))
2011 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2014 * of a duplicate COOKIE ECHO match the Verification Tags of the
2015 * current association, consider the State Cookie valid even if
2016 * the lifespan is exceeded.
2017 */
2018 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2019 &err_chk_p);
2020
2021 /* FIXME:
2022 * If the re-build failed, what is the proper error path
2023 * from here?
2024 *
2025 * [We should abort the association. --piggy]
2026 */
2027 if (!new_asoc) {
2028 /* FIXME: Several errors are possible. A bad cookie should
2029 * be silently discarded, but think about logging it too.
2030 */
2031 switch (error) {
2032 case -SCTP_IERROR_NOMEM:
2033 goto nomem;
2034
2035 case -SCTP_IERROR_STALE_COOKIE:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002036 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 err_chk_p);
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002038 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 case -SCTP_IERROR_BAD_SIG:
2040 default:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002041 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044
2045 /* Compare the tie_tag in cookie with the verification tag of
2046 * current association.
2047 */
2048 action = sctp_tietags_compare(new_asoc, asoc);
2049
2050 switch (action) {
2051 case 'A': /* Association restart. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002052 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 new_asoc);
2054 break;
2055
2056 case 'B': /* Collision case B. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002057 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 new_asoc);
2059 break;
2060
2061 case 'C': /* Collision case C. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002062 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 new_asoc);
2064 break;
2065
2066 case 'D': /* Collision case D. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002067 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 new_asoc);
2069 break;
2070
2071 default: /* Discard packet for all others. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002072 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076 /* Delete the tempory new association. */
2077 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2078 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2079
Max Matveevd5ccd492011-08-29 21:02:24 +00002080 /* Restore association pointer to provide SCTP command interpeter
2081 * with a valid context in case it needs to manipulate
2082 * the queues */
2083 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2084 SCTP_ASOC((struct sctp_association *)asoc));
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return retval;
2087
2088nomem:
2089 return SCTP_DISPOSITION_NOMEM;
2090}
2091
2092/*
2093 * Process an ABORT. (SHUTDOWN-PENDING state)
2094 *
2095 * See sctp_sf_do_9_1_abort().
2096 */
2097sctp_disposition_t sctp_sf_shutdown_pending_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002098 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 const struct sctp_endpoint *ep,
2100 const struct sctp_association *asoc,
2101 const sctp_subtype_t type,
2102 void *arg,
2103 sctp_cmd_seq_t *commands)
2104{
2105 struct sctp_chunk *chunk = arg;
2106
2107 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002108 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* Make sure that the ABORT chunk has a valid length.
2111 * Since this is an ABORT chunk, we have to discard it
2112 * because of the following text:
2113 * RFC 2960, Section 3.3.7
2114 * If an endpoint receives an ABORT with a format error or for an
2115 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002116 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * as we do not know its true length. So, to be safe, discard the
2118 * packet.
2119 */
2120 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002121 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Vlad Yasevich75205f42007-12-20 14:12:59 -08002123 /* ADD-IP: Special case for ABORT chunks
2124 * F4) One special consideration is that ABORT Chunks arriving
2125 * destined to the IP address being deleted MUST be
2126 * ignored (see Section 5.3.1 for further details).
2127 */
2128 if (SCTP_ADDR_DEL ==
2129 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002130 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002131
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002132 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135/*
2136 * Process an ABORT. (SHUTDOWN-SENT state)
2137 *
2138 * See sctp_sf_do_9_1_abort().
2139 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002140sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2141 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 const struct sctp_association *asoc,
2143 const sctp_subtype_t type,
2144 void *arg,
2145 sctp_cmd_seq_t *commands)
2146{
2147 struct sctp_chunk *chunk = arg;
2148
2149 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002150 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 /* Make sure that the ABORT chunk has a valid length.
2153 * Since this is an ABORT chunk, we have to discard it
2154 * because of the following text:
2155 * RFC 2960, Section 3.3.7
2156 * If an endpoint receives an ABORT with a format error or for an
2157 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002158 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 * as we do not know its true length. So, to be safe, discard the
2160 * packet.
2161 */
2162 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002163 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Vlad Yasevich75205f42007-12-20 14:12:59 -08002165 /* ADD-IP: Special case for ABORT chunks
2166 * F4) One special consideration is that ABORT Chunks arriving
2167 * destined to the IP address being deleted MUST be
2168 * ignored (see Section 5.3.1 for further details).
2169 */
2170 if (SCTP_ADDR_DEL ==
2171 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002172 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 /* Stop the T2-shutdown timer. */
2175 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2176 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2177
2178 /* Stop the T5-shutdown guard timer. */
2179 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2180 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2181
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002182 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185/*
2186 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2187 *
2188 * See sctp_sf_do_9_1_abort().
2189 */
2190sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002191 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 const struct sctp_endpoint *ep,
2193 const struct sctp_association *asoc,
2194 const sctp_subtype_t type,
2195 void *arg,
2196 sctp_cmd_seq_t *commands)
2197{
2198 /* The same T2 timer, so we should be able to use
2199 * common function with the SHUTDOWN-SENT state.
2200 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002201 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204/*
2205 * Handle an Error received in COOKIE_ECHOED state.
2206 *
2207 * Only handle the error type of stale COOKIE Error, the other errors will
2208 * be ignored.
2209 *
2210 * Inputs
2211 * (endpoint, asoc, chunk)
2212 *
2213 * Outputs
2214 * (asoc, reply_msg, msg_up, timers, counters)
2215 *
2216 * The return value is the disposition of the chunk.
2217 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002218sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2219 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 const struct sctp_association *asoc,
2221 const sctp_subtype_t type,
2222 void *arg,
2223 sctp_cmd_seq_t *commands)
2224{
2225 struct sctp_chunk *chunk = arg;
2226 sctp_errhdr_t *err;
2227
2228 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002229 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 /* Make sure that the ERROR chunk has a valid length.
2232 * The parameter walking depends on this as well.
2233 */
2234 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002235 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 commands);
2237
2238 /* Process the error here */
2239 /* FUTURE FIXME: When PR-SCTP related and other optional
2240 * parms are emitted, this will have to change to handle multiple
2241 * errors.
2242 */
2243 sctp_walk_errors(err, chunk->chunk_hdr) {
2244 if (SCTP_ERROR_STALE_COOKIE == err->cause)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002245 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 arg, commands);
2247 }
2248
2249 /* It is possible to have malformed error causes, and that
2250 * will cause us to end the walk early. However, since
2251 * we are discarding the packet, there should be no adverse
2252 * affects.
2253 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002254 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
2257/*
2258 * Handle a Stale COOKIE Error
2259 *
2260 * Section: 5.2.6 Handle Stale COOKIE Error
2261 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2262 * one of the following three alternatives.
2263 * ...
2264 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2265 * Preservative parameter requesting an extension to the lifetime of
2266 * the State Cookie. When calculating the time extension, an
2267 * implementation SHOULD use the RTT information measured based on the
2268 * previous COOKIE ECHO / ERROR exchange, and should add no more
2269 * than 1 second beyond the measured RTT, due to long State Cookie
2270 * lifetimes making the endpoint more subject to a replay attack.
2271 *
2272 * Verification Tag: Not explicit, but safe to ignore.
2273 *
2274 * Inputs
2275 * (endpoint, asoc, chunk)
2276 *
2277 * Outputs
2278 * (asoc, reply_msg, msg_up, timers, counters)
2279 *
2280 * The return value is the disposition of the chunk.
2281 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002282static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2283 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 const struct sctp_association *asoc,
2285 const sctp_subtype_t type,
2286 void *arg,
2287 sctp_cmd_seq_t *commands)
2288{
2289 struct sctp_chunk *chunk = arg;
2290 time_t stale;
2291 sctp_cookie_preserve_param_t bht;
2292 sctp_errhdr_t *err;
2293 struct sctp_chunk *reply;
2294 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002295 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Vlad Yasevich81845c22006-01-30 15:59:54 -08002297 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002298 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2299 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002301 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return SCTP_DISPOSITION_DELETE_TCB;
2303 }
2304
2305 err = (sctp_errhdr_t *)(chunk->skb->data);
2306
2307 /* When calculating the time extension, an implementation
2308 * SHOULD use the RTT information measured based on the
2309 * previous COOKIE ECHO / ERROR exchange, and should add no
2310 * more than 1 second beyond the measured RTT, due to long
2311 * State Cookie lifetimes making the endpoint more subject to
2312 * a replay attack.
2313 * Measure of Staleness's unit is usec. (1/1000000 sec)
2314 * Suggested Cookie Life-span Increment's unit is msec.
2315 * (1/1000 sec)
2316 * In general, if you use the suggested cookie life, the value
2317 * found in the field of measure of staleness should be doubled
2318 * to give ample time to retransmit the new cookie and thus
2319 * yield a higher probability of success on the reattempt.
2320 */
Al Viro34bcca22006-11-20 17:27:15 -08002321 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 stale = (stale * 2) / 1000;
2323
2324 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2325 bht.param_hdr.length = htons(sizeof(bht));
2326 bht.lifespan_increment = htonl(stale);
2327
2328 /* Build that new INIT chunk. */
2329 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2330 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2331 if (!reply)
2332 goto nomem;
2333
2334 sctp_addto_chunk(reply, sizeof(bht), &bht);
2335
2336 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2337 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2338
2339 /* Stop pending T3-rtx and heartbeat timers */
2340 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2341 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2342
2343 /* Delete non-primary peer ip addresses since we are transitioning
2344 * back to the COOKIE-WAIT state
2345 */
2346 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2347
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002348 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2349 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002351 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 SCTP_TRANSPORT(asoc->peer.primary_path));
2353
2354 /* Cast away the const modifier, as we want to just
2355 * rerun it through as a sideffect.
2356 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002357 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2360 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2361 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2362 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2364 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2365
2366 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2367
2368 return SCTP_DISPOSITION_CONSUME;
2369
2370nomem:
2371 return SCTP_DISPOSITION_NOMEM;
2372}
2373
2374/*
2375 * Process an ABORT.
2376 *
2377 * Section: 9.1
2378 * After checking the Verification Tag, the receiving endpoint shall
2379 * remove the association from its record, and shall report the
2380 * termination to its upper layer.
2381 *
2382 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2383 * B) Rules for packet carrying ABORT:
2384 *
2385 * - The endpoint shall always fill in the Verification Tag field of the
2386 * outbound packet with the destination endpoint's tag value if it
2387 * is known.
2388 *
2389 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2390 * MUST follow the procedure described in Section 8.4.
2391 *
2392 * - The receiver MUST accept the packet if the Verification Tag
2393 * matches either its own tag, OR the tag of its peer. Otherwise, the
2394 * receiver MUST silently discard the packet and take no further
2395 * action.
2396 *
2397 * Inputs
2398 * (endpoint, asoc, chunk)
2399 *
2400 * Outputs
2401 * (asoc, reply_msg, msg_up, timers, counters)
2402 *
2403 * The return value is the disposition of the chunk.
2404 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002405sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2406 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 const struct sctp_association *asoc,
2408 const sctp_subtype_t type,
2409 void *arg,
2410 sctp_cmd_seq_t *commands)
2411{
2412 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
2414 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002415 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 /* Make sure that the ABORT chunk has a valid length.
2418 * Since this is an ABORT chunk, we have to discard it
2419 * because of the following text:
2420 * RFC 2960, Section 3.3.7
2421 * If an endpoint receives an ABORT with a format error or for an
2422 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002423 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 * as we do not know its true length. So, to be safe, discard the
2425 * packet.
2426 */
2427 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002428 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Vlad Yasevich75205f42007-12-20 14:12:59 -08002430 /* ADD-IP: Special case for ABORT chunks
2431 * F4) One special consideration is that ABORT Chunks arriving
2432 * destined to the IP address being deleted MUST be
2433 * ignored (see Section 5.3.1 for further details).
2434 */
2435 if (SCTP_ADDR_DEL ==
2436 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002437 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002438
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002439 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
Vlad Yasevich75205f42007-12-20 14:12:59 -08002440}
2441
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002442static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2443 const struct sctp_endpoint *ep,
Vlad Yasevich75205f42007-12-20 14:12:59 -08002444 const struct sctp_association *asoc,
2445 const sctp_subtype_t type,
2446 void *arg,
2447 sctp_cmd_seq_t *commands)
2448{
2449 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002450 unsigned int len;
Vlad Yasevich75205f42007-12-20 14:12:59 -08002451 __be16 error = SCTP_ERROR_NO_ERROR;
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /* See if we have an error cause code in the chunk. */
2454 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002455 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2456
2457 sctp_errhdr_t *err;
2458 sctp_walk_errors(err, chunk->chunk_hdr);
2459 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002460 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Shan Wei96ca4682011-04-19 21:26:26 +00002461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002465 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002466 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002467 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00002468 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2469 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 return SCTP_DISPOSITION_ABORT;
2472}
2473
2474/*
2475 * Process an ABORT. (COOKIE-WAIT state)
2476 *
2477 * See sctp_sf_do_9_1_abort() above.
2478 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002479sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2480 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 const struct sctp_association *asoc,
2482 const sctp_subtype_t type,
2483 void *arg,
2484 sctp_cmd_seq_t *commands)
2485{
2486 struct sctp_chunk *chunk = arg;
Eric Dumazet95c96172012-04-15 05:58:06 +00002487 unsigned int len;
Al Virof94c0192006-11-20 17:00:25 -08002488 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490 if (!sctp_vtag_verify_either(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002491 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 /* Make sure that the ABORT chunk has a valid length.
2494 * Since this is an ABORT chunk, we have to discard it
2495 * because of the following text:
2496 * RFC 2960, Section 3.3.7
2497 * If an endpoint receives an ABORT with a format error or for an
2498 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002499 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 * as we do not know its true length. So, to be safe, discard the
2501 * packet.
2502 */
2503 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002504 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
2506 /* See if we have an error cause code in the chunk. */
2507 len = ntohs(chunk->chunk_hdr->length);
2508 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2509 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2510
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002511 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002512 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514
2515/*
2516 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2517 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002518sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2519 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 const struct sctp_association *asoc,
2521 const sctp_subtype_t type,
2522 void *arg,
2523 sctp_cmd_seq_t *commands)
2524{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002525 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002526 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002527 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528}
2529
2530/*
2531 * Process an ABORT. (COOKIE-ECHOED state)
2532 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002533sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2534 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 const struct sctp_association *asoc,
2536 const sctp_subtype_t type,
2537 void *arg,
2538 sctp_cmd_seq_t *commands)
2539{
2540 /* There is a single T1 timer, so we should be able to use
2541 * common function with the COOKIE-WAIT state.
2542 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002543 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
2546/*
2547 * Stop T1 timer and abort association with "INIT failed".
2548 *
2549 * This is common code called by several sctp_sf_*_abort() functions above.
2550 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002551static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2552 sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002553 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002554 const struct sctp_association *asoc,
2555 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002557 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2559 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002560 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2562 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002563 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /* CMD_INIT_FAILED will DELETE_TCB. */
2565 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002566 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002567 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570/*
2571 * sctp_sf_do_9_2_shut
2572 *
2573 * Section: 9.2
2574 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2575 * - enter the SHUTDOWN-RECEIVED state,
2576 *
2577 * - stop accepting new data from its SCTP user
2578 *
2579 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2580 * that all its outstanding DATA chunks have been received by the
2581 * SHUTDOWN sender.
2582 *
2583 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2584 * send a SHUTDOWN in response to a ULP request. And should discard
2585 * subsequent SHUTDOWN chunks.
2586 *
2587 * If there are still outstanding DATA chunks left, the SHUTDOWN
2588 * receiver shall continue to follow normal data transmission
2589 * procedures defined in Section 6 until all outstanding DATA chunks
2590 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2591 * new data from its SCTP user.
2592 *
2593 * Verification Tag: 8.5 Verification Tag [Normal verification]
2594 *
2595 * Inputs
2596 * (endpoint, asoc, chunk)
2597 *
2598 * Outputs
2599 * (asoc, reply_msg, msg_up, timers, counters)
2600 *
2601 * The return value is the disposition of the chunk.
2602 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002603sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2604 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 const struct sctp_association *asoc,
2606 const sctp_subtype_t type,
2607 void *arg,
2608 sctp_cmd_seq_t *commands)
2609{
2610 struct sctp_chunk *chunk = arg;
2611 sctp_shutdownhdr_t *sdh;
2612 sctp_disposition_t disposition;
2613 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002614 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002617 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 /* Make sure that the SHUTDOWN chunk has a valid length. */
2620 if (!sctp_chunk_length_valid(chunk,
2621 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002622 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 commands);
2624
2625 /* Convert the elaborate header. */
2626 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2627 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2628 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002629 ctsn = ntohl(sdh->cum_tsn_ack);
2630
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002631 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2632 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2633 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2634 return SCTP_DISPOSITION_DISCARD;
2635 }
2636
Wei Yongjundf10eec2008-10-23 01:00:21 -07002637 /* If Cumulative TSN Ack beyond the max tsn currently
2638 * send, terminating the association and respond to the
2639 * sender with an ABORT.
2640 */
2641 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002642 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002644 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2645 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2646 * inform the application that it should cease sending data.
2647 */
2648 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2649 if (!ev) {
2650 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002651 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002652 }
2653 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2656 * - enter the SHUTDOWN-RECEIVED state,
2657 * - stop accepting new data from its SCTP user
2658 *
2659 * [This is implicit in the new state.]
2660 */
2661 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2662 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2663 disposition = SCTP_DISPOSITION_CONSUME;
2664
2665 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002666 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 arg, commands);
2668 }
2669
2670 if (SCTP_DISPOSITION_NOMEM == disposition)
2671 goto out;
2672
2673 /* - verify, by checking the Cumulative TSN Ack field of the
2674 * chunk, that all its outstanding DATA chunks have been
2675 * received by the SHUTDOWN sender.
2676 */
2677 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002678 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680out:
2681 return disposition;
2682}
2683
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002684/*
2685 * sctp_sf_do_9_2_shut_ctsn
2686 *
2687 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2688 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2689 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2690 * MUST be processed.
2691 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002692sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2693 const struct sctp_endpoint *ep,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002694 const struct sctp_association *asoc,
2695 const sctp_subtype_t type,
2696 void *arg,
2697 sctp_cmd_seq_t *commands)
2698{
2699 struct sctp_chunk *chunk = arg;
2700 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002701 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002702
2703 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002704 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002705
2706 /* Make sure that the SHUTDOWN chunk has a valid length. */
2707 if (!sctp_chunk_length_valid(chunk,
2708 sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002709 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002710 commands);
2711
2712 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002713 ctsn = ntohl(sdh->cum_tsn_ack);
2714
2715 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2716 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2717 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2718 return SCTP_DISPOSITION_DISCARD;
2719 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002720
2721 /* If Cumulative TSN Ack beyond the max tsn currently
2722 * send, terminating the association and respond to the
2723 * sender with an ABORT.
2724 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002725 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002726 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002727
2728 /* verify, by checking the Cumulative TSN Ack field of the
2729 * chunk, that all its outstanding DATA chunks have been
2730 * received by the SHUTDOWN sender.
2731 */
2732 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2733 SCTP_BE32(sdh->cum_tsn_ack));
2734
2735 return SCTP_DISPOSITION_CONSUME;
2736}
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738/* RFC 2960 9.2
2739 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2740 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2741 * transport addresses (either in the IP addresses or in the INIT chunk)
2742 * that belong to this association, it should discard the INIT chunk and
2743 * retransmit the SHUTDOWN ACK chunk.
2744 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002745sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2746 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 const struct sctp_association *asoc,
2748 const sctp_subtype_t type,
2749 void *arg,
2750 sctp_cmd_seq_t *commands)
2751{
2752 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2753 struct sctp_chunk *reply;
2754
Vlad Yasevichece25df2007-09-07 16:30:54 -04002755 /* Make sure that the chunk has a valid length */
2756 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002757 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04002758 commands);
2759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /* Since we are not going to really process this INIT, there
2761 * is no point in verifying chunk boundries. Just generate
2762 * the SHUTDOWN ACK.
2763 */
2764 reply = sctp_make_shutdown_ack(asoc, chunk);
2765 if (NULL == reply)
2766 goto nomem;
2767
2768 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2769 * the T2-SHUTDOWN timer.
2770 */
2771 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2772
2773 /* and restart the T2-shutdown timer. */
2774 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2775 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2776
2777 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2778
2779 return SCTP_DISPOSITION_CONSUME;
2780nomem:
2781 return SCTP_DISPOSITION_NOMEM;
2782}
2783
2784/*
2785 * sctp_sf_do_ecn_cwr
2786 *
2787 * Section: Appendix A: Explicit Congestion Notification
2788 *
2789 * CWR:
2790 *
2791 * RFC 2481 details a specific bit for a sender to send in the header of
2792 * its next outbound TCP segment to indicate to its peer that it has
2793 * reduced its congestion window. This is termed the CWR bit. For
2794 * SCTP the same indication is made by including the CWR chunk.
2795 * This chunk contains one data element, i.e. the TSN number that
2796 * was sent in the ECNE chunk. This element represents the lowest
2797 * TSN number in the datagram that was originally marked with the
2798 * CE bit.
2799 *
2800 * Verification Tag: 8.5 Verification Tag [Normal verification]
2801 * Inputs
2802 * (endpoint, asoc, chunk)
2803 *
2804 * Outputs
2805 * (asoc, reply_msg, msg_up, timers, counters)
2806 *
2807 * The return value is the disposition of the chunk.
2808 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002809sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2810 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 const struct sctp_association *asoc,
2812 const sctp_subtype_t type,
2813 void *arg,
2814 sctp_cmd_seq_t *commands)
2815{
2816 sctp_cwrhdr_t *cwr;
2817 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002818 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
2820 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002821 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002824 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2828 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2829
Al Viro34bcca22006-11-20 17:27:15 -08002830 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002833 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /* Stop sending ECNE. */
2835 sctp_add_cmd_sf(commands,
2836 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002837 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
2839 return SCTP_DISPOSITION_CONSUME;
2840}
2841
2842/*
2843 * sctp_sf_do_ecne
2844 *
2845 * Section: Appendix A: Explicit Congestion Notification
2846 *
2847 * ECN-Echo
2848 *
2849 * RFC 2481 details a specific bit for a receiver to send back in its
2850 * TCP acknowledgements to notify the sender of the Congestion
2851 * Experienced (CE) bit having arrived from the network. For SCTP this
2852 * same indication is made by including the ECNE chunk. This chunk
2853 * contains one data element, i.e. the lowest TSN associated with the IP
2854 * datagram marked with the CE bit.....
2855 *
2856 * Verification Tag: 8.5 Verification Tag [Normal verification]
2857 * Inputs
2858 * (endpoint, asoc, chunk)
2859 *
2860 * Outputs
2861 * (asoc, reply_msg, msg_up, timers, counters)
2862 *
2863 * The return value is the disposition of the chunk.
2864 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002865sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2866 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 const struct sctp_association *asoc,
2868 const sctp_subtype_t type,
2869 void *arg,
2870 sctp_cmd_seq_t *commands)
2871{
2872 sctp_ecnehdr_t *ecne;
2873 struct sctp_chunk *chunk = arg;
2874
2875 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002876 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002879 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 commands);
2881
2882 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2883 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2884
2885 /* If this is a newer ECNE than the last CWR packet we sent out */
2886 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2887 SCTP_U32(ntohl(ecne->lowest_tsn)));
2888
2889 return SCTP_DISPOSITION_CONSUME;
2890}
2891
2892/*
2893 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2894 *
2895 * The SCTP endpoint MUST always acknowledge the reception of each valid
2896 * DATA chunk.
2897 *
2898 * The guidelines on delayed acknowledgement algorithm specified in
2899 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2900 * acknowledgement SHOULD be generated for at least every second packet
2901 * (not every second DATA chunk) received, and SHOULD be generated within
2902 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2903 * situations it may be beneficial for an SCTP transmitter to be more
2904 * conservative than the algorithms detailed in this document allow.
2905 * However, an SCTP transmitter MUST NOT be more aggressive than the
2906 * following algorithms allow.
2907 *
2908 * A SCTP receiver MUST NOT generate more than one SACK for every
2909 * incoming packet, other than to update the offered window as the
2910 * receiving application consumes new data.
2911 *
2912 * Verification Tag: 8.5 Verification Tag [Normal verification]
2913 *
2914 * Inputs
2915 * (endpoint, asoc, chunk)
2916 *
2917 * Outputs
2918 * (asoc, reply_msg, msg_up, timers, counters)
2919 *
2920 * The return value is the disposition of the chunk.
2921 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002922sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2923 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 const struct sctp_association *asoc,
2925 const sctp_subtype_t type,
2926 void *arg,
2927 sctp_cmd_seq_t *commands)
2928{
2929 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002930 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 int error;
2932
2933 if (!sctp_vtag_verify(chunk, asoc)) {
2934 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2935 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002936 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002940 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 commands);
2942
2943 error = sctp_eat_data(asoc, chunk, commands );
2944 switch (error) {
2945 case SCTP_IERROR_NO_ERROR:
2946 break;
2947 case SCTP_IERROR_HIGH_TSN:
2948 case SCTP_IERROR_BAD_STREAM:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002949 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto discard_noforce;
2951 case SCTP_IERROR_DUP_TSN:
2952 case SCTP_IERROR_IGNORE_TSN:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002953 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 goto discard_force;
2955 case SCTP_IERROR_NO_DATA:
2956 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002957 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00002958 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002959 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 default:
2961 BUG();
2962 }
2963
Wei Yongjun6dc76942009-11-23 15:53:53 -05002964 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2965 force = SCTP_FORCE();
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (asoc->autoclose) {
2968 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2969 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2970 }
2971
2972 /* If this is the last chunk in a packet, we need to count it
2973 * toward sack generation. Note that we need to SACK every
2974 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2975 * THEM. We elect to NOT generate SACK's if the chunk fails
2976 * the verification tag test.
2977 *
2978 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2979 *
2980 * The SCTP endpoint MUST always acknowledge the reception of
2981 * each valid DATA chunk.
2982 *
2983 * The guidelines on delayed acknowledgement algorithm
2984 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2985 * Specifically, an acknowledgement SHOULD be generated for at
2986 * least every second packet (not every second DATA chunk)
2987 * received, and SHOULD be generated within 200 ms of the
2988 * arrival of any unacknowledged DATA chunk. In some
2989 * situations it may be beneficial for an SCTP transmitter to
2990 * be more conservative than the algorithms detailed in this
2991 * document allow. However, an SCTP transmitter MUST NOT be
2992 * more aggressive than the following algorithms allow.
2993 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002994 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002995 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return SCTP_DISPOSITION_CONSUME;
2998
2999discard_force:
3000 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3001 *
3002 * When a packet arrives with duplicate DATA chunk(s) and with
3003 * no new DATA chunk(s), the endpoint MUST immediately send a
3004 * SACK with no delay. If a packet arrives with duplicate
3005 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3006 * MAY immediately send a SACK. Normally receipt of duplicate
3007 * DATA chunks will occur when the original SACK chunk was lost
3008 * and the peer's RTO has expired. The duplicate TSN number(s)
3009 * SHOULD be reported in the SACK as duplicate.
3010 */
3011 /* In our case, we split the MAY SACK advice up whether or not
3012 * the last chunk is a duplicate.'
3013 */
3014 if (chunk->end_of_packet)
3015 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3016 return SCTP_DISPOSITION_DISCARD;
3017
3018discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08003019 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05003020 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return SCTP_DISPOSITION_DISCARD;
3023consume:
3024 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
3028/*
3029 * sctp_sf_eat_data_fast_4_4
3030 *
3031 * Section: 4 (4)
3032 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3033 * DATA chunks without delay.
3034 *
3035 * Verification Tag: 8.5 Verification Tag [Normal verification]
3036 * Inputs
3037 * (endpoint, asoc, chunk)
3038 *
3039 * Outputs
3040 * (asoc, reply_msg, msg_up, timers, counters)
3041 *
3042 * The return value is the disposition of the chunk.
3043 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003044sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3045 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 const struct sctp_association *asoc,
3047 const sctp_subtype_t type,
3048 void *arg,
3049 sctp_cmd_seq_t *commands)
3050{
3051 struct sctp_chunk *chunk = arg;
3052 int error;
3053
3054 if (!sctp_vtag_verify(chunk, asoc)) {
3055 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3056 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003057 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059
3060 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003061 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 commands);
3063
3064 error = sctp_eat_data(asoc, chunk, commands );
3065 switch (error) {
3066 case SCTP_IERROR_NO_ERROR:
3067 case SCTP_IERROR_HIGH_TSN:
3068 case SCTP_IERROR_DUP_TSN:
3069 case SCTP_IERROR_IGNORE_TSN:
3070 case SCTP_IERROR_BAD_STREAM:
3071 break;
3072 case SCTP_IERROR_NO_DATA:
3073 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003074 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003075 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003076 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 default:
3078 BUG();
3079 }
3080
3081 /* Go a head and force a SACK, since we are shutting down. */
3082
3083 /* Implementor's Guide.
3084 *
3085 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3086 * respond to each received packet containing one or more DATA chunk(s)
3087 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3088 */
3089 if (chunk->end_of_packet) {
3090 /* We must delay the chunk creation since the cumulative
3091 * TSN has not been updated yet.
3092 */
3093 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3094 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3095 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3096 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3097 }
3098
3099consume:
3100 return SCTP_DISPOSITION_CONSUME;
3101}
3102
3103/*
3104 * Section: 6.2 Processing a Received SACK
3105 * D) Any time a SACK arrives, the endpoint performs the following:
3106 *
3107 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3108 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3109 * increasing, a SACK whose Cumulative TSN Ack is less than the
3110 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3111 *
3112 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3113 * of bytes still outstanding after processing the Cumulative TSN Ack
3114 * and the Gap Ack Blocks.
3115 *
3116 * iii) If the SACK is missing a TSN that was previously
3117 * acknowledged via a Gap Ack Block (e.g., the data receiver
3118 * reneged on the data), then mark the corresponding DATA chunk
3119 * as available for retransmit: Mark it as missing for fast
3120 * retransmit as described in Section 7.2.4 and if no retransmit
3121 * timer is running for the destination address to which the DATA
3122 * chunk was originally transmitted, then T3-rtx is started for
3123 * that destination address.
3124 *
3125 * Verification Tag: 8.5 Verification Tag [Normal verification]
3126 *
3127 * Inputs
3128 * (endpoint, asoc, chunk)
3129 *
3130 * Outputs
3131 * (asoc, reply_msg, msg_up, timers, counters)
3132 *
3133 * The return value is the disposition of the chunk.
3134 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003135sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3136 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 const struct sctp_association *asoc,
3138 const sctp_subtype_t type,
3139 void *arg,
3140 sctp_cmd_seq_t *commands)
3141{
3142 struct sctp_chunk *chunk = arg;
3143 sctp_sackhdr_t *sackh;
3144 __u32 ctsn;
3145
3146 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003147 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 /* Make sure that the SACK chunk has a valid length. */
3150 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003151 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 commands);
3153
3154 /* Pull the SACK chunk from the data buffer */
3155 sackh = sctp_sm_pull_sack(chunk);
3156 /* Was this a bogus SACK? */
3157 if (!sackh)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003158 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 chunk->subh.sack_hdr = sackh;
3160 ctsn = ntohl(sackh->cum_tsn_ack);
3161
3162 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3163 * Ack Point, then drop the SACK. Since Cumulative TSN
3164 * Ack is monotonically increasing, a SACK whose
3165 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3166 * Point indicates an out-of-order SACK.
3167 */
3168 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3169 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3170 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3171 return SCTP_DISPOSITION_DISCARD;
3172 }
3173
Wei Yongjunaecedea2007-08-02 16:57:44 +08003174 /* If Cumulative TSN Ack beyond the max tsn currently
3175 * send, terminating the association and respond to the
3176 * sender with an ABORT.
3177 */
3178 if (!TSN_lt(ctsn, asoc->next_tsn))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003179 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
Wei Yongjunaecedea2007-08-02 16:57:44 +08003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 /* Return this SACK for further processing. */
3182 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3183
3184 /* Note: We do the rest of the work on the PROCESS_SACK
3185 * sideeffect.
3186 */
3187 return SCTP_DISPOSITION_CONSUME;
3188}
3189
3190/*
3191 * Generate an ABORT in response to a packet.
3192 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003193 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003195 * 8) The receiver should respond to the sender of the OOTB packet with
3196 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3197 * MUST fill in the Verification Tag field of the outbound packet
3198 * with the value found in the Verification Tag field of the OOTB
3199 * packet and set the T-bit in the Chunk Flags to indicate that the
3200 * Verification Tag is reflected. After sending this ABORT, the
3201 * receiver of the OOTB packet shall discard the OOTB packet and take
3202 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 *
3204 * Verification Tag:
3205 *
3206 * The return value is the disposition of the chunk.
3207*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003208static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3209 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 const struct sctp_association *asoc,
3211 const sctp_subtype_t type,
3212 void *arg,
3213 sctp_cmd_seq_t *commands)
3214{
3215 struct sctp_packet *packet = NULL;
3216 struct sctp_chunk *chunk = arg;
3217 struct sctp_chunk *abort;
3218
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003219 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221 if (packet) {
3222 /* Make an ABORT. The T bit will be set if the asoc
3223 * is NULL.
3224 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003225 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (!abort) {
3227 sctp_ootb_pkt_free(packet);
3228 return SCTP_DISPOSITION_NOMEM;
3229 }
3230
Jerome Forissier047a2422005-04-28 11:58:43 -07003231 /* Reflect vtag if T-Bit is set */
3232 if (sctp_test_T_bit(abort))
3233 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 /* Set the skb to the belonging sock for accounting. */
3236 abort->skb->sk = ep->base.sk;
3237
3238 sctp_packet_append_chunk(packet, abort);
3239
3240 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3241 SCTP_PACKET(packet));
3242
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003243 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003245 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 return SCTP_DISPOSITION_CONSUME;
3247 }
3248
3249 return SCTP_DISPOSITION_NOMEM;
3250}
3251
3252/*
3253 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3254 * event as ULP notification for each cause included in the chunk.
3255 *
3256 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3257 *
3258 * The return value is the disposition of the chunk.
3259*/
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003260sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3261 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 const struct sctp_association *asoc,
3263 const sctp_subtype_t type,
3264 void *arg,
3265 sctp_cmd_seq_t *commands)
3266{
3267 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003268 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269
3270 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003271 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273 /* Make sure that the ERROR chunk has a valid length. */
3274 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003275 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003277 sctp_walk_errors(err, chunk->chunk_hdr);
3278 if ((void *)err != (void *)chunk->chunk_end)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003279 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Shan Wei8a00be12011-04-19 21:25:40 +00003280 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281
Wei Yongjun3df26782009-03-02 06:46:51 +00003282 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3283 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
3288/*
3289 * Process an inbound SHUTDOWN ACK.
3290 *
3291 * From Section 9.2:
3292 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3293 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3294 * peer, and remove all record of the association.
3295 *
3296 * The return value is the disposition.
3297 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003298sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3299 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 const struct sctp_association *asoc,
3301 const sctp_subtype_t type,
3302 void *arg,
3303 sctp_cmd_seq_t *commands)
3304{
3305 struct sctp_chunk *chunk = arg;
3306 struct sctp_chunk *reply;
3307 struct sctp_ulpevent *ev;
3308
3309 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003310 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3313 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003314 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /* 10.2 H) SHUTDOWN COMPLETE notification
3317 *
3318 * When SCTP completes the shutdown procedures (section 9.2) this
3319 * notification is passed to the upper layer.
3320 */
3321 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003322 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (!ev)
3324 goto nomem;
3325
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003326 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3327 reply = sctp_make_shutdown_complete(asoc, chunk);
3328 if (!reply)
3329 goto nomem_chunk;
3330
3331 /* Do all the commands now (after allocation), so that we
3332 * have consistent state if memory allocation failes
3333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3335
3336 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3337 * stop the T2-shutdown timer,
3338 */
3339 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3340 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3341
3342 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3343 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3346 SCTP_STATE(SCTP_STATE_CLOSED));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003347 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3348 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3350
3351 /* ...and remove all record of the association. */
3352 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3353 return SCTP_DISPOSITION_DELETE_TCB;
3354
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003355nomem_chunk:
3356 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357nomem:
3358 return SCTP_DISPOSITION_NOMEM;
3359}
3360
3361/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003362 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3363 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3365 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3366 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3367 * packet must fill in the Verification Tag field of the outbound
3368 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003369 * set the T-bit in the Chunk Flags to indicate that the Verification
3370 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 *
3372 * 8) The receiver should respond to the sender of the OOTB packet with
3373 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3374 * MUST fill in the Verification Tag field of the outbound packet
3375 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003376 * packet and set the T-bit in the Chunk Flags to indicate that the
3377 * Verification Tag is reflected. After sending this ABORT, the
3378 * receiver of the OOTB packet shall discard the OOTB packet and take
3379 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003381sctp_disposition_t sctp_sf_ootb(struct net *net,
3382 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 const struct sctp_association *asoc,
3384 const sctp_subtype_t type,
3385 void *arg,
3386 sctp_cmd_seq_t *commands)
3387{
3388 struct sctp_chunk *chunk = arg;
3389 struct sk_buff *skb = chunk->skb;
3390 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003391 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 __u8 *ch_end;
3393 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003394 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003396 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
3398 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3399 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003400 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003402 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003403 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404
Vlad Yasevichece25df2007-09-07 16:30:54 -04003405 /* Now that we know we at least have a chunk header,
3406 * do things that are type appropriate.
3407 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3409 ootb_shut_ack = 1;
3410
3411 /* RFC 2960, Section 3.3.7
3412 * Moreover, under any circumstances, an endpoint that
3413 * receives an ABORT MUST NOT respond to that ABORT by
3414 * sending an ABORT of its own.
3415 */
3416 if (SCTP_CID_ABORT == ch->type)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003417 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003418
Shan Wei85c5ed42011-04-19 21:30:01 +00003419 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3420 * or a COOKIE ACK the SCTP Packet should be silently
3421 * discarded.
3422 */
3423
3424 if (SCTP_CID_COOKIE_ACK == ch->type)
3425 ootb_cookie_ack = 1;
3426
3427 if (SCTP_CID_ERROR == ch->type) {
3428 sctp_walk_errors(err, ch) {
3429 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3430 ootb_cookie_ack = 1;
3431 break;
3432 }
3433 }
3434 }
3435
Vlad Yasevichece25df2007-09-07 16:30:54 -04003436 /* Report violation if chunk len overflows */
3437 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3438 if (ch_end > skb_tail_pointer(skb))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003439 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003440 commands);
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003443 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 if (ootb_shut_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003446 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003447 else if (ootb_cookie_ack)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003448 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 else
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003450 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
3453/*
3454 * Handle an "Out of the blue" SHUTDOWN ACK.
3455 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003456 * Section: 8.4 5, sctpimpguide 2.41.
3457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003459 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3460 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3461 * packet must fill in the Verification Tag field of the outbound
3462 * packet with the Verification Tag received in the SHUTDOWN ACK and
3463 * set the T-bit in the Chunk Flags to indicate that the Verification
3464 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 *
3466 * Inputs
3467 * (endpoint, asoc, type, arg, commands)
3468 *
3469 * Outputs
3470 * (sctp_disposition_t)
3471 *
3472 * The return value is the disposition of the chunk.
3473 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003474static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3475 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 const struct sctp_association *asoc,
3477 const sctp_subtype_t type,
3478 void *arg,
3479 sctp_cmd_seq_t *commands)
3480{
3481 struct sctp_packet *packet = NULL;
3482 struct sctp_chunk *chunk = arg;
3483 struct sctp_chunk *shut;
3484
Eric W. Biederman2ce95502012-08-06 08:43:06 +00003485 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 if (packet) {
3488 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003489 * The T bit will be set if the asoc is NULL.
3490 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 shut = sctp_make_shutdown_complete(asoc, chunk);
3492 if (!shut) {
3493 sctp_ootb_pkt_free(packet);
3494 return SCTP_DISPOSITION_NOMEM;
3495 }
3496
Jerome Forissier047a2422005-04-28 11:58:43 -07003497 /* Reflect vtag if T-Bit is set */
3498 if (sctp_test_T_bit(shut))
3499 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 /* Set the skb to the belonging sock for accounting. */
3502 shut->skb->sk = ep->base.sk;
3503
3504 sctp_packet_append_chunk(packet, shut);
3505
3506 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3507 SCTP_PACKET(packet));
3508
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003509 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
3511 /* If the chunk length is invalid, we don't want to process
3512 * the reset of the packet.
3513 */
3514 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003515 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003517 /* We need to discard the rest of the packet to prevent
3518 * potential bomming attacks from additional bundled chunks.
3519 * This is documented in SCTP Threats ID.
3520 */
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 }
3523
3524 return SCTP_DISPOSITION_NOMEM;
3525}
3526
3527/*
3528 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3529 *
3530 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3531 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3532 * procedures in section 8.4 SHOULD be followed, in other words it
3533 * should be treated as an Out Of The Blue packet.
3534 * [This means that we do NOT check the Verification Tag on these
3535 * chunks. --piggy ]
3536 *
3537 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003538sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3539 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 const struct sctp_association *asoc,
3541 const sctp_subtype_t type,
3542 void *arg,
3543 sctp_cmd_seq_t *commands)
3544{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003545 struct sctp_chunk *chunk = arg;
3546
3547 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3548 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003549 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04003550 commands);
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 /* Although we do have an association in this case, it corresponds
3553 * to a restarted association. So the packet is treated as an OOTB
3554 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3555 * called with a NULL association.
3556 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003557 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
Wei Yongjun8190f892008-09-08 12:13:55 +08003558
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003559 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
3562/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003563sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3564 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 const struct sctp_association *asoc,
3566 const sctp_subtype_t type, void *arg,
3567 sctp_cmd_seq_t *commands)
3568{
3569 struct sctp_chunk *chunk = arg;
3570 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003571 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003573 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003575 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576
3577 if (!sctp_vtag_verify(chunk, asoc)) {
3578 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3579 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003580 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 }
3582
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003583 /* ADD-IP: Section 4.1.1
3584 * This chunk MUST be sent in an authenticated way by using
3585 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3586 * is received unauthenticated it MUST be silently discarded as
3587 * described in [I-D.ietf-tsvwg-sctp-auth].
3588 */
3589 if (!sctp_addip_noauth && !chunk->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003590 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3593 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003594 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 commands);
3596
3597 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3598 serial = ntohl(hdr->serial);
3599
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003600 addr_param = (union sctp_addr_param *)hdr->params;
3601 length = ntohs(addr_param->p.length);
3602 if (length < sizeof(sctp_paramhdr_t))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003603 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003604 (void *)addr_param, commands);
3605
3606 /* Verify the ASCONF chunk before processing it. */
3607 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003608 (sctp_paramhdr_t *)((void *)addr_param + length),
3609 (void *)chunk->chunk_end,
3610 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003611 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003612 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003613
Vlad Yasevicha08de642007-12-20 14:11:47 -08003614 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003616 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 */
3618 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003619 /* If this is the first instance of ASCONF in the packet,
3620 * we can clean our old ASCONF-ACKs.
3621 */
3622 if (!chunk->has_asconf)
3623 sctp_assoc_clean_asconf_ack_cache(asoc);
3624
3625 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3626 * expected, process the ASCONF as described below and after
3627 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3628 * the response packet and cache a copy of it (in the event it
3629 * later needs to be retransmitted).
3630 *
3631 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 */
3633 asconf_ack = sctp_process_asconf((struct sctp_association *)
3634 asoc, chunk);
3635 if (!asconf_ack)
3636 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003637 } else if (serial < asoc->peer.addip_serial + 1) {
3638 /* ADDIP 5.2 E2)
3639 * If the value found in the Sequence Number is less than the
3640 * ('Peer- Sequence-Number' + 1), simply skip to the next
3641 * ASCONF, and include in the outbound response packet
3642 * any previously cached ASCONF-ACK response that was
3643 * sent and saved that matches the Sequence Number of the
3644 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3645 * Chunk exists. This will occur when an older ASCONF
3646 * arrives out of order. In such a case, the receiver
3647 * should skip the ASCONF Chunk and not include ASCONF-ACK
3648 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003650 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3651 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003653
3654 /* Reset the transport so that we select the correct one
3655 * this time around. This is to make sure that we don't
3656 * accidentally use a stale transport that's been removed.
3657 */
3658 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003660 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003662 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 return SCTP_DISPOSITION_DISCARD;
3664 }
3665
Vlad Yasevicha08de642007-12-20 14:11:47 -08003666 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3667 * containing the ASCONF-ACK Chunks MUST be the source address of
3668 * the SCTP packet that held the ASCONF Chunks.
3669 *
3670 * To do this properly, we'll set the destination address of the chunk
3671 * and at the transmit time, will try look up the transport to use.
3672 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003673 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003675 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
Michio Honda6af29cc2011-06-16 17:14:34 +09003677 if (asoc->new_transport) {
3678 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3679 commands);
3680 ((struct sctp_association *)asoc)->new_transport = NULL;
3681 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 return SCTP_DISPOSITION_CONSUME;
3684}
3685
3686/*
3687 * ADDIP Section 4.3 General rules for address manipulation
3688 * When building TLV parameters for the ASCONF Chunk that will add or
3689 * delete IP addresses the D0 to D13 rules should be applied:
3690 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003691sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3692 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003694 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 sctp_cmd_seq_t *commands)
3696{
3697 struct sctp_chunk *asconf_ack = arg;
3698 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3699 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003700 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 sctp_addiphdr_t *addip_hdr;
3702 __u32 sent_serial, rcvd_serial;
3703
3704 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3705 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3706 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003707 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
3709
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003710 /* ADD-IP, Section 4.1.2:
3711 * This chunk MUST be sent in an authenticated way by using
3712 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3713 * is received unauthenticated it MUST be silently discarded as
3714 * described in [I-D.ietf-tsvwg-sctp-auth].
3715 */
3716 if (!sctp_addip_noauth && !asconf_ack->auth)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003717 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 /* Make sure that the ADDIP chunk has a valid length. */
3720 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003721 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 commands);
3723
3724 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3725 rcvd_serial = ntohl(addip_hdr->serial);
3726
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003727 /* Verify the ASCONF-ACK chunk before processing it. */
3728 if (!sctp_verify_asconf(asoc,
3729 (sctp_paramhdr_t *)addip_hdr->params,
3730 (void *)asconf_ack->chunk_end,
3731 &err_param))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003732 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
Wei Yongjunba016672008-09-30 05:32:24 -07003733 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 if (last_asconf) {
3736 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3737 sent_serial = ntohl(addip_hdr->serial);
3738 } else {
3739 sent_serial = asoc->addip_serial - 1;
3740 }
3741
3742 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3743 * equal to the next serial number to be used but no ASCONF chunk is
3744 * outstanding the endpoint MUST ABORT the association. Note that a
3745 * sequence number is greater than if it is no more than 2^^31-1
3746 * larger than the current sequence number (using serial arithmetic).
3747 */
3748 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3749 !(asoc->addip_last_asconf)) {
3750 abort = sctp_make_abort(asoc, asconf_ack,
3751 sizeof(sctp_errhdr_t));
3752 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003753 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3755 SCTP_CHUNK(abort));
3756 }
3757 /* We are going to ABORT, so we might as well stop
3758 * processing the rest of the chunks in the packet.
3759 */
3760 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3761 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3762 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003763 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003764 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003766 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003767 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3768 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return SCTP_DISPOSITION_ABORT;
3770 }
3771
3772 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3773 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3774 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3775
3776 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003777 asconf_ack)) {
3778 /* Successfully processed ASCONF_ACK. We can
3779 * release the next asconf if we have one.
3780 */
3781 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3782 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 abort = sctp_make_abort(asoc, asconf_ack,
3787 sizeof(sctp_errhdr_t));
3788 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003789 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3791 SCTP_CHUNK(abort));
3792 }
3793 /* We are going to ABORT, so we might as well stop
3794 * processing the rest of the chunks in the packet.
3795 */
3796 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003797 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003798 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003800 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00003801 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3802 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return SCTP_DISPOSITION_ABORT;
3804 }
3805
3806 return SCTP_DISPOSITION_DISCARD;
3807}
3808
3809/*
3810 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3811 *
3812 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3813 * its cumulative TSN point to the value carried in the FORWARD TSN
3814 * chunk, and then MUST further advance its cumulative TSN point locally
3815 * if possible.
3816 * After the above processing, the data receiver MUST stop reporting any
3817 * missing TSNs earlier than or equal to the new cumulative TSN point.
3818 *
3819 * Verification Tag: 8.5 Verification Tag [Normal verification]
3820 *
3821 * The return value is the disposition of the chunk.
3822 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003823sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3824 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 const struct sctp_association *asoc,
3826 const sctp_subtype_t type,
3827 void *arg,
3828 sctp_cmd_seq_t *commands)
3829{
3830 struct sctp_chunk *chunk = arg;
3831 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003832 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 __u16 len;
3834 __u32 tsn;
3835
3836 if (!sctp_vtag_verify(chunk, asoc)) {
3837 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3838 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003839 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 }
3841
3842 /* Make sure that the FORWARD_TSN chunk has valid length. */
3843 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003844 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 commands);
3846
3847 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3848 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3849 len = ntohs(chunk->chunk_hdr->length);
3850 len -= sizeof(struct sctp_chunkhdr);
3851 skb_pull(chunk->skb, len);
3852
3853 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003854 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
3856 /* The TSN is too high--silently discard the chunk and count on it
3857 * getting retransmitted later.
3858 */
3859 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3860 goto discard_noforce;
3861
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003862 /* Silently discard the chunk if stream-id is not valid */
3863 sctp_walk_fwdtsn(skip, chunk) {
3864 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3865 goto discard_noforce;
3866 }
3867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3869 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003870 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 /* Count this as receiving DATA. */
3874 if (asoc->autoclose) {
3875 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3876 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3877 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003880 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 */
3882 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883
3884 return SCTP_DISPOSITION_CONSUME;
3885
3886discard_noforce:
3887 return SCTP_DISPOSITION_DISCARD;
3888}
3889
3890sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003891 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 const struct sctp_endpoint *ep,
3893 const struct sctp_association *asoc,
3894 const sctp_subtype_t type,
3895 void *arg,
3896 sctp_cmd_seq_t *commands)
3897{
3898 struct sctp_chunk *chunk = arg;
3899 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003900 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 __u16 len;
3902 __u32 tsn;
3903
3904 if (!sctp_vtag_verify(chunk, asoc)) {
3905 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3906 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003907 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 }
3909
3910 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3911 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003912 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 commands);
3914
3915 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3916 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3917 len = ntohs(chunk->chunk_hdr->length);
3918 len -= sizeof(struct sctp_chunkhdr);
3919 skb_pull(chunk->skb, len);
3920
3921 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003922 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
3924 /* The TSN is too high--silently discard the chunk and count on it
3925 * getting retransmitted later.
3926 */
3927 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3928 goto gen_shutdown;
3929
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003930 /* Silently discard the chunk if stream-id is not valid */
3931 sctp_walk_fwdtsn(skip, chunk) {
3932 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3933 goto gen_shutdown;
3934 }
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3937 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003938 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 /* Go a head and force a SACK, since we are shutting down. */
3942gen_shutdown:
3943 /* Implementor's Guide.
3944 *
3945 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3946 * respond to each received packet containing one or more DATA chunk(s)
3947 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3948 */
3949 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3950 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3951 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3952 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3953
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003954 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955}
3956
3957/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003958 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003959 *
3960 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3961 * Identifier field. If this algorithm was not specified by the
3962 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3963 * during association setup, the AUTH chunk and all chunks after it MUST
3964 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3965 * defined in Section 4.1.
3966 *
3967 * If an endpoint with no shared key receives a Shared Key Identifier
3968 * other than 0, it MUST silently discard all authenticated chunks. If
3969 * the endpoint has at least one endpoint pair shared key for the peer,
3970 * it MUST use the key specified by the Shared Key Identifier if a
3971 * key has been configured for that Shared Key Identifier. If no
3972 * endpoint pair shared key has been configured for that Shared Key
3973 * Identifier, all authenticated chunks MUST be silently discarded.
3974 *
3975 * Verification Tag: 8.5 Verification Tag [Normal verification]
3976 *
3977 * The return value is the disposition of the chunk.
3978 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00003979static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3980 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003981 const struct sctp_association *asoc,
3982 const sctp_subtype_t type,
3983 struct sctp_chunk *chunk)
3984{
3985 struct sctp_authhdr *auth_hdr;
3986 struct sctp_hmac *hmac;
3987 unsigned int sig_len;
3988 __u16 key_id;
3989 __u8 *save_digest;
3990 __u8 *digest;
3991
3992 /* Pull in the auth header, so we can do some more verification */
3993 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3994 chunk->subh.auth_hdr = auth_hdr;
3995 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3996
3997 /* Make sure that we suport the HMAC algorithm from the auth
3998 * chunk.
3999 */
4000 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4001 return SCTP_IERROR_AUTH_BAD_HMAC;
4002
4003 /* Make sure that the provided shared key identifier has been
4004 * configured
4005 */
4006 key_id = ntohs(auth_hdr->shkey_id);
4007 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4008 return SCTP_IERROR_AUTH_BAD_KEYID;
4009
4010
4011 /* Make sure that the length of the signature matches what
4012 * we expect.
4013 */
4014 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4015 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4016 if (sig_len != hmac->hmac_len)
4017 return SCTP_IERROR_PROTO_VIOLATION;
4018
4019 /* Now that we've done validation checks, we can compute and
4020 * verify the hmac. The steps involved are:
4021 * 1. Save the digest from the chunk.
4022 * 2. Zero out the digest in the chunk.
4023 * 3. Compute the new digest
4024 * 4. Compare saved and new digests.
4025 */
4026 digest = auth_hdr->hmac;
4027 skb_pull(chunk->skb, sig_len);
4028
4029 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4030 if (!save_digest)
4031 goto nomem;
4032
4033 memset(digest, 0, sig_len);
4034
4035 sctp_auth_calculate_hmac(asoc, chunk->skb,
4036 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4037 GFP_ATOMIC);
4038
4039 /* Discard the packet if the digests do not match */
4040 if (memcmp(save_digest, digest, sig_len)) {
4041 kfree(save_digest);
4042 return SCTP_IERROR_BAD_SIG;
4043 }
4044
4045 kfree(save_digest);
4046 chunk->auth = 1;
4047
4048 return SCTP_IERROR_NO_ERROR;
4049nomem:
4050 return SCTP_IERROR_NOMEM;
4051}
4052
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004053sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4054 const struct sctp_endpoint *ep,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004055 const struct sctp_association *asoc,
4056 const sctp_subtype_t type,
4057 void *arg,
4058 sctp_cmd_seq_t *commands)
4059{
4060 struct sctp_authhdr *auth_hdr;
4061 struct sctp_chunk *chunk = arg;
4062 struct sctp_chunk *err_chunk;
4063 sctp_ierror_t error;
4064
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004065 /* Make sure that the peer has AUTH capable */
4066 if (!asoc->peer.auth_capable)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004067 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
Wei Yongjund2f19fa2008-02-05 03:02:26 -08004068
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004069 if (!sctp_vtag_verify(chunk, asoc)) {
4070 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4071 SCTP_NULL());
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004072 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004073 }
4074
4075 /* Make sure that the AUTH chunk has valid length. */
4076 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004077 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004078 commands);
4079
4080 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004081 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004082 switch (error) {
Joe Perches7fd71b12011-07-01 09:43:11 +00004083 case SCTP_IERROR_AUTH_BAD_HMAC:
4084 /* Generate the ERROR chunk and discard the rest
4085 * of the packet
4086 */
4087 err_chunk = sctp_make_op_error(asoc, chunk,
4088 SCTP_ERROR_UNSUP_HMAC,
4089 &auth_hdr->hmac_id,
4090 sizeof(__u16), 0);
4091 if (err_chunk) {
4092 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4093 SCTP_CHUNK(err_chunk));
4094 }
4095 /* Fall Through */
4096 case SCTP_IERROR_AUTH_BAD_KEYID:
4097 case SCTP_IERROR_BAD_SIG:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004098 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Joe Perches7fd71b12011-07-01 09:43:11 +00004099
4100 case SCTP_IERROR_PROTO_VIOLATION:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004101 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Joe Perches7fd71b12011-07-01 09:43:11 +00004102 commands);
4103
4104 case SCTP_IERROR_NOMEM:
4105 return SCTP_DISPOSITION_NOMEM;
4106
4107 default: /* Prevent gcc warnings */
4108 break;
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004109 }
4110
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004111 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4112 struct sctp_ulpevent *ev;
4113
4114 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4115 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4116
4117 if (!ev)
4118 return -ENOMEM;
4119
4120 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4121 SCTP_ULPEVENT(ev));
4122 }
4123
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004124 return SCTP_DISPOSITION_CONSUME;
4125}
4126
4127/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 * Process an unknown chunk.
4129 *
4130 * Section: 3.2. Also, 2.1 in the implementor's guide.
4131 *
4132 * Chunk Types are encoded such that the highest-order two bits specify
4133 * the action that must be taken if the processing endpoint does not
4134 * recognize the Chunk Type.
4135 *
4136 * 00 - Stop processing this SCTP packet and discard it, do not process
4137 * any further chunks within it.
4138 *
4139 * 01 - Stop processing this SCTP packet and discard it, do not process
4140 * any further chunks within it, and report the unrecognized
4141 * chunk in an 'Unrecognized Chunk Type'.
4142 *
4143 * 10 - Skip this chunk and continue processing.
4144 *
4145 * 11 - Skip this chunk and continue processing, but report in an ERROR
4146 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4147 *
4148 * The return value is the disposition of the chunk.
4149 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004150sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4151 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 const struct sctp_association *asoc,
4153 const sctp_subtype_t type,
4154 void *arg,
4155 sctp_cmd_seq_t *commands)
4156{
4157 struct sctp_chunk *unk_chunk = arg;
4158 struct sctp_chunk *err_chunk;
4159 sctp_chunkhdr_t *hdr;
4160
4161 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4162
4163 if (!sctp_vtag_verify(unk_chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004164 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 /* Make sure that the chunk has a valid length.
4167 * Since we don't know the chunk type, we use a general
4168 * chunkhdr structure to make a comparison.
4169 */
4170 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004171 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 commands);
4173
4174 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4175 case SCTP_CID_ACTION_DISCARD:
4176 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004177 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 break;
4179 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 /* Generate an ERROR chunk as response. */
4181 hdr = unk_chunk->chunk_hdr;
4182 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4183 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004184 WORD_ROUND(ntohs(hdr->length)),
4185 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 if (err_chunk) {
4187 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4188 SCTP_CHUNK(err_chunk));
4189 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004190
4191 /* Discard the packet. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004192 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 return SCTP_DISPOSITION_CONSUME;
4194 break;
4195 case SCTP_CID_ACTION_SKIP:
4196 /* Skip the chunk. */
4197 return SCTP_DISPOSITION_DISCARD;
4198 break;
4199 case SCTP_CID_ACTION_SKIP_ERR:
4200 /* Generate an ERROR chunk as response. */
4201 hdr = unk_chunk->chunk_hdr;
4202 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4203 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004204 WORD_ROUND(ntohs(hdr->length)),
4205 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 if (err_chunk) {
4207 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4208 SCTP_CHUNK(err_chunk));
4209 }
4210 /* Skip the chunk. */
4211 return SCTP_DISPOSITION_CONSUME;
4212 break;
4213 default:
4214 break;
4215 }
4216
4217 return SCTP_DISPOSITION_DISCARD;
4218}
4219
4220/*
4221 * Discard the chunk.
4222 *
4223 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4224 * [Too numerous to mention...]
4225 * Verification Tag: No verification needed.
4226 * Inputs
4227 * (endpoint, asoc, chunk)
4228 *
4229 * Outputs
4230 * (asoc, reply_msg, msg_up, timers, counters)
4231 *
4232 * The return value is the disposition of the chunk.
4233 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004234sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4235 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 const struct sctp_association *asoc,
4237 const sctp_subtype_t type,
4238 void *arg,
4239 sctp_cmd_seq_t *commands)
4240{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004241 struct sctp_chunk *chunk = arg;
4242
4243 /* Make sure that the chunk has a valid length.
4244 * Since we don't know the chunk type, we use a general
4245 * chunkhdr structure to make a comparison.
4246 */
4247 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004248 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004249 commands);
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4252 return SCTP_DISPOSITION_DISCARD;
4253}
4254
4255/*
4256 * Discard the whole packet.
4257 *
4258 * Section: 8.4 2)
4259 *
4260 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4261 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 *
4263 * Verification Tag: No verification necessary
4264 *
4265 * Inputs
4266 * (endpoint, asoc, chunk)
4267 *
4268 * Outputs
4269 * (asoc, reply_msg, msg_up, timers, counters)
4270 *
4271 * The return value is the disposition of the chunk.
4272 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004273sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4274 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 const struct sctp_association *asoc,
4276 const sctp_subtype_t type,
4277 void *arg,
4278 sctp_cmd_seq_t *commands)
4279{
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004280 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4282
4283 return SCTP_DISPOSITION_CONSUME;
4284}
4285
4286
4287/*
4288 * The other end is violating protocol.
4289 *
4290 * Section: Not specified
4291 * Verification Tag: Not specified
4292 * Inputs
4293 * (endpoint, asoc, chunk)
4294 *
4295 * Outputs
4296 * (asoc, reply_msg, msg_up, timers, counters)
4297 *
4298 * We simply tag the chunk as a violation. The state machine will log
4299 * the violation and continue.
4300 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004301sctp_disposition_t sctp_sf_violation(struct net *net,
4302 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 const struct sctp_association *asoc,
4304 const sctp_subtype_t type,
4305 void *arg,
4306 sctp_cmd_seq_t *commands)
4307{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004308 struct sctp_chunk *chunk = arg;
4309
4310 /* Make sure that the chunk has a valid length. */
4311 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004312 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004313 commands);
4314
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return SCTP_DISPOSITION_VIOLATION;
4316}
4317
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004319 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004321static sctp_disposition_t sctp_sf_abort_violation(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004322 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004323 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004326 sctp_cmd_seq_t *commands,
4327 const __u8 *payload,
4328 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004330 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct sctp_chunk *chunk = arg;
4332 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004334 /* SCTP-AUTH, Section 6.3:
4335 * It should be noted that if the receiver wants to tear
4336 * down an association in an authenticated way only, the
4337 * handling of malformed packets should not result in
4338 * tearing down the association.
4339 *
4340 * This means that if we only want to abort associations
4341 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004342 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004343 * was malformed.
4344 */
4345 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4346 goto discard;
4347
Jesper Juhl9abed242007-11-11 23:57:49 +01004348 /* Make the abort chunk. */
4349 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4350 if (!abort)
4351 goto nomem;
4352
Vlad Yasevichece25df2007-09-07 16:30:54 -04004353 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004354 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4355 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4356 !asoc->peer.i.init_tag) {
4357 sctp_initack_chunk_t *initack;
4358
4359 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4360 if (!sctp_chunk_length_valid(chunk,
4361 sizeof(sctp_initack_chunk_t)))
4362 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4363 else {
4364 unsigned int inittag;
4365
4366 inittag = ntohl(initack->init_hdr.init_tag);
4367 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4368 SCTP_U32(inittag));
4369 }
4370 }
4371
Vlad Yasevichece25df2007-09-07 16:30:54 -04004372 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004373 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Vlad Yasevichece25df2007-09-07 16:30:54 -04004375 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4376 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4377 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4378 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4379 SCTP_ERROR(ECONNREFUSED));
4380 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4381 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4382 } else {
4383 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4384 SCTP_ERROR(ECONNABORTED));
4385 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4386 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004387 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 } else {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00004390 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004391
4392 if (!packet)
4393 goto nomem_pkt;
4394
4395 if (sctp_test_T_bit(abort))
4396 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4397
4398 abort->skb->sk = ep->base.sk;
4399
4400 sctp_packet_append_chunk(packet, abort);
4401
4402 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4403 SCTP_PACKET(packet));
4404
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004405 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 }
4407
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004408 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004409
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004410discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004411 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 return SCTP_DISPOSITION_ABORT;
4413
Vlad Yasevichece25df2007-09-07 16:30:54 -04004414nomem_pkt:
4415 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416nomem:
4417 return SCTP_DISPOSITION_NOMEM;
4418}
4419
Wei Yongjunaecedea2007-08-02 16:57:44 +08004420/*
4421 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004422 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004423 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004424 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004425 *
4426 * We inform the other end by sending an ABORT with a Protocol Violation
4427 * error code.
4428 *
4429 * Section: Not specified
4430 * Verification Tag: Nothing to do
4431 * Inputs
4432 * (endpoint, asoc, chunk)
4433 *
4434 * Outputs
4435 * (reply_msg, msg_up, counters)
4436 *
4437 * Generate an ABORT chunk and terminate the association.
4438 */
4439static sctp_disposition_t sctp_sf_violation_chunklen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004440 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004441 const struct sctp_endpoint *ep,
4442 const struct sctp_association *asoc,
4443 const sctp_subtype_t type,
4444 void *arg,
4445 sctp_cmd_seq_t *commands)
4446{
Florian Westphal24448442008-03-23 22:46:34 -07004447 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004448
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004449 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004450 sizeof(err_str));
4451}
4452
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004453/*
4454 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004455 * If the length is smaller than the minimum length of a given parameter,
4456 * or accumulated length in multi parameters exceeds the end of the chunk,
4457 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004458 */
4459static sctp_disposition_t sctp_sf_violation_paramlen(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004460 struct net *net,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004461 const struct sctp_endpoint *ep,
4462 const struct sctp_association *asoc,
4463 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004464 void *arg, void *ext,
4465 sctp_cmd_seq_t *commands)
4466{
4467 struct sctp_chunk *chunk = arg;
4468 struct sctp_paramhdr *param = ext;
4469 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004470
Wei Yongjunba016672008-09-30 05:32:24 -07004471 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4472 goto discard;
4473
4474 /* Make the abort chunk. */
4475 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4476 if (!abort)
4477 goto nomem;
4478
4479 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004480 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Wei Yongjunba016672008-09-30 05:32:24 -07004481
4482 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4483 SCTP_ERROR(ECONNABORTED));
4484 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4485 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004486 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4487 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004488
4489discard:
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004490 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004491 return SCTP_DISPOSITION_ABORT;
4492nomem:
4493 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004494}
4495
Wei Yongjunaecedea2007-08-02 16:57:44 +08004496/* Handle a protocol violation when the peer trying to advance the
4497 * cumulative tsn ack to a point beyond the max tsn currently sent.
4498 *
4499 * We inform the other end by sending an ABORT with a Protocol Violation
4500 * error code.
4501 */
4502static sctp_disposition_t sctp_sf_violation_ctsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004503 struct net *net,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004504 const struct sctp_endpoint *ep,
4505 const struct sctp_association *asoc,
4506 const sctp_subtype_t type,
4507 void *arg,
4508 sctp_cmd_seq_t *commands)
4509{
Florian Westphal24448442008-03-23 22:46:34 -07004510 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004511
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004512 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004513 sizeof(err_str));
4514}
4515
Vlad Yasevichece25df2007-09-07 16:30:54 -04004516/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004517 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004518 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004519 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004520 * on the path and we may not want to continue this communication.
4521 */
4522static sctp_disposition_t sctp_sf_violation_chunk(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004523 struct net *net,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004524 const struct sctp_endpoint *ep,
4525 const struct sctp_association *asoc,
4526 const sctp_subtype_t type,
4527 void *arg,
4528 sctp_cmd_seq_t *commands)
4529{
Florian Westphal24448442008-03-23 22:46:34 -07004530 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004531
4532 if (!asoc)
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004533 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -04004534
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004535 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
Vlad Yasevichece25df2007-09-07 16:30:54 -04004536 sizeof(err_str));
4537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538/***************************************************************************
4539 * These are the state functions for handling primitive (Section 10) events.
4540 ***************************************************************************/
4541/*
4542 * sctp_sf_do_prm_asoc
4543 *
4544 * Section: 10.1 ULP-to-SCTP
4545 * B) Associate
4546 *
4547 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4548 * outbound stream count)
4549 * -> association id [,destination transport addr list] [,outbound stream
4550 * count]
4551 *
4552 * This primitive allows the upper layer to initiate an association to a
4553 * specific peer endpoint.
4554 *
4555 * The peer endpoint shall be specified by one of the transport addresses
4556 * which defines the endpoint (see Section 1.4). If the local SCTP
4557 * instance has not been initialized, the ASSOCIATE is considered an
4558 * error.
4559 * [This is not relevant for the kernel implementation since we do all
4560 * initialization at boot time. It we hadn't initialized we wouldn't
4561 * get anywhere near this code.]
4562 *
4563 * An association id, which is a local handle to the SCTP association,
4564 * will be returned on successful establishment of the association. If
4565 * SCTP is not able to open an SCTP association with the peer endpoint,
4566 * an error is returned.
4567 * [In the kernel implementation, the struct sctp_association needs to
4568 * be created BEFORE causing this primitive to run.]
4569 *
4570 * Other association parameters may be returned, including the
4571 * complete destination transport addresses of the peer as well as the
4572 * outbound stream count of the local endpoint. One of the transport
4573 * address from the returned destination addresses will be selected by
4574 * the local endpoint as default primary path for sending SCTP packets
4575 * to this peer. The returned "destination transport addr list" can
4576 * be used by the ULP to change the default primary path or to force
4577 * sending a packet to a specific transport address. [All of this
4578 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4579 * function.]
4580 *
4581 * Mandatory attributes:
4582 *
4583 * o local SCTP instance name - obtained from the INITIALIZE operation.
4584 * [This is the argument asoc.]
4585 * o destination transport addr - specified as one of the transport
4586 * addresses of the peer endpoint with which the association is to be
4587 * established.
4588 * [This is asoc->peer.active_path.]
4589 * o outbound stream count - the number of outbound streams the ULP
4590 * would like to open towards this peer endpoint.
4591 * [BUG: This is not currently implemented.]
4592 * Optional attributes:
4593 *
4594 * None.
4595 *
4596 * The return value is a disposition.
4597 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004598sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4599 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 const struct sctp_association *asoc,
4601 const sctp_subtype_t type,
4602 void *arg,
4603 sctp_cmd_seq_t *commands)
4604{
4605 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004606 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
4608 /* The comment below says that we enter COOKIE-WAIT AFTER
4609 * sending the INIT, but that doesn't actually work in our
4610 * implementation...
4611 */
4612 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4613 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4614
4615 /* RFC 2960 5.1 Normal Establishment of an Association
4616 *
4617 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4618 * must provide its Verification Tag (Tag_A) in the Initiate
4619 * Tag field. Tag_A SHOULD be a random number in the range of
4620 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4621 */
4622
4623 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4624 if (!repl)
4625 goto nomem;
4626
4627 /* Cast away the const modifier, as we want to just
4628 * rerun it through as a sideffect.
4629 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004630 my_asoc = (struct sctp_association *)asoc;
4631 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Frank Filz3f7a87d2005-06-20 13:14:57 -07004633 /* Choose transport for INIT. */
4634 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4635 SCTP_CHUNK(repl));
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 /* After sending the INIT, "A" starts the T1-init timer and
4638 * enters the COOKIE-WAIT state.
4639 */
4640 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4641 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4642 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4643 return SCTP_DISPOSITION_CONSUME;
4644
4645nomem:
4646 return SCTP_DISPOSITION_NOMEM;
4647}
4648
4649/*
4650 * Process the SEND primitive.
4651 *
4652 * Section: 10.1 ULP-to-SCTP
4653 * E) Send
4654 *
4655 * Format: SEND(association id, buffer address, byte count [,context]
4656 * [,stream id] [,life time] [,destination transport address]
4657 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4658 * -> result
4659 *
4660 * This is the main method to send user data via SCTP.
4661 *
4662 * Mandatory attributes:
4663 *
4664 * o association id - local handle to the SCTP association
4665 *
4666 * o buffer address - the location where the user message to be
4667 * transmitted is stored;
4668 *
4669 * o byte count - The size of the user data in number of bytes;
4670 *
4671 * Optional attributes:
4672 *
4673 * o context - an optional 32 bit integer that will be carried in the
4674 * sending failure notification to the ULP if the transportation of
4675 * this User Message fails.
4676 *
4677 * o stream id - to indicate which stream to send the data on. If not
4678 * specified, stream 0 will be used.
4679 *
4680 * o life time - specifies the life time of the user data. The user data
4681 * will not be sent by SCTP after the life time expires. This
4682 * parameter can be used to avoid efforts to transmit stale
4683 * user messages. SCTP notifies the ULP if the data cannot be
4684 * initiated to transport (i.e. sent to the destination via SCTP's
4685 * send primitive) within the life time variable. However, the
4686 * user data will be transmitted if SCTP has attempted to transmit a
4687 * chunk before the life time expired.
4688 *
4689 * o destination transport address - specified as one of the destination
4690 * transport addresses of the peer endpoint to which this packet
4691 * should be sent. Whenever possible, SCTP should use this destination
4692 * transport address for sending the packets, instead of the current
4693 * primary path.
4694 *
4695 * o unorder flag - this flag, if present, indicates that the user
4696 * would like the data delivered in an unordered fashion to the peer
4697 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4698 * message).
4699 *
4700 * o no-bundle flag - instructs SCTP not to bundle this user data with
4701 * other outbound DATA chunks. SCTP MAY still bundle even when
4702 * this flag is present, when faced with network congestion.
4703 *
4704 * o payload protocol-id - A 32 bit unsigned integer that is to be
4705 * passed to the peer indicating the type of payload protocol data
4706 * being transmitted. This value is passed as opaque data by SCTP.
4707 *
4708 * The return value is the disposition.
4709 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004710sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4711 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 const struct sctp_association *asoc,
4713 const sctp_subtype_t type,
4714 void *arg,
4715 sctp_cmd_seq_t *commands)
4716{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004717 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004719 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return SCTP_DISPOSITION_CONSUME;
4721}
4722
4723/*
4724 * Process the SHUTDOWN primitive.
4725 *
4726 * Section: 10.1:
4727 * C) Shutdown
4728 *
4729 * Format: SHUTDOWN(association id)
4730 * -> result
4731 *
4732 * Gracefully closes an association. Any locally queued user data
4733 * will be delivered to the peer. The association will be terminated only
4734 * after the peer acknowledges all the SCTP packets sent. A success code
4735 * will be returned on successful termination of the association. If
4736 * attempting to terminate the association results in a failure, an error
4737 * code shall be returned.
4738 *
4739 * Mandatory attributes:
4740 *
4741 * o association id - local handle to the SCTP association
4742 *
4743 * Optional attributes:
4744 *
4745 * None.
4746 *
4747 * The return value is the disposition.
4748 */
4749sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004750 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 const struct sctp_endpoint *ep,
4752 const struct sctp_association *asoc,
4753 const sctp_subtype_t type,
4754 void *arg,
4755 sctp_cmd_seq_t *commands)
4756{
4757 int disposition;
4758
4759 /* From 9.2 Shutdown of an Association
4760 * Upon receipt of the SHUTDOWN primitive from its upper
4761 * layer, the endpoint enters SHUTDOWN-PENDING state and
4762 * remains there until all outstanding data has been
4763 * acknowledged by its peer. The endpoint accepts no new data
4764 * from its upper layer, but retransmits data to the far end
4765 * if necessary to fill gaps.
4766 */
4767 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4768 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4769
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 disposition = SCTP_DISPOSITION_CONSUME;
4771 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004772 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773 arg, commands);
4774 }
4775 return disposition;
4776}
4777
4778/*
4779 * Process the ABORT primitive.
4780 *
4781 * Section: 10.1:
4782 * C) Abort
4783 *
4784 * Format: Abort(association id [, cause code])
4785 * -> result
4786 *
4787 * Ungracefully closes an association. Any locally queued user data
4788 * will be discarded and an ABORT chunk is sent to the peer. A success code
4789 * will be returned on successful abortion of the association. If
4790 * attempting to abort the association results in a failure, an error
4791 * code shall be returned.
4792 *
4793 * Mandatory attributes:
4794 *
4795 * o association id - local handle to the SCTP association
4796 *
4797 * Optional attributes:
4798 *
4799 * o cause code - reason of the abort to be passed to the peer
4800 *
4801 * None.
4802 *
4803 * The return value is the disposition.
4804 */
4805sctp_disposition_t sctp_sf_do_9_1_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004806 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 const struct sctp_endpoint *ep,
4808 const struct sctp_association *asoc,
4809 const sctp_subtype_t type,
4810 void *arg,
4811 sctp_cmd_seq_t *commands)
4812{
4813 /* From 9.1 Abort of an Association
4814 * Upon receipt of the ABORT primitive from its upper
4815 * layer, the endpoint enters CLOSED state and
4816 * discard all outstanding data has been
4817 * acknowledged by its peer. The endpoint accepts no new data
4818 * from its upper layer, but retransmits data to the far end
4819 * if necessary to fill gaps.
4820 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004821 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 sctp_disposition_t retval;
4823
4824 retval = SCTP_DISPOSITION_CONSUME;
4825
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004826 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 /* Even if we can't send the ABORT due to low memory delete the
4829 * TCB. This is a departure from our typical NOMEM handling.
4830 */
4831
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004832 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4833 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 /* Delete the established association. */
4835 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004836 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004838 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4839 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
4841 return retval;
4842}
4843
4844/* We tried an illegal operation on an association which is closed. */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004845sctp_disposition_t sctp_sf_error_closed(struct net *net,
4846 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 const struct sctp_association *asoc,
4848 const sctp_subtype_t type,
4849 void *arg,
4850 sctp_cmd_seq_t *commands)
4851{
4852 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4853 return SCTP_DISPOSITION_CONSUME;
4854}
4855
4856/* We tried an illegal operation on an association which is shutting
4857 * down.
4858 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004859sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4860 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 const struct sctp_association *asoc,
4862 const sctp_subtype_t type,
4863 void *arg,
4864 sctp_cmd_seq_t *commands)
4865{
4866 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4867 SCTP_ERROR(-ESHUTDOWN));
4868 return SCTP_DISPOSITION_CONSUME;
4869}
4870
4871/*
4872 * sctp_cookie_wait_prm_shutdown
4873 *
4874 * Section: 4 Note: 2
4875 * Verification Tag:
4876 * Inputs
4877 * (endpoint, asoc)
4878 *
4879 * The RFC does not explicitly address this issue, but is the route through the
4880 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4881 *
4882 * Outputs
4883 * (timers)
4884 */
4885sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004886 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 const struct sctp_endpoint *ep,
4888 const struct sctp_association *asoc,
4889 const sctp_subtype_t type,
4890 void *arg,
4891 sctp_cmd_seq_t *commands)
4892{
4893 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4894 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4895
4896 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4897 SCTP_STATE(SCTP_STATE_CLOSED));
4898
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004899 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4902
4903 return SCTP_DISPOSITION_DELETE_TCB;
4904}
4905
4906/*
4907 * sctp_cookie_echoed_prm_shutdown
4908 *
4909 * Section: 4 Note: 2
4910 * Verification Tag:
4911 * Inputs
4912 * (endpoint, asoc)
4913 *
4914 * The RFC does not explcitly address this issue, but is the route through the
4915 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4916 *
4917 * Outputs
4918 * (timers)
4919 */
4920sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004921 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 const struct sctp_endpoint *ep,
4923 const struct sctp_association *asoc,
4924 const sctp_subtype_t type,
4925 void *arg, sctp_cmd_seq_t *commands)
4926{
4927 /* There is a single T1 timer, so we should be able to use
4928 * common function with the COOKIE-WAIT state.
4929 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004930 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
4933/*
4934 * sctp_sf_cookie_wait_prm_abort
4935 *
4936 * Section: 4 Note: 2
4937 * Verification Tag:
4938 * Inputs
4939 * (endpoint, asoc)
4940 *
4941 * The RFC does not explicitly address this issue, but is the route through the
4942 * state table when someone issues an abort while in COOKIE_WAIT state.
4943 *
4944 * Outputs
4945 * (timers)
4946 */
4947sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004948 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 const struct sctp_endpoint *ep,
4950 const struct sctp_association *asoc,
4951 const sctp_subtype_t type,
4952 void *arg,
4953 sctp_cmd_seq_t *commands)
4954{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004955 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 sctp_disposition_t retval;
4957
4958 /* Stop T1-init timer */
4959 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4960 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4961 retval = SCTP_DISPOSITION_CONSUME;
4962
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004963 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
4965 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4966 SCTP_STATE(SCTP_STATE_CLOSED));
4967
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00004968 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969
4970 /* Even if we can't send the ABORT due to low memory delete the
4971 * TCB. This is a departure from our typical NOMEM handling.
4972 */
4973
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004974 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4975 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 /* Delete the established association. */
4977 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004978 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 return retval;
4981}
4982
4983/*
4984 * sctp_sf_cookie_echoed_prm_abort
4985 *
4986 * Section: 4 Note: 3
4987 * Verification Tag:
4988 * Inputs
4989 * (endpoint, asoc)
4990 *
4991 * The RFC does not explcitly address this issue, but is the route through the
4992 * state table when someone issues an abort while in COOKIE_ECHOED state.
4993 *
4994 * Outputs
4995 * (timers)
4996 */
4997sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00004998 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 const struct sctp_endpoint *ep,
5000 const struct sctp_association *asoc,
5001 const sctp_subtype_t type,
5002 void *arg,
5003 sctp_cmd_seq_t *commands)
5004{
5005 /* There is a single T1 timer, so we should be able to use
5006 * common function with the COOKIE-WAIT state.
5007 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005008 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
5011/*
5012 * sctp_sf_shutdown_pending_prm_abort
5013 *
5014 * Inputs
5015 * (endpoint, asoc)
5016 *
5017 * The RFC does not explicitly address this issue, but is the route through the
5018 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5019 *
5020 * Outputs
5021 * (timers)
5022 */
5023sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005024 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 const struct sctp_endpoint *ep,
5026 const struct sctp_association *asoc,
5027 const sctp_subtype_t type,
5028 void *arg,
5029 sctp_cmd_seq_t *commands)
5030{
5031 /* Stop the T5-shutdown guard timer. */
5032 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5033 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5034
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005035 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036}
5037
5038/*
5039 * sctp_sf_shutdown_sent_prm_abort
5040 *
5041 * Inputs
5042 * (endpoint, asoc)
5043 *
5044 * The RFC does not explicitly address this issue, but is the route through the
5045 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5046 *
5047 * Outputs
5048 * (timers)
5049 */
5050sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005051 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 const struct sctp_endpoint *ep,
5053 const struct sctp_association *asoc,
5054 const sctp_subtype_t type,
5055 void *arg,
5056 sctp_cmd_seq_t *commands)
5057{
5058 /* Stop the T2-shutdown timer. */
5059 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5060 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5061
5062 /* Stop the T5-shutdown guard timer. */
5063 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5064 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5065
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005066 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067}
5068
5069/*
5070 * sctp_sf_cookie_echoed_prm_abort
5071 *
5072 * Inputs
5073 * (endpoint, asoc)
5074 *
5075 * The RFC does not explcitly address this issue, but is the route through the
5076 * state table when someone issues an abort while in COOKIE_ECHOED state.
5077 *
5078 * Outputs
5079 * (timers)
5080 */
5081sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005082 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 const struct sctp_endpoint *ep,
5084 const struct sctp_association *asoc,
5085 const sctp_subtype_t type,
5086 void *arg,
5087 sctp_cmd_seq_t *commands)
5088{
5089 /* The same T2 timer, so we should be able to use
5090 * common function with the SHUTDOWN-SENT state.
5091 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005092 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093}
5094
5095/*
5096 * Process the REQUESTHEARTBEAT primitive
5097 *
5098 * 10.1 ULP-to-SCTP
5099 * J) Request Heartbeat
5100 *
5101 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5102 *
5103 * -> result
5104 *
5105 * Instructs the local endpoint to perform a HeartBeat on the specified
5106 * destination transport address of the given association. The returned
5107 * result should indicate whether the transmission of the HEARTBEAT
5108 * chunk to the destination address is successful.
5109 *
5110 * Mandatory attributes:
5111 *
5112 * o association id - local handle to the SCTP association
5113 *
5114 * o destination transport address - the transport address of the
5115 * association on which a heartbeat should be issued.
5116 */
5117sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005118 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 const struct sctp_endpoint *ep,
5120 const struct sctp_association *asoc,
5121 const sctp_subtype_t type,
5122 void *arg,
5123 sctp_cmd_seq_t *commands)
5124{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005125 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5126 (struct sctp_transport *)arg, commands))
5127 return SCTP_DISPOSITION_NOMEM;
5128
5129 /*
5130 * RFC 2960 (bis), section 8.3
5131 *
5132 * D) Request an on-demand HEARTBEAT on a specific destination
5133 * transport address of a given association.
5134 *
5135 * The endpoint should increment the respective error counter of
5136 * the destination transport address each time a HEARTBEAT is sent
5137 * to that address and not acknowledged within one RTO.
5138 *
5139 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005140 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005141 SCTP_TRANSPORT(arg));
5142 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143}
5144
5145/*
5146 * ADDIP Section 4.1 ASCONF Chunk Procedures
5147 * When an endpoint has an ASCONF signaled change to be sent to the
5148 * remote endpoint it should do A1 to A9
5149 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005150sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5151 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 const struct sctp_association *asoc,
5153 const sctp_subtype_t type,
5154 void *arg,
5155 sctp_cmd_seq_t *commands)
5156{
5157 struct sctp_chunk *chunk = arg;
5158
5159 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5160 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5161 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5162 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5163 return SCTP_DISPOSITION_CONSUME;
5164}
5165
5166/*
5167 * Ignore the primitive event
5168 *
5169 * The return value is the disposition of the primitive.
5170 */
5171sctp_disposition_t sctp_sf_ignore_primitive(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005172 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 const struct sctp_endpoint *ep,
5174 const struct sctp_association *asoc,
5175 const sctp_subtype_t type,
5176 void *arg,
5177 sctp_cmd_seq_t *commands)
5178{
5179 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5180 return SCTP_DISPOSITION_DISCARD;
5181}
5182
5183/***************************************************************************
5184 * These are the state functions for the OTHER events.
5185 ***************************************************************************/
5186
5187/*
Wei Yongjune1cdd552011-04-17 17:29:03 +00005188 * When the SCTP stack has no more user data to send or retransmit, this
5189 * notification is given to the user. Also, at the time when a user app
5190 * subscribes to this event, if there is no data to be sent or
5191 * retransmit, the stack will immediately send up this notification.
5192 */
5193sctp_disposition_t sctp_sf_do_no_pending_tsn(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005194 struct net *net,
Wei Yongjune1cdd552011-04-17 17:29:03 +00005195 const struct sctp_endpoint *ep,
5196 const struct sctp_association *asoc,
5197 const sctp_subtype_t type,
5198 void *arg,
5199 sctp_cmd_seq_t *commands)
5200{
5201 struct sctp_ulpevent *event;
5202
5203 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5204 if (!event)
5205 return SCTP_DISPOSITION_NOMEM;
5206
5207 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5208
5209 return SCTP_DISPOSITION_CONSUME;
5210}
5211
5212/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 * Start the shutdown negotiation.
5214 *
5215 * From Section 9.2:
5216 * Once all its outstanding data has been acknowledged, the endpoint
5217 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5218 * TSN Ack field the last sequential TSN it has received from the peer.
5219 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5220 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5221 * with the updated last sequential TSN received from its peer.
5222 *
5223 * The return value is the disposition.
5224 */
5225sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005226 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 const struct sctp_endpoint *ep,
5228 const struct sctp_association *asoc,
5229 const sctp_subtype_t type,
5230 void *arg,
5231 sctp_cmd_seq_t *commands)
5232{
5233 struct sctp_chunk *reply;
5234
5235 /* Once all its outstanding data has been acknowledged, the
5236 * endpoint shall send a SHUTDOWN chunk to its peer including
5237 * in the Cumulative TSN Ack field the last sequential TSN it
5238 * has received from the peer.
5239 */
5240 reply = sctp_make_shutdown(asoc, NULL);
5241 if (!reply)
5242 goto nomem;
5243
5244 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5245 * T2-shutdown timer.
5246 */
5247 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5248
5249 /* It shall then start the T2-shutdown timer */
5250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5251 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5252
Wei Yongjun536428a2008-09-05 08:55:26 +08005253 /* RFC 4960 Section 9.2
5254 * The sender of the SHUTDOWN MAY also start an overall guard timer
5255 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5256 */
Thomas Graff8d96052011-07-07 00:28:35 +00005257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
Wei Yongjun536428a2008-09-05 08:55:26 +08005258 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 if (asoc->autoclose)
5261 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5262 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5263
5264 /* and enter the SHUTDOWN-SENT state. */
5265 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5266 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5267
5268 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5269 *
5270 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005271 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 */
5273 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5274
5275 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5276
5277 return SCTP_DISPOSITION_CONSUME;
5278
5279nomem:
5280 return SCTP_DISPOSITION_NOMEM;
5281}
5282
5283/*
5284 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5285 *
5286 * From Section 9.2:
5287 *
5288 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5289 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5290 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5291 * endpoint must re-send the SHUTDOWN ACK.
5292 *
5293 * The return value is the disposition.
5294 */
5295sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005296 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 const struct sctp_endpoint *ep,
5298 const struct sctp_association *asoc,
5299 const sctp_subtype_t type,
5300 void *arg,
5301 sctp_cmd_seq_t *commands)
5302{
5303 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5304 struct sctp_chunk *reply;
5305
5306 /* There are 2 ways of getting here:
5307 * 1) called in response to a SHUTDOWN chunk
5308 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5309 *
5310 * For the case (2), the arg parameter is set to NULL. We need
5311 * to check that we have a chunk before accessing it's fields.
5312 */
5313 if (chunk) {
5314 if (!sctp_vtag_verify(chunk, asoc))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005315 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316
5317 /* Make sure that the SHUTDOWN chunk has a valid length. */
5318 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005319 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 commands);
5321 }
5322
5323 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5324 * shall send a SHUTDOWN ACK ...
5325 */
5326 reply = sctp_make_shutdown_ack(asoc, chunk);
5327 if (!reply)
5328 goto nomem;
5329
5330 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5331 * the T2-shutdown timer.
5332 */
5333 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5334
5335 /* and start/restart a T2-shutdown timer of its own, */
5336 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5337 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5338
5339 if (asoc->autoclose)
5340 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5341 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5342
5343 /* Enter the SHUTDOWN-ACK-SENT state. */
5344 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5345 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5346
5347 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5348 *
5349 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005350 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 */
5352 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5353
5354 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5355
5356 return SCTP_DISPOSITION_CONSUME;
5357
5358nomem:
5359 return SCTP_DISPOSITION_NOMEM;
5360}
5361
5362/*
5363 * Ignore the event defined as other
5364 *
5365 * The return value is the disposition of the event.
5366 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005367sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5368 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 const struct sctp_association *asoc,
5370 const sctp_subtype_t type,
5371 void *arg,
5372 sctp_cmd_seq_t *commands)
5373{
5374 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5375 return SCTP_DISPOSITION_DISCARD;
5376}
5377
5378/************************************************************
5379 * These are the state functions for handling timeout events.
5380 ************************************************************/
5381
5382/*
5383 * RTX Timeout
5384 *
5385 * Section: 6.3.3 Handle T3-rtx Expiration
5386 *
5387 * Whenever the retransmission timer T3-rtx expires for a destination
5388 * address, do the following:
5389 * [See below]
5390 *
5391 * The return value is the disposition of the chunk.
5392 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005393sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5394 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 const struct sctp_association *asoc,
5396 const sctp_subtype_t type,
5397 void *arg,
5398 sctp_cmd_seq_t *commands)
5399{
5400 struct sctp_transport *transport = arg;
5401
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005402 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005403
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 if (asoc->overall_error_count >= asoc->max_retrans) {
Thomas Graff8d96052011-07-07 00:28:35 +00005405 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5406 /*
5407 * We are here likely because the receiver had its rwnd
5408 * closed for a while and we have not been able to
5409 * transmit the locally queued data within the maximum
5410 * retransmission attempts limit. Start the T5
5411 * shutdown guard timer to give the receiver one last
5412 * chance and some additional time to recover before
5413 * aborting.
5414 */
5415 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5416 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5417 } else {
5418 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5419 SCTP_ERROR(ETIMEDOUT));
5420 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5421 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5422 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005423 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5424 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Thomas Graff8d96052011-07-07 00:28:35 +00005425 return SCTP_DISPOSITION_DELETE_TCB;
5426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
5428
5429 /* E1) For the destination address for which the timer
5430 * expires, adjust its ssthresh with rules defined in Section
5431 * 7.2.3 and set the cwnd <- MTU.
5432 */
5433
5434 /* E2) For the destination address for which the timer
5435 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5436 * maximum value discussed in rule C7 above (RTO.max) may be
5437 * used to provide an upper bound to this doubling operation.
5438 */
5439
5440 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5441 * outstanding DATA chunks for the address for which the
5442 * T3-rtx has expired will fit into a single packet, subject
5443 * to the MTU constraint for the path corresponding to the
5444 * destination transport address to which the retransmission
5445 * is being sent (this may be different from the address for
5446 * which the timer expires [see Section 6.4]). Call this
5447 * value K. Bundle and retransmit those K DATA chunks in a
5448 * single packet to the destination endpoint.
5449 *
5450 * Note: Any DATA chunks that were sent to the address for
5451 * which the T3-rtx timer expired but did not fit in one MTU
5452 * (rule E3 above), should be marked for retransmission and
5453 * sent as soon as cwnd allows (normally when a SACK arrives).
5454 */
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 /* Do some failure management (Section 8.2). */
5457 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5458
Vlad Yasevich1845a572007-02-21 02:06:19 -08005459 /* NB: Rules E4 and F1 are implicit in R1. */
5460 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return SCTP_DISPOSITION_CONSUME;
5463}
5464
5465/*
5466 * Generate delayed SACK on timeout
5467 *
5468 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5469 *
5470 * The guidelines on delayed acknowledgement algorithm specified in
5471 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5472 * acknowledgement SHOULD be generated for at least every second packet
5473 * (not every second DATA chunk) received, and SHOULD be generated
5474 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5475 * some situations it may be beneficial for an SCTP transmitter to be
5476 * more conservative than the algorithms detailed in this document
5477 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5478 * the following algorithms allow.
5479 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005480sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5481 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 const struct sctp_association *asoc,
5483 const sctp_subtype_t type,
5484 void *arg,
5485 sctp_cmd_seq_t *commands)
5486{
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005487 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5489 return SCTP_DISPOSITION_CONSUME;
5490}
5491
5492/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005493 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 *
5495 * Section: 4 Note: 2
5496 * Verification Tag:
5497 * Inputs
5498 * (endpoint, asoc)
5499 *
5500 * RFC 2960 Section 4 Notes
5501 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5502 * and re-start the T1-init timer without changing state. This MUST
5503 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5504 * endpoint MUST abort the initialization process and report the
5505 * error to SCTP user.
5506 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005507 * Outputs
5508 * (timers, events)
5509 *
5510 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005511sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5512 const struct sctp_endpoint *ep,
Frank Filz3f7a87d2005-06-20 13:14:57 -07005513 const struct sctp_association *asoc,
5514 const sctp_subtype_t type,
5515 void *arg,
5516 sctp_cmd_seq_t *commands)
5517{
5518 struct sctp_chunk *repl = NULL;
5519 struct sctp_bind_addr *bp;
5520 int attempts = asoc->init_err_counter + 1;
5521
5522 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005523 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005524
Vlad Yasevich81845c22006-01-30 15:59:54 -08005525 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005526 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5527 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5528 if (!repl)
5529 return SCTP_DISPOSITION_NOMEM;
5530
5531 /* Choose transport for INIT. */
5532 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5533 SCTP_CHUNK(repl));
5534
5535 /* Issue a sideeffect to do the needed accounting. */
5536 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5537 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5538
5539 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5540 } else {
5541 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5542 " max_init_attempts: %d\n",
5543 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005544 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5545 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005546 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005547 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005548 return SCTP_DISPOSITION_DELETE_TCB;
5549 }
5550
5551 return SCTP_DISPOSITION_CONSUME;
5552}
5553
5554/*
5555 * sctp_sf_t1_cookie_timer_expire
5556 *
5557 * Section: 4 Note: 2
5558 * Verification Tag:
5559 * Inputs
5560 * (endpoint, asoc)
5561 *
5562 * RFC 2960 Section 4 Notes
5563 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 * COOKIE ECHO and re-start the T1-cookie timer without changing
5565 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5566 * After that, the endpoint MUST abort the initialization process and
5567 * report the error to SCTP user.
5568 *
5569 * Outputs
5570 * (timers, events)
5571 *
5572 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005573sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5574 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 const struct sctp_association *asoc,
5576 const sctp_subtype_t type,
5577 void *arg,
5578 sctp_cmd_seq_t *commands)
5579{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005580 struct sctp_chunk *repl = NULL;
5581 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Frank Filz3f7a87d2005-06-20 13:14:57 -07005583 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005584 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
Vlad Yasevich81845c22006-01-30 15:59:54 -08005586 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005587 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005589 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005591 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5592 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005594 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5595 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5596
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5598 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005599 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5600 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005602 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 return SCTP_DISPOSITION_DELETE_TCB;
5604 }
5605
5606 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607}
5608
5609/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5610 * with the updated last sequential TSN received from its peer.
5611 *
5612 * An endpoint should limit the number of retransmissions of the
5613 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5614 * If this threshold is exceeded the endpoint should destroy the TCB and
5615 * MUST report the peer endpoint unreachable to the upper layer (and
5616 * thus the association enters the CLOSED state). The reception of any
5617 * packet from its peer (i.e. as the peer sends all of its queued DATA
5618 * chunks) should clear the endpoint's retransmission count and restart
5619 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5620 * all of its queued DATA chunks that have not yet been sent.
5621 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005622sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5623 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 const struct sctp_association *asoc,
5625 const sctp_subtype_t type,
5626 void *arg,
5627 sctp_cmd_seq_t *commands)
5628{
5629 struct sctp_chunk *reply = NULL;
5630
5631 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005632 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005633
Neil Horman58fbbed2008-02-29 11:40:56 -08005634 ((struct sctp_association *)asoc)->shutdown_retries++;
5635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005637 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5638 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5640 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005641 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005642 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5643 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 return SCTP_DISPOSITION_DELETE_TCB;
5645 }
5646
5647 switch (asoc->state) {
5648 case SCTP_STATE_SHUTDOWN_SENT:
5649 reply = sctp_make_shutdown(asoc, NULL);
5650 break;
5651
5652 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5653 reply = sctp_make_shutdown_ack(asoc, NULL);
5654 break;
5655
5656 default:
5657 BUG();
5658 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660
5661 if (!reply)
5662 goto nomem;
5663
Wei Yongjun6345b192009-04-26 23:13:35 +08005664 /* Do some failure management (Section 8.2).
5665 * If we remove the transport an SHUTDOWN was last sent to, don't
5666 * do failure management.
5667 */
5668 if (asoc->shutdown_last_sent_to)
5669 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5670 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
5672 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5673 * the T2-shutdown timer.
5674 */
5675 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5676
5677 /* Restart the T2-shutdown timer. */
5678 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5679 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5680 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5681 return SCTP_DISPOSITION_CONSUME;
5682
5683nomem:
5684 return SCTP_DISPOSITION_NOMEM;
5685}
5686
5687/*
5688 * ADDIP Section 4.1 ASCONF CHunk Procedures
5689 * If the T4 RTO timer expires the endpoint should do B1 to B5
5690 */
5691sctp_disposition_t sctp_sf_t4_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005692 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 const struct sctp_endpoint *ep,
5694 const struct sctp_association *asoc,
5695 const sctp_subtype_t type,
5696 void *arg,
5697 sctp_cmd_seq_t *commands)
5698{
5699 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5700 struct sctp_transport *transport = chunk->transport;
5701
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005702 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5705 * detection on the appropriate destination address as defined in
5706 * RFC2960 [5] section 8.1 and 8.2.
5707 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005708 if (transport)
5709 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5710 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711
5712 /* Reconfig T4 timer and transport. */
5713 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5714
5715 /* ADDIP 4.1 B2) Increment the association error counters and perform
5716 * endpoint failure detection on the association as defined in
5717 * RFC2960 [5] section 8.1 and 8.2.
5718 * association error counter is incremented in SCTP_CMD_STRIKE.
5719 */
5720 if (asoc->overall_error_count >= asoc->max_retrans) {
5721 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5722 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005723 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5724 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005726 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005727 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5728 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 return SCTP_DISPOSITION_ABORT;
5730 }
5731
5732 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5733 * the ASCONF chunk was sent by doubling the RTO timer value.
5734 * This is done in SCTP_CMD_STRIKE.
5735 */
5736
5737 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5738 * choose an alternate destination address (please refer to RFC2960
5739 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005740 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 * ASCONF sent.
5742 */
5743 sctp_chunk_hold(asoc->addip_last_asconf);
5744 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5745 SCTP_CHUNK(asoc->addip_last_asconf));
5746
5747 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5748 * destination is selected, then the RTO used will be that of the new
5749 * destination address.
5750 */
5751 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5752 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5753
5754 return SCTP_DISPOSITION_CONSUME;
5755}
5756
5757/* sctpimpguide-05 Section 2.12.2
5758 * The sender of the SHUTDOWN MAY also start an overall guard timer
5759 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5760 * At the expiration of this timer the sender SHOULD abort the association
5761 * by sending an ABORT chunk.
5762 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005763sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5764 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 const struct sctp_association *asoc,
5766 const sctp_subtype_t type,
5767 void *arg,
5768 sctp_cmd_seq_t *commands)
5769{
5770 struct sctp_chunk *reply = NULL;
5771
5772 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005773 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
5775 reply = sctp_make_abort(asoc, NULL, 0);
5776 if (!reply)
5777 goto nomem;
5778
5779 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005780 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5781 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005783 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005785 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5786 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005787
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 return SCTP_DISPOSITION_DELETE_TCB;
5789nomem:
5790 return SCTP_DISPOSITION_NOMEM;
5791}
5792
5793/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5794 * the association is automatically closed by starting the shutdown process.
5795 * The work that needs to be done is same as when SHUTDOWN is initiated by
5796 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5797 */
5798sctp_disposition_t sctp_sf_autoclose_timer_expire(
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005799 struct net *net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 const struct sctp_endpoint *ep,
5801 const struct sctp_association *asoc,
5802 const sctp_subtype_t type,
5803 void *arg,
5804 sctp_cmd_seq_t *commands)
5805{
5806 int disposition;
5807
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00005808 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005809
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 /* From 9.2 Shutdown of an Association
5811 * Upon receipt of the SHUTDOWN primitive from its upper
5812 * layer, the endpoint enters SHUTDOWN-PENDING state and
5813 * remains there until all outstanding data has been
5814 * acknowledged by its peer. The endpoint accepts no new data
5815 * from its upper layer, but retransmits data to the far end
5816 * if necessary to fill gaps.
5817 */
5818 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5819 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5820
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 disposition = SCTP_DISPOSITION_CONSUME;
5822 if (sctp_outq_is_empty(&asoc->outqueue)) {
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005823 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 arg, commands);
5825 }
5826 return disposition;
5827}
5828
5829/*****************************************************************************
5830 * These are sa state functions which could apply to all types of events.
5831 ****************************************************************************/
5832
5833/*
5834 * This table entry is not implemented.
5835 *
5836 * Inputs
5837 * (endpoint, asoc, chunk)
5838 *
5839 * The return value is the disposition of the chunk.
5840 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005841sctp_disposition_t sctp_sf_not_impl(struct net *net,
5842 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 const struct sctp_association *asoc,
5844 const sctp_subtype_t type,
5845 void *arg,
5846 sctp_cmd_seq_t *commands)
5847{
5848 return SCTP_DISPOSITION_NOT_IMPL;
5849}
5850
5851/*
5852 * This table entry represents a bug.
5853 *
5854 * Inputs
5855 * (endpoint, asoc, chunk)
5856 *
5857 * The return value is the disposition of the chunk.
5858 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005859sctp_disposition_t sctp_sf_bug(struct net *net,
5860 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 const struct sctp_association *asoc,
5862 const sctp_subtype_t type,
5863 void *arg,
5864 sctp_cmd_seq_t *commands)
5865{
5866 return SCTP_DISPOSITION_BUG;
5867}
5868
5869/*
5870 * This table entry represents the firing of a timer in the wrong state.
5871 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5872 * when the association is in the wrong state. This event should
5873 * be ignored, so as to prevent any rearming of the timer.
5874 *
5875 * Inputs
5876 * (endpoint, asoc, chunk)
5877 *
5878 * The return value is the disposition of the chunk.
5879 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005880sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5881 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 const struct sctp_association *asoc,
5883 const sctp_subtype_t type,
5884 void *arg,
5885 sctp_cmd_seq_t *commands)
5886{
5887 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5888 return SCTP_DISPOSITION_CONSUME;
5889}
5890
5891/********************************************************************
5892 * 2nd Level Abstractions
5893 ********************************************************************/
5894
5895/* Pull the SACK chunk based on the SACK header. */
5896static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5897{
5898 struct sctp_sackhdr *sack;
5899 unsigned int len;
5900 __u16 num_blocks;
5901 __u16 num_dup_tsns;
5902
5903 /* Protect ourselves from reading too far into
5904 * the skb from a bogus sender.
5905 */
5906 sack = (struct sctp_sackhdr *) chunk->skb->data;
5907
5908 num_blocks = ntohs(sack->num_gap_ack_blocks);
5909 num_dup_tsns = ntohs(sack->num_dup_tsns);
5910 len = sizeof(struct sctp_sackhdr);
5911 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5912 if (len > chunk->skb->len)
5913 return NULL;
5914
5915 skb_pull(chunk->skb, len);
5916
5917 return sack;
5918}
5919
5920/* Create an ABORT packet to be sent as a response, with the specified
5921 * error causes.
5922 */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00005923static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5924 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925 const struct sctp_association *asoc,
5926 struct sctp_chunk *chunk,
5927 const void *payload,
5928 size_t paylen)
5929{
5930 struct sctp_packet *packet;
5931 struct sctp_chunk *abort;
5932
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005933 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
5935 if (packet) {
5936 /* Make an ABORT.
5937 * The T bit will be set if the asoc is NULL.
5938 */
5939 abort = sctp_make_abort(asoc, chunk, paylen);
5940 if (!abort) {
5941 sctp_ootb_pkt_free(packet);
5942 return NULL;
5943 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005944
5945 /* Reflect vtag if T-Bit is set */
5946 if (sctp_test_T_bit(abort))
5947 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 /* Add specified error causes, i.e., payload, to the
5950 * end of the chunk.
5951 */
5952 sctp_addto_chunk(abort, paylen, payload);
5953
5954 /* Set the skb to the belonging sock for accounting. */
5955 abort->skb->sk = ep->base.sk;
5956
5957 sctp_packet_append_chunk(packet, abort);
5958
5959 }
5960
5961 return packet;
5962}
5963
5964/* Allocate a packet for responding in the OOTB conditions. */
Eric W. Biederman2ce95502012-08-06 08:43:06 +00005965static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5966 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 const struct sctp_chunk *chunk)
5968{
5969 struct sctp_packet *packet;
5970 struct sctp_transport *transport;
5971 __u16 sport;
5972 __u16 dport;
5973 __u32 vtag;
5974
5975 /* Get the source and destination port from the inbound packet. */
5976 sport = ntohs(chunk->sctp_hdr->dest);
5977 dport = ntohs(chunk->sctp_hdr->source);
5978
5979 /* The V-tag is going to be the same as the inbound packet if no
5980 * association exists, otherwise, use the peer's vtag.
5981 */
5982 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005983 /* Special case the INIT-ACK as there is no peer's vtag
5984 * yet.
5985 */
5986 switch(chunk->chunk_hdr->type) {
5987 case SCTP_CID_INIT_ACK:
5988 {
5989 sctp_initack_chunk_t *initack;
5990
5991 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5992 vtag = ntohl(initack->init_hdr.init_tag);
5993 break;
5994 }
5995 default:
5996 vtag = asoc->peer.i.init_tag;
5997 break;
5998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 } else {
6000 /* Special case the INIT and stale COOKIE_ECHO as there is no
6001 * vtag yet.
6002 */
6003 switch(chunk->chunk_hdr->type) {
6004 case SCTP_CID_INIT:
6005 {
6006 sctp_init_chunk_t *init;
6007
6008 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6009 vtag = ntohl(init->init_hdr.init_tag);
6010 break;
6011 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006012 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 vtag = ntohl(chunk->sctp_hdr->vtag);
6014 break;
6015 }
6016 }
6017
6018 /* Make a transport for the bucket, Eliza... */
Eric W. Biederman89bf3452012-08-07 07:26:14 +00006019 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 if (!transport)
6021 goto nomem;
6022
6023 /* Cache a route for the transport with the chunk's destination as
6024 * the source address.
6025 */
Al Viro16b0a032006-11-20 17:13:38 -08006026 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006027 sctp_sk(net->sctp.ctl_sock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028
6029 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6030 packet = sctp_packet_config(packet, vtag, 0);
6031
6032 return packet;
6033
6034nomem:
6035 return NULL;
6036}
6037
6038/* Free the packet allocated earlier for responding in the OOTB condition. */
6039void sctp_ootb_pkt_free(struct sctp_packet *packet)
6040{
6041 sctp_transport_free(packet->transport);
6042}
6043
6044/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006045static void sctp_send_stale_cookie_err(struct net *net,
6046 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 const struct sctp_association *asoc,
6048 const struct sctp_chunk *chunk,
6049 sctp_cmd_seq_t *commands,
6050 struct sctp_chunk *err_chunk)
6051{
6052 struct sctp_packet *packet;
6053
6054 if (err_chunk) {
Eric W. Biederman2ce95502012-08-06 08:43:06 +00006055 packet = sctp_ootb_pkt_new(net, asoc, chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 if (packet) {
6057 struct sctp_signed_cookie *cookie;
6058
6059 /* Override the OOTB vtag from the cookie. */
6060 cookie = chunk->subh.cookie_hdr;
6061 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006062
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 /* Set the skb to the belonging sock for accounting. */
6064 err_chunk->skb->sk = ep->base.sk;
6065 sctp_packet_append_chunk(packet, err_chunk);
6066 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6067 SCTP_PACKET(packet));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006068 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 } else
6070 sctp_chunk_free (err_chunk);
6071 }
6072}
6073
6074
6075/* Process a data chunk */
6076static int sctp_eat_data(const struct sctp_association *asoc,
6077 struct sctp_chunk *chunk,
6078 sctp_cmd_seq_t *commands)
6079{
6080 sctp_datahdr_t *data_hdr;
6081 struct sctp_chunk *err;
6082 size_t datalen;
6083 sctp_verb_t deliver;
6084 int tmp;
6085 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006086 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08006087 struct sock *sk = asoc->base.sk;
Eric W. Biederman24cb81a2012-08-07 07:28:09 +00006088 struct net *net = sock_net(sk);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006089 u16 ssn;
6090 u16 sid;
6091 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
6093 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6094 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6095
6096 tsn = ntohl(data_hdr->tsn);
6097 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
6098
6099 /* ASSERT: Now skb->data is really the user data. */
6100
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 /* Process ECN based congestion.
6102 *
6103 * Since the chunk structure is reused for all chunks within
6104 * a packet, we use ecn_ce_done to track if we've already
6105 * done CE processing for this packet.
6106 *
6107 * We need to do ECN processing even if we plan to discard the
6108 * chunk later.
6109 */
6110
6111 if (!chunk->ecn_ce_done) {
6112 struct sctp_af *af;
6113 chunk->ecn_ce_done = 1;
6114
6115 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07006116 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117
6118 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6119 /* Do real work as sideffect. */
6120 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6121 SCTP_U32(tsn));
6122 }
6123 }
6124
6125 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6126 if (tmp < 0) {
6127 /* The TSN is too high--silently discard the chunk and
6128 * count on it getting retransmitted later.
6129 */
6130 return SCTP_IERROR_HIGH_TSN;
6131 } else if (tmp > 0) {
6132 /* This is a duplicate. Record it. */
6133 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6134 return SCTP_IERROR_DUP_TSN;
6135 }
6136
6137 /* This is a new TSN. */
6138
6139 /* Discard if there is no room in the receive window.
6140 * Actually, allow a little bit of overflow (up to a MTU).
6141 */
6142 datalen = ntohs(chunk->chunk_hdr->length);
6143 datalen -= sizeof(sctp_data_chunk_t);
6144
6145 deliver = SCTP_CMD_CHUNK_ULP;
6146
6147 /* Think about partial delivery. */
6148 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6149
6150 /* Even if we don't accept this chunk there is
6151 * memory pressure.
6152 */
6153 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6154 }
6155
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006156 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157 * seems a bit troublesome in that frag_point varies based on
6158 * PMTU. In cases, such as loopback, this might be a rather
6159 * large spill over.
6160 */
Neil Horman4d93df02007-08-15 16:07:44 -07006161 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6162 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163
6164 /* If this is the next TSN, consider reneging to make
6165 * room. Note: Playing nice with a confused sender. A
6166 * malicious sender can still eat up all our buffer
6167 * space and in the future we may want to detect and
6168 * do more drastic reneging.
6169 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006170 if (sctp_tsnmap_has_gap(map) &&
6171 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6173 deliver = SCTP_CMD_RENEGE;
6174 } else {
6175 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6176 "rwnd: %d\n", tsn, datalen,
6177 asoc->rwnd);
6178 return SCTP_IERROR_IGNORE_TSN;
6179 }
6180 }
6181
6182 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006183 * Also try to renege to limit our memory usage in the event that
6184 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006185 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006186 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6187 * memory usage too much
6188 */
6189 if (*sk->sk_prot_creator->memory_pressure) {
6190 if (sctp_tsnmap_has_gap(map) &&
6191 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6192 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6193 deliver = SCTP_CMD_RENEGE;
6194 }
6195 }
6196
6197 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 * Section 3.3.10.9 No User Data (9)
6199 *
6200 * Cause of error
6201 * ---------------
6202 * No User Data: This error cause is returned to the originator of a
6203 * DATA chunk if a received DATA chunk has no user data.
6204 */
6205 if (unlikely(0 == datalen)) {
6206 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6207 if (err) {
6208 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6209 SCTP_CHUNK(err));
6210 }
6211 /* We are going to ABORT, so we might as well stop
6212 * processing the rest of the chunks in the packet.
6213 */
6214 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006215 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6216 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006218 SCTP_PERR(SCTP_ERROR_NO_DATA));
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006219 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6220 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 return SCTP_IERROR_NO_DATA;
6222 }
6223
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006224 chunk->data_accepted = 1;
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6227 * if we renege and the chunk arrives again.
6228 */
6229 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006230 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006231 else {
Eric W. Biedermanb01a2402012-08-06 08:47:55 +00006232 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006233 ordered = 1;
6234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
6236 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6237 *
6238 * If an endpoint receive a DATA chunk with an invalid stream
6239 * identifier, it shall acknowledge the reception of the DATA chunk
6240 * following the normal procedure, immediately send an ERROR chunk
6241 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6242 * and discard the DATA chunk.
6243 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006244 sid = ntohs(data_hdr->stream);
6245 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006246 /* Mark tsn as received even though we drop it */
6247 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6248
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6250 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006251 sizeof(data_hdr->stream),
6252 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 if (err)
6254 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6255 SCTP_CHUNK(err));
6256 return SCTP_IERROR_BAD_STREAM;
6257 }
6258
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006259 /* Check to see if the SSN is possible for this TSN.
6260 * The biggest gap we can record is 4K wide. Since SSNs wrap
6261 * at an unsigned short, there is no way that an SSN can
6262 * wrap and for a valid TSN. We can simply check if the current
6263 * SSN is smaller then the next expected one. If it is, it wrapped
6264 * and is invalid.
6265 */
6266 ssn = ntohs(data_hdr->ssn);
6267 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6268 return SCTP_IERROR_PROTO_VIOLATION;
6269 }
6270
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 /* Send the data up to the user. Note: Schedule the
6272 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6273 * chunk needs the updated rwnd.
6274 */
6275 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6276
6277 return SCTP_IERROR_NO_ERROR;
6278}