blob: ab949320468dc19e9c8ce407e5566b69ebd5e28c [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
69static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
70 const struct sctp_association *asoc,
71 struct sctp_chunk *chunk,
72 const void *payload,
73 size_t paylen);
74static int sctp_eat_data(const struct sctp_association *asoc,
75 struct sctp_chunk *chunk,
76 sctp_cmd_seq_t *commands);
77static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk);
79static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
80 const struct sctp_association *asoc,
81 const struct sctp_chunk *chunk,
82 sctp_cmd_seq_t *commands,
83 struct sctp_chunk *err_chunk);
84static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
85 const struct sctp_association *asoc,
86 const sctp_subtype_t type,
87 void *arg,
88 sctp_cmd_seq_t *commands);
89static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
90 const struct sctp_association *asoc,
91 const sctp_subtype_t type,
92 void *arg,
93 sctp_cmd_seq_t *commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -040094static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
95 const struct sctp_association *asoc,
96 const sctp_subtype_t type,
97 void *arg,
98 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
100
Adrian Bunk52c1da32005-06-23 22:05:33 -0700101static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -0800102 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
105
Wei Yongjunaecedea2007-08-02 16:57:44 +0800106static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -0400107 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800108 const struct sctp_association *asoc,
109 void *arg,
110 sctp_cmd_seq_t *commands,
111 const __u8 *payload,
112 const size_t paylen);
113
Adrian Bunk52c1da32005-06-23 22:05:33 -0700114static sctp_disposition_t sctp_sf_violation_chunklen(
115 const struct sctp_endpoint *ep,
116 const struct sctp_association *asoc,
117 const sctp_subtype_t type,
118 void *arg,
119 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800121static sctp_disposition_t sctp_sf_violation_paramlen(
122 const struct sctp_endpoint *ep,
123 const struct sctp_association *asoc,
124 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700125 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800126 sctp_cmd_seq_t *commands);
127
Wei Yongjunaecedea2007-08-02 16:57:44 +0800128static sctp_disposition_t sctp_sf_violation_ctsn(
129 const struct sctp_endpoint *ep,
130 const struct sctp_association *asoc,
131 const sctp_subtype_t type,
132 void *arg,
133 sctp_cmd_seq_t *commands);
134
Vlad Yasevichece25df2007-09-07 16:30:54 -0400135static sctp_disposition_t sctp_sf_violation_chunk(
136 const struct sctp_endpoint *ep,
137 const struct sctp_association *asoc,
138 const sctp_subtype_t type,
139 void *arg,
140 sctp_cmd_seq_t *commands);
141
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700142static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
143 const struct sctp_association *asoc,
144 const sctp_subtype_t type,
145 struct sctp_chunk *chunk);
146
Vlad Yasevich75205f42007-12-20 14:12:59 -0800147static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
148 const struct sctp_association *asoc,
149 const sctp_subtype_t type,
150 void *arg,
151 sctp_cmd_seq_t *commands);
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Small helper function that checks if the chunk length
154 * is of the appropriate length. The 'required_length' argument
155 * is set to be the size of a specific chunk we are testing.
156 * Return Values: 1 = Valid length
157 * 0 = Invalid length
158 *
159 */
160static inline int
161sctp_chunk_length_valid(struct sctp_chunk *chunk,
162 __u16 required_length)
163{
164 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
165
166 if (unlikely(chunk_length < required_length))
167 return 0;
168
169 return 1;
170}
171
172/**********************************************************
173 * These are the state functions for handling chunk events.
174 **********************************************************/
175
176/*
177 * Process the final SHUTDOWN COMPLETE.
178 *
179 * Section: 4 (C) (diagram), 9.2
180 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
181 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
182 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
183 * should stop the T2-shutdown timer and remove all knowledge of the
184 * association (and thus the association enters the CLOSED state).
185 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700186 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * C) Rules for packet carrying SHUTDOWN COMPLETE:
188 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700189 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
190 * if the Verification Tag field of the packet matches its own tag and
191 * the T bit is not set
192 * OR
193 * it is set to its peer's tag and the T bit is set in the Chunk
194 * Flags.
195 * Otherwise, the receiver MUST silently discard the packet
196 * and take no further action. An endpoint MUST ignore the
197 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * Inputs
200 * (endpoint, asoc, chunk)
201 *
202 * Outputs
203 * (asoc, reply_msg, msg_up, timers, counters)
204 *
205 * The return value is the disposition of the chunk.
206 */
207sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
208 const struct sctp_association *asoc,
209 const sctp_subtype_t type,
210 void *arg,
211 sctp_cmd_seq_t *commands)
212{
213 struct sctp_chunk *chunk = arg;
214 struct sctp_ulpevent *ev;
215
Vlad Yasevichece25df2007-09-07 16:30:54 -0400216 if (!sctp_vtag_verify_either(chunk, asoc))
217 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* RFC 2960 6.10 Bundling
220 *
221 * An endpoint MUST NOT bundle INIT, INIT ACK or
222 * SHUTDOWN COMPLETE with any other chunks.
223 */
224 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400225 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Vlad Yasevichece25df2007-09-07 16:30:54 -0400227 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
228 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
229 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
230 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* RFC 2960 10.2 SCTP-to-ULP
233 *
234 * H) SHUTDOWN COMPLETE notification
235 *
236 * When SCTP completes the shutdown procedures (section 9.2) this
237 * notification is passed to the upper layer.
238 */
239 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700240 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700241 if (ev)
242 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900243 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
246 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
247 * not the chunk should be discarded. If the endpoint is in
248 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
249 * T2-shutdown timer and remove all knowledge of the
250 * association (and thus the association enters the CLOSED
251 * state).
252 */
253 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
255
256 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
257 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
258
259 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
260 SCTP_STATE(SCTP_STATE_CLOSED));
261
262 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
263 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
264
265 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
266
267 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
270/*
271 * Respond to a normal INIT chunk.
272 * We are the side that is being asked for an association.
273 *
274 * Section: 5.1 Normal Establishment of an Association, B
275 * B) "Z" shall respond immediately with an INIT ACK chunk. The
276 * destination IP address of the INIT ACK MUST be set to the source
277 * IP address of the INIT to which this INIT ACK is responding. In
278 * the response, besides filling in other parameters, "Z" must set the
279 * Verification Tag field to Tag_A, and also provide its own
280 * Verification Tag (Tag_Z) in the Initiate Tag field.
281 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900282 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 *
284 * Inputs
285 * (endpoint, asoc, chunk)
286 *
287 * Outputs
288 * (asoc, reply_msg, msg_up, timers, counters)
289 *
290 * The return value is the disposition of the chunk.
291 */
292sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
293 const struct sctp_association *asoc,
294 const sctp_subtype_t type,
295 void *arg,
296 sctp_cmd_seq_t *commands)
297{
298 struct sctp_chunk *chunk = arg;
299 struct sctp_chunk *repl;
300 struct sctp_association *new_asoc;
301 struct sctp_chunk *err_chunk;
302 struct sctp_packet *packet;
303 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 int len;
305
306 /* 6.10 Bundling
307 * An endpoint MUST NOT bundle INIT, INIT ACK or
308 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900309 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 * IG Section 2.11.2
311 * Furthermore, we require that the receiver of an INIT chunk MUST
312 * enforce these rules by silently discarding an arriving packet
313 * with an INIT chunk that is bundled with other chunks.
314 */
315 if (!chunk->singleton)
316 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
317
318 /* If the packet is an OOTB packet which is temporarily on the
319 * control endpoint, respond with an ABORT.
320 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800321 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
322 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900327 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
329 if (chunk->sctp_hdr->vtag != 0)
330 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
331
332 /* Make sure that the INIT chunk has a valid length.
333 * Normally, this would cause an ABORT with a Protocol Violation
334 * error, but since we don't have an association, we'll
335 * just discard the packet.
336 */
337 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
338 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
339
Vlad Yasevichbec96402009-07-30 18:08:28 -0400340 /* If the INIT is coming toward a closing socket, we'll send back
341 * and ABORT. Essentially, this catches the race of INIT being
342 * backloged to the socket at the same time as the user isses close().
343 * Since the socket and all its associations are going away, we
344 * can treat this OOTB
345 */
346 if (sctp_sstate(ep->base.sk, CLOSING))
347 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Verify the INIT chunk before processing it. */
350 err_chunk = NULL;
351 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
352 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
353 &err_chunk)) {
354 /* This chunk contains fatal error. It is to be discarded.
355 * Send an ABORT, with causes if there is any.
356 */
357 if (err_chunk) {
358 packet = sctp_abort_pkt_new(ep, asoc, arg,
359 (__u8 *)(err_chunk->chunk_hdr) +
360 sizeof(sctp_chunkhdr_t),
361 ntohs(err_chunk->chunk_hdr->length) -
362 sizeof(sctp_chunkhdr_t));
363
364 sctp_chunk_free(err_chunk);
365
366 if (packet) {
367 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
368 SCTP_PACKET(packet));
369 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
370 return SCTP_DISPOSITION_CONSUME;
371 } else {
372 return SCTP_DISPOSITION_NOMEM;
373 }
374 } else {
375 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
376 commands);
377 }
378 }
379
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900380 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
382
383 /* Tag the variable length parameters. */
384 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
385
386 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
387 if (!new_asoc)
388 goto nomem;
389
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000390 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
391 sctp_scope(sctp_source(chunk)),
392 GFP_ATOMIC) < 0)
393 goto nomem_init;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* The call, sctp_process_init(), can fail on memory allocation. */
Wei Yongjunde6becd2011-04-19 21:30:51 +0000396 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 (sctp_init_chunk_t *)chunk->chunk_hdr,
398 GFP_ATOMIC))
399 goto nomem_init;
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
402
403 /* If there are errors need to be reported for unknown parameters,
404 * make sure to reserve enough room in the INIT ACK for them.
405 */
406 len = 0;
407 if (err_chunk)
408 len = ntohs(err_chunk->chunk_hdr->length) -
409 sizeof(sctp_chunkhdr_t);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
412 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700413 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /* If there are errors need to be reported for unknown parameters,
416 * include them in the outgoing INIT ACK as "Unrecognized parameter"
417 * parameter.
418 */
419 if (err_chunk) {
420 /* Get the "Unrecognized parameter" parameter(s) out of the
421 * ERROR chunk generated by sctp_verify_init(). Since the
422 * error cause code for "unknown parameter" and the
423 * "Unrecognized parameter" type is the same, we can
424 * construct the parameters in INIT ACK by copying the
425 * ERROR causes over.
426 */
427 unk_param = (sctp_unrecognized_param_t *)
428 ((__u8 *)(err_chunk->chunk_hdr) +
429 sizeof(sctp_chunkhdr_t));
430 /* Replace the cause code with the "Unrecognized parameter"
431 * parameter type.
432 */
433 sctp_addto_chunk(repl, len, unk_param);
434 sctp_chunk_free(err_chunk);
435 }
436
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700437 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
440
441 /*
442 * Note: After sending out INIT ACK with the State Cookie parameter,
443 * "Z" MUST NOT allocate any resources, nor keep any states for the
444 * new association. Otherwise, "Z" will be vulnerable to resource
445 * attacks.
446 */
447 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
448
449 return SCTP_DISPOSITION_DELETE_TCB;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451nomem_init:
452 sctp_association_free(new_asoc);
453nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700454 if (err_chunk)
455 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return SCTP_DISPOSITION_NOMEM;
457}
458
459/*
460 * Respond to a normal INIT ACK chunk.
461 * We are the side that is initiating the association.
462 *
463 * Section: 5.1 Normal Establishment of an Association, C
464 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
465 * timer and leave COOKIE-WAIT state. "A" shall then send the State
466 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
467 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
468 *
469 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
470 * DATA chunks, but it MUST be the first chunk in the packet and
471 * until the COOKIE ACK is returned the sender MUST NOT send any
472 * other packets to the peer.
473 *
474 * Verification Tag: 3.3.3
475 * If the value of the Initiate Tag in a received INIT ACK chunk is
476 * found to be 0, the receiver MUST treat it as an error and close the
477 * association by transmitting an ABORT.
478 *
479 * Inputs
480 * (endpoint, asoc, chunk)
481 *
482 * Outputs
483 * (asoc, reply_msg, msg_up, timers, counters)
484 *
485 * The return value is the disposition of the chunk.
486 */
487sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
488 const struct sctp_association *asoc,
489 const sctp_subtype_t type,
490 void *arg,
491 sctp_cmd_seq_t *commands)
492{
493 struct sctp_chunk *chunk = arg;
494 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct sctp_chunk *err_chunk;
496 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (!sctp_vtag_verify(chunk, asoc))
499 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* 6.10 Bundling
502 * An endpoint MUST NOT bundle INIT, INIT ACK or
503 * SHUTDOWN COMPLETE with any other chunks.
504 */
505 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400506 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Vlad Yasevichece25df2007-09-07 16:30:54 -0400508 /* Make sure that the INIT-ACK chunk has a valid length */
509 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
510 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
511 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /* Grab the INIT header. */
513 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* Verify the INIT chunk before processing it. */
516 err_chunk = NULL;
517 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
518 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
519 &err_chunk)) {
520
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800521 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800524 * Send an ABORT, with causes. If there are no causes,
525 * then there wasn't enough memory. Just terminate
526 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
528 if (err_chunk) {
529 packet = sctp_abort_pkt_new(ep, asoc, arg,
530 (__u8 *)(err_chunk->chunk_hdr) +
531 sizeof(sctp_chunkhdr_t),
532 ntohs(err_chunk->chunk_hdr->length) -
533 sizeof(sctp_chunkhdr_t));
534
535 sctp_chunk_free(err_chunk);
536
537 if (packet) {
538 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
539 SCTP_PACKET(packet));
540 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700541 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700544
545 /* SCTP-AUTH, Section 6.3:
546 * It should be noted that if the receiver wants to tear
547 * down an association in an authenticated way only, the
548 * handling of malformed packets should not result in
549 * tearing down the association.
550 *
551 * This means that if we only want to abort associations
552 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300553 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700554 * was malformed.
555 */
556 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
557 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
558
559 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700560 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
561 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
564 /* Tag the variable length parameters. Note that we never
565 * convert the parameters in an INIT chunk.
566 */
567 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
568
569 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
570
571 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
572 SCTP_PEER_INIT(initchunk));
573
Frank Filz3f7a87d2005-06-20 13:14:57 -0700574 /* Reset init error count upon receipt of INIT-ACK. */
575 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* 5.1 C) "A" shall stop the T1-init timer and leave
578 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
579 * timer, and enter the COOKIE-ECHOED state.
580 */
581 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
582 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
583 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
584 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
585 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
586 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
587
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700588 /* SCTP-AUTH: genereate the assocition shared keys so that
589 * we can potentially signe the COOKIE-ECHO.
590 */
591 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* 5.1 C) "A" shall then send the State Cookie received in the
594 * INIT ACK chunk in a COOKIE ECHO chunk, ...
595 */
596 /* If there is any errors to report, send the ERROR chunk generated
597 * for unknown parameters as well.
598 */
599 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
600 SCTP_CHUNK(err_chunk));
601
602 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
606 * Respond to a normal COOKIE ECHO chunk.
607 * We are the side that is being asked for an association.
608 *
609 * Section: 5.1 Normal Establishment of an Association, D
610 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
611 * with a COOKIE ACK chunk after building a TCB and moving to
612 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
613 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
614 * chunk MUST be the first chunk in the packet.
615 *
616 * IMPLEMENTATION NOTE: An implementation may choose to send the
617 * Communication Up notification to the SCTP user upon reception
618 * of a valid COOKIE ECHO chunk.
619 *
620 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
621 * D) Rules for packet carrying a COOKIE ECHO
622 *
623 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
624 * Initial Tag received in the INIT ACK.
625 *
626 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
627 *
628 * Inputs
629 * (endpoint, asoc, chunk)
630 *
631 * Outputs
632 * (asoc, reply_msg, msg_up, timers, counters)
633 *
634 * The return value is the disposition of the chunk.
635 */
636sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
637 const struct sctp_association *asoc,
638 const sctp_subtype_t type, void *arg,
639 sctp_cmd_seq_t *commands)
640{
641 struct sctp_chunk *chunk = arg;
642 struct sctp_association *new_asoc;
643 sctp_init_chunk_t *peer_init;
644 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700645 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int error = 0;
647 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900648 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* If the packet is an OOTB packet which is temporarily on the
651 * control endpoint, respond with an ABORT.
652 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800653 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
654 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400655 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /* Make sure that the COOKIE_ECHO chunk has a valid length.
659 * In this case, we check that we have enough for at least a
660 * chunk header. More detailed verification is done
661 * in sctp_unpack_cookie().
662 */
663 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
664 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
665
Vlad Yasevich609ee462007-08-31 03:10:59 +0900666 /* If the endpoint is not listening or if the number of associations
667 * on the TCP-style socket exceed the max backlog, respond with an
668 * ABORT.
669 */
670 sk = ep->base.sk;
671 if (!sctp_sstate(sk, LISTENING) ||
672 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
673 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /* "Decode" the chunk. We have no optional parameters so we
676 * are in good shape.
677 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900678 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700680 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
681 sizeof(sctp_chunkhdr_t)))
682 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
685 * "Z" will reply with a COOKIE ACK chunk after building a TCB
686 * and moving to the ESTABLISHED state.
687 */
688 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
689 &err_chk_p);
690
691 /* FIXME:
692 * If the re-build failed, what is the proper error path
693 * from here?
694 *
695 * [We should abort the association. --piggy]
696 */
697 if (!new_asoc) {
698 /* FIXME: Several errors are possible. A bad cookie should
699 * be silently discarded, but think about logging it too.
700 */
701 switch (error) {
702 case -SCTP_IERROR_NOMEM:
703 goto nomem;
704
705 case -SCTP_IERROR_STALE_COOKIE:
706 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
707 err_chk_p);
708 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
709
710 case -SCTP_IERROR_BAD_SIG:
711 default:
712 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700717 /* Delay state machine commands until later.
718 *
719 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * the sctp_unpack_cookie() already.
721 */
722 /* This is a brand-new association, so these are not yet side
723 * effects--it is safe to run them here.
724 */
725 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
726
Wei Yongjunde6becd2011-04-19 21:30:51 +0000727 if (!sctp_process_init(new_asoc, chunk,
Al Viro6a1e5f32006-11-20 17:12:25 -0800728 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 peer_init, GFP_ATOMIC))
730 goto nomem_init;
731
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700732 /* SCTP-AUTH: Now that we've populate required fields in
733 * sctp_process_init, set up the assocaition shared keys as
734 * necessary so that we can potentially authenticate the ACK
735 */
736 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
737 if (error)
738 goto nomem_init;
739
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700740 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
741 * is supposed to be authenticated and we have to do delayed
742 * authentication. We've just recreated the association using
743 * the information in the cookie and now it's much easier to
744 * do the authentication.
745 */
746 if (chunk->auth_chunk) {
747 struct sctp_chunk auth;
748 sctp_ierror_t ret;
749
750 /* set-up our fake chunk so that we can process it */
751 auth.skb = chunk->auth_chunk;
752 auth.asoc = chunk->asoc;
753 auth.sctp_hdr = chunk->sctp_hdr;
754 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
755 sizeof(sctp_chunkhdr_t));
756 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
757 auth.transport = chunk->transport;
758
759 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
760
761 /* We can now safely free the auth_chunk clone */
762 kfree_skb(chunk->auth_chunk);
763
764 if (ret != SCTP_IERROR_NO_ERROR) {
765 sctp_association_free(new_asoc);
766 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
767 }
768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 repl = sctp_make_cookie_ack(new_asoc, chunk);
771 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700772 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 /* RFC 2960 5.1 Normal Establishment of an Association
775 *
776 * D) IMPLEMENTATION NOTE: An implementation may choose to
777 * send the Communication Up notification to the SCTP user
778 * upon reception of a valid COOKIE ECHO chunk.
779 */
780 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
781 new_asoc->c.sinit_num_ostreams,
782 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700783 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!ev)
785 goto nomem_ev;
786
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900787 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800788 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800790 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800792 if (new_asoc->peer.adaptation_ind) {
793 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700795 if (!ai_ev)
796 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700799 /* Add all the state machine commands now since we've created
800 * everything. This way we don't introduce memory corruptions
801 * during side-effect processing and correclty count established
802 * associations.
803 */
804 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
805 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
806 SCTP_STATE(SCTP_STATE_ESTABLISHED));
807 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
808 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
809 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
810
811 if (new_asoc->autoclose)
812 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
813 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
814
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700815 /* This will send the COOKIE ACK */
816 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
817
818 /* Queue the ASSOC_CHANGE event */
819 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
820
821 /* Send up the Adaptation Layer Indication event */
822 if (ai_ev)
823 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
824 SCTP_ULPEVENT(ai_ev));
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return SCTP_DISPOSITION_CONSUME;
827
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700828nomem_aiev:
829 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830nomem_ev:
831 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832nomem_init:
833 sctp_association_free(new_asoc);
834nomem:
835 return SCTP_DISPOSITION_NOMEM;
836}
837
838/*
839 * Respond to a normal COOKIE ACK chunk.
840 * We are the side that is being asked for an association.
841 *
842 * RFC 2960 5.1 Normal Establishment of an Association
843 *
844 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
845 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
846 * timer. It may also notify its ULP about the successful
847 * establishment of the association with a Communication Up
848 * notification (see Section 10).
849 *
850 * Verification Tag:
851 * Inputs
852 * (endpoint, asoc, chunk)
853 *
854 * Outputs
855 * (asoc, reply_msg, msg_up, timers, counters)
856 *
857 * The return value is the disposition of the chunk.
858 */
859sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
860 const struct sctp_association *asoc,
861 const sctp_subtype_t type, void *arg,
862 sctp_cmd_seq_t *commands)
863{
864 struct sctp_chunk *chunk = arg;
865 struct sctp_ulpevent *ev;
866
867 if (!sctp_vtag_verify(chunk, asoc))
868 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
869
870 /* Verify that the chunk length for the COOKIE-ACK is OK.
871 * If we don't do this, any bundled chunks may be junked.
872 */
873 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
874 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
875 commands);
876
877 /* Reset init error count upon receipt of COOKIE-ACK,
878 * to avoid problems with the managemement of this
879 * counter in stale cookie situations when a transition back
880 * from the COOKIE-ECHOED state to the COOKIE-WAIT
881 * state is performed.
882 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700883 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* RFC 2960 5.1 Normal Establishment of an Association
886 *
887 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
888 * from the COOKIE-ECHOED state to the ESTABLISHED state,
889 * stopping the T1-cookie timer.
890 */
891 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
892 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
893 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
894 SCTP_STATE(SCTP_STATE_ESTABLISHED));
895 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
896 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
897 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
898 if (asoc->autoclose)
899 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
900 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* It may also notify its ULP about the successful
903 * establishment of the association with a Communication Up
904 * notification (see Section 10).
905 */
906 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
907 0, asoc->c.sinit_num_ostreams,
908 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700909 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 if (!ev)
912 goto nomem;
913
914 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
915
916 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800917 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800919 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800921 if (asoc->peer.adaptation_ind) {
922 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!ev)
924 goto nomem;
925
926 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
927 SCTP_ULPEVENT(ev));
928 }
929
930 return SCTP_DISPOSITION_CONSUME;
931nomem:
932 return SCTP_DISPOSITION_NOMEM;
933}
934
935/* Generate and sendout a heartbeat packet. */
936static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
937 const struct sctp_association *asoc,
938 const sctp_subtype_t type,
939 void *arg,
940 sctp_cmd_seq_t *commands)
941{
942 struct sctp_transport *transport = (struct sctp_transport *) arg;
943 struct sctp_chunk *reply;
944 sctp_sender_hb_info_t hbinfo;
945 size_t paylen = 0;
946
947 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
948 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
Al Viro63de08f2006-11-20 17:07:25 -0800949 hbinfo.daddr = transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 hbinfo.sent_at = jiffies;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700951 hbinfo.hb_nonce = transport->hb_nonce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* Send a heartbeat to our peer. */
954 paylen = sizeof(sctp_sender_hb_info_t);
955 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
956 if (!reply)
957 return SCTP_DISPOSITION_NOMEM;
958
959 /* Set rto_pending indicating that an RTT measurement
960 * is started with this heartbeat chunk.
961 */
962 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
963 SCTP_TRANSPORT(transport));
964
965 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
966 return SCTP_DISPOSITION_CONSUME;
967}
968
969/* Generate a HEARTBEAT packet on the given transport. */
970sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
971 const struct sctp_association *asoc,
972 const sctp_subtype_t type,
973 void *arg,
974 sctp_cmd_seq_t *commands)
975{
976 struct sctp_transport *transport = (struct sctp_transport *) arg;
977
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400978 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700979 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
980 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
982 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800983 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
985 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
986 return SCTP_DISPOSITION_DELETE_TCB;
987 }
988
989 /* Section 3.3.5.
990 * The Sender-specific Heartbeat Info field should normally include
991 * information about the sender's current time when this HEARTBEAT
992 * chunk is sent and the destination transport address to which this
993 * HEARTBEAT is sent (see Section 8.3).
994 */
995
Frank Filz52ccb8e2005-12-22 11:36:46 -0800996 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (SCTP_DISPOSITION_NOMEM ==
998 sctp_sf_heartbeat(ep, asoc, type, arg,
999 commands))
1000 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001001
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 /* Set transport error counter and association error counter
1003 * when sending heartbeat.
1004 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001005 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 SCTP_TRANSPORT(transport));
1007 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001008 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1009 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1011 SCTP_TRANSPORT(transport));
1012
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001013 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
1016/*
1017 * Process an heartbeat request.
1018 *
1019 * Section: 8.3 Path Heartbeat
1020 * The receiver of the HEARTBEAT should immediately respond with a
1021 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1022 * from the received HEARTBEAT chunk.
1023 *
1024 * Verification Tag: 8.5 Verification Tag [Normal verification]
1025 * When receiving an SCTP packet, the endpoint MUST ensure that the
1026 * value in the Verification Tag field of the received SCTP packet
1027 * matches its own Tag. If the received Verification Tag value does not
1028 * match the receiver's own tag value, the receiver shall silently
1029 * discard the packet and shall not process it any further except for
1030 * those cases listed in Section 8.5.1 below.
1031 *
1032 * Inputs
1033 * (endpoint, asoc, chunk)
1034 *
1035 * Outputs
1036 * (asoc, reply_msg, msg_up, timers, counters)
1037 *
1038 * The return value is the disposition of the chunk.
1039 */
1040sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1041 const struct sctp_association *asoc,
1042 const sctp_subtype_t type,
1043 void *arg,
1044 sctp_cmd_seq_t *commands)
1045{
1046 struct sctp_chunk *chunk = arg;
1047 struct sctp_chunk *reply;
1048 size_t paylen = 0;
1049
1050 if (!sctp_vtag_verify(chunk, asoc))
1051 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1052
1053 /* Make sure that the HEARTBEAT chunk has a valid length. */
1054 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1055 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1056 commands);
1057
1058 /* 8.3 The receiver of the HEARTBEAT should immediately
1059 * respond with a HEARTBEAT ACK that contains the Heartbeat
1060 * Information field copied from the received HEARTBEAT chunk.
1061 */
1062 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1063 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001064 if (!pskb_pull(chunk->skb, paylen))
1065 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 reply = sctp_make_heartbeat_ack(asoc, chunk,
1068 chunk->subh.hb_hdr, paylen);
1069 if (!reply)
1070 goto nomem;
1071
1072 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1073 return SCTP_DISPOSITION_CONSUME;
1074
1075nomem:
1076 return SCTP_DISPOSITION_NOMEM;
1077}
1078
1079/*
1080 * Process the returning HEARTBEAT ACK.
1081 *
1082 * Section: 8.3 Path Heartbeat
1083 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1084 * should clear the error counter of the destination transport
1085 * address to which the HEARTBEAT was sent, and mark the destination
1086 * transport address as active if it is not so marked. The endpoint may
1087 * optionally report to the upper layer when an inactive destination
1088 * address is marked as active due to the reception of the latest
1089 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1090 * clear the association overall error count as well (as defined
1091 * in section 8.1).
1092 *
1093 * The receiver of the HEARTBEAT ACK should also perform an RTT
1094 * measurement for that destination transport address using the time
1095 * value carried in the HEARTBEAT ACK chunk.
1096 *
1097 * Verification Tag: 8.5 Verification Tag [Normal verification]
1098 *
1099 * Inputs
1100 * (endpoint, asoc, chunk)
1101 *
1102 * Outputs
1103 * (asoc, reply_msg, msg_up, timers, counters)
1104 *
1105 * The return value is the disposition of the chunk.
1106 */
1107sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1108 const struct sctp_association *asoc,
1109 const sctp_subtype_t type,
1110 void *arg,
1111 sctp_cmd_seq_t *commands)
1112{
1113 struct sctp_chunk *chunk = arg;
1114 union sctp_addr from_addr;
1115 struct sctp_transport *link;
1116 sctp_sender_hb_info_t *hbinfo;
1117 unsigned long max_interval;
1118
1119 if (!sctp_vtag_verify(chunk, asoc))
1120 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1121
1122 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001123 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1124 sizeof(sctp_sender_hb_info_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1126 commands);
1127
1128 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001129 /* Make sure that the length of the parameter is what we expect */
1130 if (ntohs(hbinfo->param_hdr.length) !=
1131 sizeof(sctp_sender_hb_info_t)) {
1132 return SCTP_DISPOSITION_DISCARD;
1133 }
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001136 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001139 if (unlikely(!link)) {
1140 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001141 if (net_ratelimit())
Joe Perches145ce502010-08-24 13:21:08 +00001142 pr_warn("%s association %p could not find address %pI6\n",
1143 __func__,
1144 asoc,
1145 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001146 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001147 if (net_ratelimit())
Joe Perches145ce502010-08-24 13:21:08 +00001148 pr_warn("%s association %p could not find address %pI4\n",
1149 __func__,
1150 asoc,
1151 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return SCTP_DISPOSITION_DISCARD;
1154 }
1155
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001156 /* Validate the 64-bit random nonce. */
1157 if (hbinfo->hb_nonce != link->hb_nonce)
1158 return SCTP_DISPOSITION_DISCARD;
1159
Frank Filz52ccb8e2005-12-22 11:36:46 -08001160 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 /* Check if the timestamp looks valid. */
1163 if (time_after(hbinfo->sent_at, jiffies) ||
1164 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001165 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001167 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 return SCTP_DISPOSITION_DISCARD;
1169 }
1170
1171 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1172 * the HEARTBEAT should clear the error counter of the
1173 * destination transport address to which the HEARTBEAT was
1174 * sent and mark the destination transport address as active if
1175 * it is not so marked.
1176 */
1177 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1178
1179 return SCTP_DISPOSITION_CONSUME;
1180}
1181
1182/* Helper function to send out an abort for the restart
1183 * condition.
1184 */
1185static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1186 struct sctp_chunk *init,
1187 sctp_cmd_seq_t *commands)
1188{
1189 int len;
1190 struct sctp_packet *pkt;
1191 union sctp_addr_param *addrparm;
1192 struct sctp_errhdr *errhdr;
1193 struct sctp_endpoint *ep;
1194 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1195 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1196
1197 /* Build the error on the stack. We are way to malloc crazy
1198 * throughout the code today.
1199 */
1200 errhdr = (struct sctp_errhdr *)buffer;
1201 addrparm = (union sctp_addr_param *)errhdr->variable;
1202
1203 /* Copy into a parm format. */
1204 len = af->to_addr_param(ssa, addrparm);
1205 len += sizeof(sctp_errhdr_t);
1206
1207 errhdr->cause = SCTP_ERROR_RESTART;
1208 errhdr->length = htons(len);
1209
1210 /* Assign to the control socket. */
1211 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1212
1213 /* Association is NULL since this may be a restart attack and we
1214 * want to send back the attacker's vtag.
1215 */
1216 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1217
1218 if (!pkt)
1219 goto out;
1220 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1221
1222 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1223
1224 /* Discard the rest of the inbound packet. */
1225 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1226
1227out:
1228 /* Even if there is no memory, treat as a failure so
1229 * the packet will get dropped.
1230 */
1231 return 0;
1232}
1233
Joe Perches123031c2010-09-08 11:04:21 +00001234static bool list_has_sctp_addr(const struct list_head *list,
1235 union sctp_addr *ipaddr)
1236{
1237 struct sctp_transport *addr;
1238
1239 list_for_each_entry(addr, list, transports) {
1240 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1241 return true;
1242 }
1243
1244 return false;
1245}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/* A restart is occurring, check to make sure no new addresses
1247 * are being added as we may be under a takeover attack.
1248 */
1249static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1250 const struct sctp_association *asoc,
1251 struct sctp_chunk *init,
1252 sctp_cmd_seq_t *commands)
1253{
Joe Perches123031c2010-09-08 11:04:21 +00001254 struct sctp_transport *new_addr;
1255 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Joe Perches123031c2010-09-08 11:04:21 +00001257 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * ...
1259 * Before responding the endpoint MUST check to see if the
1260 * unexpected INIT adds new addresses to the association. If new
1261 * addresses are added to the association, the endpoint MUST respond
1262 * with an ABORT..
1263 */
1264
1265 /* Search through all current addresses and make sure
1266 * we aren't adding any new ones.
1267 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001268 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001269 transports) {
1270 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1271 &new_addr->ipaddr)) {
1272 sctp_sf_send_restart_abort(&new_addr->ipaddr, init,
1273 commands);
1274 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 break;
Joe Perches123031c2010-09-08 11:04:21 +00001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
1279 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001280 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283/* Populate the verification/tie tags based on overlapping INIT
1284 * scenario.
1285 *
1286 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1287 */
1288static void sctp_tietags_populate(struct sctp_association *new_asoc,
1289 const struct sctp_association *asoc)
1290{
1291 switch (asoc->state) {
1292
1293 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1294
1295 case SCTP_STATE_COOKIE_WAIT:
1296 new_asoc->c.my_vtag = asoc->c.my_vtag;
1297 new_asoc->c.my_ttag = asoc->c.my_vtag;
1298 new_asoc->c.peer_ttag = 0;
1299 break;
1300
1301 case SCTP_STATE_COOKIE_ECHOED:
1302 new_asoc->c.my_vtag = asoc->c.my_vtag;
1303 new_asoc->c.my_ttag = asoc->c.my_vtag;
1304 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1305 break;
1306
1307 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1308 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1309 */
1310 default:
1311 new_asoc->c.my_ttag = asoc->c.my_vtag;
1312 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1313 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /* Other parameters for the endpoint SHOULD be copied from the
1317 * existing parameters of the association (e.g. number of
1318 * outbound streams) into the INIT ACK and cookie.
1319 */
1320 new_asoc->rwnd = asoc->rwnd;
1321 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1322 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1323 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1324}
1325
1326/*
1327 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1328 * handling action.
1329 *
1330 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1331 *
1332 * Returns value representing action to be taken. These action values
1333 * correspond to Action/Description values in RFC 2960, Table 2.
1334 */
1335static char sctp_tietags_compare(struct sctp_association *new_asoc,
1336 const struct sctp_association *asoc)
1337{
1338 /* In this case, the peer may have restarted. */
1339 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1340 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1341 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1342 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1343 return 'A';
1344
1345 /* Collision case B. */
1346 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1347 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1348 (0 == asoc->c.peer_vtag))) {
1349 return 'B';
1350 }
1351
1352 /* Collision case D. */
1353 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1354 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1355 return 'D';
1356
1357 /* Collision case C. */
1358 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1359 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1360 (0 == new_asoc->c.my_ttag) &&
1361 (0 == new_asoc->c.peer_ttag))
1362 return 'C';
1363
1364 /* No match to any of the special cases; discard this packet. */
1365 return 'E';
1366}
1367
1368/* Common helper routine for both duplicate and simulataneous INIT
1369 * chunk handling.
1370 */
1371static sctp_disposition_t sctp_sf_do_unexpected_init(
1372 const struct sctp_endpoint *ep,
1373 const struct sctp_association *asoc,
1374 const sctp_subtype_t type,
1375 void *arg, sctp_cmd_seq_t *commands)
1376{
1377 sctp_disposition_t retval;
1378 struct sctp_chunk *chunk = arg;
1379 struct sctp_chunk *repl;
1380 struct sctp_association *new_asoc;
1381 struct sctp_chunk *err_chunk;
1382 struct sctp_packet *packet;
1383 sctp_unrecognized_param_t *unk_param;
1384 int len;
1385
1386 /* 6.10 Bundling
1387 * An endpoint MUST NOT bundle INIT, INIT ACK or
1388 * SHUTDOWN COMPLETE with any other chunks.
1389 *
1390 * IG Section 2.11.2
1391 * Furthermore, we require that the receiver of an INIT chunk MUST
1392 * enforce these rules by silently discarding an arriving packet
1393 * with an INIT chunk that is bundled with other chunks.
1394 */
1395 if (!chunk->singleton)
1396 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1397
1398 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001399 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
1401 if (chunk->sctp_hdr->vtag != 0)
1402 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1403
1404 /* Make sure that the INIT chunk has a valid length.
1405 * In this case, we generate a protocol violation since we have
1406 * an association established.
1407 */
1408 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1409 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1410 commands);
1411 /* Grab the INIT header. */
1412 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1413
1414 /* Tag the variable length parameters. */
1415 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1416
1417 /* Verify the INIT chunk before processing it. */
1418 err_chunk = NULL;
1419 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1420 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1421 &err_chunk)) {
1422 /* This chunk contains fatal error. It is to be discarded.
1423 * Send an ABORT, with causes if there is any.
1424 */
1425 if (err_chunk) {
1426 packet = sctp_abort_pkt_new(ep, asoc, arg,
1427 (__u8 *)(err_chunk->chunk_hdr) +
1428 sizeof(sctp_chunkhdr_t),
1429 ntohs(err_chunk->chunk_hdr->length) -
1430 sizeof(sctp_chunkhdr_t));
1431
1432 if (packet) {
1433 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1434 SCTP_PACKET(packet));
1435 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1436 retval = SCTP_DISPOSITION_CONSUME;
1437 } else {
1438 retval = SCTP_DISPOSITION_NOMEM;
1439 }
1440 goto cleanup;
1441 } else {
1442 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1443 commands);
1444 }
1445 }
1446
1447 /*
1448 * Other parameters for the endpoint SHOULD be copied from the
1449 * existing parameters of the association (e.g. number of
1450 * outbound streams) into the INIT ACK and cookie.
1451 * FIXME: We are copying parameters from the endpoint not the
1452 * association.
1453 */
1454 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1455 if (!new_asoc)
1456 goto nomem;
1457
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001458 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1459 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1460 goto nomem;
1461
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 /* In the outbound INIT ACK the endpoint MUST copy its current
1463 * Verification Tag and Peers Verification tag into a reserved
1464 * place (local tie-tag and per tie-tag) within the state cookie.
1465 */
Wei Yongjunde6becd2011-04-19 21:30:51 +00001466 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001468 GFP_ATOMIC))
1469 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /* Make sure no new addresses are being added during the
1472 * restart. Do not do this check for COOKIE-WAIT state,
1473 * since there are no peer addresses to check against.
1474 * Upon return an ABORT will have been sent if needed.
1475 */
1476 if (!sctp_state(asoc, COOKIE_WAIT)) {
1477 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1478 commands)) {
1479 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001480 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 }
1483
1484 sctp_tietags_populate(new_asoc, asoc);
1485
1486 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1487
1488 /* If there are errors need to be reported for unknown parameters,
1489 * make sure to reserve enough room in the INIT ACK for them.
1490 */
1491 len = 0;
1492 if (err_chunk) {
1493 len = ntohs(err_chunk->chunk_hdr->length) -
1494 sizeof(sctp_chunkhdr_t);
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1498 if (!repl)
1499 goto nomem;
1500
1501 /* If there are errors need to be reported for unknown parameters,
1502 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1503 * parameter.
1504 */
1505 if (err_chunk) {
1506 /* Get the "Unrecognized parameter" parameter(s) out of the
1507 * ERROR chunk generated by sctp_verify_init(). Since the
1508 * error cause code for "unknown parameter" and the
1509 * "Unrecognized parameter" type is the same, we can
1510 * construct the parameters in INIT ACK by copying the
1511 * ERROR causes over.
1512 */
1513 unk_param = (sctp_unrecognized_param_t *)
1514 ((__u8 *)(err_chunk->chunk_hdr) +
1515 sizeof(sctp_chunkhdr_t));
1516 /* Replace the cause code with the "Unrecognized parameter"
1517 * parameter type.
1518 */
1519 sctp_addto_chunk(repl, len, unk_param);
1520 }
1521
1522 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1523 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1524
1525 /*
1526 * Note: After sending out INIT ACK with the State Cookie parameter,
1527 * "Z" MUST NOT allocate any resources for this new association.
1528 * Otherwise, "Z" will be vulnerable to resource attacks.
1529 */
1530 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1531 retval = SCTP_DISPOSITION_CONSUME;
1532
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001533 return retval;
1534
1535nomem:
1536 retval = SCTP_DISPOSITION_NOMEM;
1537nomem_retval:
1538 if (new_asoc)
1539 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540cleanup:
1541 if (err_chunk)
1542 sctp_chunk_free(err_chunk);
1543 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001547 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 * This means we started an INIT and then we got an INIT request from
1549 * our peer.
1550 *
1551 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1552 * This usually indicates an initialization collision, i.e., each
1553 * endpoint is attempting, at about the same time, to establish an
1554 * association with the other endpoint.
1555 *
1556 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1557 * endpoint MUST respond with an INIT ACK using the same parameters it
1558 * sent in its original INIT chunk (including its Verification Tag,
1559 * unchanged). These original parameters are combined with those from the
1560 * newly received INIT chunk. The endpoint shall also generate a State
1561 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1562 * INIT to calculate the State Cookie.
1563 *
1564 * After that, the endpoint MUST NOT change its state, the T1-init
1565 * timer shall be left running and the corresponding TCB MUST NOT be
1566 * destroyed. The normal procedures for handling State Cookies when
1567 * a TCB exists will resolve the duplicate INITs to a single association.
1568 *
1569 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1570 * its Tie-Tags with the Tag information of itself and its peer (see
1571 * section 5.2.2 for a description of the Tie-Tags).
1572 *
1573 * Verification Tag: Not explicit, but an INIT can not have a valid
1574 * verification tag, so we skip the check.
1575 *
1576 * Inputs
1577 * (endpoint, asoc, chunk)
1578 *
1579 * Outputs
1580 * (asoc, reply_msg, msg_up, timers, counters)
1581 *
1582 * The return value is the disposition of the chunk.
1583 */
1584sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1585 const struct sctp_association *asoc,
1586 const sctp_subtype_t type,
1587 void *arg,
1588 sctp_cmd_seq_t *commands)
1589{
1590 /* Call helper to do the real work for both simulataneous and
1591 * duplicate INIT chunk handling.
1592 */
1593 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1594}
1595
1596/*
1597 * Handle duplicated INIT messages. These are usually delayed
1598 * restransmissions.
1599 *
1600 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1601 * COOKIE-ECHOED and COOKIE-WAIT
1602 *
1603 * Unless otherwise stated, upon reception of an unexpected INIT for
1604 * this association, the endpoint shall generate an INIT ACK with a
1605 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1606 * current Verification Tag and peer's Verification Tag into a reserved
1607 * place within the state cookie. We shall refer to these locations as
1608 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1609 * containing this INIT ACK MUST carry a Verification Tag value equal to
1610 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1611 * MUST contain a new Initiation Tag (randomly generated see Section
1612 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1613 * existing parameters of the association (e.g. number of outbound
1614 * streams) into the INIT ACK and cookie.
1615 *
1616 * After sending out the INIT ACK, the endpoint shall take no further
1617 * actions, i.e., the existing association, including its current state,
1618 * and the corresponding TCB MUST NOT be changed.
1619 *
1620 * Note: Only when a TCB exists and the association is not in a COOKIE-
1621 * WAIT state are the Tie-Tags populated. For a normal association INIT
1622 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1623 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1624 * State Cookie are populated as specified in section 5.2.1.
1625 *
1626 * Verification Tag: Not specified, but an INIT has no way of knowing
1627 * what the verification tag could be, so we ignore it.
1628 *
1629 * Inputs
1630 * (endpoint, asoc, chunk)
1631 *
1632 * Outputs
1633 * (asoc, reply_msg, msg_up, timers, counters)
1634 *
1635 * The return value is the disposition of the chunk.
1636 */
1637sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1638 const struct sctp_association *asoc,
1639 const sctp_subtype_t type,
1640 void *arg,
1641 sctp_cmd_seq_t *commands)
1642{
1643 /* Call helper to do the real work for both simulataneous and
1644 * duplicate INIT chunk handling.
1645 */
1646 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1647}
1648
1649
Vlad Yasevich610ab732007-01-15 19:18:30 -08001650/*
1651 * Unexpected INIT-ACK handler.
1652 *
1653 * Section 5.2.3
1654 * If an INIT ACK received by an endpoint in any state other than the
1655 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1656 * An unexpected INIT ACK usually indicates the processing of an old or
1657 * duplicated INIT chunk.
1658*/
1659sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1660 const struct sctp_association *asoc,
1661 const sctp_subtype_t type,
1662 void *arg, sctp_cmd_seq_t *commands)
1663{
1664 /* Per the above section, we'll discard the chunk if we have an
1665 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1666 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001667 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001668 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1669 else
1670 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1674 *
1675 * Section 5.2.4
1676 * A) In this case, the peer may have restarted.
1677 */
1678static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1679 const struct sctp_association *asoc,
1680 struct sctp_chunk *chunk,
1681 sctp_cmd_seq_t *commands,
1682 struct sctp_association *new_asoc)
1683{
1684 sctp_init_chunk_t *peer_init;
1685 struct sctp_ulpevent *ev;
1686 struct sctp_chunk *repl;
1687 struct sctp_chunk *err;
1688 sctp_disposition_t disposition;
1689
1690 /* new_asoc is a brand-new association, so these are not yet
1691 * side effects--it is safe to run them here.
1692 */
1693 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1694
Wei Yongjunde6becd2011-04-19 21:30:51 +00001695 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 GFP_ATOMIC))
1697 goto nomem;
1698
1699 /* Make sure no new addresses are being added during the
1700 * restart. Though this is a pretty complicated attack
1701 * since you'd have to get inside the cookie.
1702 */
1703 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1704 return SCTP_DISPOSITION_CONSUME;
1705 }
1706
1707 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1708 * the peer has restarted (Action A), it MUST NOT setup a new
1709 * association but instead resend the SHUTDOWN ACK and send an ERROR
1710 * chunk with a "Cookie Received while Shutting Down" error cause to
1711 * its peer.
1712 */
1713 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1714 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1715 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1716 chunk, commands);
1717 if (SCTP_DISPOSITION_NOMEM == disposition)
1718 goto nomem;
1719
1720 err = sctp_make_op_error(asoc, chunk,
1721 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001722 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if (err)
1724 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1725 SCTP_CHUNK(err));
1726
1727 return SCTP_DISPOSITION_CONSUME;
1728 }
1729
1730 /* For now, fail any unsent/unacked data. Consider the optional
1731 * choice of resending of this data.
1732 */
1733 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 repl = sctp_make_cookie_ack(new_asoc, chunk);
1736 if (!repl)
1737 goto nomem;
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /* Report association restart to upper layer. */
1740 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1741 new_asoc->c.sinit_num_ostreams,
1742 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001743 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (!ev)
1745 goto nomem_ev;
1746
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001747 /* Update the content of current association. */
1748 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1749 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1751 return SCTP_DISPOSITION_CONSUME;
1752
1753nomem_ev:
1754 sctp_chunk_free(repl);
1755nomem:
1756 return SCTP_DISPOSITION_NOMEM;
1757}
1758
1759/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1760 *
1761 * Section 5.2.4
1762 * B) In this case, both sides may be attempting to start an association
1763 * at about the same time but the peer endpoint started its INIT
1764 * after responding to the local endpoint's INIT
1765 */
1766/* This case represents an initialization collision. */
1767static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1768 const struct sctp_association *asoc,
1769 struct sctp_chunk *chunk,
1770 sctp_cmd_seq_t *commands,
1771 struct sctp_association *new_asoc)
1772{
1773 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct sctp_chunk *repl;
1775
1776 /* new_asoc is a brand-new association, so these are not yet
1777 * side effects--it is safe to run them here.
1778 */
1779 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
Wei Yongjunde6becd2011-04-19 21:30:51 +00001780 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 GFP_ATOMIC))
1782 goto nomem;
1783
1784 /* Update the content of current association. */
1785 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1786 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1787 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1788 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1789 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1790
1791 repl = sctp_make_cookie_ack(new_asoc, chunk);
1792 if (!repl)
1793 goto nomem;
1794
1795 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 /* RFC 2960 5.1 Normal Establishment of an Association
1798 *
1799 * D) IMPLEMENTATION NOTE: An implementation may choose to
1800 * send the Communication Up notification to the SCTP user
1801 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001802 *
1803 * Sadly, this needs to be implemented as a side-effect, because
1804 * we are not guaranteed to have set the association id of the real
1805 * association and so these notifications need to be delayed until
1806 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Vlad Yasevich07d93962007-05-04 13:55:27 -07001809 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001812 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001814 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001815 *
1816 * This also needs to be done as a side effect for the same reason as
1817 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001819 if (asoc->peer.adaptation_ind)
1820 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822 return SCTP_DISPOSITION_CONSUME;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824nomem:
1825 return SCTP_DISPOSITION_NOMEM;
1826}
1827
1828/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1829 *
1830 * Section 5.2.4
1831 * C) In this case, the local endpoint's cookie has arrived late.
1832 * Before it arrived, the local endpoint sent an INIT and received an
1833 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1834 * but a new tag of its own.
1835 */
1836/* This case represents an initialization collision. */
1837static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1838 const struct sctp_association *asoc,
1839 struct sctp_chunk *chunk,
1840 sctp_cmd_seq_t *commands,
1841 struct sctp_association *new_asoc)
1842{
1843 /* The cookie should be silently discarded.
1844 * The endpoint SHOULD NOT change states and should leave
1845 * any timers running.
1846 */
1847 return SCTP_DISPOSITION_DISCARD;
1848}
1849
1850/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1851 *
1852 * Section 5.2.4
1853 *
1854 * D) When both local and remote tags match the endpoint should always
1855 * enter the ESTABLISHED state, if it has not already done so.
1856 */
1857/* This case represents an initialization collision. */
1858static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1859 const struct sctp_association *asoc,
1860 struct sctp_chunk *chunk,
1861 sctp_cmd_seq_t *commands,
1862 struct sctp_association *new_asoc)
1863{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001864 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct sctp_chunk *repl;
1866
1867 /* Clarification from Implementor's Guide:
1868 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001869 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1870 * It should stop any cookie timer that may be running and send
1871 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
1873
1874 /* Don't accidentally move back into established state. */
1875 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1876 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1877 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1878 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1879 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1880 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1881 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1882 SCTP_NULL());
1883
1884 /* RFC 2960 5.1 Normal Establishment of an Association
1885 *
1886 * D) IMPLEMENTATION NOTE: An implementation may choose
1887 * to send the Communication Up notification to the
1888 * SCTP user upon reception of a valid COOKIE
1889 * ECHO chunk.
1890 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001891 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001893 asoc->c.sinit_num_ostreams,
1894 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001895 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if (!ev)
1897 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001900 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001902 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001904 if (asoc->peer.adaptation_ind) {
1905 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001907 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto nomem;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 repl = sctp_make_cookie_ack(new_asoc, chunk);
1914 if (!repl)
1915 goto nomem;
1916
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001917 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1918
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001919 if (ev)
1920 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1921 SCTP_ULPEVENT(ev));
1922 if (ai_ev)
1923 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1924 SCTP_ULPEVENT(ai_ev));
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 return SCTP_DISPOSITION_CONSUME;
1927
1928nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001929 if (ai_ev)
1930 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (ev)
1932 sctp_ulpevent_free(ev);
1933 return SCTP_DISPOSITION_NOMEM;
1934}
1935
1936/*
1937 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1938 * chunk was retransmitted and then delayed in the network.
1939 *
1940 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1941 *
1942 * Verification Tag: None. Do cookie validation.
1943 *
1944 * Inputs
1945 * (endpoint, asoc, chunk)
1946 *
1947 * Outputs
1948 * (asoc, reply_msg, msg_up, timers, counters)
1949 *
1950 * The return value is the disposition of the chunk.
1951 */
1952sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1953 const struct sctp_association *asoc,
1954 const sctp_subtype_t type,
1955 void *arg,
1956 sctp_cmd_seq_t *commands)
1957{
1958 sctp_disposition_t retval;
1959 struct sctp_chunk *chunk = arg;
1960 struct sctp_association *new_asoc;
1961 int error = 0;
1962 char action;
1963 struct sctp_chunk *err_chk_p;
1964
1965 /* Make sure that the chunk has a valid length from the protocol
1966 * perspective. In this case check to make sure we have at least
1967 * enough for the chunk header. Cookie length verification is
1968 * done later.
1969 */
1970 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1971 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1972 commands);
1973
1974 /* "Decode" the chunk. We have no optional parameters so we
1975 * are in good shape.
1976 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001977 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001978 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1979 sizeof(sctp_chunkhdr_t)))
1980 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1983 * of a duplicate COOKIE ECHO match the Verification Tags of the
1984 * current association, consider the State Cookie valid even if
1985 * the lifespan is exceeded.
1986 */
1987 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1988 &err_chk_p);
1989
1990 /* FIXME:
1991 * If the re-build failed, what is the proper error path
1992 * from here?
1993 *
1994 * [We should abort the association. --piggy]
1995 */
1996 if (!new_asoc) {
1997 /* FIXME: Several errors are possible. A bad cookie should
1998 * be silently discarded, but think about logging it too.
1999 */
2000 switch (error) {
2001 case -SCTP_IERROR_NOMEM:
2002 goto nomem;
2003
2004 case -SCTP_IERROR_STALE_COOKIE:
2005 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2006 err_chk_p);
2007 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2008 case -SCTP_IERROR_BAD_SIG:
2009 default:
2010 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002011 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
2014 /* Compare the tie_tag in cookie with the verification tag of
2015 * current association.
2016 */
2017 action = sctp_tietags_compare(new_asoc, asoc);
2018
2019 switch (action) {
2020 case 'A': /* Association restart. */
2021 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2022 new_asoc);
2023 break;
2024
2025 case 'B': /* Collision case B. */
2026 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2027 new_asoc);
2028 break;
2029
2030 case 'C': /* Collision case C. */
2031 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2032 new_asoc);
2033 break;
2034
2035 case 'D': /* Collision case D. */
2036 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2037 new_asoc);
2038 break;
2039
2040 default: /* Discard packet for all others. */
2041 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2042 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /* Delete the tempory new association. */
2046 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2047 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2048
2049 return retval;
2050
2051nomem:
2052 return SCTP_DISPOSITION_NOMEM;
2053}
2054
2055/*
2056 * Process an ABORT. (SHUTDOWN-PENDING state)
2057 *
2058 * See sctp_sf_do_9_1_abort().
2059 */
2060sctp_disposition_t sctp_sf_shutdown_pending_abort(
2061 const struct sctp_endpoint *ep,
2062 const struct sctp_association *asoc,
2063 const sctp_subtype_t type,
2064 void *arg,
2065 sctp_cmd_seq_t *commands)
2066{
2067 struct sctp_chunk *chunk = arg;
2068
2069 if (!sctp_vtag_verify_either(chunk, asoc))
2070 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2071
2072 /* Make sure that the ABORT chunk has a valid length.
2073 * Since this is an ABORT chunk, we have to discard it
2074 * because of the following text:
2075 * RFC 2960, Section 3.3.7
2076 * If an endpoint receives an ABORT with a format error or for an
2077 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002078 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 * as we do not know its true length. So, to be safe, discard the
2080 * packet.
2081 */
2082 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2083 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2084
Vlad Yasevich75205f42007-12-20 14:12:59 -08002085 /* ADD-IP: Special case for ABORT chunks
2086 * F4) One special consideration is that ABORT Chunks arriving
2087 * destined to the IP address being deleted MUST be
2088 * ignored (see Section 5.3.1 for further details).
2089 */
2090 if (SCTP_ADDR_DEL ==
2091 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2092 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2093
Vlad Yasevich75205f42007-12-20 14:12:59 -08002094 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
2097/*
2098 * Process an ABORT. (SHUTDOWN-SENT state)
2099 *
2100 * See sctp_sf_do_9_1_abort().
2101 */
2102sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2103 const struct sctp_association *asoc,
2104 const sctp_subtype_t type,
2105 void *arg,
2106 sctp_cmd_seq_t *commands)
2107{
2108 struct sctp_chunk *chunk = arg;
2109
2110 if (!sctp_vtag_verify_either(chunk, asoc))
2111 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2112
2113 /* Make sure that the ABORT chunk has a valid length.
2114 * Since this is an ABORT chunk, we have to discard it
2115 * because of the following text:
2116 * RFC 2960, Section 3.3.7
2117 * If an endpoint receives an ABORT with a format error or for an
2118 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002119 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 * as we do not know its true length. So, to be safe, discard the
2121 * packet.
2122 */
2123 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2124 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2125
Vlad Yasevich75205f42007-12-20 14:12:59 -08002126 /* ADD-IP: Special case for ABORT chunks
2127 * F4) One special consideration is that ABORT Chunks arriving
2128 * destined to the IP address being deleted MUST be
2129 * ignored (see Section 5.3.1 for further details).
2130 */
2131 if (SCTP_ADDR_DEL ==
2132 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2133 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /* Stop the T2-shutdown timer. */
2136 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2137 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2138
2139 /* Stop the T5-shutdown guard timer. */
2140 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2141 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2142
Vlad Yasevich75205f42007-12-20 14:12:59 -08002143 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/*
2147 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2148 *
2149 * See sctp_sf_do_9_1_abort().
2150 */
2151sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2152 const struct sctp_endpoint *ep,
2153 const struct sctp_association *asoc,
2154 const sctp_subtype_t type,
2155 void *arg,
2156 sctp_cmd_seq_t *commands)
2157{
2158 /* The same T2 timer, so we should be able to use
2159 * common function with the SHUTDOWN-SENT state.
2160 */
2161 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2162}
2163
2164/*
2165 * Handle an Error received in COOKIE_ECHOED state.
2166 *
2167 * Only handle the error type of stale COOKIE Error, the other errors will
2168 * be ignored.
2169 *
2170 * Inputs
2171 * (endpoint, asoc, chunk)
2172 *
2173 * Outputs
2174 * (asoc, reply_msg, msg_up, timers, counters)
2175 *
2176 * The return value is the disposition of the chunk.
2177 */
2178sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2179 const struct sctp_association *asoc,
2180 const sctp_subtype_t type,
2181 void *arg,
2182 sctp_cmd_seq_t *commands)
2183{
2184 struct sctp_chunk *chunk = arg;
2185 sctp_errhdr_t *err;
2186
2187 if (!sctp_vtag_verify(chunk, asoc))
2188 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2189
2190 /* Make sure that the ERROR chunk has a valid length.
2191 * The parameter walking depends on this as well.
2192 */
2193 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2194 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2195 commands);
2196
2197 /* Process the error here */
2198 /* FUTURE FIXME: When PR-SCTP related and other optional
2199 * parms are emitted, this will have to change to handle multiple
2200 * errors.
2201 */
2202 sctp_walk_errors(err, chunk->chunk_hdr) {
2203 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002204 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 arg, commands);
2206 }
2207
2208 /* It is possible to have malformed error causes, and that
2209 * will cause us to end the walk early. However, since
2210 * we are discarding the packet, there should be no adverse
2211 * affects.
2212 */
2213 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2214}
2215
2216/*
2217 * Handle a Stale COOKIE Error
2218 *
2219 * Section: 5.2.6 Handle Stale COOKIE Error
2220 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2221 * one of the following three alternatives.
2222 * ...
2223 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2224 * Preservative parameter requesting an extension to the lifetime of
2225 * the State Cookie. When calculating the time extension, an
2226 * implementation SHOULD use the RTT information measured based on the
2227 * previous COOKIE ECHO / ERROR exchange, and should add no more
2228 * than 1 second beyond the measured RTT, due to long State Cookie
2229 * lifetimes making the endpoint more subject to a replay attack.
2230 *
2231 * Verification Tag: Not explicit, but safe to ignore.
2232 *
2233 * Inputs
2234 * (endpoint, asoc, chunk)
2235 *
2236 * Outputs
2237 * (asoc, reply_msg, msg_up, timers, counters)
2238 *
2239 * The return value is the disposition of the chunk.
2240 */
2241static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2242 const struct sctp_association *asoc,
2243 const sctp_subtype_t type,
2244 void *arg,
2245 sctp_cmd_seq_t *commands)
2246{
2247 struct sctp_chunk *chunk = arg;
2248 time_t stale;
2249 sctp_cookie_preserve_param_t bht;
2250 sctp_errhdr_t *err;
2251 struct sctp_chunk *reply;
2252 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002253 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Vlad Yasevich81845c22006-01-30 15:59:54 -08002255 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002256 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2257 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002259 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return SCTP_DISPOSITION_DELETE_TCB;
2261 }
2262
2263 err = (sctp_errhdr_t *)(chunk->skb->data);
2264
2265 /* When calculating the time extension, an implementation
2266 * SHOULD use the RTT information measured based on the
2267 * previous COOKIE ECHO / ERROR exchange, and should add no
2268 * more than 1 second beyond the measured RTT, due to long
2269 * State Cookie lifetimes making the endpoint more subject to
2270 * a replay attack.
2271 * Measure of Staleness's unit is usec. (1/1000000 sec)
2272 * Suggested Cookie Life-span Increment's unit is msec.
2273 * (1/1000 sec)
2274 * In general, if you use the suggested cookie life, the value
2275 * found in the field of measure of staleness should be doubled
2276 * to give ample time to retransmit the new cookie and thus
2277 * yield a higher probability of success on the reattempt.
2278 */
Al Viro34bcca22006-11-20 17:27:15 -08002279 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 stale = (stale * 2) / 1000;
2281
2282 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2283 bht.param_hdr.length = htons(sizeof(bht));
2284 bht.lifespan_increment = htonl(stale);
2285
2286 /* Build that new INIT chunk. */
2287 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2288 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2289 if (!reply)
2290 goto nomem;
2291
2292 sctp_addto_chunk(reply, sizeof(bht), &bht);
2293
2294 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2295 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2296
2297 /* Stop pending T3-rtx and heartbeat timers */
2298 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2299 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2300
2301 /* Delete non-primary peer ip addresses since we are transitioning
2302 * back to the COOKIE-WAIT state
2303 */
2304 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2305
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002306 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2307 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002309 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 SCTP_TRANSPORT(asoc->peer.primary_path));
2311
2312 /* Cast away the const modifier, as we want to just
2313 * rerun it through as a sideffect.
2314 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002315 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2318 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2319 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2320 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2321 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2322 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2323
2324 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2325
2326 return SCTP_DISPOSITION_CONSUME;
2327
2328nomem:
2329 return SCTP_DISPOSITION_NOMEM;
2330}
2331
2332/*
2333 * Process an ABORT.
2334 *
2335 * Section: 9.1
2336 * After checking the Verification Tag, the receiving endpoint shall
2337 * remove the association from its record, and shall report the
2338 * termination to its upper layer.
2339 *
2340 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2341 * B) Rules for packet carrying ABORT:
2342 *
2343 * - The endpoint shall always fill in the Verification Tag field of the
2344 * outbound packet with the destination endpoint's tag value if it
2345 * is known.
2346 *
2347 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2348 * MUST follow the procedure described in Section 8.4.
2349 *
2350 * - The receiver MUST accept the packet if the Verification Tag
2351 * matches either its own tag, OR the tag of its peer. Otherwise, the
2352 * receiver MUST silently discard the packet and take no further
2353 * action.
2354 *
2355 * Inputs
2356 * (endpoint, asoc, chunk)
2357 *
2358 * Outputs
2359 * (asoc, reply_msg, msg_up, timers, counters)
2360 *
2361 * The return value is the disposition of the chunk.
2362 */
2363sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2364 const struct sctp_association *asoc,
2365 const sctp_subtype_t type,
2366 void *arg,
2367 sctp_cmd_seq_t *commands)
2368{
2369 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 if (!sctp_vtag_verify_either(chunk, asoc))
2372 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2373
2374 /* Make sure that the ABORT chunk has a valid length.
2375 * Since this is an ABORT chunk, we have to discard it
2376 * because of the following text:
2377 * RFC 2960, Section 3.3.7
2378 * If an endpoint receives an ABORT with a format error or for an
2379 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002380 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 * as we do not know its true length. So, to be safe, discard the
2382 * packet.
2383 */
2384 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2385 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2386
Vlad Yasevich75205f42007-12-20 14:12:59 -08002387 /* ADD-IP: Special case for ABORT chunks
2388 * F4) One special consideration is that ABORT Chunks arriving
2389 * destined to the IP address being deleted MUST be
2390 * ignored (see Section 5.3.1 for further details).
2391 */
2392 if (SCTP_ADDR_DEL ==
2393 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2394 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2395
2396 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2397}
2398
2399static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2400 const struct sctp_association *asoc,
2401 const sctp_subtype_t type,
2402 void *arg,
2403 sctp_cmd_seq_t *commands)
2404{
2405 struct sctp_chunk *chunk = arg;
2406 unsigned len;
2407 __be16 error = SCTP_ERROR_NO_ERROR;
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 /* See if we have an error cause code in the chunk. */
2410 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002411 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2412
2413 sctp_errhdr_t *err;
2414 sctp_walk_errors(err, chunk->chunk_hdr);
2415 if ((void *)err != (void *)chunk->chunk_end)
2416 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002421 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002422 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002423 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2425 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2426
2427 return SCTP_DISPOSITION_ABORT;
2428}
2429
2430/*
2431 * Process an ABORT. (COOKIE-WAIT state)
2432 *
2433 * See sctp_sf_do_9_1_abort() above.
2434 */
2435sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2436 const struct sctp_association *asoc,
2437 const sctp_subtype_t type,
2438 void *arg,
2439 sctp_cmd_seq_t *commands)
2440{
2441 struct sctp_chunk *chunk = arg;
2442 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002443 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 if (!sctp_vtag_verify_either(chunk, asoc))
2446 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2447
2448 /* Make sure that the ABORT chunk has a valid length.
2449 * Since this is an ABORT chunk, we have to discard it
2450 * because of the following text:
2451 * RFC 2960, Section 3.3.7
2452 * If an endpoint receives an ABORT with a format error or for an
2453 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002454 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * as we do not know its true length. So, to be safe, discard the
2456 * packet.
2457 */
2458 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2459 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2460
2461 /* See if we have an error cause code in the chunk. */
2462 len = ntohs(chunk->chunk_hdr->length);
2463 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2464 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2465
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002466 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2467 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
2470/*
2471 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2472 */
2473sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2474 const struct sctp_association *asoc,
2475 const sctp_subtype_t type,
2476 void *arg,
2477 sctp_cmd_seq_t *commands)
2478{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002479 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2480 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002481 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
2484/*
2485 * Process an ABORT. (COOKIE-ECHOED state)
2486 */
2487sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2488 const struct sctp_association *asoc,
2489 const sctp_subtype_t type,
2490 void *arg,
2491 sctp_cmd_seq_t *commands)
2492{
2493 /* There is a single T1 timer, so we should be able to use
2494 * common function with the COOKIE-WAIT state.
2495 */
2496 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2497}
2498
2499/*
2500 * Stop T1 timer and abort association with "INIT failed".
2501 *
2502 * This is common code called by several sctp_sf_*_abort() functions above.
2503 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002504static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002505 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002506 const struct sctp_association *asoc,
2507 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002509 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2511 SCTP_STATE(SCTP_STATE_CLOSED));
2512 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2513 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2514 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002515 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /* CMD_INIT_FAILED will DELETE_TCB. */
2517 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002518 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002519 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
2522/*
2523 * sctp_sf_do_9_2_shut
2524 *
2525 * Section: 9.2
2526 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2527 * - enter the SHUTDOWN-RECEIVED state,
2528 *
2529 * - stop accepting new data from its SCTP user
2530 *
2531 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2532 * that all its outstanding DATA chunks have been received by the
2533 * SHUTDOWN sender.
2534 *
2535 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2536 * send a SHUTDOWN in response to a ULP request. And should discard
2537 * subsequent SHUTDOWN chunks.
2538 *
2539 * If there are still outstanding DATA chunks left, the SHUTDOWN
2540 * receiver shall continue to follow normal data transmission
2541 * procedures defined in Section 6 until all outstanding DATA chunks
2542 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2543 * new data from its SCTP user.
2544 *
2545 * Verification Tag: 8.5 Verification Tag [Normal verification]
2546 *
2547 * Inputs
2548 * (endpoint, asoc, chunk)
2549 *
2550 * Outputs
2551 * (asoc, reply_msg, msg_up, timers, counters)
2552 *
2553 * The return value is the disposition of the chunk.
2554 */
2555sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2556 const struct sctp_association *asoc,
2557 const sctp_subtype_t type,
2558 void *arg,
2559 sctp_cmd_seq_t *commands)
2560{
2561 struct sctp_chunk *chunk = arg;
2562 sctp_shutdownhdr_t *sdh;
2563 sctp_disposition_t disposition;
2564 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002565 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 if (!sctp_vtag_verify(chunk, asoc))
2568 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2569
2570 /* Make sure that the SHUTDOWN chunk has a valid length. */
2571 if (!sctp_chunk_length_valid(chunk,
2572 sizeof(struct sctp_shutdown_chunk_t)))
2573 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2574 commands);
2575
2576 /* Convert the elaborate header. */
2577 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2578 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2579 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002580 ctsn = ntohl(sdh->cum_tsn_ack);
2581
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002582 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2583 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2584 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2585 return SCTP_DISPOSITION_DISCARD;
2586 }
2587
Wei Yongjundf10eec2008-10-23 01:00:21 -07002588 /* If Cumulative TSN Ack beyond the max tsn currently
2589 * send, terminating the association and respond to the
2590 * sender with an ABORT.
2591 */
2592 if (!TSN_lt(ctsn, asoc->next_tsn))
2593 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002595 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2596 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2597 * inform the application that it should cease sending data.
2598 */
2599 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2600 if (!ev) {
2601 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002602 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002603 }
2604 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2607 * - enter the SHUTDOWN-RECEIVED state,
2608 * - stop accepting new data from its SCTP user
2609 *
2610 * [This is implicit in the new state.]
2611 */
2612 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2613 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2614 disposition = SCTP_DISPOSITION_CONSUME;
2615
2616 if (sctp_outq_is_empty(&asoc->outqueue)) {
2617 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2618 arg, commands);
2619 }
2620
2621 if (SCTP_DISPOSITION_NOMEM == disposition)
2622 goto out;
2623
2624 /* - verify, by checking the Cumulative TSN Ack field of the
2625 * chunk, that all its outstanding DATA chunks have been
2626 * received by the SHUTDOWN sender.
2627 */
2628 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002629 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631out:
2632 return disposition;
2633}
2634
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002635/*
2636 * sctp_sf_do_9_2_shut_ctsn
2637 *
2638 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2639 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2640 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2641 * MUST be processed.
2642 */
2643sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2644 const struct sctp_association *asoc,
2645 const sctp_subtype_t type,
2646 void *arg,
2647 sctp_cmd_seq_t *commands)
2648{
2649 struct sctp_chunk *chunk = arg;
2650 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002651 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002652
2653 if (!sctp_vtag_verify(chunk, asoc))
2654 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2655
2656 /* Make sure that the SHUTDOWN chunk has a valid length. */
2657 if (!sctp_chunk_length_valid(chunk,
2658 sizeof(struct sctp_shutdown_chunk_t)))
2659 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2660 commands);
2661
2662 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002663 ctsn = ntohl(sdh->cum_tsn_ack);
2664
2665 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2666 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2667 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2668 return SCTP_DISPOSITION_DISCARD;
2669 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002670
2671 /* If Cumulative TSN Ack beyond the max tsn currently
2672 * send, terminating the association and respond to the
2673 * sender with an ABORT.
2674 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002675 if (!TSN_lt(ctsn, asoc->next_tsn))
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002676 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2677
2678 /* verify, by checking the Cumulative TSN Ack field of the
2679 * chunk, that all its outstanding DATA chunks have been
2680 * received by the SHUTDOWN sender.
2681 */
2682 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2683 SCTP_BE32(sdh->cum_tsn_ack));
2684
2685 return SCTP_DISPOSITION_CONSUME;
2686}
2687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688/* RFC 2960 9.2
2689 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2690 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2691 * transport addresses (either in the IP addresses or in the INIT chunk)
2692 * that belong to this association, it should discard the INIT chunk and
2693 * retransmit the SHUTDOWN ACK chunk.
2694 */
2695sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2696 const struct sctp_association *asoc,
2697 const sctp_subtype_t type,
2698 void *arg,
2699 sctp_cmd_seq_t *commands)
2700{
2701 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2702 struct sctp_chunk *reply;
2703
Vlad Yasevichece25df2007-09-07 16:30:54 -04002704 /* Make sure that the chunk has a valid length */
2705 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2706 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2707 commands);
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /* Since we are not going to really process this INIT, there
2710 * is no point in verifying chunk boundries. Just generate
2711 * the SHUTDOWN ACK.
2712 */
2713 reply = sctp_make_shutdown_ack(asoc, chunk);
2714 if (NULL == reply)
2715 goto nomem;
2716
2717 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2718 * the T2-SHUTDOWN timer.
2719 */
2720 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2721
2722 /* and restart the T2-shutdown timer. */
2723 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2724 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2725
2726 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2727
2728 return SCTP_DISPOSITION_CONSUME;
2729nomem:
2730 return SCTP_DISPOSITION_NOMEM;
2731}
2732
2733/*
2734 * sctp_sf_do_ecn_cwr
2735 *
2736 * Section: Appendix A: Explicit Congestion Notification
2737 *
2738 * CWR:
2739 *
2740 * RFC 2481 details a specific bit for a sender to send in the header of
2741 * its next outbound TCP segment to indicate to its peer that it has
2742 * reduced its congestion window. This is termed the CWR bit. For
2743 * SCTP the same indication is made by including the CWR chunk.
2744 * This chunk contains one data element, i.e. the TSN number that
2745 * was sent in the ECNE chunk. This element represents the lowest
2746 * TSN number in the datagram that was originally marked with the
2747 * CE bit.
2748 *
2749 * Verification Tag: 8.5 Verification Tag [Normal verification]
2750 * Inputs
2751 * (endpoint, asoc, chunk)
2752 *
2753 * Outputs
2754 * (asoc, reply_msg, msg_up, timers, counters)
2755 *
2756 * The return value is the disposition of the chunk.
2757 */
2758sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2759 const struct sctp_association *asoc,
2760 const sctp_subtype_t type,
2761 void *arg,
2762 sctp_cmd_seq_t *commands)
2763{
2764 sctp_cwrhdr_t *cwr;
2765 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002766 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 if (!sctp_vtag_verify(chunk, asoc))
2769 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2770
2771 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2772 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2773 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2776 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2777
Al Viro34bcca22006-11-20 17:27:15 -08002778 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002781 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 /* Stop sending ECNE. */
2783 sctp_add_cmd_sf(commands,
2784 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002785 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
2787 return SCTP_DISPOSITION_CONSUME;
2788}
2789
2790/*
2791 * sctp_sf_do_ecne
2792 *
2793 * Section: Appendix A: Explicit Congestion Notification
2794 *
2795 * ECN-Echo
2796 *
2797 * RFC 2481 details a specific bit for a receiver to send back in its
2798 * TCP acknowledgements to notify the sender of the Congestion
2799 * Experienced (CE) bit having arrived from the network. For SCTP this
2800 * same indication is made by including the ECNE chunk. This chunk
2801 * contains one data element, i.e. the lowest TSN associated with the IP
2802 * datagram marked with the CE bit.....
2803 *
2804 * Verification Tag: 8.5 Verification Tag [Normal verification]
2805 * Inputs
2806 * (endpoint, asoc, chunk)
2807 *
2808 * Outputs
2809 * (asoc, reply_msg, msg_up, timers, counters)
2810 *
2811 * The return value is the disposition of the chunk.
2812 */
2813sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2814 const struct sctp_association *asoc,
2815 const sctp_subtype_t type,
2816 void *arg,
2817 sctp_cmd_seq_t *commands)
2818{
2819 sctp_ecnehdr_t *ecne;
2820 struct sctp_chunk *chunk = arg;
2821
2822 if (!sctp_vtag_verify(chunk, asoc))
2823 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2824
2825 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2826 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2827 commands);
2828
2829 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2830 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2831
2832 /* If this is a newer ECNE than the last CWR packet we sent out */
2833 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2834 SCTP_U32(ntohl(ecne->lowest_tsn)));
2835
2836 return SCTP_DISPOSITION_CONSUME;
2837}
2838
2839/*
2840 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2841 *
2842 * The SCTP endpoint MUST always acknowledge the reception of each valid
2843 * DATA chunk.
2844 *
2845 * The guidelines on delayed acknowledgement algorithm specified in
2846 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2847 * acknowledgement SHOULD be generated for at least every second packet
2848 * (not every second DATA chunk) received, and SHOULD be generated within
2849 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2850 * situations it may be beneficial for an SCTP transmitter to be more
2851 * conservative than the algorithms detailed in this document allow.
2852 * However, an SCTP transmitter MUST NOT be more aggressive than the
2853 * following algorithms allow.
2854 *
2855 * A SCTP receiver MUST NOT generate more than one SACK for every
2856 * incoming packet, other than to update the offered window as the
2857 * receiving application consumes new data.
2858 *
2859 * Verification Tag: 8.5 Verification Tag [Normal verification]
2860 *
2861 * Inputs
2862 * (endpoint, asoc, chunk)
2863 *
2864 * Outputs
2865 * (asoc, reply_msg, msg_up, timers, counters)
2866 *
2867 * The return value is the disposition of the chunk.
2868 */
2869sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2870 const struct sctp_association *asoc,
2871 const sctp_subtype_t type,
2872 void *arg,
2873 sctp_cmd_seq_t *commands)
2874{
2875 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002876 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 int error;
2878
2879 if (!sctp_vtag_verify(chunk, asoc)) {
2880 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2881 SCTP_NULL());
2882 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2886 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2887 commands);
2888
2889 error = sctp_eat_data(asoc, chunk, commands );
2890 switch (error) {
2891 case SCTP_IERROR_NO_ERROR:
2892 break;
2893 case SCTP_IERROR_HIGH_TSN:
2894 case SCTP_IERROR_BAD_STREAM:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002895 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 goto discard_noforce;
2897 case SCTP_IERROR_DUP_TSN:
2898 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002899 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 goto discard_force;
2901 case SCTP_IERROR_NO_DATA:
2902 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002903 case SCTP_IERROR_PROTO_VIOLATION:
2904 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
2905 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 default:
2907 BUG();
2908 }
2909
Wei Yongjun6dc76942009-11-23 15:53:53 -05002910 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2911 force = SCTP_FORCE();
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (asoc->autoclose) {
2914 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2915 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2916 }
2917
2918 /* If this is the last chunk in a packet, we need to count it
2919 * toward sack generation. Note that we need to SACK every
2920 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2921 * THEM. We elect to NOT generate SACK's if the chunk fails
2922 * the verification tag test.
2923 *
2924 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2925 *
2926 * The SCTP endpoint MUST always acknowledge the reception of
2927 * each valid DATA chunk.
2928 *
2929 * The guidelines on delayed acknowledgement algorithm
2930 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2931 * Specifically, an acknowledgement SHOULD be generated for at
2932 * least every second packet (not every second DATA chunk)
2933 * received, and SHOULD be generated within 200 ms of the
2934 * arrival of any unacknowledged DATA chunk. In some
2935 * situations it may be beneficial for an SCTP transmitter to
2936 * be more conservative than the algorithms detailed in this
2937 * document allow. However, an SCTP transmitter MUST NOT be
2938 * more aggressive than the following algorithms allow.
2939 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002940 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002941 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return SCTP_DISPOSITION_CONSUME;
2944
2945discard_force:
2946 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2947 *
2948 * When a packet arrives with duplicate DATA chunk(s) and with
2949 * no new DATA chunk(s), the endpoint MUST immediately send a
2950 * SACK with no delay. If a packet arrives with duplicate
2951 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2952 * MAY immediately send a SACK. Normally receipt of duplicate
2953 * DATA chunks will occur when the original SACK chunk was lost
2954 * and the peer's RTO has expired. The duplicate TSN number(s)
2955 * SHOULD be reported in the SACK as duplicate.
2956 */
2957 /* In our case, we split the MAY SACK advice up whether or not
2958 * the last chunk is a duplicate.'
2959 */
2960 if (chunk->end_of_packet)
2961 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2962 return SCTP_DISPOSITION_DISCARD;
2963
2964discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002965 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002966 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 return SCTP_DISPOSITION_DISCARD;
2969consume:
2970 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
2974/*
2975 * sctp_sf_eat_data_fast_4_4
2976 *
2977 * Section: 4 (4)
2978 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2979 * DATA chunks without delay.
2980 *
2981 * Verification Tag: 8.5 Verification Tag [Normal verification]
2982 * Inputs
2983 * (endpoint, asoc, chunk)
2984 *
2985 * Outputs
2986 * (asoc, reply_msg, msg_up, timers, counters)
2987 *
2988 * The return value is the disposition of the chunk.
2989 */
2990sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2991 const struct sctp_association *asoc,
2992 const sctp_subtype_t type,
2993 void *arg,
2994 sctp_cmd_seq_t *commands)
2995{
2996 struct sctp_chunk *chunk = arg;
2997 int error;
2998
2999 if (!sctp_vtag_verify(chunk, asoc)) {
3000 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3001 SCTP_NULL());
3002 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3003 }
3004
3005 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3006 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3007 commands);
3008
3009 error = sctp_eat_data(asoc, chunk, commands );
3010 switch (error) {
3011 case SCTP_IERROR_NO_ERROR:
3012 case SCTP_IERROR_HIGH_TSN:
3013 case SCTP_IERROR_DUP_TSN:
3014 case SCTP_IERROR_IGNORE_TSN:
3015 case SCTP_IERROR_BAD_STREAM:
3016 break;
3017 case SCTP_IERROR_NO_DATA:
3018 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003019 case SCTP_IERROR_PROTO_VIOLATION:
3020 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
3021 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 default:
3023 BUG();
3024 }
3025
3026 /* Go a head and force a SACK, since we are shutting down. */
3027
3028 /* Implementor's Guide.
3029 *
3030 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3031 * respond to each received packet containing one or more DATA chunk(s)
3032 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3033 */
3034 if (chunk->end_of_packet) {
3035 /* We must delay the chunk creation since the cumulative
3036 * TSN has not been updated yet.
3037 */
3038 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3039 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3040 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3041 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3042 }
3043
3044consume:
3045 return SCTP_DISPOSITION_CONSUME;
3046}
3047
3048/*
3049 * Section: 6.2 Processing a Received SACK
3050 * D) Any time a SACK arrives, the endpoint performs the following:
3051 *
3052 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3053 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3054 * increasing, a SACK whose Cumulative TSN Ack is less than the
3055 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3056 *
3057 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3058 * of bytes still outstanding after processing the Cumulative TSN Ack
3059 * and the Gap Ack Blocks.
3060 *
3061 * iii) If the SACK is missing a TSN that was previously
3062 * acknowledged via a Gap Ack Block (e.g., the data receiver
3063 * reneged on the data), then mark the corresponding DATA chunk
3064 * as available for retransmit: Mark it as missing for fast
3065 * retransmit as described in Section 7.2.4 and if no retransmit
3066 * timer is running for the destination address to which the DATA
3067 * chunk was originally transmitted, then T3-rtx is started for
3068 * that destination address.
3069 *
3070 * Verification Tag: 8.5 Verification Tag [Normal verification]
3071 *
3072 * Inputs
3073 * (endpoint, asoc, chunk)
3074 *
3075 * Outputs
3076 * (asoc, reply_msg, msg_up, timers, counters)
3077 *
3078 * The return value is the disposition of the chunk.
3079 */
3080sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3081 const struct sctp_association *asoc,
3082 const sctp_subtype_t type,
3083 void *arg,
3084 sctp_cmd_seq_t *commands)
3085{
3086 struct sctp_chunk *chunk = arg;
3087 sctp_sackhdr_t *sackh;
3088 __u32 ctsn;
3089
3090 if (!sctp_vtag_verify(chunk, asoc))
3091 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3092
3093 /* Make sure that the SACK chunk has a valid length. */
3094 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3095 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3096 commands);
3097
3098 /* Pull the SACK chunk from the data buffer */
3099 sackh = sctp_sm_pull_sack(chunk);
3100 /* Was this a bogus SACK? */
3101 if (!sackh)
3102 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3103 chunk->subh.sack_hdr = sackh;
3104 ctsn = ntohl(sackh->cum_tsn_ack);
3105
3106 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3107 * Ack Point, then drop the SACK. Since Cumulative TSN
3108 * Ack is monotonically increasing, a SACK whose
3109 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3110 * Point indicates an out-of-order SACK.
3111 */
3112 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3113 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3114 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3115 return SCTP_DISPOSITION_DISCARD;
3116 }
3117
Wei Yongjunaecedea2007-08-02 16:57:44 +08003118 /* If Cumulative TSN Ack beyond the max tsn currently
3119 * send, terminating the association and respond to the
3120 * sender with an ABORT.
3121 */
3122 if (!TSN_lt(ctsn, asoc->next_tsn))
3123 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 /* Return this SACK for further processing. */
3126 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3127
3128 /* Note: We do the rest of the work on the PROCESS_SACK
3129 * sideeffect.
3130 */
3131 return SCTP_DISPOSITION_CONSUME;
3132}
3133
3134/*
3135 * Generate an ABORT in response to a packet.
3136 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003137 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003139 * 8) The receiver should respond to the sender of the OOTB packet with
3140 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3141 * MUST fill in the Verification Tag field of the outbound packet
3142 * with the value found in the Verification Tag field of the OOTB
3143 * packet and set the T-bit in the Chunk Flags to indicate that the
3144 * Verification Tag is reflected. After sending this ABORT, the
3145 * receiver of the OOTB packet shall discard the OOTB packet and take
3146 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 *
3148 * Verification Tag:
3149 *
3150 * The return value is the disposition of the chunk.
3151*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003152static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 const struct sctp_association *asoc,
3154 const sctp_subtype_t type,
3155 void *arg,
3156 sctp_cmd_seq_t *commands)
3157{
3158 struct sctp_packet *packet = NULL;
3159 struct sctp_chunk *chunk = arg;
3160 struct sctp_chunk *abort;
3161
3162 packet = sctp_ootb_pkt_new(asoc, chunk);
3163
3164 if (packet) {
3165 /* Make an ABORT. The T bit will be set if the asoc
3166 * is NULL.
3167 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003168 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (!abort) {
3170 sctp_ootb_pkt_free(packet);
3171 return SCTP_DISPOSITION_NOMEM;
3172 }
3173
Jerome Forissier047a2422005-04-28 11:58:43 -07003174 /* Reflect vtag if T-Bit is set */
3175 if (sctp_test_T_bit(abort))
3176 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3177
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 /* Set the skb to the belonging sock for accounting. */
3179 abort->skb->sk = ep->base.sk;
3180
3181 sctp_packet_append_chunk(packet, abort);
3182
3183 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3184 SCTP_PACKET(packet));
3185
3186 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3187
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003188 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 return SCTP_DISPOSITION_CONSUME;
3190 }
3191
3192 return SCTP_DISPOSITION_NOMEM;
3193}
3194
3195/*
3196 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3197 * event as ULP notification for each cause included in the chunk.
3198 *
3199 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3200 *
3201 * The return value is the disposition of the chunk.
3202*/
3203sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3204 const struct sctp_association *asoc,
3205 const sctp_subtype_t type,
3206 void *arg,
3207 sctp_cmd_seq_t *commands)
3208{
3209 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003210 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
3212 if (!sctp_vtag_verify(chunk, asoc))
3213 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3214
3215 /* Make sure that the ERROR chunk has a valid length. */
3216 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3217 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3218 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003219 sctp_walk_errors(err, chunk->chunk_hdr);
3220 if ((void *)err != (void *)chunk->chunk_end)
3221 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3222 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Wei Yongjun3df26782009-03-02 06:46:51 +00003224 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3225 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
3230/*
3231 * Process an inbound SHUTDOWN ACK.
3232 *
3233 * From Section 9.2:
3234 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3235 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3236 * peer, and remove all record of the association.
3237 *
3238 * The return value is the disposition.
3239 */
3240sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3241 const struct sctp_association *asoc,
3242 const sctp_subtype_t type,
3243 void *arg,
3244 sctp_cmd_seq_t *commands)
3245{
3246 struct sctp_chunk *chunk = arg;
3247 struct sctp_chunk *reply;
3248 struct sctp_ulpevent *ev;
3249
3250 if (!sctp_vtag_verify(chunk, asoc))
3251 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3252
3253 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3254 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3255 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3256 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 /* 10.2 H) SHUTDOWN COMPLETE notification
3258 *
3259 * When SCTP completes the shutdown procedures (section 9.2) this
3260 * notification is passed to the upper layer.
3261 */
3262 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003263 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 if (!ev)
3265 goto nomem;
3266
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003267 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3268 reply = sctp_make_shutdown_complete(asoc, chunk);
3269 if (!reply)
3270 goto nomem_chunk;
3271
3272 /* Do all the commands now (after allocation), so that we
3273 * have consistent state if memory allocation failes
3274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3276
3277 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3278 * stop the T2-shutdown timer,
3279 */
3280 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3281 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3282
3283 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3284 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3287 SCTP_STATE(SCTP_STATE_CLOSED));
3288 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3289 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3290 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3291
3292 /* ...and remove all record of the association. */
3293 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3294 return SCTP_DISPOSITION_DELETE_TCB;
3295
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003296nomem_chunk:
3297 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298nomem:
3299 return SCTP_DISPOSITION_NOMEM;
3300}
3301
3302/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003303 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3304 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3306 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3307 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3308 * packet must fill in the Verification Tag field of the outbound
3309 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003310 * set the T-bit in the Chunk Flags to indicate that the Verification
3311 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 *
3313 * 8) The receiver should respond to the sender of the OOTB packet with
3314 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3315 * MUST fill in the Verification Tag field of the outbound packet
3316 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003317 * packet and set the T-bit in the Chunk Flags to indicate that the
3318 * Verification Tag is reflected. After sending this ABORT, the
3319 * receiver of the OOTB packet shall discard the OOTB packet and take
3320 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 */
3322sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3323 const struct sctp_association *asoc,
3324 const sctp_subtype_t type,
3325 void *arg,
3326 sctp_cmd_seq_t *commands)
3327{
3328 struct sctp_chunk *chunk = arg;
3329 struct sk_buff *skb = chunk->skb;
3330 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003331 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 __u8 *ch_end;
3333 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003334 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3337
3338 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3339 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003340 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003342 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3343 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Vlad Yasevichece25df2007-09-07 16:30:54 -04003345 /* Now that we know we at least have a chunk header,
3346 * do things that are type appropriate.
3347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3349 ootb_shut_ack = 1;
3350
3351 /* RFC 2960, Section 3.3.7
3352 * Moreover, under any circumstances, an endpoint that
3353 * receives an ABORT MUST NOT respond to that ABORT by
3354 * sending an ABORT of its own.
3355 */
3356 if (SCTP_CID_ABORT == ch->type)
3357 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003358
Shan Wei85c5ed42011-04-19 21:30:01 +00003359 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3360 * or a COOKIE ACK the SCTP Packet should be silently
3361 * discarded.
3362 */
3363
3364 if (SCTP_CID_COOKIE_ACK == ch->type)
3365 ootb_cookie_ack = 1;
3366
3367 if (SCTP_CID_ERROR == ch->type) {
3368 sctp_walk_errors(err, ch) {
3369 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3370 ootb_cookie_ack = 1;
3371 break;
3372 }
3373 }
3374 }
3375
Vlad Yasevichece25df2007-09-07 16:30:54 -04003376 /* Report violation if chunk len overflows */
3377 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3378 if (ch_end > skb_tail_pointer(skb))
3379 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3380 commands);
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003383 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003386 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003387 else if (ootb_cookie_ack)
3388 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003390 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391}
3392
3393/*
3394 * Handle an "Out of the blue" SHUTDOWN ACK.
3395 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003396 * Section: 8.4 5, sctpimpguide 2.41.
3397 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003399 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3400 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3401 * packet must fill in the Verification Tag field of the outbound
3402 * packet with the Verification Tag received in the SHUTDOWN ACK and
3403 * set the T-bit in the Chunk Flags to indicate that the Verification
3404 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 *
3406 * Inputs
3407 * (endpoint, asoc, type, arg, commands)
3408 *
3409 * Outputs
3410 * (sctp_disposition_t)
3411 *
3412 * The return value is the disposition of the chunk.
3413 */
3414static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3415 const struct sctp_association *asoc,
3416 const sctp_subtype_t type,
3417 void *arg,
3418 sctp_cmd_seq_t *commands)
3419{
3420 struct sctp_packet *packet = NULL;
3421 struct sctp_chunk *chunk = arg;
3422 struct sctp_chunk *shut;
3423
3424 packet = sctp_ootb_pkt_new(asoc, chunk);
3425
3426 if (packet) {
3427 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003428 * The T bit will be set if the asoc is NULL.
3429 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 shut = sctp_make_shutdown_complete(asoc, chunk);
3431 if (!shut) {
3432 sctp_ootb_pkt_free(packet);
3433 return SCTP_DISPOSITION_NOMEM;
3434 }
3435
Jerome Forissier047a2422005-04-28 11:58:43 -07003436 /* Reflect vtag if T-Bit is set */
3437 if (sctp_test_T_bit(shut))
3438 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 /* Set the skb to the belonging sock for accounting. */
3441 shut->skb->sk = ep->base.sk;
3442
3443 sctp_packet_append_chunk(packet, shut);
3444
3445 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3446 SCTP_PACKET(packet));
3447
3448 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3449
3450 /* If the chunk length is invalid, we don't want to process
3451 * the reset of the packet.
3452 */
3453 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3454 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3455
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003456 /* We need to discard the rest of the packet to prevent
3457 * potential bomming attacks from additional bundled chunks.
3458 * This is documented in SCTP Threats ID.
3459 */
3460 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
3462
3463 return SCTP_DISPOSITION_NOMEM;
3464}
3465
3466/*
3467 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3468 *
3469 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3470 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3471 * procedures in section 8.4 SHOULD be followed, in other words it
3472 * should be treated as an Out Of The Blue packet.
3473 * [This means that we do NOT check the Verification Tag on these
3474 * chunks. --piggy ]
3475 *
3476 */
3477sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3478 const struct sctp_association *asoc,
3479 const sctp_subtype_t type,
3480 void *arg,
3481 sctp_cmd_seq_t *commands)
3482{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003483 struct sctp_chunk *chunk = arg;
3484
3485 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3486 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3487 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3488 commands);
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 /* Although we do have an association in this case, it corresponds
3491 * to a restarted association. So the packet is treated as an OOTB
3492 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3493 * called with a NULL association.
3494 */
Wei Yongjun8190f892008-09-08 12:13:55 +08003495 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3496
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3498}
3499
3500/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3501sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3502 const struct sctp_association *asoc,
3503 const sctp_subtype_t type, void *arg,
3504 sctp_cmd_seq_t *commands)
3505{
3506 struct sctp_chunk *chunk = arg;
3507 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003508 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003510 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003512 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513
3514 if (!sctp_vtag_verify(chunk, asoc)) {
3515 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3516 SCTP_NULL());
3517 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3518 }
3519
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003520 /* ADD-IP: Section 4.1.1
3521 * This chunk MUST be sent in an authenticated way by using
3522 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3523 * is received unauthenticated it MUST be silently discarded as
3524 * described in [I-D.ietf-tsvwg-sctp-auth].
3525 */
3526 if (!sctp_addip_noauth && !chunk->auth)
3527 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3528
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3530 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3531 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3532 commands);
3533
3534 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3535 serial = ntohl(hdr->serial);
3536
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003537 addr_param = (union sctp_addr_param *)hdr->params;
3538 length = ntohs(addr_param->p.length);
3539 if (length < sizeof(sctp_paramhdr_t))
Wei Yongjunba016672008-09-30 05:32:24 -07003540 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003541 (void *)addr_param, commands);
3542
3543 /* Verify the ASCONF chunk before processing it. */
3544 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003545 (sctp_paramhdr_t *)((void *)addr_param + length),
3546 (void *)chunk->chunk_end,
3547 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003548 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3549 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003550
Vlad Yasevicha08de642007-12-20 14:11:47 -08003551 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003553 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 */
3555 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003556 /* If this is the first instance of ASCONF in the packet,
3557 * we can clean our old ASCONF-ACKs.
3558 */
3559 if (!chunk->has_asconf)
3560 sctp_assoc_clean_asconf_ack_cache(asoc);
3561
3562 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3563 * expected, process the ASCONF as described below and after
3564 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3565 * the response packet and cache a copy of it (in the event it
3566 * later needs to be retransmitted).
3567 *
3568 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 */
3570 asconf_ack = sctp_process_asconf((struct sctp_association *)
3571 asoc, chunk);
3572 if (!asconf_ack)
3573 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003574 } else if (serial < asoc->peer.addip_serial + 1) {
3575 /* ADDIP 5.2 E2)
3576 * If the value found in the Sequence Number is less than the
3577 * ('Peer- Sequence-Number' + 1), simply skip to the next
3578 * ASCONF, and include in the outbound response packet
3579 * any previously cached ASCONF-ACK response that was
3580 * sent and saved that matches the Sequence Number of the
3581 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3582 * Chunk exists. This will occur when an older ASCONF
3583 * arrives out of order. In such a case, the receiver
3584 * should skip the ASCONF Chunk and not include ASCONF-ACK
3585 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003587 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3588 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003590
3591 /* Reset the transport so that we select the correct one
3592 * this time around. This is to make sure that we don't
3593 * accidentally use a stale transport that's been removed.
3594 */
3595 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003597 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 return SCTP_DISPOSITION_DISCARD;
3601 }
3602
Vlad Yasevicha08de642007-12-20 14:11:47 -08003603 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3604 * containing the ASCONF-ACK Chunks MUST be the source address of
3605 * the SCTP packet that held the ASCONF Chunks.
3606 *
3607 * To do this properly, we'll set the destination address of the chunk
3608 * and at the transmit time, will try look up the transport to use.
3609 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003610 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003612 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return SCTP_DISPOSITION_CONSUME;
3616}
3617
3618/*
3619 * ADDIP Section 4.3 General rules for address manipulation
3620 * When building TLV parameters for the ASCONF Chunk that will add or
3621 * delete IP addresses the D0 to D13 rules should be applied:
3622 */
3623sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3624 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003625 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 sctp_cmd_seq_t *commands)
3627{
3628 struct sctp_chunk *asconf_ack = arg;
3629 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3630 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003631 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 sctp_addiphdr_t *addip_hdr;
3633 __u32 sent_serial, rcvd_serial;
3634
3635 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3636 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3637 SCTP_NULL());
3638 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3639 }
3640
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003641 /* ADD-IP, Section 4.1.2:
3642 * This chunk MUST be sent in an authenticated way by using
3643 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3644 * is received unauthenticated it MUST be silently discarded as
3645 * described in [I-D.ietf-tsvwg-sctp-auth].
3646 */
3647 if (!sctp_addip_noauth && !asconf_ack->auth)
3648 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3649
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 /* Make sure that the ADDIP chunk has a valid length. */
3651 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3652 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3653 commands);
3654
3655 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3656 rcvd_serial = ntohl(addip_hdr->serial);
3657
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003658 /* Verify the ASCONF-ACK chunk before processing it. */
3659 if (!sctp_verify_asconf(asoc,
3660 (sctp_paramhdr_t *)addip_hdr->params,
3661 (void *)asconf_ack->chunk_end,
3662 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003663 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3664 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 if (last_asconf) {
3667 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3668 sent_serial = ntohl(addip_hdr->serial);
3669 } else {
3670 sent_serial = asoc->addip_serial - 1;
3671 }
3672
3673 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3674 * equal to the next serial number to be used but no ASCONF chunk is
3675 * outstanding the endpoint MUST ABORT the association. Note that a
3676 * sequence number is greater than if it is no more than 2^^31-1
3677 * larger than the current sequence number (using serial arithmetic).
3678 */
3679 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3680 !(asoc->addip_last_asconf)) {
3681 abort = sctp_make_abort(asoc, asconf_ack,
3682 sizeof(sctp_errhdr_t));
3683 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003684 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3686 SCTP_CHUNK(abort));
3687 }
3688 /* We are going to ABORT, so we might as well stop
3689 * processing the rest of the chunks in the packet.
3690 */
3691 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3692 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3693 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003694 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003695 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003697 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3699 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3700 return SCTP_DISPOSITION_ABORT;
3701 }
3702
3703 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3704 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3705 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3706
3707 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003708 asconf_ack)) {
3709 /* Successfully processed ASCONF_ACK. We can
3710 * release the next asconf if we have one.
3711 */
3712 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3713 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 abort = sctp_make_abort(asoc, asconf_ack,
3718 sizeof(sctp_errhdr_t));
3719 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003720 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3722 SCTP_CHUNK(abort));
3723 }
3724 /* We are going to ABORT, so we might as well stop
3725 * processing the rest of the chunks in the packet.
3726 */
3727 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003728 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003729 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003731 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3733 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3734 return SCTP_DISPOSITION_ABORT;
3735 }
3736
3737 return SCTP_DISPOSITION_DISCARD;
3738}
3739
3740/*
3741 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3742 *
3743 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3744 * its cumulative TSN point to the value carried in the FORWARD TSN
3745 * chunk, and then MUST further advance its cumulative TSN point locally
3746 * if possible.
3747 * After the above processing, the data receiver MUST stop reporting any
3748 * missing TSNs earlier than or equal to the new cumulative TSN point.
3749 *
3750 * Verification Tag: 8.5 Verification Tag [Normal verification]
3751 *
3752 * The return value is the disposition of the chunk.
3753 */
3754sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3755 const struct sctp_association *asoc,
3756 const sctp_subtype_t type,
3757 void *arg,
3758 sctp_cmd_seq_t *commands)
3759{
3760 struct sctp_chunk *chunk = arg;
3761 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003762 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 __u16 len;
3764 __u32 tsn;
3765
3766 if (!sctp_vtag_verify(chunk, asoc)) {
3767 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3768 SCTP_NULL());
3769 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3770 }
3771
3772 /* Make sure that the FORWARD_TSN chunk has valid length. */
3773 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3774 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3775 commands);
3776
3777 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3778 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3779 len = ntohs(chunk->chunk_hdr->length);
3780 len -= sizeof(struct sctp_chunkhdr);
3781 skb_pull(chunk->skb, len);
3782
3783 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003784 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 /* The TSN is too high--silently discard the chunk and count on it
3787 * getting retransmitted later.
3788 */
3789 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3790 goto discard_noforce;
3791
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003792 /* Silently discard the chunk if stream-id is not valid */
3793 sctp_walk_fwdtsn(skip, chunk) {
3794 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3795 goto discard_noforce;
3796 }
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3799 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003800 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 /* Count this as receiving DATA. */
3804 if (asoc->autoclose) {
3805 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3806 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3807 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003810 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 */
3812 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
3814 return SCTP_DISPOSITION_CONSUME;
3815
3816discard_noforce:
3817 return SCTP_DISPOSITION_DISCARD;
3818}
3819
3820sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3821 const struct sctp_endpoint *ep,
3822 const struct sctp_association *asoc,
3823 const sctp_subtype_t type,
3824 void *arg,
3825 sctp_cmd_seq_t *commands)
3826{
3827 struct sctp_chunk *chunk = arg;
3828 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003829 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 __u16 len;
3831 __u32 tsn;
3832
3833 if (!sctp_vtag_verify(chunk, asoc)) {
3834 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3835 SCTP_NULL());
3836 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3837 }
3838
3839 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3840 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3841 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3842 commands);
3843
3844 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3845 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3846 len = ntohs(chunk->chunk_hdr->length);
3847 len -= sizeof(struct sctp_chunkhdr);
3848 skb_pull(chunk->skb, len);
3849
3850 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003851 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
3853 /* The TSN is too high--silently discard the chunk and count on it
3854 * getting retransmitted later.
3855 */
3856 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3857 goto gen_shutdown;
3858
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003859 /* Silently discard the chunk if stream-id is not valid */
3860 sctp_walk_fwdtsn(skip, chunk) {
3861 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3862 goto gen_shutdown;
3863 }
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3866 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003867 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 /* Go a head and force a SACK, since we are shutting down. */
3871gen_shutdown:
3872 /* Implementor's Guide.
3873 *
3874 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3875 * respond to each received packet containing one or more DATA chunk(s)
3876 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3877 */
3878 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3879 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3880 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3881 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3882
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003883 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884}
3885
3886/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003887 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003888 *
3889 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3890 * Identifier field. If this algorithm was not specified by the
3891 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3892 * during association setup, the AUTH chunk and all chunks after it MUST
3893 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3894 * defined in Section 4.1.
3895 *
3896 * If an endpoint with no shared key receives a Shared Key Identifier
3897 * other than 0, it MUST silently discard all authenticated chunks. If
3898 * the endpoint has at least one endpoint pair shared key for the peer,
3899 * it MUST use the key specified by the Shared Key Identifier if a
3900 * key has been configured for that Shared Key Identifier. If no
3901 * endpoint pair shared key has been configured for that Shared Key
3902 * Identifier, all authenticated chunks MUST be silently discarded.
3903 *
3904 * Verification Tag: 8.5 Verification Tag [Normal verification]
3905 *
3906 * The return value is the disposition of the chunk.
3907 */
3908static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3909 const struct sctp_association *asoc,
3910 const sctp_subtype_t type,
3911 struct sctp_chunk *chunk)
3912{
3913 struct sctp_authhdr *auth_hdr;
3914 struct sctp_hmac *hmac;
3915 unsigned int sig_len;
3916 __u16 key_id;
3917 __u8 *save_digest;
3918 __u8 *digest;
3919
3920 /* Pull in the auth header, so we can do some more verification */
3921 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3922 chunk->subh.auth_hdr = auth_hdr;
3923 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3924
3925 /* Make sure that we suport the HMAC algorithm from the auth
3926 * chunk.
3927 */
3928 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3929 return SCTP_IERROR_AUTH_BAD_HMAC;
3930
3931 /* Make sure that the provided shared key identifier has been
3932 * configured
3933 */
3934 key_id = ntohs(auth_hdr->shkey_id);
3935 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3936 return SCTP_IERROR_AUTH_BAD_KEYID;
3937
3938
3939 /* Make sure that the length of the signature matches what
3940 * we expect.
3941 */
3942 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3943 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3944 if (sig_len != hmac->hmac_len)
3945 return SCTP_IERROR_PROTO_VIOLATION;
3946
3947 /* Now that we've done validation checks, we can compute and
3948 * verify the hmac. The steps involved are:
3949 * 1. Save the digest from the chunk.
3950 * 2. Zero out the digest in the chunk.
3951 * 3. Compute the new digest
3952 * 4. Compare saved and new digests.
3953 */
3954 digest = auth_hdr->hmac;
3955 skb_pull(chunk->skb, sig_len);
3956
3957 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3958 if (!save_digest)
3959 goto nomem;
3960
3961 memset(digest, 0, sig_len);
3962
3963 sctp_auth_calculate_hmac(asoc, chunk->skb,
3964 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3965 GFP_ATOMIC);
3966
3967 /* Discard the packet if the digests do not match */
3968 if (memcmp(save_digest, digest, sig_len)) {
3969 kfree(save_digest);
3970 return SCTP_IERROR_BAD_SIG;
3971 }
3972
3973 kfree(save_digest);
3974 chunk->auth = 1;
3975
3976 return SCTP_IERROR_NO_ERROR;
3977nomem:
3978 return SCTP_IERROR_NOMEM;
3979}
3980
3981sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3982 const struct sctp_association *asoc,
3983 const sctp_subtype_t type,
3984 void *arg,
3985 sctp_cmd_seq_t *commands)
3986{
3987 struct sctp_authhdr *auth_hdr;
3988 struct sctp_chunk *chunk = arg;
3989 struct sctp_chunk *err_chunk;
3990 sctp_ierror_t error;
3991
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003992 /* Make sure that the peer has AUTH capable */
3993 if (!asoc->peer.auth_capable)
3994 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3995
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003996 if (!sctp_vtag_verify(chunk, asoc)) {
3997 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3998 SCTP_NULL());
3999 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4000 }
4001
4002 /* Make sure that the AUTH chunk has valid length. */
4003 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4004 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4005 commands);
4006
4007 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4008 error = sctp_sf_authenticate(ep, asoc, type, chunk);
4009 switch (error) {
4010 case SCTP_IERROR_AUTH_BAD_HMAC:
4011 /* Generate the ERROR chunk and discard the rest
4012 * of the packet
4013 */
4014 err_chunk = sctp_make_op_error(asoc, chunk,
4015 SCTP_ERROR_UNSUP_HMAC,
4016 &auth_hdr->hmac_id,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004017 sizeof(__u16), 0);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004018 if (err_chunk) {
4019 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4020 SCTP_CHUNK(err_chunk));
4021 }
4022 /* Fall Through */
4023 case SCTP_IERROR_AUTH_BAD_KEYID:
4024 case SCTP_IERROR_BAD_SIG:
4025 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4026 break;
4027 case SCTP_IERROR_PROTO_VIOLATION:
4028 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4029 commands);
4030 break;
4031 case SCTP_IERROR_NOMEM:
4032 return SCTP_DISPOSITION_NOMEM;
4033 default:
4034 break;
4035 }
4036
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004037 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4038 struct sctp_ulpevent *ev;
4039
4040 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4041 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4042
4043 if (!ev)
4044 return -ENOMEM;
4045
4046 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4047 SCTP_ULPEVENT(ev));
4048 }
4049
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004050 return SCTP_DISPOSITION_CONSUME;
4051}
4052
4053/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 * Process an unknown chunk.
4055 *
4056 * Section: 3.2. Also, 2.1 in the implementor's guide.
4057 *
4058 * Chunk Types are encoded such that the highest-order two bits specify
4059 * the action that must be taken if the processing endpoint does not
4060 * recognize the Chunk Type.
4061 *
4062 * 00 - Stop processing this SCTP packet and discard it, do not process
4063 * any further chunks within it.
4064 *
4065 * 01 - Stop processing this SCTP packet and discard it, do not process
4066 * any further chunks within it, and report the unrecognized
4067 * chunk in an 'Unrecognized Chunk Type'.
4068 *
4069 * 10 - Skip this chunk and continue processing.
4070 *
4071 * 11 - Skip this chunk and continue processing, but report in an ERROR
4072 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4073 *
4074 * The return value is the disposition of the chunk.
4075 */
4076sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4077 const struct sctp_association *asoc,
4078 const sctp_subtype_t type,
4079 void *arg,
4080 sctp_cmd_seq_t *commands)
4081{
4082 struct sctp_chunk *unk_chunk = arg;
4083 struct sctp_chunk *err_chunk;
4084 sctp_chunkhdr_t *hdr;
4085
4086 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4087
4088 if (!sctp_vtag_verify(unk_chunk, asoc))
4089 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4090
4091 /* Make sure that the chunk has a valid length.
4092 * Since we don't know the chunk type, we use a general
4093 * chunkhdr structure to make a comparison.
4094 */
4095 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4096 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4097 commands);
4098
4099 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4100 case SCTP_CID_ACTION_DISCARD:
4101 /* Discard the packet. */
4102 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4103 break;
4104 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 /* Generate an ERROR chunk as response. */
4106 hdr = unk_chunk->chunk_hdr;
4107 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4108 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004109 WORD_ROUND(ntohs(hdr->length)),
4110 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (err_chunk) {
4112 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4113 SCTP_CHUNK(err_chunk));
4114 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004115
4116 /* Discard the packet. */
4117 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return SCTP_DISPOSITION_CONSUME;
4119 break;
4120 case SCTP_CID_ACTION_SKIP:
4121 /* Skip the chunk. */
4122 return SCTP_DISPOSITION_DISCARD;
4123 break;
4124 case SCTP_CID_ACTION_SKIP_ERR:
4125 /* Generate an ERROR chunk as response. */
4126 hdr = unk_chunk->chunk_hdr;
4127 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4128 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004129 WORD_ROUND(ntohs(hdr->length)),
4130 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 if (err_chunk) {
4132 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4133 SCTP_CHUNK(err_chunk));
4134 }
4135 /* Skip the chunk. */
4136 return SCTP_DISPOSITION_CONSUME;
4137 break;
4138 default:
4139 break;
4140 }
4141
4142 return SCTP_DISPOSITION_DISCARD;
4143}
4144
4145/*
4146 * Discard the chunk.
4147 *
4148 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4149 * [Too numerous to mention...]
4150 * Verification Tag: No verification needed.
4151 * Inputs
4152 * (endpoint, asoc, chunk)
4153 *
4154 * Outputs
4155 * (asoc, reply_msg, msg_up, timers, counters)
4156 *
4157 * The return value is the disposition of the chunk.
4158 */
4159sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4160 const struct sctp_association *asoc,
4161 const sctp_subtype_t type,
4162 void *arg,
4163 sctp_cmd_seq_t *commands)
4164{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004165 struct sctp_chunk *chunk = arg;
4166
4167 /* Make sure that the chunk has a valid length.
4168 * Since we don't know the chunk type, we use a general
4169 * chunkhdr structure to make a comparison.
4170 */
4171 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4172 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4173 commands);
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4176 return SCTP_DISPOSITION_DISCARD;
4177}
4178
4179/*
4180 * Discard the whole packet.
4181 *
4182 * Section: 8.4 2)
4183 *
4184 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4185 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 *
4187 * Verification Tag: No verification necessary
4188 *
4189 * Inputs
4190 * (endpoint, asoc, chunk)
4191 *
4192 * Outputs
4193 * (asoc, reply_msg, msg_up, timers, counters)
4194 *
4195 * The return value is the disposition of the chunk.
4196 */
4197sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4198 const struct sctp_association *asoc,
4199 const sctp_subtype_t type,
4200 void *arg,
4201 sctp_cmd_seq_t *commands)
4202{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004203 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4205
4206 return SCTP_DISPOSITION_CONSUME;
4207}
4208
4209
4210/*
4211 * The other end is violating protocol.
4212 *
4213 * Section: Not specified
4214 * Verification Tag: Not specified
4215 * Inputs
4216 * (endpoint, asoc, chunk)
4217 *
4218 * Outputs
4219 * (asoc, reply_msg, msg_up, timers, counters)
4220 *
4221 * We simply tag the chunk as a violation. The state machine will log
4222 * the violation and continue.
4223 */
4224sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4225 const struct sctp_association *asoc,
4226 const sctp_subtype_t type,
4227 void *arg,
4228 sctp_cmd_seq_t *commands)
4229{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004230 struct sctp_chunk *chunk = arg;
4231
4232 /* Make sure that the chunk has a valid length. */
4233 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4234 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4235 commands);
4236
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return SCTP_DISPOSITION_VIOLATION;
4238}
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004241 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004243static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004244 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004247 sctp_cmd_seq_t *commands,
4248 const __u8 *payload,
4249 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004251 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 struct sctp_chunk *chunk = arg;
4253 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004255 /* SCTP-AUTH, Section 6.3:
4256 * It should be noted that if the receiver wants to tear
4257 * down an association in an authenticated way only, the
4258 * handling of malformed packets should not result in
4259 * tearing down the association.
4260 *
4261 * This means that if we only want to abort associations
4262 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004263 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004264 * was malformed.
4265 */
4266 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4267 goto discard;
4268
Jesper Juhl9abed242007-11-11 23:57:49 +01004269 /* Make the abort chunk. */
4270 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4271 if (!abort)
4272 goto nomem;
4273
Vlad Yasevichece25df2007-09-07 16:30:54 -04004274 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004275 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4276 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4277 !asoc->peer.i.init_tag) {
4278 sctp_initack_chunk_t *initack;
4279
4280 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4281 if (!sctp_chunk_length_valid(chunk,
4282 sizeof(sctp_initack_chunk_t)))
4283 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4284 else {
4285 unsigned int inittag;
4286
4287 inittag = ntohl(initack->init_hdr.init_tag);
4288 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4289 SCTP_U32(inittag));
4290 }
4291 }
4292
Vlad Yasevichece25df2007-09-07 16:30:54 -04004293 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4294 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
Vlad Yasevichece25df2007-09-07 16:30:54 -04004296 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4297 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4298 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4299 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4300 SCTP_ERROR(ECONNREFUSED));
4301 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4302 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4303 } else {
4304 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4305 SCTP_ERROR(ECONNABORTED));
4306 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4307 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4308 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004311 packet = sctp_ootb_pkt_new(asoc, chunk);
4312
4313 if (!packet)
4314 goto nomem_pkt;
4315
4316 if (sctp_test_T_bit(abort))
4317 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4318
4319 abort->skb->sk = ep->base.sk;
4320
4321 sctp_packet_append_chunk(packet, abort);
4322
4323 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4324 SCTP_PACKET(packet));
4325
4326 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
4328
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004330
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004331discard:
4332 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 return SCTP_DISPOSITION_ABORT;
4334
Vlad Yasevichece25df2007-09-07 16:30:54 -04004335nomem_pkt:
4336 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337nomem:
4338 return SCTP_DISPOSITION_NOMEM;
4339}
4340
Wei Yongjunaecedea2007-08-02 16:57:44 +08004341/*
4342 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004343 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004344 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004345 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004346 *
4347 * We inform the other end by sending an ABORT with a Protocol Violation
4348 * error code.
4349 *
4350 * Section: Not specified
4351 * Verification Tag: Nothing to do
4352 * Inputs
4353 * (endpoint, asoc, chunk)
4354 *
4355 * Outputs
4356 * (reply_msg, msg_up, counters)
4357 *
4358 * Generate an ABORT chunk and terminate the association.
4359 */
4360static sctp_disposition_t sctp_sf_violation_chunklen(
4361 const struct sctp_endpoint *ep,
4362 const struct sctp_association *asoc,
4363 const sctp_subtype_t type,
4364 void *arg,
4365 sctp_cmd_seq_t *commands)
4366{
Florian Westphal24448442008-03-23 22:46:34 -07004367 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004368
Vlad Yasevichece25df2007-09-07 16:30:54 -04004369 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004370 sizeof(err_str));
4371}
4372
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004373/*
4374 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004375 * If the length is smaller than the minimum length of a given parameter,
4376 * or accumulated length in multi parameters exceeds the end of the chunk,
4377 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004378 */
4379static sctp_disposition_t sctp_sf_violation_paramlen(
4380 const struct sctp_endpoint *ep,
4381 const struct sctp_association *asoc,
4382 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004383 void *arg, void *ext,
4384 sctp_cmd_seq_t *commands)
4385{
4386 struct sctp_chunk *chunk = arg;
4387 struct sctp_paramhdr *param = ext;
4388 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004389
Wei Yongjunba016672008-09-30 05:32:24 -07004390 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4391 goto discard;
4392
4393 /* Make the abort chunk. */
4394 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4395 if (!abort)
4396 goto nomem;
4397
4398 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4399 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4400
4401 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4402 SCTP_ERROR(ECONNABORTED));
4403 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4404 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4405 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004406 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004407
4408discard:
4409 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004410 return SCTP_DISPOSITION_ABORT;
4411nomem:
4412 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004413}
4414
Wei Yongjunaecedea2007-08-02 16:57:44 +08004415/* Handle a protocol violation when the peer trying to advance the
4416 * cumulative tsn ack to a point beyond the max tsn currently sent.
4417 *
4418 * We inform the other end by sending an ABORT with a Protocol Violation
4419 * error code.
4420 */
4421static sctp_disposition_t sctp_sf_violation_ctsn(
4422 const struct sctp_endpoint *ep,
4423 const struct sctp_association *asoc,
4424 const sctp_subtype_t type,
4425 void *arg,
4426 sctp_cmd_seq_t *commands)
4427{
Florian Westphal24448442008-03-23 22:46:34 -07004428 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004429
Vlad Yasevichece25df2007-09-07 16:30:54 -04004430 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004431 sizeof(err_str));
4432}
4433
Vlad Yasevichece25df2007-09-07 16:30:54 -04004434/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004435 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004436 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004437 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004438 * on the path and we may not want to continue this communication.
4439 */
4440static sctp_disposition_t sctp_sf_violation_chunk(
4441 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 violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004448
4449 if (!asoc)
4450 return sctp_sf_violation(ep, asoc, type, arg, commands);
4451
4452 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4453 sizeof(err_str));
4454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455/***************************************************************************
4456 * These are the state functions for handling primitive (Section 10) events.
4457 ***************************************************************************/
4458/*
4459 * sctp_sf_do_prm_asoc
4460 *
4461 * Section: 10.1 ULP-to-SCTP
4462 * B) Associate
4463 *
4464 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4465 * outbound stream count)
4466 * -> association id [,destination transport addr list] [,outbound stream
4467 * count]
4468 *
4469 * This primitive allows the upper layer to initiate an association to a
4470 * specific peer endpoint.
4471 *
4472 * The peer endpoint shall be specified by one of the transport addresses
4473 * which defines the endpoint (see Section 1.4). If the local SCTP
4474 * instance has not been initialized, the ASSOCIATE is considered an
4475 * error.
4476 * [This is not relevant for the kernel implementation since we do all
4477 * initialization at boot time. It we hadn't initialized we wouldn't
4478 * get anywhere near this code.]
4479 *
4480 * An association id, which is a local handle to the SCTP association,
4481 * will be returned on successful establishment of the association. If
4482 * SCTP is not able to open an SCTP association with the peer endpoint,
4483 * an error is returned.
4484 * [In the kernel implementation, the struct sctp_association needs to
4485 * be created BEFORE causing this primitive to run.]
4486 *
4487 * Other association parameters may be returned, including the
4488 * complete destination transport addresses of the peer as well as the
4489 * outbound stream count of the local endpoint. One of the transport
4490 * address from the returned destination addresses will be selected by
4491 * the local endpoint as default primary path for sending SCTP packets
4492 * to this peer. The returned "destination transport addr list" can
4493 * be used by the ULP to change the default primary path or to force
4494 * sending a packet to a specific transport address. [All of this
4495 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4496 * function.]
4497 *
4498 * Mandatory attributes:
4499 *
4500 * o local SCTP instance name - obtained from the INITIALIZE operation.
4501 * [This is the argument asoc.]
4502 * o destination transport addr - specified as one of the transport
4503 * addresses of the peer endpoint with which the association is to be
4504 * established.
4505 * [This is asoc->peer.active_path.]
4506 * o outbound stream count - the number of outbound streams the ULP
4507 * would like to open towards this peer endpoint.
4508 * [BUG: This is not currently implemented.]
4509 * Optional attributes:
4510 *
4511 * None.
4512 *
4513 * The return value is a disposition.
4514 */
4515sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4516 const struct sctp_association *asoc,
4517 const sctp_subtype_t type,
4518 void *arg,
4519 sctp_cmd_seq_t *commands)
4520{
4521 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004522 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 /* The comment below says that we enter COOKIE-WAIT AFTER
4525 * sending the INIT, but that doesn't actually work in our
4526 * implementation...
4527 */
4528 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4529 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4530
4531 /* RFC 2960 5.1 Normal Establishment of an Association
4532 *
4533 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4534 * must provide its Verification Tag (Tag_A) in the Initiate
4535 * Tag field. Tag_A SHOULD be a random number in the range of
4536 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4537 */
4538
4539 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4540 if (!repl)
4541 goto nomem;
4542
4543 /* Cast away the const modifier, as we want to just
4544 * rerun it through as a sideffect.
4545 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004546 my_asoc = (struct sctp_association *)asoc;
4547 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Frank Filz3f7a87d2005-06-20 13:14:57 -07004549 /* Choose transport for INIT. */
4550 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4551 SCTP_CHUNK(repl));
4552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 /* After sending the INIT, "A" starts the T1-init timer and
4554 * enters the COOKIE-WAIT state.
4555 */
4556 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4557 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4558 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4559 return SCTP_DISPOSITION_CONSUME;
4560
4561nomem:
4562 return SCTP_DISPOSITION_NOMEM;
4563}
4564
4565/*
4566 * Process the SEND primitive.
4567 *
4568 * Section: 10.1 ULP-to-SCTP
4569 * E) Send
4570 *
4571 * Format: SEND(association id, buffer address, byte count [,context]
4572 * [,stream id] [,life time] [,destination transport address]
4573 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4574 * -> result
4575 *
4576 * This is the main method to send user data via SCTP.
4577 *
4578 * Mandatory attributes:
4579 *
4580 * o association id - local handle to the SCTP association
4581 *
4582 * o buffer address - the location where the user message to be
4583 * transmitted is stored;
4584 *
4585 * o byte count - The size of the user data in number of bytes;
4586 *
4587 * Optional attributes:
4588 *
4589 * o context - an optional 32 bit integer that will be carried in the
4590 * sending failure notification to the ULP if the transportation of
4591 * this User Message fails.
4592 *
4593 * o stream id - to indicate which stream to send the data on. If not
4594 * specified, stream 0 will be used.
4595 *
4596 * o life time - specifies the life time of the user data. The user data
4597 * will not be sent by SCTP after the life time expires. This
4598 * parameter can be used to avoid efforts to transmit stale
4599 * user messages. SCTP notifies the ULP if the data cannot be
4600 * initiated to transport (i.e. sent to the destination via SCTP's
4601 * send primitive) within the life time variable. However, the
4602 * user data will be transmitted if SCTP has attempted to transmit a
4603 * chunk before the life time expired.
4604 *
4605 * o destination transport address - specified as one of the destination
4606 * transport addresses of the peer endpoint to which this packet
4607 * should be sent. Whenever possible, SCTP should use this destination
4608 * transport address for sending the packets, instead of the current
4609 * primary path.
4610 *
4611 * o unorder flag - this flag, if present, indicates that the user
4612 * would like the data delivered in an unordered fashion to the peer
4613 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4614 * message).
4615 *
4616 * o no-bundle flag - instructs SCTP not to bundle this user data with
4617 * other outbound DATA chunks. SCTP MAY still bundle even when
4618 * this flag is present, when faced with network congestion.
4619 *
4620 * o payload protocol-id - A 32 bit unsigned integer that is to be
4621 * passed to the peer indicating the type of payload protocol data
4622 * being transmitted. This value is passed as opaque data by SCTP.
4623 *
4624 * The return value is the disposition.
4625 */
4626sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4627 const struct sctp_association *asoc,
4628 const sctp_subtype_t type,
4629 void *arg,
4630 sctp_cmd_seq_t *commands)
4631{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004632 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004634 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 return SCTP_DISPOSITION_CONSUME;
4636}
4637
4638/*
4639 * Process the SHUTDOWN primitive.
4640 *
4641 * Section: 10.1:
4642 * C) Shutdown
4643 *
4644 * Format: SHUTDOWN(association id)
4645 * -> result
4646 *
4647 * Gracefully closes an association. Any locally queued user data
4648 * will be delivered to the peer. The association will be terminated only
4649 * after the peer acknowledges all the SCTP packets sent. A success code
4650 * will be returned on successful termination of the association. If
4651 * attempting to terminate the association results in a failure, an error
4652 * code shall be returned.
4653 *
4654 * Mandatory attributes:
4655 *
4656 * o association id - local handle to the SCTP association
4657 *
4658 * Optional attributes:
4659 *
4660 * None.
4661 *
4662 * The return value is the disposition.
4663 */
4664sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4665 const struct sctp_endpoint *ep,
4666 const struct sctp_association *asoc,
4667 const sctp_subtype_t type,
4668 void *arg,
4669 sctp_cmd_seq_t *commands)
4670{
4671 int disposition;
4672
4673 /* From 9.2 Shutdown of an Association
4674 * Upon receipt of the SHUTDOWN primitive from its upper
4675 * layer, the endpoint enters SHUTDOWN-PENDING state and
4676 * remains there until all outstanding data has been
4677 * acknowledged by its peer. The endpoint accepts no new data
4678 * from its upper layer, but retransmits data to the far end
4679 * if necessary to fill gaps.
4680 */
4681 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4682 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 disposition = SCTP_DISPOSITION_CONSUME;
4685 if (sctp_outq_is_empty(&asoc->outqueue)) {
4686 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4687 arg, commands);
4688 }
4689 return disposition;
4690}
4691
4692/*
4693 * Process the ABORT primitive.
4694 *
4695 * Section: 10.1:
4696 * C) Abort
4697 *
4698 * Format: Abort(association id [, cause code])
4699 * -> result
4700 *
4701 * Ungracefully closes an association. Any locally queued user data
4702 * will be discarded and an ABORT chunk is sent to the peer. A success code
4703 * will be returned on successful abortion of the association. If
4704 * attempting to abort the association results in a failure, an error
4705 * code shall be returned.
4706 *
4707 * Mandatory attributes:
4708 *
4709 * o association id - local handle to the SCTP association
4710 *
4711 * Optional attributes:
4712 *
4713 * o cause code - reason of the abort to be passed to the peer
4714 *
4715 * None.
4716 *
4717 * The return value is the disposition.
4718 */
4719sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4720 const struct sctp_endpoint *ep,
4721 const struct sctp_association *asoc,
4722 const sctp_subtype_t type,
4723 void *arg,
4724 sctp_cmd_seq_t *commands)
4725{
4726 /* From 9.1 Abort of an Association
4727 * Upon receipt of the ABORT primitive from its upper
4728 * layer, the endpoint enters CLOSED state and
4729 * discard all outstanding data has been
4730 * acknowledged by its peer. The endpoint accepts no new data
4731 * from its upper layer, but retransmits data to the far end
4732 * if necessary to fill gaps.
4733 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004734 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 sctp_disposition_t retval;
4736
4737 retval = SCTP_DISPOSITION_CONSUME;
4738
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004739 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
4741 /* Even if we can't send the ABORT due to low memory delete the
4742 * TCB. This is a departure from our typical NOMEM handling.
4743 */
4744
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004745 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4746 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 /* Delete the established association. */
4748 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004749 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750
4751 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4752 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4753
4754 return retval;
4755}
4756
4757/* We tried an illegal operation on an association which is closed. */
4758sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4759 const struct sctp_association *asoc,
4760 const sctp_subtype_t type,
4761 void *arg,
4762 sctp_cmd_seq_t *commands)
4763{
4764 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4765 return SCTP_DISPOSITION_CONSUME;
4766}
4767
4768/* We tried an illegal operation on an association which is shutting
4769 * down.
4770 */
4771sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4772 const struct sctp_association *asoc,
4773 const sctp_subtype_t type,
4774 void *arg,
4775 sctp_cmd_seq_t *commands)
4776{
4777 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4778 SCTP_ERROR(-ESHUTDOWN));
4779 return SCTP_DISPOSITION_CONSUME;
4780}
4781
4782/*
4783 * sctp_cookie_wait_prm_shutdown
4784 *
4785 * Section: 4 Note: 2
4786 * Verification Tag:
4787 * Inputs
4788 * (endpoint, asoc)
4789 *
4790 * The RFC does not explicitly address this issue, but is the route through the
4791 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4792 *
4793 * Outputs
4794 * (timers)
4795 */
4796sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4797 const struct sctp_endpoint *ep,
4798 const struct sctp_association *asoc,
4799 const sctp_subtype_t type,
4800 void *arg,
4801 sctp_cmd_seq_t *commands)
4802{
4803 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4804 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4805
4806 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4807 SCTP_STATE(SCTP_STATE_CLOSED));
4808
4809 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4810
4811 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4812
4813 return SCTP_DISPOSITION_DELETE_TCB;
4814}
4815
4816/*
4817 * sctp_cookie_echoed_prm_shutdown
4818 *
4819 * Section: 4 Note: 2
4820 * Verification Tag:
4821 * Inputs
4822 * (endpoint, asoc)
4823 *
4824 * The RFC does not explcitly address this issue, but is the route through the
4825 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4826 *
4827 * Outputs
4828 * (timers)
4829 */
4830sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4831 const struct sctp_endpoint *ep,
4832 const struct sctp_association *asoc,
4833 const sctp_subtype_t type,
4834 void *arg, sctp_cmd_seq_t *commands)
4835{
4836 /* There is a single T1 timer, so we should be able to use
4837 * common function with the COOKIE-WAIT state.
4838 */
4839 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4840}
4841
4842/*
4843 * sctp_sf_cookie_wait_prm_abort
4844 *
4845 * Section: 4 Note: 2
4846 * Verification Tag:
4847 * Inputs
4848 * (endpoint, asoc)
4849 *
4850 * The RFC does not explicitly address this issue, but is the route through the
4851 * state table when someone issues an abort while in COOKIE_WAIT state.
4852 *
4853 * Outputs
4854 * (timers)
4855 */
4856sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4857 const struct sctp_endpoint *ep,
4858 const struct sctp_association *asoc,
4859 const sctp_subtype_t type,
4860 void *arg,
4861 sctp_cmd_seq_t *commands)
4862{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004863 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 sctp_disposition_t retval;
4865
4866 /* Stop T1-init timer */
4867 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4868 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4869 retval = SCTP_DISPOSITION_CONSUME;
4870
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004871 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4874 SCTP_STATE(SCTP_STATE_CLOSED));
4875
4876 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4877
4878 /* Even if we can't send the ABORT due to low memory delete the
4879 * TCB. This is a departure from our typical NOMEM handling.
4880 */
4881
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004882 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4883 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 /* Delete the established association. */
4885 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004886 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887
4888 return retval;
4889}
4890
4891/*
4892 * sctp_sf_cookie_echoed_prm_abort
4893 *
4894 * Section: 4 Note: 3
4895 * Verification Tag:
4896 * Inputs
4897 * (endpoint, asoc)
4898 *
4899 * The RFC does not explcitly address this issue, but is the route through the
4900 * state table when someone issues an abort while in COOKIE_ECHOED state.
4901 *
4902 * Outputs
4903 * (timers)
4904 */
4905sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4906 const struct sctp_endpoint *ep,
4907 const struct sctp_association *asoc,
4908 const sctp_subtype_t type,
4909 void *arg,
4910 sctp_cmd_seq_t *commands)
4911{
4912 /* There is a single T1 timer, so we should be able to use
4913 * common function with the COOKIE-WAIT state.
4914 */
4915 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4916}
4917
4918/*
4919 * sctp_sf_shutdown_pending_prm_abort
4920 *
4921 * Inputs
4922 * (endpoint, asoc)
4923 *
4924 * The RFC does not explicitly address this issue, but is the route through the
4925 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4926 *
4927 * Outputs
4928 * (timers)
4929 */
4930sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4931 const struct sctp_endpoint *ep,
4932 const struct sctp_association *asoc,
4933 const sctp_subtype_t type,
4934 void *arg,
4935 sctp_cmd_seq_t *commands)
4936{
4937 /* Stop the T5-shutdown guard timer. */
4938 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4939 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4940
4941 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4942}
4943
4944/*
4945 * sctp_sf_shutdown_sent_prm_abort
4946 *
4947 * Inputs
4948 * (endpoint, asoc)
4949 *
4950 * The RFC does not explicitly address this issue, but is the route through the
4951 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4952 *
4953 * Outputs
4954 * (timers)
4955 */
4956sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4957 const struct sctp_endpoint *ep,
4958 const struct sctp_association *asoc,
4959 const sctp_subtype_t type,
4960 void *arg,
4961 sctp_cmd_seq_t *commands)
4962{
4963 /* Stop the T2-shutdown timer. */
4964 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4965 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4966
4967 /* Stop the T5-shutdown guard timer. */
4968 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4969 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4970
4971 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4972}
4973
4974/*
4975 * sctp_sf_cookie_echoed_prm_abort
4976 *
4977 * Inputs
4978 * (endpoint, asoc)
4979 *
4980 * The RFC does not explcitly address this issue, but is the route through the
4981 * state table when someone issues an abort while in COOKIE_ECHOED state.
4982 *
4983 * Outputs
4984 * (timers)
4985 */
4986sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4987 const struct sctp_endpoint *ep,
4988 const struct sctp_association *asoc,
4989 const sctp_subtype_t type,
4990 void *arg,
4991 sctp_cmd_seq_t *commands)
4992{
4993 /* The same T2 timer, so we should be able to use
4994 * common function with the SHUTDOWN-SENT state.
4995 */
4996 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4997}
4998
4999/*
5000 * Process the REQUESTHEARTBEAT primitive
5001 *
5002 * 10.1 ULP-to-SCTP
5003 * J) Request Heartbeat
5004 *
5005 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5006 *
5007 * -> result
5008 *
5009 * Instructs the local endpoint to perform a HeartBeat on the specified
5010 * destination transport address of the given association. The returned
5011 * result should indicate whether the transmission of the HEARTBEAT
5012 * chunk to the destination address is successful.
5013 *
5014 * Mandatory attributes:
5015 *
5016 * o association id - local handle to the SCTP association
5017 *
5018 * o destination transport address - the transport address of the
5019 * association on which a heartbeat should be issued.
5020 */
5021sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5022 const struct sctp_endpoint *ep,
5023 const struct sctp_association *asoc,
5024 const sctp_subtype_t type,
5025 void *arg,
5026 sctp_cmd_seq_t *commands)
5027{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005028 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5029 (struct sctp_transport *)arg, commands))
5030 return SCTP_DISPOSITION_NOMEM;
5031
5032 /*
5033 * RFC 2960 (bis), section 8.3
5034 *
5035 * D) Request an on-demand HEARTBEAT on a specific destination
5036 * transport address of a given association.
5037 *
5038 * The endpoint should increment the respective error counter of
5039 * the destination transport address each time a HEARTBEAT is sent
5040 * to that address and not acknowledged within one RTO.
5041 *
5042 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005043 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005044 SCTP_TRANSPORT(arg));
5045 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046}
5047
5048/*
5049 * ADDIP Section 4.1 ASCONF Chunk Procedures
5050 * When an endpoint has an ASCONF signaled change to be sent to the
5051 * remote endpoint it should do A1 to A9
5052 */
5053sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5054 const struct sctp_association *asoc,
5055 const sctp_subtype_t type,
5056 void *arg,
5057 sctp_cmd_seq_t *commands)
5058{
5059 struct sctp_chunk *chunk = arg;
5060
5061 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5062 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5063 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5064 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5065 return SCTP_DISPOSITION_CONSUME;
5066}
5067
5068/*
5069 * Ignore the primitive event
5070 *
5071 * The return value is the disposition of the primitive.
5072 */
5073sctp_disposition_t sctp_sf_ignore_primitive(
5074 const struct sctp_endpoint *ep,
5075 const struct sctp_association *asoc,
5076 const sctp_subtype_t type,
5077 void *arg,
5078 sctp_cmd_seq_t *commands)
5079{
5080 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5081 return SCTP_DISPOSITION_DISCARD;
5082}
5083
5084/***************************************************************************
5085 * These are the state functions for the OTHER events.
5086 ***************************************************************************/
5087
5088/*
5089 * Start the shutdown negotiation.
5090 *
5091 * From Section 9.2:
5092 * Once all its outstanding data has been acknowledged, the endpoint
5093 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5094 * TSN Ack field the last sequential TSN it has received from the peer.
5095 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5096 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5097 * with the updated last sequential TSN received from its peer.
5098 *
5099 * The return value is the disposition.
5100 */
5101sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5102 const struct sctp_endpoint *ep,
5103 const struct sctp_association *asoc,
5104 const sctp_subtype_t type,
5105 void *arg,
5106 sctp_cmd_seq_t *commands)
5107{
5108 struct sctp_chunk *reply;
5109
5110 /* Once all its outstanding data has been acknowledged, the
5111 * endpoint shall send a SHUTDOWN chunk to its peer including
5112 * in the Cumulative TSN Ack field the last sequential TSN it
5113 * has received from the peer.
5114 */
5115 reply = sctp_make_shutdown(asoc, NULL);
5116 if (!reply)
5117 goto nomem;
5118
5119 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5120 * T2-shutdown timer.
5121 */
5122 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5123
5124 /* It shall then start the T2-shutdown timer */
5125 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5126 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5127
Wei Yongjun536428a2008-09-05 08:55:26 +08005128 /* RFC 4960 Section 9.2
5129 * The sender of the SHUTDOWN MAY also start an overall guard timer
5130 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5131 */
5132 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5133 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 if (asoc->autoclose)
5136 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5137 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5138
5139 /* and enter the SHUTDOWN-SENT state. */
5140 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5141 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5142
5143 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5144 *
5145 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005146 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 */
5148 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5149
5150 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5151
5152 return SCTP_DISPOSITION_CONSUME;
5153
5154nomem:
5155 return SCTP_DISPOSITION_NOMEM;
5156}
5157
5158/*
5159 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5160 *
5161 * From Section 9.2:
5162 *
5163 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5164 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5165 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5166 * endpoint must re-send the SHUTDOWN ACK.
5167 *
5168 * The return value is the disposition.
5169 */
5170sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5171 const struct sctp_endpoint *ep,
5172 const struct sctp_association *asoc,
5173 const sctp_subtype_t type,
5174 void *arg,
5175 sctp_cmd_seq_t *commands)
5176{
5177 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5178 struct sctp_chunk *reply;
5179
5180 /* There are 2 ways of getting here:
5181 * 1) called in response to a SHUTDOWN chunk
5182 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5183 *
5184 * For the case (2), the arg parameter is set to NULL. We need
5185 * to check that we have a chunk before accessing it's fields.
5186 */
5187 if (chunk) {
5188 if (!sctp_vtag_verify(chunk, asoc))
5189 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5190
5191 /* Make sure that the SHUTDOWN chunk has a valid length. */
5192 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5193 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5194 commands);
5195 }
5196
5197 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5198 * shall send a SHUTDOWN ACK ...
5199 */
5200 reply = sctp_make_shutdown_ack(asoc, chunk);
5201 if (!reply)
5202 goto nomem;
5203
5204 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5205 * the T2-shutdown timer.
5206 */
5207 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5208
5209 /* and start/restart a T2-shutdown timer of its own, */
5210 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5211 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5212
5213 if (asoc->autoclose)
5214 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5215 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5216
5217 /* Enter the SHUTDOWN-ACK-SENT state. */
5218 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5219 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5220
5221 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5222 *
5223 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005224 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 */
5226 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5227
5228 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5229
5230 return SCTP_DISPOSITION_CONSUME;
5231
5232nomem:
5233 return SCTP_DISPOSITION_NOMEM;
5234}
5235
5236/*
5237 * Ignore the event defined as other
5238 *
5239 * The return value is the disposition of the event.
5240 */
5241sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5242 const struct sctp_association *asoc,
5243 const sctp_subtype_t type,
5244 void *arg,
5245 sctp_cmd_seq_t *commands)
5246{
5247 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5248 return SCTP_DISPOSITION_DISCARD;
5249}
5250
5251/************************************************************
5252 * These are the state functions for handling timeout events.
5253 ************************************************************/
5254
5255/*
5256 * RTX Timeout
5257 *
5258 * Section: 6.3.3 Handle T3-rtx Expiration
5259 *
5260 * Whenever the retransmission timer T3-rtx expires for a destination
5261 * address, do the following:
5262 * [See below]
5263 *
5264 * The return value is the disposition of the chunk.
5265 */
5266sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5267 const struct sctp_association *asoc,
5268 const sctp_subtype_t type,
5269 void *arg,
5270 sctp_cmd_seq_t *commands)
5271{
5272 struct sctp_transport *transport = arg;
5273
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005274 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5275
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005277 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5278 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5280 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005281 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5283 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5284 return SCTP_DISPOSITION_DELETE_TCB;
5285 }
5286
5287 /* E1) For the destination address for which the timer
5288 * expires, adjust its ssthresh with rules defined in Section
5289 * 7.2.3 and set the cwnd <- MTU.
5290 */
5291
5292 /* E2) For the destination address for which the timer
5293 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5294 * maximum value discussed in rule C7 above (RTO.max) may be
5295 * used to provide an upper bound to this doubling operation.
5296 */
5297
5298 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5299 * outstanding DATA chunks for the address for which the
5300 * T3-rtx has expired will fit into a single packet, subject
5301 * to the MTU constraint for the path corresponding to the
5302 * destination transport address to which the retransmission
5303 * is being sent (this may be different from the address for
5304 * which the timer expires [see Section 6.4]). Call this
5305 * value K. Bundle and retransmit those K DATA chunks in a
5306 * single packet to the destination endpoint.
5307 *
5308 * Note: Any DATA chunks that were sent to the address for
5309 * which the T3-rtx timer expired but did not fit in one MTU
5310 * (rule E3 above), should be marked for retransmission and
5311 * sent as soon as cwnd allows (normally when a SACK arrives).
5312 */
5313
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 /* Do some failure management (Section 8.2). */
5315 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5316
Vlad Yasevich1845a572007-02-21 02:06:19 -08005317 /* NB: Rules E4 and F1 are implicit in R1. */
5318 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5319
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 return SCTP_DISPOSITION_CONSUME;
5321}
5322
5323/*
5324 * Generate delayed SACK on timeout
5325 *
5326 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5327 *
5328 * The guidelines on delayed acknowledgement algorithm specified in
5329 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5330 * acknowledgement SHOULD be generated for at least every second packet
5331 * (not every second DATA chunk) received, and SHOULD be generated
5332 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5333 * some situations it may be beneficial for an SCTP transmitter to be
5334 * more conservative than the algorithms detailed in this document
5335 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5336 * the following algorithms allow.
5337 */
5338sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5339 const struct sctp_association *asoc,
5340 const sctp_subtype_t type,
5341 void *arg,
5342 sctp_cmd_seq_t *commands)
5343{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005344 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5346 return SCTP_DISPOSITION_CONSUME;
5347}
5348
5349/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005350 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 *
5352 * Section: 4 Note: 2
5353 * Verification Tag:
5354 * Inputs
5355 * (endpoint, asoc)
5356 *
5357 * RFC 2960 Section 4 Notes
5358 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5359 * and re-start the T1-init timer without changing state. This MUST
5360 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5361 * endpoint MUST abort the initialization process and report the
5362 * error to SCTP user.
5363 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005364 * Outputs
5365 * (timers, events)
5366 *
5367 */
5368sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5369 const struct sctp_association *asoc,
5370 const sctp_subtype_t type,
5371 void *arg,
5372 sctp_cmd_seq_t *commands)
5373{
5374 struct sctp_chunk *repl = NULL;
5375 struct sctp_bind_addr *bp;
5376 int attempts = asoc->init_err_counter + 1;
5377
5378 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005379 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005380
Vlad Yasevich81845c22006-01-30 15:59:54 -08005381 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005382 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5383 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5384 if (!repl)
5385 return SCTP_DISPOSITION_NOMEM;
5386
5387 /* Choose transport for INIT. */
5388 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5389 SCTP_CHUNK(repl));
5390
5391 /* Issue a sideeffect to do the needed accounting. */
5392 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5393 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5394
5395 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5396 } else {
5397 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5398 " max_init_attempts: %d\n",
5399 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005400 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5401 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005402 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005403 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005404 return SCTP_DISPOSITION_DELETE_TCB;
5405 }
5406
5407 return SCTP_DISPOSITION_CONSUME;
5408}
5409
5410/*
5411 * sctp_sf_t1_cookie_timer_expire
5412 *
5413 * Section: 4 Note: 2
5414 * Verification Tag:
5415 * Inputs
5416 * (endpoint, asoc)
5417 *
5418 * RFC 2960 Section 4 Notes
5419 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 * COOKIE ECHO and re-start the T1-cookie timer without changing
5421 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5422 * After that, the endpoint MUST abort the initialization process and
5423 * report the error to SCTP user.
5424 *
5425 * Outputs
5426 * (timers, events)
5427 *
5428 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005429sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 const struct sctp_association *asoc,
5431 const sctp_subtype_t type,
5432 void *arg,
5433 sctp_cmd_seq_t *commands)
5434{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005435 struct sctp_chunk *repl = NULL;
5436 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437
Frank Filz3f7a87d2005-06-20 13:14:57 -07005438 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005439 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
Vlad Yasevich81845c22006-01-30 15:59:54 -08005441 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005442 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005444 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005446 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5447 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005449 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5450 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5451
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5453 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005454 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5455 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005457 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 return SCTP_DISPOSITION_DELETE_TCB;
5459 }
5460
5461 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462}
5463
5464/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5465 * with the updated last sequential TSN received from its peer.
5466 *
5467 * An endpoint should limit the number of retransmissions of the
5468 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5469 * If this threshold is exceeded the endpoint should destroy the TCB and
5470 * MUST report the peer endpoint unreachable to the upper layer (and
5471 * thus the association enters the CLOSED state). The reception of any
5472 * packet from its peer (i.e. as the peer sends all of its queued DATA
5473 * chunks) should clear the endpoint's retransmission count and restart
5474 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5475 * all of its queued DATA chunks that have not yet been sent.
5476 */
5477sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5478 const struct sctp_association *asoc,
5479 const sctp_subtype_t type,
5480 void *arg,
5481 sctp_cmd_seq_t *commands)
5482{
5483 struct sctp_chunk *reply = NULL;
5484
5485 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005486 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5487
Neil Horman58fbbed2008-02-29 11:40:56 -08005488 ((struct sctp_association *)asoc)->shutdown_retries++;
5489
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005491 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5492 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5494 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005495 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5497 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5498 return SCTP_DISPOSITION_DELETE_TCB;
5499 }
5500
5501 switch (asoc->state) {
5502 case SCTP_STATE_SHUTDOWN_SENT:
5503 reply = sctp_make_shutdown(asoc, NULL);
5504 break;
5505
5506 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5507 reply = sctp_make_shutdown_ack(asoc, NULL);
5508 break;
5509
5510 default:
5511 BUG();
5512 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514
5515 if (!reply)
5516 goto nomem;
5517
Wei Yongjun6345b192009-04-26 23:13:35 +08005518 /* Do some failure management (Section 8.2).
5519 * If we remove the transport an SHUTDOWN was last sent to, don't
5520 * do failure management.
5521 */
5522 if (asoc->shutdown_last_sent_to)
5523 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5524 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525
5526 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5527 * the T2-shutdown timer.
5528 */
5529 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5530
5531 /* Restart the T2-shutdown timer. */
5532 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5533 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5534 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5535 return SCTP_DISPOSITION_CONSUME;
5536
5537nomem:
5538 return SCTP_DISPOSITION_NOMEM;
5539}
5540
5541/*
5542 * ADDIP Section 4.1 ASCONF CHunk Procedures
5543 * If the T4 RTO timer expires the endpoint should do B1 to B5
5544 */
5545sctp_disposition_t sctp_sf_t4_timer_expire(
5546 const struct sctp_endpoint *ep,
5547 const struct sctp_association *asoc,
5548 const sctp_subtype_t type,
5549 void *arg,
5550 sctp_cmd_seq_t *commands)
5551{
5552 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5553 struct sctp_transport *transport = chunk->transport;
5554
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005555 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5556
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5558 * detection on the appropriate destination address as defined in
5559 * RFC2960 [5] section 8.1 and 8.2.
5560 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005561 if (transport)
5562 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5563 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564
5565 /* Reconfig T4 timer and transport. */
5566 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5567
5568 /* ADDIP 4.1 B2) Increment the association error counters and perform
5569 * endpoint failure detection on the association as defined in
5570 * RFC2960 [5] section 8.1 and 8.2.
5571 * association error counter is incremented in SCTP_CMD_STRIKE.
5572 */
5573 if (asoc->overall_error_count >= asoc->max_retrans) {
5574 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5575 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005576 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5577 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005579 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjun3d5a0192008-10-09 14:32:24 -07005581 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 return SCTP_DISPOSITION_ABORT;
5583 }
5584
5585 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5586 * the ASCONF chunk was sent by doubling the RTO timer value.
5587 * This is done in SCTP_CMD_STRIKE.
5588 */
5589
5590 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5591 * choose an alternate destination address (please refer to RFC2960
5592 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005593 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 * ASCONF sent.
5595 */
5596 sctp_chunk_hold(asoc->addip_last_asconf);
5597 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5598 SCTP_CHUNK(asoc->addip_last_asconf));
5599
5600 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5601 * destination is selected, then the RTO used will be that of the new
5602 * destination address.
5603 */
5604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5605 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5606
5607 return SCTP_DISPOSITION_CONSUME;
5608}
5609
5610/* sctpimpguide-05 Section 2.12.2
5611 * The sender of the SHUTDOWN MAY also start an overall guard timer
5612 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5613 * At the expiration of this timer the sender SHOULD abort the association
5614 * by sending an ABORT chunk.
5615 */
5616sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5617 const struct sctp_association *asoc,
5618 const sctp_subtype_t type,
5619 void *arg,
5620 sctp_cmd_seq_t *commands)
5621{
5622 struct sctp_chunk *reply = NULL;
5623
5624 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005625 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
5627 reply = sctp_make_abort(asoc, NULL, 0);
5628 if (!reply)
5629 goto nomem;
5630
5631 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005632 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5633 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005635 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005637 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5638 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5639
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 return SCTP_DISPOSITION_DELETE_TCB;
5641nomem:
5642 return SCTP_DISPOSITION_NOMEM;
5643}
5644
5645/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5646 * the association is automatically closed by starting the shutdown process.
5647 * The work that needs to be done is same as when SHUTDOWN is initiated by
5648 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5649 */
5650sctp_disposition_t sctp_sf_autoclose_timer_expire(
5651 const struct sctp_endpoint *ep,
5652 const struct sctp_association *asoc,
5653 const sctp_subtype_t type,
5654 void *arg,
5655 sctp_cmd_seq_t *commands)
5656{
5657 int disposition;
5658
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005659 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5660
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 /* From 9.2 Shutdown of an Association
5662 * Upon receipt of the SHUTDOWN primitive from its upper
5663 * layer, the endpoint enters SHUTDOWN-PENDING state and
5664 * remains there until all outstanding data has been
5665 * acknowledged by its peer. The endpoint accepts no new data
5666 * from its upper layer, but retransmits data to the far end
5667 * if necessary to fill gaps.
5668 */
5669 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5670 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 disposition = SCTP_DISPOSITION_CONSUME;
5673 if (sctp_outq_is_empty(&asoc->outqueue)) {
5674 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5675 arg, commands);
5676 }
5677 return disposition;
5678}
5679
5680/*****************************************************************************
5681 * These are sa state functions which could apply to all types of events.
5682 ****************************************************************************/
5683
5684/*
5685 * This table entry is not implemented.
5686 *
5687 * Inputs
5688 * (endpoint, asoc, chunk)
5689 *
5690 * The return value is the disposition of the chunk.
5691 */
5692sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5693 const struct sctp_association *asoc,
5694 const sctp_subtype_t type,
5695 void *arg,
5696 sctp_cmd_seq_t *commands)
5697{
5698 return SCTP_DISPOSITION_NOT_IMPL;
5699}
5700
5701/*
5702 * This table entry represents a bug.
5703 *
5704 * Inputs
5705 * (endpoint, asoc, chunk)
5706 *
5707 * The return value is the disposition of the chunk.
5708 */
5709sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5710 const struct sctp_association *asoc,
5711 const sctp_subtype_t type,
5712 void *arg,
5713 sctp_cmd_seq_t *commands)
5714{
5715 return SCTP_DISPOSITION_BUG;
5716}
5717
5718/*
5719 * This table entry represents the firing of a timer in the wrong state.
5720 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5721 * when the association is in the wrong state. This event should
5722 * be ignored, so as to prevent any rearming of the timer.
5723 *
5724 * Inputs
5725 * (endpoint, asoc, chunk)
5726 *
5727 * The return value is the disposition of the chunk.
5728 */
5729sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5730 const struct sctp_association *asoc,
5731 const sctp_subtype_t type,
5732 void *arg,
5733 sctp_cmd_seq_t *commands)
5734{
5735 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5736 return SCTP_DISPOSITION_CONSUME;
5737}
5738
5739/********************************************************************
5740 * 2nd Level Abstractions
5741 ********************************************************************/
5742
5743/* Pull the SACK chunk based on the SACK header. */
5744static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5745{
5746 struct sctp_sackhdr *sack;
5747 unsigned int len;
5748 __u16 num_blocks;
5749 __u16 num_dup_tsns;
5750
5751 /* Protect ourselves from reading too far into
5752 * the skb from a bogus sender.
5753 */
5754 sack = (struct sctp_sackhdr *) chunk->skb->data;
5755
5756 num_blocks = ntohs(sack->num_gap_ack_blocks);
5757 num_dup_tsns = ntohs(sack->num_dup_tsns);
5758 len = sizeof(struct sctp_sackhdr);
5759 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5760 if (len > chunk->skb->len)
5761 return NULL;
5762
5763 skb_pull(chunk->skb, len);
5764
5765 return sack;
5766}
5767
5768/* Create an ABORT packet to be sent as a response, with the specified
5769 * error causes.
5770 */
5771static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5772 const struct sctp_association *asoc,
5773 struct sctp_chunk *chunk,
5774 const void *payload,
5775 size_t paylen)
5776{
5777 struct sctp_packet *packet;
5778 struct sctp_chunk *abort;
5779
5780 packet = sctp_ootb_pkt_new(asoc, chunk);
5781
5782 if (packet) {
5783 /* Make an ABORT.
5784 * The T bit will be set if the asoc is NULL.
5785 */
5786 abort = sctp_make_abort(asoc, chunk, paylen);
5787 if (!abort) {
5788 sctp_ootb_pkt_free(packet);
5789 return NULL;
5790 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005791
5792 /* Reflect vtag if T-Bit is set */
5793 if (sctp_test_T_bit(abort))
5794 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5795
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 /* Add specified error causes, i.e., payload, to the
5797 * end of the chunk.
5798 */
5799 sctp_addto_chunk(abort, paylen, payload);
5800
5801 /* Set the skb to the belonging sock for accounting. */
5802 abort->skb->sk = ep->base.sk;
5803
5804 sctp_packet_append_chunk(packet, abort);
5805
5806 }
5807
5808 return packet;
5809}
5810
5811/* Allocate a packet for responding in the OOTB conditions. */
5812static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5813 const struct sctp_chunk *chunk)
5814{
5815 struct sctp_packet *packet;
5816 struct sctp_transport *transport;
5817 __u16 sport;
5818 __u16 dport;
5819 __u32 vtag;
5820
5821 /* Get the source and destination port from the inbound packet. */
5822 sport = ntohs(chunk->sctp_hdr->dest);
5823 dport = ntohs(chunk->sctp_hdr->source);
5824
5825 /* The V-tag is going to be the same as the inbound packet if no
5826 * association exists, otherwise, use the peer's vtag.
5827 */
5828 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005829 /* Special case the INIT-ACK as there is no peer's vtag
5830 * yet.
5831 */
5832 switch(chunk->chunk_hdr->type) {
5833 case SCTP_CID_INIT_ACK:
5834 {
5835 sctp_initack_chunk_t *initack;
5836
5837 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5838 vtag = ntohl(initack->init_hdr.init_tag);
5839 break;
5840 }
5841 default:
5842 vtag = asoc->peer.i.init_tag;
5843 break;
5844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 } else {
5846 /* Special case the INIT and stale COOKIE_ECHO as there is no
5847 * vtag yet.
5848 */
5849 switch(chunk->chunk_hdr->type) {
5850 case SCTP_CID_INIT:
5851 {
5852 sctp_init_chunk_t *init;
5853
5854 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5855 vtag = ntohl(init->init_hdr.init_tag);
5856 break;
5857 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005858 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 vtag = ntohl(chunk->sctp_hdr->vtag);
5860 break;
5861 }
5862 }
5863
5864 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005865 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 if (!transport)
5867 goto nomem;
5868
5869 /* Cache a route for the transport with the chunk's destination as
5870 * the source address.
5871 */
Al Viro16b0a032006-11-20 17:13:38 -08005872 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 sctp_sk(sctp_get_ctl_sock()));
5874
5875 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5876 packet = sctp_packet_config(packet, vtag, 0);
5877
5878 return packet;
5879
5880nomem:
5881 return NULL;
5882}
5883
5884/* Free the packet allocated earlier for responding in the OOTB condition. */
5885void sctp_ootb_pkt_free(struct sctp_packet *packet)
5886{
5887 sctp_transport_free(packet->transport);
5888}
5889
5890/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5891static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5892 const struct sctp_association *asoc,
5893 const struct sctp_chunk *chunk,
5894 sctp_cmd_seq_t *commands,
5895 struct sctp_chunk *err_chunk)
5896{
5897 struct sctp_packet *packet;
5898
5899 if (err_chunk) {
5900 packet = sctp_ootb_pkt_new(asoc, chunk);
5901 if (packet) {
5902 struct sctp_signed_cookie *cookie;
5903
5904 /* Override the OOTB vtag from the cookie. */
5905 cookie = chunk->subh.cookie_hdr;
5906 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005907
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 /* Set the skb to the belonging sock for accounting. */
5909 err_chunk->skb->sk = ep->base.sk;
5910 sctp_packet_append_chunk(packet, err_chunk);
5911 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5912 SCTP_PACKET(packet));
5913 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5914 } else
5915 sctp_chunk_free (err_chunk);
5916 }
5917}
5918
5919
5920/* Process a data chunk */
5921static int sctp_eat_data(const struct sctp_association *asoc,
5922 struct sctp_chunk *chunk,
5923 sctp_cmd_seq_t *commands)
5924{
5925 sctp_datahdr_t *data_hdr;
5926 struct sctp_chunk *err;
5927 size_t datalen;
5928 sctp_verb_t deliver;
5929 int tmp;
5930 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005931 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005932 struct sock *sk = asoc->base.sk;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04005933 u16 ssn;
5934 u16 sid;
5935 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
5937 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5938 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5939
5940 tsn = ntohl(data_hdr->tsn);
5941 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5942
5943 /* ASSERT: Now skb->data is really the user data. */
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 /* Process ECN based congestion.
5946 *
5947 * Since the chunk structure is reused for all chunks within
5948 * a packet, we use ecn_ce_done to track if we've already
5949 * done CE processing for this packet.
5950 *
5951 * We need to do ECN processing even if we plan to discard the
5952 * chunk later.
5953 */
5954
5955 if (!chunk->ecn_ce_done) {
5956 struct sctp_af *af;
5957 chunk->ecn_ce_done = 1;
5958
5959 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005960 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
5962 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5963 /* Do real work as sideffect. */
5964 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5965 SCTP_U32(tsn));
5966 }
5967 }
5968
5969 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5970 if (tmp < 0) {
5971 /* The TSN is too high--silently discard the chunk and
5972 * count on it getting retransmitted later.
5973 */
5974 return SCTP_IERROR_HIGH_TSN;
5975 } else if (tmp > 0) {
5976 /* This is a duplicate. Record it. */
5977 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5978 return SCTP_IERROR_DUP_TSN;
5979 }
5980
5981 /* This is a new TSN. */
5982
5983 /* Discard if there is no room in the receive window.
5984 * Actually, allow a little bit of overflow (up to a MTU).
5985 */
5986 datalen = ntohs(chunk->chunk_hdr->length);
5987 datalen -= sizeof(sctp_data_chunk_t);
5988
5989 deliver = SCTP_CMD_CHUNK_ULP;
5990
5991 /* Think about partial delivery. */
5992 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5993
5994 /* Even if we don't accept this chunk there is
5995 * memory pressure.
5996 */
5997 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5998 }
5999
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006000 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 * seems a bit troublesome in that frag_point varies based on
6002 * PMTU. In cases, such as loopback, this might be a rather
6003 * large spill over.
6004 */
Neil Horman4d93df02007-08-15 16:07:44 -07006005 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6006 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007
6008 /* If this is the next TSN, consider reneging to make
6009 * room. Note: Playing nice with a confused sender. A
6010 * malicious sender can still eat up all our buffer
6011 * space and in the future we may want to detect and
6012 * do more drastic reneging.
6013 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006014 if (sctp_tsnmap_has_gap(map) &&
6015 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6017 deliver = SCTP_CMD_RENEGE;
6018 } else {
6019 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6020 "rwnd: %d\n", tsn, datalen,
6021 asoc->rwnd);
6022 return SCTP_IERROR_IGNORE_TSN;
6023 }
6024 }
6025
6026 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006027 * Also try to renege to limit our memory usage in the event that
6028 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006029 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006030 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6031 * memory usage too much
6032 */
6033 if (*sk->sk_prot_creator->memory_pressure) {
6034 if (sctp_tsnmap_has_gap(map) &&
6035 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6036 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6037 deliver = SCTP_CMD_RENEGE;
6038 }
6039 }
6040
6041 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006042 * Section 3.3.10.9 No User Data (9)
6043 *
6044 * Cause of error
6045 * ---------------
6046 * No User Data: This error cause is returned to the originator of a
6047 * DATA chunk if a received DATA chunk has no user data.
6048 */
6049 if (unlikely(0 == datalen)) {
6050 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6051 if (err) {
6052 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6053 SCTP_CHUNK(err));
6054 }
6055 /* We are going to ABORT, so we might as well stop
6056 * processing the rest of the chunks in the packet.
6057 */
6058 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006059 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6060 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006062 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6064 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6065 return SCTP_IERROR_NO_DATA;
6066 }
6067
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006068 chunk->data_accepted = 1;
6069
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6071 * if we renege and the chunk arrives again.
6072 */
6073 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6074 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006075 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006077 ordered = 1;
6078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6081 *
6082 * If an endpoint receive a DATA chunk with an invalid stream
6083 * identifier, it shall acknowledge the reception of the DATA chunk
6084 * following the normal procedure, immediately send an ERROR chunk
6085 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6086 * and discard the DATA chunk.
6087 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006088 sid = ntohs(data_hdr->stream);
6089 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006090 /* Mark tsn as received even though we drop it */
6091 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6092
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6094 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006095 sizeof(data_hdr->stream),
6096 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 if (err)
6098 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6099 SCTP_CHUNK(err));
6100 return SCTP_IERROR_BAD_STREAM;
6101 }
6102
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006103 /* Check to see if the SSN is possible for this TSN.
6104 * The biggest gap we can record is 4K wide. Since SSNs wrap
6105 * at an unsigned short, there is no way that an SSN can
6106 * wrap and for a valid TSN. We can simply check if the current
6107 * SSN is smaller then the next expected one. If it is, it wrapped
6108 * and is invalid.
6109 */
6110 ssn = ntohs(data_hdr->ssn);
6111 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6112 return SCTP_IERROR_PROTO_VIOLATION;
6113 }
6114
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 /* Send the data up to the user. Note: Schedule the
6116 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6117 * chunk needs the updated rwnd.
6118 */
6119 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6120
6121 return SCTP_IERROR_NO_ERROR;
6122}