blob: ad3b43bb75ccc8efa0b130a7774bac471270c486 [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. */
396 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
397 sctp_source(chunk),
398 (sctp_init_chunk_t *)chunk->chunk_hdr,
399 GFP_ATOMIC))
400 goto nomem_init;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
403
404 /* If there are errors need to be reported for unknown parameters,
405 * make sure to reserve enough room in the INIT ACK for them.
406 */
407 len = 0;
408 if (err_chunk)
409 len = ntohs(err_chunk->chunk_hdr->length) -
410 sizeof(sctp_chunkhdr_t);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
413 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700414 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 /* If there are errors need to be reported for unknown parameters,
417 * include them in the outgoing INIT ACK as "Unrecognized parameter"
418 * parameter.
419 */
420 if (err_chunk) {
421 /* Get the "Unrecognized parameter" parameter(s) out of the
422 * ERROR chunk generated by sctp_verify_init(). Since the
423 * error cause code for "unknown parameter" and the
424 * "Unrecognized parameter" type is the same, we can
425 * construct the parameters in INIT ACK by copying the
426 * ERROR causes over.
427 */
428 unk_param = (sctp_unrecognized_param_t *)
429 ((__u8 *)(err_chunk->chunk_hdr) +
430 sizeof(sctp_chunkhdr_t));
431 /* Replace the cause code with the "Unrecognized parameter"
432 * parameter type.
433 */
434 sctp_addto_chunk(repl, len, unk_param);
435 sctp_chunk_free(err_chunk);
436 }
437
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700438 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
441
442 /*
443 * Note: After sending out INIT ACK with the State Cookie parameter,
444 * "Z" MUST NOT allocate any resources, nor keep any states for the
445 * new association. Otherwise, "Z" will be vulnerable to resource
446 * attacks.
447 */
448 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
449
450 return SCTP_DISPOSITION_DELETE_TCB;
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452nomem_init:
453 sctp_association_free(new_asoc);
454nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700455 if (err_chunk)
456 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return SCTP_DISPOSITION_NOMEM;
458}
459
460/*
461 * Respond to a normal INIT ACK chunk.
462 * We are the side that is initiating the association.
463 *
464 * Section: 5.1 Normal Establishment of an Association, C
465 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
466 * timer and leave COOKIE-WAIT state. "A" shall then send the State
467 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
468 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
469 *
470 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
471 * DATA chunks, but it MUST be the first chunk in the packet and
472 * until the COOKIE ACK is returned the sender MUST NOT send any
473 * other packets to the peer.
474 *
475 * Verification Tag: 3.3.3
476 * If the value of the Initiate Tag in a received INIT ACK chunk is
477 * found to be 0, the receiver MUST treat it as an error and close the
478 * association by transmitting an ABORT.
479 *
480 * Inputs
481 * (endpoint, asoc, chunk)
482 *
483 * Outputs
484 * (asoc, reply_msg, msg_up, timers, counters)
485 *
486 * The return value is the disposition of the chunk.
487 */
488sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
489 const struct sctp_association *asoc,
490 const sctp_subtype_t type,
491 void *arg,
492 sctp_cmd_seq_t *commands)
493{
494 struct sctp_chunk *chunk = arg;
495 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct sctp_chunk *err_chunk;
497 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 if (!sctp_vtag_verify(chunk, asoc))
500 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* 6.10 Bundling
503 * An endpoint MUST NOT bundle INIT, INIT ACK or
504 * SHUTDOWN COMPLETE with any other chunks.
505 */
506 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400507 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Vlad Yasevichece25df2007-09-07 16:30:54 -0400509 /* Make sure that the INIT-ACK chunk has a valid length */
510 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
511 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
512 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Grab the INIT header. */
514 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 /* Verify the INIT chunk before processing it. */
517 err_chunk = NULL;
518 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
519 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
520 &err_chunk)) {
521
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800522 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800525 * Send an ABORT, with causes. If there are no causes,
526 * then there wasn't enough memory. Just terminate
527 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
529 if (err_chunk) {
530 packet = sctp_abort_pkt_new(ep, asoc, arg,
531 (__u8 *)(err_chunk->chunk_hdr) +
532 sizeof(sctp_chunkhdr_t),
533 ntohs(err_chunk->chunk_hdr->length) -
534 sizeof(sctp_chunkhdr_t));
535
536 sctp_chunk_free(err_chunk);
537
538 if (packet) {
539 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
540 SCTP_PACKET(packet));
541 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700542 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700545
546 /* SCTP-AUTH, Section 6.3:
547 * It should be noted that if the receiver wants to tear
548 * down an association in an authenticated way only, the
549 * handling of malformed packets should not result in
550 * tearing down the association.
551 *
552 * This means that if we only want to abort associations
553 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300554 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700555 * was malformed.
556 */
557 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
558 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
559
560 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700561 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
562 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564
565 /* Tag the variable length parameters. Note that we never
566 * convert the parameters in an INIT chunk.
567 */
568 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
569
570 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
571
572 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
573 SCTP_PEER_INIT(initchunk));
574
Frank Filz3f7a87d2005-06-20 13:14:57 -0700575 /* Reset init error count upon receipt of INIT-ACK. */
576 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* 5.1 C) "A" shall stop the T1-init timer and leave
579 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
580 * timer, and enter the COOKIE-ECHOED state.
581 */
582 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
583 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
584 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
585 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
586 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
587 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
588
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700589 /* SCTP-AUTH: genereate the assocition shared keys so that
590 * we can potentially signe the COOKIE-ECHO.
591 */
592 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* 5.1 C) "A" shall then send the State Cookie received in the
595 * INIT ACK chunk in a COOKIE ECHO chunk, ...
596 */
597 /* If there is any errors to report, send the ERROR chunk generated
598 * for unknown parameters as well.
599 */
600 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
601 SCTP_CHUNK(err_chunk));
602
603 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * Respond to a normal COOKIE ECHO chunk.
608 * We are the side that is being asked for an association.
609 *
610 * Section: 5.1 Normal Establishment of an Association, D
611 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
612 * with a COOKIE ACK chunk after building a TCB and moving to
613 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
614 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
615 * chunk MUST be the first chunk in the packet.
616 *
617 * IMPLEMENTATION NOTE: An implementation may choose to send the
618 * Communication Up notification to the SCTP user upon reception
619 * of a valid COOKIE ECHO chunk.
620 *
621 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
622 * D) Rules for packet carrying a COOKIE ECHO
623 *
624 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
625 * Initial Tag received in the INIT ACK.
626 *
627 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
628 *
629 * Inputs
630 * (endpoint, asoc, chunk)
631 *
632 * Outputs
633 * (asoc, reply_msg, msg_up, timers, counters)
634 *
635 * The return value is the disposition of the chunk.
636 */
637sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
638 const struct sctp_association *asoc,
639 const sctp_subtype_t type, void *arg,
640 sctp_cmd_seq_t *commands)
641{
642 struct sctp_chunk *chunk = arg;
643 struct sctp_association *new_asoc;
644 sctp_init_chunk_t *peer_init;
645 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700646 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int error = 0;
648 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900649 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /* If the packet is an OOTB packet which is temporarily on the
652 * control endpoint, respond with an ABORT.
653 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800654 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
655 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400656 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Make sure that the COOKIE_ECHO chunk has a valid length.
660 * In this case, we check that we have enough for at least a
661 * chunk header. More detailed verification is done
662 * in sctp_unpack_cookie().
663 */
664 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
665 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
666
Vlad Yasevich609ee462007-08-31 03:10:59 +0900667 /* If the endpoint is not listening or if the number of associations
668 * on the TCP-style socket exceed the max backlog, respond with an
669 * ABORT.
670 */
671 sk = ep->base.sk;
672 if (!sctp_sstate(sk, LISTENING) ||
673 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
674 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* "Decode" the chunk. We have no optional parameters so we
677 * are in good shape.
678 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900679 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700681 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
682 sizeof(sctp_chunkhdr_t)))
683 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
686 * "Z" will reply with a COOKIE ACK chunk after building a TCB
687 * and moving to the ESTABLISHED state.
688 */
689 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
690 &err_chk_p);
691
692 /* FIXME:
693 * If the re-build failed, what is the proper error path
694 * from here?
695 *
696 * [We should abort the association. --piggy]
697 */
698 if (!new_asoc) {
699 /* FIXME: Several errors are possible. A bad cookie should
700 * be silently discarded, but think about logging it too.
701 */
702 switch (error) {
703 case -SCTP_IERROR_NOMEM:
704 goto nomem;
705
706 case -SCTP_IERROR_STALE_COOKIE:
707 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
708 err_chk_p);
709 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
710
711 case -SCTP_IERROR_BAD_SIG:
712 default:
713 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700718 /* Delay state machine commands until later.
719 *
720 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 * the sctp_unpack_cookie() already.
722 */
723 /* This is a brand-new association, so these are not yet side
724 * effects--it is safe to run them here.
725 */
726 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
727
728 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
Al Viro6a1e5f32006-11-20 17:12:25 -0800729 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 peer_init, GFP_ATOMIC))
731 goto nomem_init;
732
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700733 /* SCTP-AUTH: Now that we've populate required fields in
734 * sctp_process_init, set up the assocaition shared keys as
735 * necessary so that we can potentially authenticate the ACK
736 */
737 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
738 if (error)
739 goto nomem_init;
740
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700741 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
742 * is supposed to be authenticated and we have to do delayed
743 * authentication. We've just recreated the association using
744 * the information in the cookie and now it's much easier to
745 * do the authentication.
746 */
747 if (chunk->auth_chunk) {
748 struct sctp_chunk auth;
749 sctp_ierror_t ret;
750
751 /* set-up our fake chunk so that we can process it */
752 auth.skb = chunk->auth_chunk;
753 auth.asoc = chunk->asoc;
754 auth.sctp_hdr = chunk->sctp_hdr;
755 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
756 sizeof(sctp_chunkhdr_t));
757 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
758 auth.transport = chunk->transport;
759
760 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
761
762 /* We can now safely free the auth_chunk clone */
763 kfree_skb(chunk->auth_chunk);
764
765 if (ret != SCTP_IERROR_NO_ERROR) {
766 sctp_association_free(new_asoc);
767 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
768 }
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 repl = sctp_make_cookie_ack(new_asoc, chunk);
772 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700773 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 /* RFC 2960 5.1 Normal Establishment of an Association
776 *
777 * D) IMPLEMENTATION NOTE: An implementation may choose to
778 * send the Communication Up notification to the SCTP user
779 * upon reception of a valid COOKIE ECHO chunk.
780 */
781 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
782 new_asoc->c.sinit_num_ostreams,
783 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700784 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!ev)
786 goto nomem_ev;
787
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900788 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800789 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800791 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800793 if (new_asoc->peer.adaptation_ind) {
794 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700796 if (!ai_ev)
797 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700800 /* Add all the state machine commands now since we've created
801 * everything. This way we don't introduce memory corruptions
802 * during side-effect processing and correclty count established
803 * associations.
804 */
805 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
806 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
807 SCTP_STATE(SCTP_STATE_ESTABLISHED));
808 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
809 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
810 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
811
812 if (new_asoc->autoclose)
813 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
814 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
815
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700816 /* This will send the COOKIE ACK */
817 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
818
819 /* Queue the ASSOC_CHANGE event */
820 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
821
822 /* Send up the Adaptation Layer Indication event */
823 if (ai_ev)
824 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
825 SCTP_ULPEVENT(ai_ev));
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return SCTP_DISPOSITION_CONSUME;
828
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700829nomem_aiev:
830 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831nomem_ev:
832 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833nomem_init:
834 sctp_association_free(new_asoc);
835nomem:
836 return SCTP_DISPOSITION_NOMEM;
837}
838
839/*
840 * Respond to a normal COOKIE ACK chunk.
841 * We are the side that is being asked for an association.
842 *
843 * RFC 2960 5.1 Normal Establishment of an Association
844 *
845 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
846 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
847 * timer. It may also notify its ULP about the successful
848 * establishment of the association with a Communication Up
849 * notification (see Section 10).
850 *
851 * Verification Tag:
852 * Inputs
853 * (endpoint, asoc, chunk)
854 *
855 * Outputs
856 * (asoc, reply_msg, msg_up, timers, counters)
857 *
858 * The return value is the disposition of the chunk.
859 */
860sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
861 const struct sctp_association *asoc,
862 const sctp_subtype_t type, void *arg,
863 sctp_cmd_seq_t *commands)
864{
865 struct sctp_chunk *chunk = arg;
866 struct sctp_ulpevent *ev;
867
868 if (!sctp_vtag_verify(chunk, asoc))
869 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
870
871 /* Verify that the chunk length for the COOKIE-ACK is OK.
872 * If we don't do this, any bundled chunks may be junked.
873 */
874 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
875 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
876 commands);
877
878 /* Reset init error count upon receipt of COOKIE-ACK,
879 * to avoid problems with the managemement of this
880 * counter in stale cookie situations when a transition back
881 * from the COOKIE-ECHOED state to the COOKIE-WAIT
882 * state is performed.
883 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700884 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* RFC 2960 5.1 Normal Establishment of an Association
887 *
888 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
889 * from the COOKIE-ECHOED state to the ESTABLISHED state,
890 * stopping the T1-cookie timer.
891 */
892 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
893 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
894 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
895 SCTP_STATE(SCTP_STATE_ESTABLISHED));
896 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
897 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
898 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
899 if (asoc->autoclose)
900 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
901 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /* It may also notify its ULP about the successful
904 * establishment of the association with a Communication Up
905 * notification (see Section 10).
906 */
907 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
908 0, asoc->c.sinit_num_ostreams,
909 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700910 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 if (!ev)
913 goto nomem;
914
915 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
916
917 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800918 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800920 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800922 if (asoc->peer.adaptation_ind) {
923 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!ev)
925 goto nomem;
926
927 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
928 SCTP_ULPEVENT(ev));
929 }
930
931 return SCTP_DISPOSITION_CONSUME;
932nomem:
933 return SCTP_DISPOSITION_NOMEM;
934}
935
936/* Generate and sendout a heartbeat packet. */
937static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
938 const struct sctp_association *asoc,
939 const sctp_subtype_t type,
940 void *arg,
941 sctp_cmd_seq_t *commands)
942{
943 struct sctp_transport *transport = (struct sctp_transport *) arg;
944 struct sctp_chunk *reply;
945 sctp_sender_hb_info_t hbinfo;
946 size_t paylen = 0;
947
948 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
949 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
Al Viro63de08f2006-11-20 17:07:25 -0800950 hbinfo.daddr = transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 hbinfo.sent_at = jiffies;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700952 hbinfo.hb_nonce = transport->hb_nonce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /* Send a heartbeat to our peer. */
955 paylen = sizeof(sctp_sender_hb_info_t);
956 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
957 if (!reply)
958 return SCTP_DISPOSITION_NOMEM;
959
960 /* Set rto_pending indicating that an RTT measurement
961 * is started with this heartbeat chunk.
962 */
963 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
964 SCTP_TRANSPORT(transport));
965
966 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
967 return SCTP_DISPOSITION_CONSUME;
968}
969
970/* Generate a HEARTBEAT packet on the given transport. */
971sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
972 const struct sctp_association *asoc,
973 const sctp_subtype_t type,
974 void *arg,
975 sctp_cmd_seq_t *commands)
976{
977 struct sctp_transport *transport = (struct sctp_transport *) arg;
978
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400979 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700980 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
981 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
983 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800984 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
986 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
987 return SCTP_DISPOSITION_DELETE_TCB;
988 }
989
990 /* Section 3.3.5.
991 * The Sender-specific Heartbeat Info field should normally include
992 * information about the sender's current time when this HEARTBEAT
993 * chunk is sent and the destination transport address to which this
994 * HEARTBEAT is sent (see Section 8.3).
995 */
996
Frank Filz52ccb8e2005-12-22 11:36:46 -0800997 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (SCTP_DISPOSITION_NOMEM ==
999 sctp_sf_heartbeat(ep, asoc, type, arg,
1000 commands))
1001 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevich245cba72009-11-23 15:53:58 -05001002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* Set transport error counter and association error counter
1004 * when sending heartbeat.
1005 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001006 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 SCTP_TRANSPORT(transport));
1008 }
Vlad Yasevich245cba72009-11-23 15:53:58 -05001009 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1010 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1012 SCTP_TRANSPORT(transport));
1013
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001014 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017/*
1018 * Process an heartbeat request.
1019 *
1020 * Section: 8.3 Path Heartbeat
1021 * The receiver of the HEARTBEAT should immediately respond with a
1022 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1023 * from the received HEARTBEAT chunk.
1024 *
1025 * Verification Tag: 8.5 Verification Tag [Normal verification]
1026 * When receiving an SCTP packet, the endpoint MUST ensure that the
1027 * value in the Verification Tag field of the received SCTP packet
1028 * matches its own Tag. If the received Verification Tag value does not
1029 * match the receiver's own tag value, the receiver shall silently
1030 * discard the packet and shall not process it any further except for
1031 * those cases listed in Section 8.5.1 below.
1032 *
1033 * Inputs
1034 * (endpoint, asoc, chunk)
1035 *
1036 * Outputs
1037 * (asoc, reply_msg, msg_up, timers, counters)
1038 *
1039 * The return value is the disposition of the chunk.
1040 */
1041sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1042 const struct sctp_association *asoc,
1043 const sctp_subtype_t type,
1044 void *arg,
1045 sctp_cmd_seq_t *commands)
1046{
1047 struct sctp_chunk *chunk = arg;
1048 struct sctp_chunk *reply;
1049 size_t paylen = 0;
1050
1051 if (!sctp_vtag_verify(chunk, asoc))
1052 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1053
1054 /* Make sure that the HEARTBEAT chunk has a valid length. */
1055 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1056 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1057 commands);
1058
1059 /* 8.3 The receiver of the HEARTBEAT should immediately
1060 * respond with a HEARTBEAT ACK that contains the Heartbeat
1061 * Information field copied from the received HEARTBEAT chunk.
1062 */
1063 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1064 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001065 if (!pskb_pull(chunk->skb, paylen))
1066 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 reply = sctp_make_heartbeat_ack(asoc, chunk,
1069 chunk->subh.hb_hdr, paylen);
1070 if (!reply)
1071 goto nomem;
1072
1073 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1074 return SCTP_DISPOSITION_CONSUME;
1075
1076nomem:
1077 return SCTP_DISPOSITION_NOMEM;
1078}
1079
1080/*
1081 * Process the returning HEARTBEAT ACK.
1082 *
1083 * Section: 8.3 Path Heartbeat
1084 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1085 * should clear the error counter of the destination transport
1086 * address to which the HEARTBEAT was sent, and mark the destination
1087 * transport address as active if it is not so marked. The endpoint may
1088 * optionally report to the upper layer when an inactive destination
1089 * address is marked as active due to the reception of the latest
1090 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1091 * clear the association overall error count as well (as defined
1092 * in section 8.1).
1093 *
1094 * The receiver of the HEARTBEAT ACK should also perform an RTT
1095 * measurement for that destination transport address using the time
1096 * value carried in the HEARTBEAT ACK chunk.
1097 *
1098 * Verification Tag: 8.5 Verification Tag [Normal verification]
1099 *
1100 * Inputs
1101 * (endpoint, asoc, chunk)
1102 *
1103 * Outputs
1104 * (asoc, reply_msg, msg_up, timers, counters)
1105 *
1106 * The return value is the disposition of the chunk.
1107 */
1108sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1109 const struct sctp_association *asoc,
1110 const sctp_subtype_t type,
1111 void *arg,
1112 sctp_cmd_seq_t *commands)
1113{
1114 struct sctp_chunk *chunk = arg;
1115 union sctp_addr from_addr;
1116 struct sctp_transport *link;
1117 sctp_sender_hb_info_t *hbinfo;
1118 unsigned long max_interval;
1119
1120 if (!sctp_vtag_verify(chunk, asoc))
1121 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1122
1123 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001124 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1125 sizeof(sctp_sender_hb_info_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1127 commands);
1128
1129 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001130 /* Make sure that the length of the parameter is what we expect */
1131 if (ntohs(hbinfo->param_hdr.length) !=
1132 sizeof(sctp_sender_hb_info_t)) {
1133 return SCTP_DISPOSITION_DISCARD;
1134 }
1135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001137 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001140 if (unlikely(!link)) {
1141 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001142 if (net_ratelimit())
Joe Perches145ce502010-08-24 13:21:08 +00001143 pr_warn("%s association %p could not find address %pI6\n",
1144 __func__,
1145 asoc,
1146 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001147 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001148 if (net_ratelimit())
Joe Perches145ce502010-08-24 13:21:08 +00001149 pr_warn("%s association %p could not find address %pI4\n",
1150 __func__,
1151 asoc,
1152 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return SCTP_DISPOSITION_DISCARD;
1155 }
1156
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001157 /* Validate the 64-bit random nonce. */
1158 if (hbinfo->hb_nonce != link->hb_nonce)
1159 return SCTP_DISPOSITION_DISCARD;
1160
Frank Filz52ccb8e2005-12-22 11:36:46 -08001161 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /* Check if the timestamp looks valid. */
1164 if (time_after(hbinfo->sent_at, jiffies) ||
1165 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001166 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001168 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return SCTP_DISPOSITION_DISCARD;
1170 }
1171
1172 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1173 * the HEARTBEAT should clear the error counter of the
1174 * destination transport address to which the HEARTBEAT was
1175 * sent and mark the destination transport address as active if
1176 * it is not so marked.
1177 */
1178 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1179
1180 return SCTP_DISPOSITION_CONSUME;
1181}
1182
1183/* Helper function to send out an abort for the restart
1184 * condition.
1185 */
1186static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1187 struct sctp_chunk *init,
1188 sctp_cmd_seq_t *commands)
1189{
1190 int len;
1191 struct sctp_packet *pkt;
1192 union sctp_addr_param *addrparm;
1193 struct sctp_errhdr *errhdr;
1194 struct sctp_endpoint *ep;
1195 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1196 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1197
1198 /* Build the error on the stack. We are way to malloc crazy
1199 * throughout the code today.
1200 */
1201 errhdr = (struct sctp_errhdr *)buffer;
1202 addrparm = (union sctp_addr_param *)errhdr->variable;
1203
1204 /* Copy into a parm format. */
1205 len = af->to_addr_param(ssa, addrparm);
1206 len += sizeof(sctp_errhdr_t);
1207
1208 errhdr->cause = SCTP_ERROR_RESTART;
1209 errhdr->length = htons(len);
1210
1211 /* Assign to the control socket. */
1212 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1213
1214 /* Association is NULL since this may be a restart attack and we
1215 * want to send back the attacker's vtag.
1216 */
1217 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1218
1219 if (!pkt)
1220 goto out;
1221 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1222
1223 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1224
1225 /* Discard the rest of the inbound packet. */
1226 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1227
1228out:
1229 /* Even if there is no memory, treat as a failure so
1230 * the packet will get dropped.
1231 */
1232 return 0;
1233}
1234
Joe Perches123031c2010-09-08 11:04:21 +00001235static bool list_has_sctp_addr(const struct list_head *list,
1236 union sctp_addr *ipaddr)
1237{
1238 struct sctp_transport *addr;
1239
1240 list_for_each_entry(addr, list, transports) {
1241 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1242 return true;
1243 }
1244
1245 return false;
1246}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247/* A restart is occurring, check to make sure no new addresses
1248 * are being added as we may be under a takeover attack.
1249 */
1250static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1251 const struct sctp_association *asoc,
1252 struct sctp_chunk *init,
1253 sctp_cmd_seq_t *commands)
1254{
Joe Perches123031c2010-09-08 11:04:21 +00001255 struct sctp_transport *new_addr;
1256 int ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Joe Perches123031c2010-09-08 11:04:21 +00001258 /* Implementor's Guide - Section 5.2.2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * ...
1260 * Before responding the endpoint MUST check to see if the
1261 * unexpected INIT adds new addresses to the association. If new
1262 * addresses are added to the association, the endpoint MUST respond
1263 * with an ABORT..
1264 */
1265
1266 /* Search through all current addresses and make sure
1267 * we aren't adding any new ones.
1268 */
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001269 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
Joe Perches123031c2010-09-08 11:04:21 +00001270 transports) {
1271 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1272 &new_addr->ipaddr)) {
1273 sctp_sf_send_restart_abort(&new_addr->ipaddr, init,
1274 commands);
1275 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 break;
Joe Perches123031c2010-09-08 11:04:21 +00001277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 }
1279
1280 /* Return success if all addresses were found. */
Joe Perches123031c2010-09-08 11:04:21 +00001281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284/* Populate the verification/tie tags based on overlapping INIT
1285 * scenario.
1286 *
1287 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1288 */
1289static void sctp_tietags_populate(struct sctp_association *new_asoc,
1290 const struct sctp_association *asoc)
1291{
1292 switch (asoc->state) {
1293
1294 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1295
1296 case SCTP_STATE_COOKIE_WAIT:
1297 new_asoc->c.my_vtag = asoc->c.my_vtag;
1298 new_asoc->c.my_ttag = asoc->c.my_vtag;
1299 new_asoc->c.peer_ttag = 0;
1300 break;
1301
1302 case SCTP_STATE_COOKIE_ECHOED:
1303 new_asoc->c.my_vtag = asoc->c.my_vtag;
1304 new_asoc->c.my_ttag = asoc->c.my_vtag;
1305 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1306 break;
1307
1308 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1309 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1310 */
1311 default:
1312 new_asoc->c.my_ttag = asoc->c.my_vtag;
1313 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1314 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 /* Other parameters for the endpoint SHOULD be copied from the
1318 * existing parameters of the association (e.g. number of
1319 * outbound streams) into the INIT ACK and cookie.
1320 */
1321 new_asoc->rwnd = asoc->rwnd;
1322 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1323 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1324 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1325}
1326
1327/*
1328 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1329 * handling action.
1330 *
1331 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1332 *
1333 * Returns value representing action to be taken. These action values
1334 * correspond to Action/Description values in RFC 2960, Table 2.
1335 */
1336static char sctp_tietags_compare(struct sctp_association *new_asoc,
1337 const struct sctp_association *asoc)
1338{
1339 /* In this case, the peer may have restarted. */
1340 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1341 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1342 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1343 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1344 return 'A';
1345
1346 /* Collision case B. */
1347 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1348 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1349 (0 == asoc->c.peer_vtag))) {
1350 return 'B';
1351 }
1352
1353 /* Collision case D. */
1354 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1355 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1356 return 'D';
1357
1358 /* Collision case C. */
1359 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1360 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1361 (0 == new_asoc->c.my_ttag) &&
1362 (0 == new_asoc->c.peer_ttag))
1363 return 'C';
1364
1365 /* No match to any of the special cases; discard this packet. */
1366 return 'E';
1367}
1368
1369/* Common helper routine for both duplicate and simulataneous INIT
1370 * chunk handling.
1371 */
1372static sctp_disposition_t sctp_sf_do_unexpected_init(
1373 const struct sctp_endpoint *ep,
1374 const struct sctp_association *asoc,
1375 const sctp_subtype_t type,
1376 void *arg, sctp_cmd_seq_t *commands)
1377{
1378 sctp_disposition_t retval;
1379 struct sctp_chunk *chunk = arg;
1380 struct sctp_chunk *repl;
1381 struct sctp_association *new_asoc;
1382 struct sctp_chunk *err_chunk;
1383 struct sctp_packet *packet;
1384 sctp_unrecognized_param_t *unk_param;
1385 int len;
1386
1387 /* 6.10 Bundling
1388 * An endpoint MUST NOT bundle INIT, INIT ACK or
1389 * SHUTDOWN COMPLETE with any other chunks.
1390 *
1391 * IG Section 2.11.2
1392 * Furthermore, we require that the receiver of an INIT chunk MUST
1393 * enforce these rules by silently discarding an arriving packet
1394 * with an INIT chunk that is bundled with other chunks.
1395 */
1396 if (!chunk->singleton)
1397 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1398
1399 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001400 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 */
1402 if (chunk->sctp_hdr->vtag != 0)
1403 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1404
1405 /* Make sure that the INIT chunk has a valid length.
1406 * In this case, we generate a protocol violation since we have
1407 * an association established.
1408 */
1409 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1410 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1411 commands);
1412 /* Grab the INIT header. */
1413 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1414
1415 /* Tag the variable length parameters. */
1416 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1417
1418 /* Verify the INIT chunk before processing it. */
1419 err_chunk = NULL;
1420 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1421 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1422 &err_chunk)) {
1423 /* This chunk contains fatal error. It is to be discarded.
1424 * Send an ABORT, with causes if there is any.
1425 */
1426 if (err_chunk) {
1427 packet = sctp_abort_pkt_new(ep, asoc, arg,
1428 (__u8 *)(err_chunk->chunk_hdr) +
1429 sizeof(sctp_chunkhdr_t),
1430 ntohs(err_chunk->chunk_hdr->length) -
1431 sizeof(sctp_chunkhdr_t));
1432
1433 if (packet) {
1434 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1435 SCTP_PACKET(packet));
1436 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1437 retval = SCTP_DISPOSITION_CONSUME;
1438 } else {
1439 retval = SCTP_DISPOSITION_NOMEM;
1440 }
1441 goto cleanup;
1442 } else {
1443 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1444 commands);
1445 }
1446 }
1447
1448 /*
1449 * Other parameters for the endpoint SHOULD be copied from the
1450 * existing parameters of the association (e.g. number of
1451 * outbound streams) into the INIT ACK and cookie.
1452 * FIXME: We are copying parameters from the endpoint not the
1453 * association.
1454 */
1455 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1456 if (!new_asoc)
1457 goto nomem;
1458
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001459 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1460 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1461 goto nomem;
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 /* In the outbound INIT ACK the endpoint MUST copy its current
1464 * Verification Tag and Peers Verification tag into a reserved
1465 * place (local tie-tag and per tie-tag) within the state cookie.
1466 */
1467 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1468 sctp_source(chunk),
1469 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001470 GFP_ATOMIC))
1471 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* Make sure no new addresses are being added during the
1474 * restart. Do not do this check for COOKIE-WAIT state,
1475 * since there are no peer addresses to check against.
1476 * Upon return an ABORT will have been sent if needed.
1477 */
1478 if (!sctp_state(asoc, COOKIE_WAIT)) {
1479 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1480 commands)) {
1481 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001482 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484 }
1485
1486 sctp_tietags_populate(new_asoc, asoc);
1487
1488 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1489
1490 /* If there are errors need to be reported for unknown parameters,
1491 * make sure to reserve enough room in the INIT ACK for them.
1492 */
1493 len = 0;
1494 if (err_chunk) {
1495 len = ntohs(err_chunk->chunk_hdr->length) -
1496 sizeof(sctp_chunkhdr_t);
1497 }
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1500 if (!repl)
1501 goto nomem;
1502
1503 /* If there are errors need to be reported for unknown parameters,
1504 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1505 * parameter.
1506 */
1507 if (err_chunk) {
1508 /* Get the "Unrecognized parameter" parameter(s) out of the
1509 * ERROR chunk generated by sctp_verify_init(). Since the
1510 * error cause code for "unknown parameter" and the
1511 * "Unrecognized parameter" type is the same, we can
1512 * construct the parameters in INIT ACK by copying the
1513 * ERROR causes over.
1514 */
1515 unk_param = (sctp_unrecognized_param_t *)
1516 ((__u8 *)(err_chunk->chunk_hdr) +
1517 sizeof(sctp_chunkhdr_t));
1518 /* Replace the cause code with the "Unrecognized parameter"
1519 * parameter type.
1520 */
1521 sctp_addto_chunk(repl, len, unk_param);
1522 }
1523
1524 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1525 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1526
1527 /*
1528 * Note: After sending out INIT ACK with the State Cookie parameter,
1529 * "Z" MUST NOT allocate any resources for this new association.
1530 * Otherwise, "Z" will be vulnerable to resource attacks.
1531 */
1532 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1533 retval = SCTP_DISPOSITION_CONSUME;
1534
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001535 return retval;
1536
1537nomem:
1538 retval = SCTP_DISPOSITION_NOMEM;
1539nomem_retval:
1540 if (new_asoc)
1541 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542cleanup:
1543 if (err_chunk)
1544 sctp_chunk_free(err_chunk);
1545 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001549 * Handle simultaneous INIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 * This means we started an INIT and then we got an INIT request from
1551 * our peer.
1552 *
1553 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1554 * This usually indicates an initialization collision, i.e., each
1555 * endpoint is attempting, at about the same time, to establish an
1556 * association with the other endpoint.
1557 *
1558 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1559 * endpoint MUST respond with an INIT ACK using the same parameters it
1560 * sent in its original INIT chunk (including its Verification Tag,
1561 * unchanged). These original parameters are combined with those from the
1562 * newly received INIT chunk. The endpoint shall also generate a State
1563 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1564 * INIT to calculate the State Cookie.
1565 *
1566 * After that, the endpoint MUST NOT change its state, the T1-init
1567 * timer shall be left running and the corresponding TCB MUST NOT be
1568 * destroyed. The normal procedures for handling State Cookies when
1569 * a TCB exists will resolve the duplicate INITs to a single association.
1570 *
1571 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1572 * its Tie-Tags with the Tag information of itself and its peer (see
1573 * section 5.2.2 for a description of the Tie-Tags).
1574 *
1575 * Verification Tag: Not explicit, but an INIT can not have a valid
1576 * verification tag, so we skip the check.
1577 *
1578 * Inputs
1579 * (endpoint, asoc, chunk)
1580 *
1581 * Outputs
1582 * (asoc, reply_msg, msg_up, timers, counters)
1583 *
1584 * The return value is the disposition of the chunk.
1585 */
1586sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1587 const struct sctp_association *asoc,
1588 const sctp_subtype_t type,
1589 void *arg,
1590 sctp_cmd_seq_t *commands)
1591{
1592 /* Call helper to do the real work for both simulataneous and
1593 * duplicate INIT chunk handling.
1594 */
1595 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1596}
1597
1598/*
1599 * Handle duplicated INIT messages. These are usually delayed
1600 * restransmissions.
1601 *
1602 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1603 * COOKIE-ECHOED and COOKIE-WAIT
1604 *
1605 * Unless otherwise stated, upon reception of an unexpected INIT for
1606 * this association, the endpoint shall generate an INIT ACK with a
1607 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1608 * current Verification Tag and peer's Verification Tag into a reserved
1609 * place within the state cookie. We shall refer to these locations as
1610 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1611 * containing this INIT ACK MUST carry a Verification Tag value equal to
1612 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1613 * MUST contain a new Initiation Tag (randomly generated see Section
1614 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1615 * existing parameters of the association (e.g. number of outbound
1616 * streams) into the INIT ACK and cookie.
1617 *
1618 * After sending out the INIT ACK, the endpoint shall take no further
1619 * actions, i.e., the existing association, including its current state,
1620 * and the corresponding TCB MUST NOT be changed.
1621 *
1622 * Note: Only when a TCB exists and the association is not in a COOKIE-
1623 * WAIT state are the Tie-Tags populated. For a normal association INIT
1624 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1625 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1626 * State Cookie are populated as specified in section 5.2.1.
1627 *
1628 * Verification Tag: Not specified, but an INIT has no way of knowing
1629 * what the verification tag could be, so we ignore it.
1630 *
1631 * Inputs
1632 * (endpoint, asoc, chunk)
1633 *
1634 * Outputs
1635 * (asoc, reply_msg, msg_up, timers, counters)
1636 *
1637 * The return value is the disposition of the chunk.
1638 */
1639sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1640 const struct sctp_association *asoc,
1641 const sctp_subtype_t type,
1642 void *arg,
1643 sctp_cmd_seq_t *commands)
1644{
1645 /* Call helper to do the real work for both simulataneous and
1646 * duplicate INIT chunk handling.
1647 */
1648 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1649}
1650
1651
Vlad Yasevich610ab732007-01-15 19:18:30 -08001652/*
1653 * Unexpected INIT-ACK handler.
1654 *
1655 * Section 5.2.3
1656 * If an INIT ACK received by an endpoint in any state other than the
1657 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1658 * An unexpected INIT ACK usually indicates the processing of an old or
1659 * duplicated INIT chunk.
1660*/
1661sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1662 const struct sctp_association *asoc,
1663 const sctp_subtype_t type,
1664 void *arg, sctp_cmd_seq_t *commands)
1665{
1666 /* Per the above section, we'll discard the chunk if we have an
1667 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1668 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001669 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001670 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1671 else
1672 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1676 *
1677 * Section 5.2.4
1678 * A) In this case, the peer may have restarted.
1679 */
1680static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1681 const struct sctp_association *asoc,
1682 struct sctp_chunk *chunk,
1683 sctp_cmd_seq_t *commands,
1684 struct sctp_association *new_asoc)
1685{
1686 sctp_init_chunk_t *peer_init;
1687 struct sctp_ulpevent *ev;
1688 struct sctp_chunk *repl;
1689 struct sctp_chunk *err;
1690 sctp_disposition_t disposition;
1691
1692 /* new_asoc is a brand-new association, so these are not yet
1693 * side effects--it is safe to run them here.
1694 */
1695 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1696
1697 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1698 sctp_source(chunk), peer_init,
1699 GFP_ATOMIC))
1700 goto nomem;
1701
1702 /* Make sure no new addresses are being added during the
1703 * restart. Though this is a pretty complicated attack
1704 * since you'd have to get inside the cookie.
1705 */
1706 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1707 return SCTP_DISPOSITION_CONSUME;
1708 }
1709
1710 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1711 * the peer has restarted (Action A), it MUST NOT setup a new
1712 * association but instead resend the SHUTDOWN ACK and send an ERROR
1713 * chunk with a "Cookie Received while Shutting Down" error cause to
1714 * its peer.
1715 */
1716 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1717 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1718 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1719 chunk, commands);
1720 if (SCTP_DISPOSITION_NOMEM == disposition)
1721 goto nomem;
1722
1723 err = sctp_make_op_error(asoc, chunk,
1724 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05001725 NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (err)
1727 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1728 SCTP_CHUNK(err));
1729
1730 return SCTP_DISPOSITION_CONSUME;
1731 }
1732
1733 /* For now, fail any unsent/unacked data. Consider the optional
1734 * choice of resending of this data.
1735 */
1736 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 repl = sctp_make_cookie_ack(new_asoc, chunk);
1739 if (!repl)
1740 goto nomem;
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 /* Report association restart to upper layer. */
1743 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1744 new_asoc->c.sinit_num_ostreams,
1745 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001746 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (!ev)
1748 goto nomem_ev;
1749
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001750 /* Update the content of current association. */
1751 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1752 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1754 return SCTP_DISPOSITION_CONSUME;
1755
1756nomem_ev:
1757 sctp_chunk_free(repl);
1758nomem:
1759 return SCTP_DISPOSITION_NOMEM;
1760}
1761
1762/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1763 *
1764 * Section 5.2.4
1765 * B) In this case, both sides may be attempting to start an association
1766 * at about the same time but the peer endpoint started its INIT
1767 * after responding to the local endpoint's INIT
1768 */
1769/* This case represents an initialization collision. */
1770static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1771 const struct sctp_association *asoc,
1772 struct sctp_chunk *chunk,
1773 sctp_cmd_seq_t *commands,
1774 struct sctp_association *new_asoc)
1775{
1776 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 struct sctp_chunk *repl;
1778
1779 /* new_asoc is a brand-new association, so these are not yet
1780 * side effects--it is safe to run them here.
1781 */
1782 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1783 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1784 sctp_source(chunk), peer_init,
1785 GFP_ATOMIC))
1786 goto nomem;
1787
1788 /* Update the content of current association. */
1789 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1790 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1791 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1792 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1793 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1794
1795 repl = sctp_make_cookie_ack(new_asoc, chunk);
1796 if (!repl)
1797 goto nomem;
1798
1799 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 /* RFC 2960 5.1 Normal Establishment of an Association
1802 *
1803 * D) IMPLEMENTATION NOTE: An implementation may choose to
1804 * send the Communication Up notification to the SCTP user
1805 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001806 *
1807 * Sadly, this needs to be implemented as a side-effect, because
1808 * we are not guaranteed to have set the association id of the real
1809 * association and so these notifications need to be delayed until
1810 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Vlad Yasevich07d93962007-05-04 13:55:27 -07001813 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001816 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001818 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001819 *
1820 * This also needs to be done as a side effect for the same reason as
1821 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001823 if (asoc->peer.adaptation_ind)
1824 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 return SCTP_DISPOSITION_CONSUME;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828nomem:
1829 return SCTP_DISPOSITION_NOMEM;
1830}
1831
1832/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1833 *
1834 * Section 5.2.4
1835 * C) In this case, the local endpoint's cookie has arrived late.
1836 * Before it arrived, the local endpoint sent an INIT and received an
1837 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1838 * but a new tag of its own.
1839 */
1840/* This case represents an initialization collision. */
1841static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1842 const struct sctp_association *asoc,
1843 struct sctp_chunk *chunk,
1844 sctp_cmd_seq_t *commands,
1845 struct sctp_association *new_asoc)
1846{
1847 /* The cookie should be silently discarded.
1848 * The endpoint SHOULD NOT change states and should leave
1849 * any timers running.
1850 */
1851 return SCTP_DISPOSITION_DISCARD;
1852}
1853
1854/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1855 *
1856 * Section 5.2.4
1857 *
1858 * D) When both local and remote tags match the endpoint should always
1859 * enter the ESTABLISHED state, if it has not already done so.
1860 */
1861/* This case represents an initialization collision. */
1862static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1863 const struct sctp_association *asoc,
1864 struct sctp_chunk *chunk,
1865 sctp_cmd_seq_t *commands,
1866 struct sctp_association *new_asoc)
1867{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001868 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 struct sctp_chunk *repl;
1870
1871 /* Clarification from Implementor's Guide:
1872 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001873 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1874 * It should stop any cookie timer that may be running and send
1875 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 */
1877
1878 /* Don't accidentally move back into established state. */
1879 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1880 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1881 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1882 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1883 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1884 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1885 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1886 SCTP_NULL());
1887
1888 /* RFC 2960 5.1 Normal Establishment of an Association
1889 *
1890 * D) IMPLEMENTATION NOTE: An implementation may choose
1891 * to send the Communication Up notification to the
1892 * SCTP user upon reception of a valid COOKIE
1893 * ECHO chunk.
1894 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001895 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001897 asoc->c.sinit_num_ostreams,
1898 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001899 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (!ev)
1901 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001904 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001906 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001908 if (asoc->peer.adaptation_ind) {
1909 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001911 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto nomem;
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 repl = sctp_make_cookie_ack(new_asoc, chunk);
1918 if (!repl)
1919 goto nomem;
1920
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001921 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1922
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001923 if (ev)
1924 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1925 SCTP_ULPEVENT(ev));
1926 if (ai_ev)
1927 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1928 SCTP_ULPEVENT(ai_ev));
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 return SCTP_DISPOSITION_CONSUME;
1931
1932nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001933 if (ai_ev)
1934 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (ev)
1936 sctp_ulpevent_free(ev);
1937 return SCTP_DISPOSITION_NOMEM;
1938}
1939
1940/*
1941 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1942 * chunk was retransmitted and then delayed in the network.
1943 *
1944 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1945 *
1946 * Verification Tag: None. Do cookie validation.
1947 *
1948 * Inputs
1949 * (endpoint, asoc, chunk)
1950 *
1951 * Outputs
1952 * (asoc, reply_msg, msg_up, timers, counters)
1953 *
1954 * The return value is the disposition of the chunk.
1955 */
1956sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1957 const struct sctp_association *asoc,
1958 const sctp_subtype_t type,
1959 void *arg,
1960 sctp_cmd_seq_t *commands)
1961{
1962 sctp_disposition_t retval;
1963 struct sctp_chunk *chunk = arg;
1964 struct sctp_association *new_asoc;
1965 int error = 0;
1966 char action;
1967 struct sctp_chunk *err_chk_p;
1968
1969 /* Make sure that the chunk has a valid length from the protocol
1970 * perspective. In this case check to make sure we have at least
1971 * enough for the chunk header. Cookie length verification is
1972 * done later.
1973 */
1974 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1975 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1976 commands);
1977
1978 /* "Decode" the chunk. We have no optional parameters so we
1979 * are in good shape.
1980 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001981 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001982 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1983 sizeof(sctp_chunkhdr_t)))
1984 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1987 * of a duplicate COOKIE ECHO match the Verification Tags of the
1988 * current association, consider the State Cookie valid even if
1989 * the lifespan is exceeded.
1990 */
1991 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1992 &err_chk_p);
1993
1994 /* FIXME:
1995 * If the re-build failed, what is the proper error path
1996 * from here?
1997 *
1998 * [We should abort the association. --piggy]
1999 */
2000 if (!new_asoc) {
2001 /* FIXME: Several errors are possible. A bad cookie should
2002 * be silently discarded, but think about logging it too.
2003 */
2004 switch (error) {
2005 case -SCTP_IERROR_NOMEM:
2006 goto nomem;
2007
2008 case -SCTP_IERROR_STALE_COOKIE:
2009 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2010 err_chk_p);
2011 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2012 case -SCTP_IERROR_BAD_SIG:
2013 default:
2014 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
2018 /* Compare the tie_tag in cookie with the verification tag of
2019 * current association.
2020 */
2021 action = sctp_tietags_compare(new_asoc, asoc);
2022
2023 switch (action) {
2024 case 'A': /* Association restart. */
2025 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2026 new_asoc);
2027 break;
2028
2029 case 'B': /* Collision case B. */
2030 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2031 new_asoc);
2032 break;
2033
2034 case 'C': /* Collision case C. */
2035 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2036 new_asoc);
2037 break;
2038
2039 case 'D': /* Collision case D. */
2040 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2041 new_asoc);
2042 break;
2043
2044 default: /* Discard packet for all others. */
2045 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2046 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002047 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 /* Delete the tempory new association. */
2050 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2051 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2052
2053 return retval;
2054
2055nomem:
2056 return SCTP_DISPOSITION_NOMEM;
2057}
2058
2059/*
2060 * Process an ABORT. (SHUTDOWN-PENDING state)
2061 *
2062 * See sctp_sf_do_9_1_abort().
2063 */
2064sctp_disposition_t sctp_sf_shutdown_pending_abort(
2065 const struct sctp_endpoint *ep,
2066 const struct sctp_association *asoc,
2067 const sctp_subtype_t type,
2068 void *arg,
2069 sctp_cmd_seq_t *commands)
2070{
2071 struct sctp_chunk *chunk = arg;
2072
2073 if (!sctp_vtag_verify_either(chunk, asoc))
2074 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2075
2076 /* Make sure that the ABORT chunk has a valid length.
2077 * Since this is an ABORT chunk, we have to discard it
2078 * because of the following text:
2079 * RFC 2960, Section 3.3.7
2080 * If an endpoint receives an ABORT with a format error or for an
2081 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002082 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 * as we do not know its true length. So, to be safe, discard the
2084 * packet.
2085 */
2086 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2087 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2088
Vlad Yasevich75205f42007-12-20 14:12:59 -08002089 /* ADD-IP: Special case for ABORT chunks
2090 * F4) One special consideration is that ABORT Chunks arriving
2091 * destined to the IP address being deleted MUST be
2092 * ignored (see Section 5.3.1 for further details).
2093 */
2094 if (SCTP_ADDR_DEL ==
2095 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2096 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2097
Vlad Yasevich75205f42007-12-20 14:12:59 -08002098 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
2101/*
2102 * Process an ABORT. (SHUTDOWN-SENT state)
2103 *
2104 * See sctp_sf_do_9_1_abort().
2105 */
2106sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2107 const struct sctp_association *asoc,
2108 const sctp_subtype_t type,
2109 void *arg,
2110 sctp_cmd_seq_t *commands)
2111{
2112 struct sctp_chunk *chunk = arg;
2113
2114 if (!sctp_vtag_verify_either(chunk, asoc))
2115 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2116
2117 /* Make sure that the ABORT chunk has a valid length.
2118 * Since this is an ABORT chunk, we have to discard it
2119 * because of the following text:
2120 * RFC 2960, Section 3.3.7
2121 * If an endpoint receives an ABORT with a format error or for an
2122 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002123 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 * as we do not know its true length. So, to be safe, discard the
2125 * packet.
2126 */
2127 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2128 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2129
Vlad Yasevich75205f42007-12-20 14:12:59 -08002130 /* ADD-IP: Special case for ABORT chunks
2131 * F4) One special consideration is that ABORT Chunks arriving
2132 * destined to the IP address being deleted MUST be
2133 * ignored (see Section 5.3.1 for further details).
2134 */
2135 if (SCTP_ADDR_DEL ==
2136 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2137 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /* Stop the T2-shutdown timer. */
2140 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2141 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2142
2143 /* Stop the T5-shutdown guard timer. */
2144 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2145 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2146
Vlad Yasevich75205f42007-12-20 14:12:59 -08002147 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/*
2151 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2152 *
2153 * See sctp_sf_do_9_1_abort().
2154 */
2155sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2156 const struct sctp_endpoint *ep,
2157 const struct sctp_association *asoc,
2158 const sctp_subtype_t type,
2159 void *arg,
2160 sctp_cmd_seq_t *commands)
2161{
2162 /* The same T2 timer, so we should be able to use
2163 * common function with the SHUTDOWN-SENT state.
2164 */
2165 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2166}
2167
2168/*
2169 * Handle an Error received in COOKIE_ECHOED state.
2170 *
2171 * Only handle the error type of stale COOKIE Error, the other errors will
2172 * be ignored.
2173 *
2174 * Inputs
2175 * (endpoint, asoc, chunk)
2176 *
2177 * Outputs
2178 * (asoc, reply_msg, msg_up, timers, counters)
2179 *
2180 * The return value is the disposition of the chunk.
2181 */
2182sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2183 const struct sctp_association *asoc,
2184 const sctp_subtype_t type,
2185 void *arg,
2186 sctp_cmd_seq_t *commands)
2187{
2188 struct sctp_chunk *chunk = arg;
2189 sctp_errhdr_t *err;
2190
2191 if (!sctp_vtag_verify(chunk, asoc))
2192 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2193
2194 /* Make sure that the ERROR chunk has a valid length.
2195 * The parameter walking depends on this as well.
2196 */
2197 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2198 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2199 commands);
2200
2201 /* Process the error here */
2202 /* FUTURE FIXME: When PR-SCTP related and other optional
2203 * parms are emitted, this will have to change to handle multiple
2204 * errors.
2205 */
2206 sctp_walk_errors(err, chunk->chunk_hdr) {
2207 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002208 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 arg, commands);
2210 }
2211
2212 /* It is possible to have malformed error causes, and that
2213 * will cause us to end the walk early. However, since
2214 * we are discarding the packet, there should be no adverse
2215 * affects.
2216 */
2217 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2218}
2219
2220/*
2221 * Handle a Stale COOKIE Error
2222 *
2223 * Section: 5.2.6 Handle Stale COOKIE Error
2224 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2225 * one of the following three alternatives.
2226 * ...
2227 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2228 * Preservative parameter requesting an extension to the lifetime of
2229 * the State Cookie. When calculating the time extension, an
2230 * implementation SHOULD use the RTT information measured based on the
2231 * previous COOKIE ECHO / ERROR exchange, and should add no more
2232 * than 1 second beyond the measured RTT, due to long State Cookie
2233 * lifetimes making the endpoint more subject to a replay attack.
2234 *
2235 * Verification Tag: Not explicit, but safe to ignore.
2236 *
2237 * Inputs
2238 * (endpoint, asoc, chunk)
2239 *
2240 * Outputs
2241 * (asoc, reply_msg, msg_up, timers, counters)
2242 *
2243 * The return value is the disposition of the chunk.
2244 */
2245static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2246 const struct sctp_association *asoc,
2247 const sctp_subtype_t type,
2248 void *arg,
2249 sctp_cmd_seq_t *commands)
2250{
2251 struct sctp_chunk *chunk = arg;
2252 time_t stale;
2253 sctp_cookie_preserve_param_t bht;
2254 sctp_errhdr_t *err;
2255 struct sctp_chunk *reply;
2256 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002257 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Vlad Yasevich81845c22006-01-30 15:59:54 -08002259 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002260 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2261 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002263 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return SCTP_DISPOSITION_DELETE_TCB;
2265 }
2266
2267 err = (sctp_errhdr_t *)(chunk->skb->data);
2268
2269 /* When calculating the time extension, an implementation
2270 * SHOULD use the RTT information measured based on the
2271 * previous COOKIE ECHO / ERROR exchange, and should add no
2272 * more than 1 second beyond the measured RTT, due to long
2273 * State Cookie lifetimes making the endpoint more subject to
2274 * a replay attack.
2275 * Measure of Staleness's unit is usec. (1/1000000 sec)
2276 * Suggested Cookie Life-span Increment's unit is msec.
2277 * (1/1000 sec)
2278 * In general, if you use the suggested cookie life, the value
2279 * found in the field of measure of staleness should be doubled
2280 * to give ample time to retransmit the new cookie and thus
2281 * yield a higher probability of success on the reattempt.
2282 */
Al Viro34bcca22006-11-20 17:27:15 -08002283 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 stale = (stale * 2) / 1000;
2285
2286 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2287 bht.param_hdr.length = htons(sizeof(bht));
2288 bht.lifespan_increment = htonl(stale);
2289
2290 /* Build that new INIT chunk. */
2291 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2292 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2293 if (!reply)
2294 goto nomem;
2295
2296 sctp_addto_chunk(reply, sizeof(bht), &bht);
2297
2298 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2299 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2300
2301 /* Stop pending T3-rtx and heartbeat timers */
2302 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2303 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2304
2305 /* Delete non-primary peer ip addresses since we are transitioning
2306 * back to the COOKIE-WAIT state
2307 */
2308 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2309
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002310 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2311 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002313 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 SCTP_TRANSPORT(asoc->peer.primary_path));
2315
2316 /* Cast away the const modifier, as we want to just
2317 * rerun it through as a sideffect.
2318 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002319 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2322 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2323 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2324 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2325 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2326 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2327
2328 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2329
2330 return SCTP_DISPOSITION_CONSUME;
2331
2332nomem:
2333 return SCTP_DISPOSITION_NOMEM;
2334}
2335
2336/*
2337 * Process an ABORT.
2338 *
2339 * Section: 9.1
2340 * After checking the Verification Tag, the receiving endpoint shall
2341 * remove the association from its record, and shall report the
2342 * termination to its upper layer.
2343 *
2344 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2345 * B) Rules for packet carrying ABORT:
2346 *
2347 * - The endpoint shall always fill in the Verification Tag field of the
2348 * outbound packet with the destination endpoint's tag value if it
2349 * is known.
2350 *
2351 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2352 * MUST follow the procedure described in Section 8.4.
2353 *
2354 * - The receiver MUST accept the packet if the Verification Tag
2355 * matches either its own tag, OR the tag of its peer. Otherwise, the
2356 * receiver MUST silently discard the packet and take no further
2357 * action.
2358 *
2359 * Inputs
2360 * (endpoint, asoc, chunk)
2361 *
2362 * Outputs
2363 * (asoc, reply_msg, msg_up, timers, counters)
2364 *
2365 * The return value is the disposition of the chunk.
2366 */
2367sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2368 const struct sctp_association *asoc,
2369 const sctp_subtype_t type,
2370 void *arg,
2371 sctp_cmd_seq_t *commands)
2372{
2373 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 if (!sctp_vtag_verify_either(chunk, asoc))
2376 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2377
2378 /* Make sure that the ABORT chunk has a valid length.
2379 * Since this is an ABORT chunk, we have to discard it
2380 * because of the following text:
2381 * RFC 2960, Section 3.3.7
2382 * If an endpoint receives an ABORT with a format error or for an
2383 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002384 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 * as we do not know its true length. So, to be safe, discard the
2386 * packet.
2387 */
2388 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2389 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2390
Vlad Yasevich75205f42007-12-20 14:12:59 -08002391 /* ADD-IP: Special case for ABORT chunks
2392 * F4) One special consideration is that ABORT Chunks arriving
2393 * destined to the IP address being deleted MUST be
2394 * ignored (see Section 5.3.1 for further details).
2395 */
2396 if (SCTP_ADDR_DEL ==
2397 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2398 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2399
2400 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2401}
2402
2403static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2404 const struct sctp_association *asoc,
2405 const sctp_subtype_t type,
2406 void *arg,
2407 sctp_cmd_seq_t *commands)
2408{
2409 struct sctp_chunk *chunk = arg;
2410 unsigned len;
2411 __be16 error = SCTP_ERROR_NO_ERROR;
2412
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /* See if we have an error cause code in the chunk. */
2414 len = ntohs(chunk->chunk_hdr->length);
Shan Wei96ca4682011-04-19 21:26:26 +00002415 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2416
2417 sctp_errhdr_t *err;
2418 sctp_walk_errors(err, chunk->chunk_hdr);
2419 if ((void *)err != (void *)chunk->chunk_end)
2420 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
Shan Wei96ca4682011-04-19 21:26:26 +00002423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002425 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002426 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002427 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2429 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2430
2431 return SCTP_DISPOSITION_ABORT;
2432}
2433
2434/*
2435 * Process an ABORT. (COOKIE-WAIT state)
2436 *
2437 * See sctp_sf_do_9_1_abort() above.
2438 */
2439sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2440 const struct sctp_association *asoc,
2441 const sctp_subtype_t type,
2442 void *arg,
2443 sctp_cmd_seq_t *commands)
2444{
2445 struct sctp_chunk *chunk = arg;
2446 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002447 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 if (!sctp_vtag_verify_either(chunk, asoc))
2450 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2451
2452 /* Make sure that the ABORT chunk has a valid length.
2453 * Since this is an ABORT chunk, we have to discard it
2454 * because of the following text:
2455 * RFC 2960, Section 3.3.7
2456 * If an endpoint receives an ABORT with a format error or for an
2457 * association that doesn't exist, it MUST silently discard it.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002458 * Because the length is "invalid", we can't really discard just
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * as we do not know its true length. So, to be safe, discard the
2460 * packet.
2461 */
2462 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2463 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2464
2465 /* See if we have an error cause code in the chunk. */
2466 len = ntohs(chunk->chunk_hdr->length);
2467 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2468 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2469
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002470 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2471 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474/*
2475 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2476 */
2477sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2478 const struct sctp_association *asoc,
2479 const sctp_subtype_t type,
2480 void *arg,
2481 sctp_cmd_seq_t *commands)
2482{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002483 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2484 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002485 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
2488/*
2489 * Process an ABORT. (COOKIE-ECHOED state)
2490 */
2491sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2492 const struct sctp_association *asoc,
2493 const sctp_subtype_t type,
2494 void *arg,
2495 sctp_cmd_seq_t *commands)
2496{
2497 /* There is a single T1 timer, so we should be able to use
2498 * common function with the COOKIE-WAIT state.
2499 */
2500 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2501}
2502
2503/*
2504 * Stop T1 timer and abort association with "INIT failed".
2505 *
2506 * This is common code called by several sctp_sf_*_abort() functions above.
2507 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002508static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002509 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002510 const struct sctp_association *asoc,
2511 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002513 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2515 SCTP_STATE(SCTP_STATE_CLOSED));
2516 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2517 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2518 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002519 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 /* CMD_INIT_FAILED will DELETE_TCB. */
2521 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002522 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002523 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
2526/*
2527 * sctp_sf_do_9_2_shut
2528 *
2529 * Section: 9.2
2530 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2531 * - enter the SHUTDOWN-RECEIVED state,
2532 *
2533 * - stop accepting new data from its SCTP user
2534 *
2535 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2536 * that all its outstanding DATA chunks have been received by the
2537 * SHUTDOWN sender.
2538 *
2539 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2540 * send a SHUTDOWN in response to a ULP request. And should discard
2541 * subsequent SHUTDOWN chunks.
2542 *
2543 * If there are still outstanding DATA chunks left, the SHUTDOWN
2544 * receiver shall continue to follow normal data transmission
2545 * procedures defined in Section 6 until all outstanding DATA chunks
2546 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2547 * new data from its SCTP user.
2548 *
2549 * Verification Tag: 8.5 Verification Tag [Normal verification]
2550 *
2551 * Inputs
2552 * (endpoint, asoc, chunk)
2553 *
2554 * Outputs
2555 * (asoc, reply_msg, msg_up, timers, counters)
2556 *
2557 * The return value is the disposition of the chunk.
2558 */
2559sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2560 const struct sctp_association *asoc,
2561 const sctp_subtype_t type,
2562 void *arg,
2563 sctp_cmd_seq_t *commands)
2564{
2565 struct sctp_chunk *chunk = arg;
2566 sctp_shutdownhdr_t *sdh;
2567 sctp_disposition_t disposition;
2568 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002569 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 if (!sctp_vtag_verify(chunk, asoc))
2572 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2573
2574 /* Make sure that the SHUTDOWN chunk has a valid length. */
2575 if (!sctp_chunk_length_valid(chunk,
2576 sizeof(struct sctp_shutdown_chunk_t)))
2577 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2578 commands);
2579
2580 /* Convert the elaborate header. */
2581 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2582 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2583 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002584 ctsn = ntohl(sdh->cum_tsn_ack);
2585
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002586 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2587 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2588 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2589 return SCTP_DISPOSITION_DISCARD;
2590 }
2591
Wei Yongjundf10eec2008-10-23 01:00:21 -07002592 /* If Cumulative TSN Ack beyond the max tsn currently
2593 * send, terminating the association and respond to the
2594 * sender with an ABORT.
2595 */
2596 if (!TSN_lt(ctsn, asoc->next_tsn))
2597 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002599 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2600 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2601 * inform the application that it should cease sending data.
2602 */
2603 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2604 if (!ev) {
2605 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002606 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002607 }
2608 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2611 * - enter the SHUTDOWN-RECEIVED state,
2612 * - stop accepting new data from its SCTP user
2613 *
2614 * [This is implicit in the new state.]
2615 */
2616 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2617 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2618 disposition = SCTP_DISPOSITION_CONSUME;
2619
2620 if (sctp_outq_is_empty(&asoc->outqueue)) {
2621 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2622 arg, commands);
2623 }
2624
2625 if (SCTP_DISPOSITION_NOMEM == disposition)
2626 goto out;
2627
2628 /* - verify, by checking the Cumulative TSN Ack field of the
2629 * chunk, that all its outstanding DATA chunks have been
2630 * received by the SHUTDOWN sender.
2631 */
2632 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002633 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635out:
2636 return disposition;
2637}
2638
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002639/*
2640 * sctp_sf_do_9_2_shut_ctsn
2641 *
2642 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2643 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2644 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2645 * MUST be processed.
2646 */
2647sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2648 const struct sctp_association *asoc,
2649 const sctp_subtype_t type,
2650 void *arg,
2651 sctp_cmd_seq_t *commands)
2652{
2653 struct sctp_chunk *chunk = arg;
2654 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002655 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002656
2657 if (!sctp_vtag_verify(chunk, asoc))
2658 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2659
2660 /* Make sure that the SHUTDOWN chunk has a valid length. */
2661 if (!sctp_chunk_length_valid(chunk,
2662 sizeof(struct sctp_shutdown_chunk_t)))
2663 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2664 commands);
2665
2666 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002667 ctsn = ntohl(sdh->cum_tsn_ack);
2668
2669 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2670 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2671 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2672 return SCTP_DISPOSITION_DISCARD;
2673 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002674
2675 /* If Cumulative TSN Ack beyond the max tsn currently
2676 * send, terminating the association and respond to the
2677 * sender with an ABORT.
2678 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002679 if (!TSN_lt(ctsn, asoc->next_tsn))
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002680 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2681
2682 /* verify, by checking the Cumulative TSN Ack field of the
2683 * chunk, that all its outstanding DATA chunks have been
2684 * received by the SHUTDOWN sender.
2685 */
2686 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2687 SCTP_BE32(sdh->cum_tsn_ack));
2688
2689 return SCTP_DISPOSITION_CONSUME;
2690}
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692/* RFC 2960 9.2
2693 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2694 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2695 * transport addresses (either in the IP addresses or in the INIT chunk)
2696 * that belong to this association, it should discard the INIT chunk and
2697 * retransmit the SHUTDOWN ACK chunk.
2698 */
2699sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2700 const struct sctp_association *asoc,
2701 const sctp_subtype_t type,
2702 void *arg,
2703 sctp_cmd_seq_t *commands)
2704{
2705 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2706 struct sctp_chunk *reply;
2707
Vlad Yasevichece25df2007-09-07 16:30:54 -04002708 /* Make sure that the chunk has a valid length */
2709 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2710 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2711 commands);
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /* Since we are not going to really process this INIT, there
2714 * is no point in verifying chunk boundries. Just generate
2715 * the SHUTDOWN ACK.
2716 */
2717 reply = sctp_make_shutdown_ack(asoc, chunk);
2718 if (NULL == reply)
2719 goto nomem;
2720
2721 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2722 * the T2-SHUTDOWN timer.
2723 */
2724 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2725
2726 /* and restart the T2-shutdown timer. */
2727 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2728 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2729
2730 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2731
2732 return SCTP_DISPOSITION_CONSUME;
2733nomem:
2734 return SCTP_DISPOSITION_NOMEM;
2735}
2736
2737/*
2738 * sctp_sf_do_ecn_cwr
2739 *
2740 * Section: Appendix A: Explicit Congestion Notification
2741 *
2742 * CWR:
2743 *
2744 * RFC 2481 details a specific bit for a sender to send in the header of
2745 * its next outbound TCP segment to indicate to its peer that it has
2746 * reduced its congestion window. This is termed the CWR bit. For
2747 * SCTP the same indication is made by including the CWR chunk.
2748 * This chunk contains one data element, i.e. the TSN number that
2749 * was sent in the ECNE chunk. This element represents the lowest
2750 * TSN number in the datagram that was originally marked with the
2751 * CE bit.
2752 *
2753 * Verification Tag: 8.5 Verification Tag [Normal verification]
2754 * Inputs
2755 * (endpoint, asoc, chunk)
2756 *
2757 * Outputs
2758 * (asoc, reply_msg, msg_up, timers, counters)
2759 *
2760 * The return value is the disposition of the chunk.
2761 */
2762sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2763 const struct sctp_association *asoc,
2764 const sctp_subtype_t type,
2765 void *arg,
2766 sctp_cmd_seq_t *commands)
2767{
2768 sctp_cwrhdr_t *cwr;
2769 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002770 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 if (!sctp_vtag_verify(chunk, asoc))
2773 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2774
2775 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2776 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2777 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2780 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2781
Al Viro34bcca22006-11-20 17:27:15 -08002782 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
2784 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002785 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 /* Stop sending ECNE. */
2787 sctp_add_cmd_sf(commands,
2788 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002789 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 }
2791 return SCTP_DISPOSITION_CONSUME;
2792}
2793
2794/*
2795 * sctp_sf_do_ecne
2796 *
2797 * Section: Appendix A: Explicit Congestion Notification
2798 *
2799 * ECN-Echo
2800 *
2801 * RFC 2481 details a specific bit for a receiver to send back in its
2802 * TCP acknowledgements to notify the sender of the Congestion
2803 * Experienced (CE) bit having arrived from the network. For SCTP this
2804 * same indication is made by including the ECNE chunk. This chunk
2805 * contains one data element, i.e. the lowest TSN associated with the IP
2806 * datagram marked with the CE bit.....
2807 *
2808 * Verification Tag: 8.5 Verification Tag [Normal verification]
2809 * Inputs
2810 * (endpoint, asoc, chunk)
2811 *
2812 * Outputs
2813 * (asoc, reply_msg, msg_up, timers, counters)
2814 *
2815 * The return value is the disposition of the chunk.
2816 */
2817sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2818 const struct sctp_association *asoc,
2819 const sctp_subtype_t type,
2820 void *arg,
2821 sctp_cmd_seq_t *commands)
2822{
2823 sctp_ecnehdr_t *ecne;
2824 struct sctp_chunk *chunk = arg;
2825
2826 if (!sctp_vtag_verify(chunk, asoc))
2827 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2828
2829 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2830 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2831 commands);
2832
2833 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2834 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2835
2836 /* If this is a newer ECNE than the last CWR packet we sent out */
2837 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2838 SCTP_U32(ntohl(ecne->lowest_tsn)));
2839
2840 return SCTP_DISPOSITION_CONSUME;
2841}
2842
2843/*
2844 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2845 *
2846 * The SCTP endpoint MUST always acknowledge the reception of each valid
2847 * DATA chunk.
2848 *
2849 * The guidelines on delayed acknowledgement algorithm specified in
2850 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2851 * acknowledgement SHOULD be generated for at least every second packet
2852 * (not every second DATA chunk) received, and SHOULD be generated within
2853 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2854 * situations it may be beneficial for an SCTP transmitter to be more
2855 * conservative than the algorithms detailed in this document allow.
2856 * However, an SCTP transmitter MUST NOT be more aggressive than the
2857 * following algorithms allow.
2858 *
2859 * A SCTP receiver MUST NOT generate more than one SACK for every
2860 * incoming packet, other than to update the offered window as the
2861 * receiving application consumes new data.
2862 *
2863 * Verification Tag: 8.5 Verification Tag [Normal verification]
2864 *
2865 * Inputs
2866 * (endpoint, asoc, chunk)
2867 *
2868 * Outputs
2869 * (asoc, reply_msg, msg_up, timers, counters)
2870 *
2871 * The return value is the disposition of the chunk.
2872 */
2873sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2874 const struct sctp_association *asoc,
2875 const sctp_subtype_t type,
2876 void *arg,
2877 sctp_cmd_seq_t *commands)
2878{
2879 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002880 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 int error;
2882
2883 if (!sctp_vtag_verify(chunk, asoc)) {
2884 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2885 SCTP_NULL());
2886 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2890 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2891 commands);
2892
2893 error = sctp_eat_data(asoc, chunk, commands );
2894 switch (error) {
2895 case SCTP_IERROR_NO_ERROR:
2896 break;
2897 case SCTP_IERROR_HIGH_TSN:
2898 case SCTP_IERROR_BAD_STREAM:
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_noforce;
2901 case SCTP_IERROR_DUP_TSN:
2902 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002903 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 goto discard_force;
2905 case SCTP_IERROR_NO_DATA:
2906 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002907 case SCTP_IERROR_PROTO_VIOLATION:
2908 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
2909 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 default:
2911 BUG();
2912 }
2913
Wei Yongjun6dc76942009-11-23 15:53:53 -05002914 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2915 force = SCTP_FORCE();
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 if (asoc->autoclose) {
2918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2919 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2920 }
2921
2922 /* If this is the last chunk in a packet, we need to count it
2923 * toward sack generation. Note that we need to SACK every
2924 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2925 * THEM. We elect to NOT generate SACK's if the chunk fails
2926 * the verification tag test.
2927 *
2928 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2929 *
2930 * The SCTP endpoint MUST always acknowledge the reception of
2931 * each valid DATA chunk.
2932 *
2933 * The guidelines on delayed acknowledgement algorithm
2934 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2935 * Specifically, an acknowledgement SHOULD be generated for at
2936 * least every second packet (not every second DATA chunk)
2937 * received, and SHOULD be generated within 200 ms of the
2938 * arrival of any unacknowledged DATA chunk. In some
2939 * situations it may be beneficial for an SCTP transmitter to
2940 * be more conservative than the algorithms detailed in this
2941 * document allow. However, an SCTP transmitter MUST NOT be
2942 * more aggressive than the following algorithms allow.
2943 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002944 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002945 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return SCTP_DISPOSITION_CONSUME;
2948
2949discard_force:
2950 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2951 *
2952 * When a packet arrives with duplicate DATA chunk(s) and with
2953 * no new DATA chunk(s), the endpoint MUST immediately send a
2954 * SACK with no delay. If a packet arrives with duplicate
2955 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2956 * MAY immediately send a SACK. Normally receipt of duplicate
2957 * DATA chunks will occur when the original SACK chunk was lost
2958 * and the peer's RTO has expired. The duplicate TSN number(s)
2959 * SHOULD be reported in the SACK as duplicate.
2960 */
2961 /* In our case, we split the MAY SACK advice up whether or not
2962 * the last chunk is a duplicate.'
2963 */
2964 if (chunk->end_of_packet)
2965 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2966 return SCTP_DISPOSITION_DISCARD;
2967
2968discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002969 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002970 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return SCTP_DISPOSITION_DISCARD;
2973consume:
2974 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978/*
2979 * sctp_sf_eat_data_fast_4_4
2980 *
2981 * Section: 4 (4)
2982 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2983 * DATA chunks without delay.
2984 *
2985 * Verification Tag: 8.5 Verification Tag [Normal verification]
2986 * Inputs
2987 * (endpoint, asoc, chunk)
2988 *
2989 * Outputs
2990 * (asoc, reply_msg, msg_up, timers, counters)
2991 *
2992 * The return value is the disposition of the chunk.
2993 */
2994sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2995 const struct sctp_association *asoc,
2996 const sctp_subtype_t type,
2997 void *arg,
2998 sctp_cmd_seq_t *commands)
2999{
3000 struct sctp_chunk *chunk = arg;
3001 int error;
3002
3003 if (!sctp_vtag_verify(chunk, asoc)) {
3004 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3005 SCTP_NULL());
3006 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3007 }
3008
3009 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3010 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3011 commands);
3012
3013 error = sctp_eat_data(asoc, chunk, commands );
3014 switch (error) {
3015 case SCTP_IERROR_NO_ERROR:
3016 case SCTP_IERROR_HIGH_TSN:
3017 case SCTP_IERROR_DUP_TSN:
3018 case SCTP_IERROR_IGNORE_TSN:
3019 case SCTP_IERROR_BAD_STREAM:
3020 break;
3021 case SCTP_IERROR_NO_DATA:
3022 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003023 case SCTP_IERROR_PROTO_VIOLATION:
3024 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
3025 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 default:
3027 BUG();
3028 }
3029
3030 /* Go a head and force a SACK, since we are shutting down. */
3031
3032 /* Implementor's Guide.
3033 *
3034 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3035 * respond to each received packet containing one or more DATA chunk(s)
3036 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3037 */
3038 if (chunk->end_of_packet) {
3039 /* We must delay the chunk creation since the cumulative
3040 * TSN has not been updated yet.
3041 */
3042 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3043 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3044 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3045 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3046 }
3047
3048consume:
3049 return SCTP_DISPOSITION_CONSUME;
3050}
3051
3052/*
3053 * Section: 6.2 Processing a Received SACK
3054 * D) Any time a SACK arrives, the endpoint performs the following:
3055 *
3056 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3057 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3058 * increasing, a SACK whose Cumulative TSN Ack is less than the
3059 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3060 *
3061 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3062 * of bytes still outstanding after processing the Cumulative TSN Ack
3063 * and the Gap Ack Blocks.
3064 *
3065 * iii) If the SACK is missing a TSN that was previously
3066 * acknowledged via a Gap Ack Block (e.g., the data receiver
3067 * reneged on the data), then mark the corresponding DATA chunk
3068 * as available for retransmit: Mark it as missing for fast
3069 * retransmit as described in Section 7.2.4 and if no retransmit
3070 * timer is running for the destination address to which the DATA
3071 * chunk was originally transmitted, then T3-rtx is started for
3072 * that destination address.
3073 *
3074 * Verification Tag: 8.5 Verification Tag [Normal verification]
3075 *
3076 * Inputs
3077 * (endpoint, asoc, chunk)
3078 *
3079 * Outputs
3080 * (asoc, reply_msg, msg_up, timers, counters)
3081 *
3082 * The return value is the disposition of the chunk.
3083 */
3084sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3085 const struct sctp_association *asoc,
3086 const sctp_subtype_t type,
3087 void *arg,
3088 sctp_cmd_seq_t *commands)
3089{
3090 struct sctp_chunk *chunk = arg;
3091 sctp_sackhdr_t *sackh;
3092 __u32 ctsn;
3093
3094 if (!sctp_vtag_verify(chunk, asoc))
3095 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3096
3097 /* Make sure that the SACK chunk has a valid length. */
3098 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3099 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3100 commands);
3101
3102 /* Pull the SACK chunk from the data buffer */
3103 sackh = sctp_sm_pull_sack(chunk);
3104 /* Was this a bogus SACK? */
3105 if (!sackh)
3106 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3107 chunk->subh.sack_hdr = sackh;
3108 ctsn = ntohl(sackh->cum_tsn_ack);
3109
3110 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3111 * Ack Point, then drop the SACK. Since Cumulative TSN
3112 * Ack is monotonically increasing, a SACK whose
3113 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3114 * Point indicates an out-of-order SACK.
3115 */
3116 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3117 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3118 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3119 return SCTP_DISPOSITION_DISCARD;
3120 }
3121
Wei Yongjunaecedea2007-08-02 16:57:44 +08003122 /* If Cumulative TSN Ack beyond the max tsn currently
3123 * send, terminating the association and respond to the
3124 * sender with an ABORT.
3125 */
3126 if (!TSN_lt(ctsn, asoc->next_tsn))
3127 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3128
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 /* Return this SACK for further processing. */
3130 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3131
3132 /* Note: We do the rest of the work on the PROCESS_SACK
3133 * sideeffect.
3134 */
3135 return SCTP_DISPOSITION_CONSUME;
3136}
3137
3138/*
3139 * Generate an ABORT in response to a packet.
3140 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003141 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003143 * 8) The receiver should respond to the sender of the OOTB packet with
3144 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3145 * MUST fill in the Verification Tag field of the outbound packet
3146 * with the value found in the Verification Tag field of the OOTB
3147 * packet and set the T-bit in the Chunk Flags to indicate that the
3148 * Verification Tag is reflected. After sending this ABORT, the
3149 * receiver of the OOTB packet shall discard the OOTB packet and take
3150 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 *
3152 * Verification Tag:
3153 *
3154 * The return value is the disposition of the chunk.
3155*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003156static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 const struct sctp_association *asoc,
3158 const sctp_subtype_t type,
3159 void *arg,
3160 sctp_cmd_seq_t *commands)
3161{
3162 struct sctp_packet *packet = NULL;
3163 struct sctp_chunk *chunk = arg;
3164 struct sctp_chunk *abort;
3165
3166 packet = sctp_ootb_pkt_new(asoc, chunk);
3167
3168 if (packet) {
3169 /* Make an ABORT. The T bit will be set if the asoc
3170 * is NULL.
3171 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003172 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (!abort) {
3174 sctp_ootb_pkt_free(packet);
3175 return SCTP_DISPOSITION_NOMEM;
3176 }
3177
Jerome Forissier047a2422005-04-28 11:58:43 -07003178 /* Reflect vtag if T-Bit is set */
3179 if (sctp_test_T_bit(abort))
3180 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3181
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 /* Set the skb to the belonging sock for accounting. */
3183 abort->skb->sk = ep->base.sk;
3184
3185 sctp_packet_append_chunk(packet, abort);
3186
3187 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3188 SCTP_PACKET(packet));
3189
3190 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3191
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003192 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return SCTP_DISPOSITION_CONSUME;
3194 }
3195
3196 return SCTP_DISPOSITION_NOMEM;
3197}
3198
3199/*
3200 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3201 * event as ULP notification for each cause included in the chunk.
3202 *
3203 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3204 *
3205 * The return value is the disposition of the chunk.
3206*/
3207sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3208 const struct sctp_association *asoc,
3209 const sctp_subtype_t type,
3210 void *arg,
3211 sctp_cmd_seq_t *commands)
3212{
3213 struct sctp_chunk *chunk = arg;
Shan Wei8a00be12011-04-19 21:25:40 +00003214 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 if (!sctp_vtag_verify(chunk, asoc))
3217 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3218
3219 /* Make sure that the ERROR chunk has a valid length. */
3220 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3221 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3222 commands);
Shan Wei8a00be12011-04-19 21:25:40 +00003223 sctp_walk_errors(err, chunk->chunk_hdr);
3224 if ((void *)err != (void *)chunk->chunk_end)
3225 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3226 (void *)err, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Wei Yongjun3df26782009-03-02 06:46:51 +00003228 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3229 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232}
3233
3234/*
3235 * Process an inbound SHUTDOWN ACK.
3236 *
3237 * From Section 9.2:
3238 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3239 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3240 * peer, and remove all record of the association.
3241 *
3242 * The return value is the disposition.
3243 */
3244sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3245 const struct sctp_association *asoc,
3246 const sctp_subtype_t type,
3247 void *arg,
3248 sctp_cmd_seq_t *commands)
3249{
3250 struct sctp_chunk *chunk = arg;
3251 struct sctp_chunk *reply;
3252 struct sctp_ulpevent *ev;
3253
3254 if (!sctp_vtag_verify(chunk, asoc))
3255 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3256
3257 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3258 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3259 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3260 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 /* 10.2 H) SHUTDOWN COMPLETE notification
3262 *
3263 * When SCTP completes the shutdown procedures (section 9.2) this
3264 * notification is passed to the upper layer.
3265 */
3266 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003267 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 if (!ev)
3269 goto nomem;
3270
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003271 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3272 reply = sctp_make_shutdown_complete(asoc, chunk);
3273 if (!reply)
3274 goto nomem_chunk;
3275
3276 /* Do all the commands now (after allocation), so that we
3277 * have consistent state if memory allocation failes
3278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3280
3281 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3282 * stop the T2-shutdown timer,
3283 */
3284 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3285 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3286
3287 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3288 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3291 SCTP_STATE(SCTP_STATE_CLOSED));
3292 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3293 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3294 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3295
3296 /* ...and remove all record of the association. */
3297 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3298 return SCTP_DISPOSITION_DELETE_TCB;
3299
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003300nomem_chunk:
3301 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302nomem:
3303 return SCTP_DISPOSITION_NOMEM;
3304}
3305
3306/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003307 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3308 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3310 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3311 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3312 * packet must fill in the Verification Tag field of the outbound
3313 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003314 * set the T-bit in the Chunk Flags to indicate that the Verification
3315 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 *
3317 * 8) The receiver should respond to the sender of the OOTB packet with
3318 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3319 * MUST fill in the Verification Tag field of the outbound packet
3320 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003321 * packet and set the T-bit in the Chunk Flags to indicate that the
3322 * Verification Tag is reflected. After sending this ABORT, the
3323 * receiver of the OOTB packet shall discard the OOTB packet and take
3324 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 */
3326sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3327 const struct sctp_association *asoc,
3328 const sctp_subtype_t type,
3329 void *arg,
3330 sctp_cmd_seq_t *commands)
3331{
3332 struct sctp_chunk *chunk = arg;
3333 struct sk_buff *skb = chunk->skb;
3334 sctp_chunkhdr_t *ch;
Shan Wei85c5ed42011-04-19 21:30:01 +00003335 sctp_errhdr_t *err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 __u8 *ch_end;
3337 int ootb_shut_ack = 0;
Shan Wei85c5ed42011-04-19 21:30:01 +00003338 int ootb_cookie_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
3340 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3341
3342 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3343 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003344 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003346 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3347 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Vlad Yasevichece25df2007-09-07 16:30:54 -04003349 /* Now that we know we at least have a chunk header,
3350 * do things that are type appropriate.
3351 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3353 ootb_shut_ack = 1;
3354
3355 /* RFC 2960, Section 3.3.7
3356 * Moreover, under any circumstances, an endpoint that
3357 * receives an ABORT MUST NOT respond to that ABORT by
3358 * sending an ABORT of its own.
3359 */
3360 if (SCTP_CID_ABORT == ch->type)
3361 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003362
Shan Wei85c5ed42011-04-19 21:30:01 +00003363 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3364 * or a COOKIE ACK the SCTP Packet should be silently
3365 * discarded.
3366 */
3367
3368 if (SCTP_CID_COOKIE_ACK == ch->type)
3369 ootb_cookie_ack = 1;
3370
3371 if (SCTP_CID_ERROR == ch->type) {
3372 sctp_walk_errors(err, ch) {
3373 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3374 ootb_cookie_ack = 1;
3375 break;
3376 }
3377 }
3378 }
3379
Vlad Yasevichece25df2007-09-07 16:30:54 -04003380 /* Report violation if chunk len overflows */
3381 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3382 if (ch_end > skb_tail_pointer(skb))
3383 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3384 commands);
3385
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003387 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003390 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Shan Wei85c5ed42011-04-19 21:30:01 +00003391 else if (ootb_cookie_ack)
3392 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003394 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395}
3396
3397/*
3398 * Handle an "Out of the blue" SHUTDOWN ACK.
3399 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003400 * Section: 8.4 5, sctpimpguide 2.41.
3401 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003403 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3404 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3405 * packet must fill in the Verification Tag field of the outbound
3406 * packet with the Verification Tag received in the SHUTDOWN ACK and
3407 * set the T-bit in the Chunk Flags to indicate that the Verification
3408 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 *
3410 * Inputs
3411 * (endpoint, asoc, type, arg, commands)
3412 *
3413 * Outputs
3414 * (sctp_disposition_t)
3415 *
3416 * The return value is the disposition of the chunk.
3417 */
3418static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3419 const struct sctp_association *asoc,
3420 const sctp_subtype_t type,
3421 void *arg,
3422 sctp_cmd_seq_t *commands)
3423{
3424 struct sctp_packet *packet = NULL;
3425 struct sctp_chunk *chunk = arg;
3426 struct sctp_chunk *shut;
3427
3428 packet = sctp_ootb_pkt_new(asoc, chunk);
3429
3430 if (packet) {
3431 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003432 * The T bit will be set if the asoc is NULL.
3433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 shut = sctp_make_shutdown_complete(asoc, chunk);
3435 if (!shut) {
3436 sctp_ootb_pkt_free(packet);
3437 return SCTP_DISPOSITION_NOMEM;
3438 }
3439
Jerome Forissier047a2422005-04-28 11:58:43 -07003440 /* Reflect vtag if T-Bit is set */
3441 if (sctp_test_T_bit(shut))
3442 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3443
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 /* Set the skb to the belonging sock for accounting. */
3445 shut->skb->sk = ep->base.sk;
3446
3447 sctp_packet_append_chunk(packet, shut);
3448
3449 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3450 SCTP_PACKET(packet));
3451
3452 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3453
3454 /* If the chunk length is invalid, we don't want to process
3455 * the reset of the packet.
3456 */
3457 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3458 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3459
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003460 /* We need to discard the rest of the packet to prevent
3461 * potential bomming attacks from additional bundled chunks.
3462 * This is documented in SCTP Threats ID.
3463 */
3464 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
3466
3467 return SCTP_DISPOSITION_NOMEM;
3468}
3469
3470/*
3471 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3472 *
3473 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3474 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3475 * procedures in section 8.4 SHOULD be followed, in other words it
3476 * should be treated as an Out Of The Blue packet.
3477 * [This means that we do NOT check the Verification Tag on these
3478 * chunks. --piggy ]
3479 *
3480 */
3481sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3482 const struct sctp_association *asoc,
3483 const sctp_subtype_t type,
3484 void *arg,
3485 sctp_cmd_seq_t *commands)
3486{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003487 struct sctp_chunk *chunk = arg;
3488
3489 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3490 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3491 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3492 commands);
3493
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 /* Although we do have an association in this case, it corresponds
3495 * to a restarted association. So the packet is treated as an OOTB
3496 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3497 * called with a NULL association.
3498 */
Wei Yongjun8190f892008-09-08 12:13:55 +08003499 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3500
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3502}
3503
3504/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3505sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3506 const struct sctp_association *asoc,
3507 const sctp_subtype_t type, void *arg,
3508 sctp_cmd_seq_t *commands)
3509{
3510 struct sctp_chunk *chunk = arg;
3511 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003512 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003514 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003516 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 if (!sctp_vtag_verify(chunk, asoc)) {
3519 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3520 SCTP_NULL());
3521 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3522 }
3523
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003524 /* ADD-IP: Section 4.1.1
3525 * This chunk MUST be sent in an authenticated way by using
3526 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3527 * is received unauthenticated it MUST be silently discarded as
3528 * described in [I-D.ietf-tsvwg-sctp-auth].
3529 */
3530 if (!sctp_addip_noauth && !chunk->auth)
3531 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3534 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3535 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3536 commands);
3537
3538 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3539 serial = ntohl(hdr->serial);
3540
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003541 addr_param = (union sctp_addr_param *)hdr->params;
3542 length = ntohs(addr_param->p.length);
3543 if (length < sizeof(sctp_paramhdr_t))
Wei Yongjunba016672008-09-30 05:32:24 -07003544 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003545 (void *)addr_param, commands);
3546
3547 /* Verify the ASCONF chunk before processing it. */
3548 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003549 (sctp_paramhdr_t *)((void *)addr_param + length),
3550 (void *)chunk->chunk_end,
3551 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003552 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3553 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003554
Vlad Yasevicha08de642007-12-20 14:11:47 -08003555 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003557 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 */
3559 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003560 /* If this is the first instance of ASCONF in the packet,
3561 * we can clean our old ASCONF-ACKs.
3562 */
3563 if (!chunk->has_asconf)
3564 sctp_assoc_clean_asconf_ack_cache(asoc);
3565
3566 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3567 * expected, process the ASCONF as described below and after
3568 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3569 * the response packet and cache a copy of it (in the event it
3570 * later needs to be retransmitted).
3571 *
3572 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 */
3574 asconf_ack = sctp_process_asconf((struct sctp_association *)
3575 asoc, chunk);
3576 if (!asconf_ack)
3577 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003578 } else if (serial < asoc->peer.addip_serial + 1) {
3579 /* ADDIP 5.2 E2)
3580 * If the value found in the Sequence Number is less than the
3581 * ('Peer- Sequence-Number' + 1), simply skip to the next
3582 * ASCONF, and include in the outbound response packet
3583 * any previously cached ASCONF-ACK response that was
3584 * sent and saved that matches the Sequence Number of the
3585 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3586 * Chunk exists. This will occur when an older ASCONF
3587 * arrives out of order. In such a case, the receiver
3588 * should skip the ASCONF Chunk and not include ASCONF-ACK
3589 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003591 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3592 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003594
3595 /* Reset the transport so that we select the correct one
3596 * this time around. This is to make sure that we don't
3597 * accidentally use a stale transport that's been removed.
3598 */
3599 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003601 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 return SCTP_DISPOSITION_DISCARD;
3605 }
3606
Vlad Yasevicha08de642007-12-20 14:11:47 -08003607 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3608 * containing the ASCONF-ACK Chunks MUST be the source address of
3609 * the SCTP packet that held the ASCONF Chunks.
3610 *
3611 * To do this properly, we'll set the destination address of the chunk
3612 * and at the transmit time, will try look up the transport to use.
3613 * Since ASCONFs may be bundled, the correct transport may not be
Thadeu Lima de Souza Cascardo94e2bd62009-10-16 15:20:49 +02003614 * created until we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003616 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return SCTP_DISPOSITION_CONSUME;
3620}
3621
3622/*
3623 * ADDIP Section 4.3 General rules for address manipulation
3624 * When building TLV parameters for the ASCONF Chunk that will add or
3625 * delete IP addresses the D0 to D13 rules should be applied:
3626 */
3627sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3628 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003629 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 sctp_cmd_seq_t *commands)
3631{
3632 struct sctp_chunk *asconf_ack = arg;
3633 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3634 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003635 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 sctp_addiphdr_t *addip_hdr;
3637 __u32 sent_serial, rcvd_serial;
3638
3639 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3640 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3641 SCTP_NULL());
3642 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3643 }
3644
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003645 /* ADD-IP, Section 4.1.2:
3646 * This chunk MUST be sent in an authenticated way by using
3647 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3648 * is received unauthenticated it MUST be silently discarded as
3649 * described in [I-D.ietf-tsvwg-sctp-auth].
3650 */
3651 if (!sctp_addip_noauth && !asconf_ack->auth)
3652 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /* Make sure that the ADDIP chunk has a valid length. */
3655 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3656 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3657 commands);
3658
3659 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3660 rcvd_serial = ntohl(addip_hdr->serial);
3661
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003662 /* Verify the ASCONF-ACK chunk before processing it. */
3663 if (!sctp_verify_asconf(asoc,
3664 (sctp_paramhdr_t *)addip_hdr->params,
3665 (void *)asconf_ack->chunk_end,
3666 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003667 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3668 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 if (last_asconf) {
3671 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3672 sent_serial = ntohl(addip_hdr->serial);
3673 } else {
3674 sent_serial = asoc->addip_serial - 1;
3675 }
3676
3677 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3678 * equal to the next serial number to be used but no ASCONF chunk is
3679 * outstanding the endpoint MUST ABORT the association. Note that a
3680 * sequence number is greater than if it is no more than 2^^31-1
3681 * larger than the current sequence number (using serial arithmetic).
3682 */
3683 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3684 !(asoc->addip_last_asconf)) {
3685 abort = sctp_make_abort(asoc, asconf_ack,
3686 sizeof(sctp_errhdr_t));
3687 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003688 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3690 SCTP_CHUNK(abort));
3691 }
3692 /* We are going to ABORT, so we might as well stop
3693 * processing the rest of the chunks in the packet.
3694 */
3695 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3696 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3697 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003698 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003699 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003701 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3703 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3704 return SCTP_DISPOSITION_ABORT;
3705 }
3706
3707 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3708 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3709 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3710
3711 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
Vlad Yasevichc0786692010-04-28 08:47:22 +00003712 asconf_ack)) {
3713 /* Successfully processed ASCONF_ACK. We can
3714 * release the next asconf if we have one.
3715 */
3716 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3717 SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return SCTP_DISPOSITION_CONSUME;
Vlad Yasevichc0786692010-04-28 08:47:22 +00003719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 abort = sctp_make_abort(asoc, asconf_ack,
3722 sizeof(sctp_errhdr_t));
3723 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003724 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3726 SCTP_CHUNK(abort));
3727 }
3728 /* We are going to ABORT, so we might as well stop
3729 * processing the rest of the chunks in the packet.
3730 */
3731 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003732 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003733 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003735 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3737 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3738 return SCTP_DISPOSITION_ABORT;
3739 }
3740
3741 return SCTP_DISPOSITION_DISCARD;
3742}
3743
3744/*
3745 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3746 *
3747 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3748 * its cumulative TSN point to the value carried in the FORWARD TSN
3749 * chunk, and then MUST further advance its cumulative TSN point locally
3750 * if possible.
3751 * After the above processing, the data receiver MUST stop reporting any
3752 * missing TSNs earlier than or equal to the new cumulative TSN point.
3753 *
3754 * Verification Tag: 8.5 Verification Tag [Normal verification]
3755 *
3756 * The return value is the disposition of the chunk.
3757 */
3758sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3759 const struct sctp_association *asoc,
3760 const sctp_subtype_t type,
3761 void *arg,
3762 sctp_cmd_seq_t *commands)
3763{
3764 struct sctp_chunk *chunk = arg;
3765 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003766 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 __u16 len;
3768 __u32 tsn;
3769
3770 if (!sctp_vtag_verify(chunk, asoc)) {
3771 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3772 SCTP_NULL());
3773 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3774 }
3775
3776 /* Make sure that the FORWARD_TSN chunk has valid length. */
3777 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3778 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3779 commands);
3780
3781 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3782 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3783 len = ntohs(chunk->chunk_hdr->length);
3784 len -= sizeof(struct sctp_chunkhdr);
3785 skb_pull(chunk->skb, len);
3786
3787 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003788 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789
3790 /* The TSN is too high--silently discard the chunk and count on it
3791 * getting retransmitted later.
3792 */
3793 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3794 goto discard_noforce;
3795
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003796 /* Silently discard the chunk if stream-id is not valid */
3797 sctp_walk_fwdtsn(skip, chunk) {
3798 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3799 goto discard_noforce;
3800 }
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3803 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003804 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 /* Count this as receiving DATA. */
3808 if (asoc->autoclose) {
3809 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3810 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3811 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003814 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 */
3816 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
3818 return SCTP_DISPOSITION_CONSUME;
3819
3820discard_noforce:
3821 return SCTP_DISPOSITION_DISCARD;
3822}
3823
3824sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3825 const struct sctp_endpoint *ep,
3826 const struct sctp_association *asoc,
3827 const sctp_subtype_t type,
3828 void *arg,
3829 sctp_cmd_seq_t *commands)
3830{
3831 struct sctp_chunk *chunk = arg;
3832 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003833 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 __u16 len;
3835 __u32 tsn;
3836
3837 if (!sctp_vtag_verify(chunk, asoc)) {
3838 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3839 SCTP_NULL());
3840 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3841 }
3842
3843 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3844 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3845 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3846 commands);
3847
3848 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3849 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3850 len = ntohs(chunk->chunk_hdr->length);
3851 len -= sizeof(struct sctp_chunkhdr);
3852 skb_pull(chunk->skb, len);
3853
3854 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003855 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 /* The TSN is too high--silently discard the chunk and count on it
3858 * getting retransmitted later.
3859 */
3860 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3861 goto gen_shutdown;
3862
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003863 /* Silently discard the chunk if stream-id is not valid */
3864 sctp_walk_fwdtsn(skip, chunk) {
3865 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3866 goto gen_shutdown;
3867 }
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3870 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003871 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003873
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 /* Go a head and force a SACK, since we are shutting down. */
3875gen_shutdown:
3876 /* Implementor's Guide.
3877 *
3878 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3879 * respond to each received packet containing one or more DATA chunk(s)
3880 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3881 */
3882 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3883 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3884 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3885 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3886
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003887 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888}
3889
3890/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003891 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003892 *
3893 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3894 * Identifier field. If this algorithm was not specified by the
3895 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3896 * during association setup, the AUTH chunk and all chunks after it MUST
3897 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3898 * defined in Section 4.1.
3899 *
3900 * If an endpoint with no shared key receives a Shared Key Identifier
3901 * other than 0, it MUST silently discard all authenticated chunks. If
3902 * the endpoint has at least one endpoint pair shared key for the peer,
3903 * it MUST use the key specified by the Shared Key Identifier if a
3904 * key has been configured for that Shared Key Identifier. If no
3905 * endpoint pair shared key has been configured for that Shared Key
3906 * Identifier, all authenticated chunks MUST be silently discarded.
3907 *
3908 * Verification Tag: 8.5 Verification Tag [Normal verification]
3909 *
3910 * The return value is the disposition of the chunk.
3911 */
3912static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3913 const struct sctp_association *asoc,
3914 const sctp_subtype_t type,
3915 struct sctp_chunk *chunk)
3916{
3917 struct sctp_authhdr *auth_hdr;
3918 struct sctp_hmac *hmac;
3919 unsigned int sig_len;
3920 __u16 key_id;
3921 __u8 *save_digest;
3922 __u8 *digest;
3923
3924 /* Pull in the auth header, so we can do some more verification */
3925 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3926 chunk->subh.auth_hdr = auth_hdr;
3927 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3928
3929 /* Make sure that we suport the HMAC algorithm from the auth
3930 * chunk.
3931 */
3932 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3933 return SCTP_IERROR_AUTH_BAD_HMAC;
3934
3935 /* Make sure that the provided shared key identifier has been
3936 * configured
3937 */
3938 key_id = ntohs(auth_hdr->shkey_id);
3939 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3940 return SCTP_IERROR_AUTH_BAD_KEYID;
3941
3942
3943 /* Make sure that the length of the signature matches what
3944 * we expect.
3945 */
3946 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3947 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3948 if (sig_len != hmac->hmac_len)
3949 return SCTP_IERROR_PROTO_VIOLATION;
3950
3951 /* Now that we've done validation checks, we can compute and
3952 * verify the hmac. The steps involved are:
3953 * 1. Save the digest from the chunk.
3954 * 2. Zero out the digest in the chunk.
3955 * 3. Compute the new digest
3956 * 4. Compare saved and new digests.
3957 */
3958 digest = auth_hdr->hmac;
3959 skb_pull(chunk->skb, sig_len);
3960
3961 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3962 if (!save_digest)
3963 goto nomem;
3964
3965 memset(digest, 0, sig_len);
3966
3967 sctp_auth_calculate_hmac(asoc, chunk->skb,
3968 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3969 GFP_ATOMIC);
3970
3971 /* Discard the packet if the digests do not match */
3972 if (memcmp(save_digest, digest, sig_len)) {
3973 kfree(save_digest);
3974 return SCTP_IERROR_BAD_SIG;
3975 }
3976
3977 kfree(save_digest);
3978 chunk->auth = 1;
3979
3980 return SCTP_IERROR_NO_ERROR;
3981nomem:
3982 return SCTP_IERROR_NOMEM;
3983}
3984
3985sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3986 const struct sctp_association *asoc,
3987 const sctp_subtype_t type,
3988 void *arg,
3989 sctp_cmd_seq_t *commands)
3990{
3991 struct sctp_authhdr *auth_hdr;
3992 struct sctp_chunk *chunk = arg;
3993 struct sctp_chunk *err_chunk;
3994 sctp_ierror_t error;
3995
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003996 /* Make sure that the peer has AUTH capable */
3997 if (!asoc->peer.auth_capable)
3998 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3999
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004000 if (!sctp_vtag_verify(chunk, asoc)) {
4001 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4002 SCTP_NULL());
4003 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4004 }
4005
4006 /* Make sure that the AUTH chunk has valid length. */
4007 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4008 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4009 commands);
4010
4011 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4012 error = sctp_sf_authenticate(ep, asoc, type, chunk);
4013 switch (error) {
4014 case SCTP_IERROR_AUTH_BAD_HMAC:
4015 /* Generate the ERROR chunk and discard the rest
4016 * of the packet
4017 */
4018 err_chunk = sctp_make_op_error(asoc, chunk,
4019 SCTP_ERROR_UNSUP_HMAC,
4020 &auth_hdr->hmac_id,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004021 sizeof(__u16), 0);
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004022 if (err_chunk) {
4023 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4024 SCTP_CHUNK(err_chunk));
4025 }
4026 /* Fall Through */
4027 case SCTP_IERROR_AUTH_BAD_KEYID:
4028 case SCTP_IERROR_BAD_SIG:
4029 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4030 break;
4031 case SCTP_IERROR_PROTO_VIOLATION:
4032 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4033 commands);
4034 break;
4035 case SCTP_IERROR_NOMEM:
4036 return SCTP_DISPOSITION_NOMEM;
4037 default:
4038 break;
4039 }
4040
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004041 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4042 struct sctp_ulpevent *ev;
4043
4044 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4045 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4046
4047 if (!ev)
4048 return -ENOMEM;
4049
4050 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4051 SCTP_ULPEVENT(ev));
4052 }
4053
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004054 return SCTP_DISPOSITION_CONSUME;
4055}
4056
4057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 * Process an unknown chunk.
4059 *
4060 * Section: 3.2. Also, 2.1 in the implementor's guide.
4061 *
4062 * Chunk Types are encoded such that the highest-order two bits specify
4063 * the action that must be taken if the processing endpoint does not
4064 * recognize the Chunk Type.
4065 *
4066 * 00 - Stop processing this SCTP packet and discard it, do not process
4067 * any further chunks within it.
4068 *
4069 * 01 - Stop processing this SCTP packet and discard it, do not process
4070 * any further chunks within it, and report the unrecognized
4071 * chunk in an 'Unrecognized Chunk Type'.
4072 *
4073 * 10 - Skip this chunk and continue processing.
4074 *
4075 * 11 - Skip this chunk and continue processing, but report in an ERROR
4076 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4077 *
4078 * The return value is the disposition of the chunk.
4079 */
4080sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4081 const struct sctp_association *asoc,
4082 const sctp_subtype_t type,
4083 void *arg,
4084 sctp_cmd_seq_t *commands)
4085{
4086 struct sctp_chunk *unk_chunk = arg;
4087 struct sctp_chunk *err_chunk;
4088 sctp_chunkhdr_t *hdr;
4089
4090 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4091
4092 if (!sctp_vtag_verify(unk_chunk, asoc))
4093 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4094
4095 /* Make sure that the chunk has a valid length.
4096 * Since we don't know the chunk type, we use a general
4097 * chunkhdr structure to make a comparison.
4098 */
4099 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4100 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4101 commands);
4102
4103 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4104 case SCTP_CID_ACTION_DISCARD:
4105 /* Discard the packet. */
4106 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4107 break;
4108 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 /* Generate an ERROR chunk as response. */
4110 hdr = unk_chunk->chunk_hdr;
4111 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4112 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004113 WORD_ROUND(ntohs(hdr->length)),
4114 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (err_chunk) {
4116 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4117 SCTP_CHUNK(err_chunk));
4118 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004119
4120 /* Discard the packet. */
4121 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 return SCTP_DISPOSITION_CONSUME;
4123 break;
4124 case SCTP_CID_ACTION_SKIP:
4125 /* Skip the chunk. */
4126 return SCTP_DISPOSITION_DISCARD;
4127 break;
4128 case SCTP_CID_ACTION_SKIP_ERR:
4129 /* Generate an ERROR chunk as response. */
4130 hdr = unk_chunk->chunk_hdr;
4131 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4132 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05004133 WORD_ROUND(ntohs(hdr->length)),
4134 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (err_chunk) {
4136 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4137 SCTP_CHUNK(err_chunk));
4138 }
4139 /* Skip the chunk. */
4140 return SCTP_DISPOSITION_CONSUME;
4141 break;
4142 default:
4143 break;
4144 }
4145
4146 return SCTP_DISPOSITION_DISCARD;
4147}
4148
4149/*
4150 * Discard the chunk.
4151 *
4152 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4153 * [Too numerous to mention...]
4154 * Verification Tag: No verification needed.
4155 * Inputs
4156 * (endpoint, asoc, chunk)
4157 *
4158 * Outputs
4159 * (asoc, reply_msg, msg_up, timers, counters)
4160 *
4161 * The return value is the disposition of the chunk.
4162 */
4163sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4164 const struct sctp_association *asoc,
4165 const sctp_subtype_t type,
4166 void *arg,
4167 sctp_cmd_seq_t *commands)
4168{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004169 struct sctp_chunk *chunk = arg;
4170
4171 /* Make sure that the chunk has a valid length.
4172 * Since we don't know the chunk type, we use a general
4173 * chunkhdr structure to make a comparison.
4174 */
4175 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4176 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4177 commands);
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4180 return SCTP_DISPOSITION_DISCARD;
4181}
4182
4183/*
4184 * Discard the whole packet.
4185 *
4186 * Section: 8.4 2)
4187 *
4188 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4189 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 *
4191 * Verification Tag: No verification necessary
4192 *
4193 * Inputs
4194 * (endpoint, asoc, chunk)
4195 *
4196 * Outputs
4197 * (asoc, reply_msg, msg_up, timers, counters)
4198 *
4199 * The return value is the disposition of the chunk.
4200 */
4201sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4202 const struct sctp_association *asoc,
4203 const sctp_subtype_t type,
4204 void *arg,
4205 sctp_cmd_seq_t *commands)
4206{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004207 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4209
4210 return SCTP_DISPOSITION_CONSUME;
4211}
4212
4213
4214/*
4215 * The other end is violating protocol.
4216 *
4217 * Section: Not specified
4218 * Verification Tag: Not specified
4219 * Inputs
4220 * (endpoint, asoc, chunk)
4221 *
4222 * Outputs
4223 * (asoc, reply_msg, msg_up, timers, counters)
4224 *
4225 * We simply tag the chunk as a violation. The state machine will log
4226 * the violation and continue.
4227 */
4228sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4229 const struct sctp_association *asoc,
4230 const sctp_subtype_t type,
4231 void *arg,
4232 sctp_cmd_seq_t *commands)
4233{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004234 struct sctp_chunk *chunk = arg;
4235
4236 /* Make sure that the chunk has a valid length. */
4237 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4238 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4239 commands);
4240
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 return SCTP_DISPOSITION_VIOLATION;
4242}
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004245 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004247static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004248 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004251 sctp_cmd_seq_t *commands,
4252 const __u8 *payload,
4253 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004255 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 struct sctp_chunk *chunk = arg;
4257 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004259 /* SCTP-AUTH, Section 6.3:
4260 * It should be noted that if the receiver wants to tear
4261 * down an association in an authenticated way only, the
4262 * handling of malformed packets should not result in
4263 * tearing down the association.
4264 *
4265 * This means that if we only want to abort associations
4266 * in an authenticated way (i.e AUTH+ABORT), then we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004267 * can't destroy this association just because the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004268 * was malformed.
4269 */
4270 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4271 goto discard;
4272
Jesper Juhl9abed242007-11-11 23:57:49 +01004273 /* Make the abort chunk. */
4274 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4275 if (!abort)
4276 goto nomem;
4277
Vlad Yasevichece25df2007-09-07 16:30:54 -04004278 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004279 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4280 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4281 !asoc->peer.i.init_tag) {
4282 sctp_initack_chunk_t *initack;
4283
4284 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4285 if (!sctp_chunk_length_valid(chunk,
4286 sizeof(sctp_initack_chunk_t)))
4287 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4288 else {
4289 unsigned int inittag;
4290
4291 inittag = ntohl(initack->init_hdr.init_tag);
4292 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4293 SCTP_U32(inittag));
4294 }
4295 }
4296
Vlad Yasevichece25df2007-09-07 16:30:54 -04004297 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4298 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Vlad Yasevichece25df2007-09-07 16:30:54 -04004300 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4301 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4302 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4303 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4304 SCTP_ERROR(ECONNREFUSED));
4305 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4306 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4307 } else {
4308 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4309 SCTP_ERROR(ECONNABORTED));
4310 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4311 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4312 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004315 packet = sctp_ootb_pkt_new(asoc, chunk);
4316
4317 if (!packet)
4318 goto nomem_pkt;
4319
4320 if (sctp_test_T_bit(abort))
4321 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4322
4323 abort->skb->sk = ep->base.sk;
4324
4325 sctp_packet_append_chunk(packet, abort);
4326
4327 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4328 SCTP_PACKET(packet));
4329
4330 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 }
4332
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004334
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004335discard:
4336 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 return SCTP_DISPOSITION_ABORT;
4338
Vlad Yasevichece25df2007-09-07 16:30:54 -04004339nomem_pkt:
4340 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341nomem:
4342 return SCTP_DISPOSITION_NOMEM;
4343}
4344
Wei Yongjunaecedea2007-08-02 16:57:44 +08004345/*
4346 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004347 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004348 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004349 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004350 *
4351 * We inform the other end by sending an ABORT with a Protocol Violation
4352 * error code.
4353 *
4354 * Section: Not specified
4355 * Verification Tag: Nothing to do
4356 * Inputs
4357 * (endpoint, asoc, chunk)
4358 *
4359 * Outputs
4360 * (reply_msg, msg_up, counters)
4361 *
4362 * Generate an ABORT chunk and terminate the association.
4363 */
4364static sctp_disposition_t sctp_sf_violation_chunklen(
4365 const struct sctp_endpoint *ep,
4366 const struct sctp_association *asoc,
4367 const sctp_subtype_t type,
4368 void *arg,
4369 sctp_cmd_seq_t *commands)
4370{
Florian Westphal24448442008-03-23 22:46:34 -07004371 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004372
Vlad Yasevichece25df2007-09-07 16:30:54 -04004373 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004374 sizeof(err_str));
4375}
4376
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004377/*
4378 * Handle a protocol violation when the parameter length is invalid.
Shan Wei33c7cfd2011-04-18 19:11:01 +00004379 * If the length is smaller than the minimum length of a given parameter,
4380 * or accumulated length in multi parameters exceeds the end of the chunk,
4381 * the length is considered as invalid.
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004382 */
4383static sctp_disposition_t sctp_sf_violation_paramlen(
4384 const struct sctp_endpoint *ep,
4385 const struct sctp_association *asoc,
4386 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004387 void *arg, void *ext,
4388 sctp_cmd_seq_t *commands)
4389{
4390 struct sctp_chunk *chunk = arg;
4391 struct sctp_paramhdr *param = ext;
4392 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004393
Wei Yongjunba016672008-09-30 05:32:24 -07004394 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4395 goto discard;
4396
4397 /* Make the abort chunk. */
4398 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4399 if (!abort)
4400 goto nomem;
4401
4402 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4403 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4404
4405 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4406 SCTP_ERROR(ECONNABORTED));
4407 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4408 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4409 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004410 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004411
4412discard:
4413 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004414 return SCTP_DISPOSITION_ABORT;
4415nomem:
4416 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004417}
4418
Wei Yongjunaecedea2007-08-02 16:57:44 +08004419/* Handle a protocol violation when the peer trying to advance the
4420 * cumulative tsn ack to a point beyond the max tsn currently sent.
4421 *
4422 * We inform the other end by sending an ABORT with a Protocol Violation
4423 * error code.
4424 */
4425static sctp_disposition_t sctp_sf_violation_ctsn(
4426 const struct sctp_endpoint *ep,
4427 const struct sctp_association *asoc,
4428 const sctp_subtype_t type,
4429 void *arg,
4430 sctp_cmd_seq_t *commands)
4431{
Florian Westphal24448442008-03-23 22:46:34 -07004432 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004433
Vlad Yasevichece25df2007-09-07 16:30:54 -04004434 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004435 sizeof(err_str));
4436}
4437
Vlad Yasevichece25df2007-09-07 16:30:54 -04004438/* Handle protocol violation of an invalid chunk bundling. For example,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004439 * when we have an association and we receive bundled INIT-ACK, or
Vlad Yasevichece25df2007-09-07 16:30:54 -04004440 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004441 * statement from the specs. Additionally, there might be an attacker
Vlad Yasevichece25df2007-09-07 16:30:54 -04004442 * on the path and we may not want to continue this communication.
4443 */
4444static sctp_disposition_t sctp_sf_violation_chunk(
4445 const struct sctp_endpoint *ep,
4446 const struct sctp_association *asoc,
4447 const sctp_subtype_t type,
4448 void *arg,
4449 sctp_cmd_seq_t *commands)
4450{
Florian Westphal24448442008-03-23 22:46:34 -07004451 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004452
4453 if (!asoc)
4454 return sctp_sf_violation(ep, asoc, type, arg, commands);
4455
4456 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4457 sizeof(err_str));
4458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459/***************************************************************************
4460 * These are the state functions for handling primitive (Section 10) events.
4461 ***************************************************************************/
4462/*
4463 * sctp_sf_do_prm_asoc
4464 *
4465 * Section: 10.1 ULP-to-SCTP
4466 * B) Associate
4467 *
4468 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4469 * outbound stream count)
4470 * -> association id [,destination transport addr list] [,outbound stream
4471 * count]
4472 *
4473 * This primitive allows the upper layer to initiate an association to a
4474 * specific peer endpoint.
4475 *
4476 * The peer endpoint shall be specified by one of the transport addresses
4477 * which defines the endpoint (see Section 1.4). If the local SCTP
4478 * instance has not been initialized, the ASSOCIATE is considered an
4479 * error.
4480 * [This is not relevant for the kernel implementation since we do all
4481 * initialization at boot time. It we hadn't initialized we wouldn't
4482 * get anywhere near this code.]
4483 *
4484 * An association id, which is a local handle to the SCTP association,
4485 * will be returned on successful establishment of the association. If
4486 * SCTP is not able to open an SCTP association with the peer endpoint,
4487 * an error is returned.
4488 * [In the kernel implementation, the struct sctp_association needs to
4489 * be created BEFORE causing this primitive to run.]
4490 *
4491 * Other association parameters may be returned, including the
4492 * complete destination transport addresses of the peer as well as the
4493 * outbound stream count of the local endpoint. One of the transport
4494 * address from the returned destination addresses will be selected by
4495 * the local endpoint as default primary path for sending SCTP packets
4496 * to this peer. The returned "destination transport addr list" can
4497 * be used by the ULP to change the default primary path or to force
4498 * sending a packet to a specific transport address. [All of this
4499 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4500 * function.]
4501 *
4502 * Mandatory attributes:
4503 *
4504 * o local SCTP instance name - obtained from the INITIALIZE operation.
4505 * [This is the argument asoc.]
4506 * o destination transport addr - specified as one of the transport
4507 * addresses of the peer endpoint with which the association is to be
4508 * established.
4509 * [This is asoc->peer.active_path.]
4510 * o outbound stream count - the number of outbound streams the ULP
4511 * would like to open towards this peer endpoint.
4512 * [BUG: This is not currently implemented.]
4513 * Optional attributes:
4514 *
4515 * None.
4516 *
4517 * The return value is a disposition.
4518 */
4519sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4520 const struct sctp_association *asoc,
4521 const sctp_subtype_t type,
4522 void *arg,
4523 sctp_cmd_seq_t *commands)
4524{
4525 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004526 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
4528 /* The comment below says that we enter COOKIE-WAIT AFTER
4529 * sending the INIT, but that doesn't actually work in our
4530 * implementation...
4531 */
4532 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4533 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4534
4535 /* RFC 2960 5.1 Normal Establishment of an Association
4536 *
4537 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4538 * must provide its Verification Tag (Tag_A) in the Initiate
4539 * Tag field. Tag_A SHOULD be a random number in the range of
4540 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4541 */
4542
4543 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4544 if (!repl)
4545 goto nomem;
4546
4547 /* Cast away the const modifier, as we want to just
4548 * rerun it through as a sideffect.
4549 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004550 my_asoc = (struct sctp_association *)asoc;
4551 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Frank Filz3f7a87d2005-06-20 13:14:57 -07004553 /* Choose transport for INIT. */
4554 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4555 SCTP_CHUNK(repl));
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 /* After sending the INIT, "A" starts the T1-init timer and
4558 * enters the COOKIE-WAIT state.
4559 */
4560 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4561 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4562 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4563 return SCTP_DISPOSITION_CONSUME;
4564
4565nomem:
4566 return SCTP_DISPOSITION_NOMEM;
4567}
4568
4569/*
4570 * Process the SEND primitive.
4571 *
4572 * Section: 10.1 ULP-to-SCTP
4573 * E) Send
4574 *
4575 * Format: SEND(association id, buffer address, byte count [,context]
4576 * [,stream id] [,life time] [,destination transport address]
4577 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4578 * -> result
4579 *
4580 * This is the main method to send user data via SCTP.
4581 *
4582 * Mandatory attributes:
4583 *
4584 * o association id - local handle to the SCTP association
4585 *
4586 * o buffer address - the location where the user message to be
4587 * transmitted is stored;
4588 *
4589 * o byte count - The size of the user data in number of bytes;
4590 *
4591 * Optional attributes:
4592 *
4593 * o context - an optional 32 bit integer that will be carried in the
4594 * sending failure notification to the ULP if the transportation of
4595 * this User Message fails.
4596 *
4597 * o stream id - to indicate which stream to send the data on. If not
4598 * specified, stream 0 will be used.
4599 *
4600 * o life time - specifies the life time of the user data. The user data
4601 * will not be sent by SCTP after the life time expires. This
4602 * parameter can be used to avoid efforts to transmit stale
4603 * user messages. SCTP notifies the ULP if the data cannot be
4604 * initiated to transport (i.e. sent to the destination via SCTP's
4605 * send primitive) within the life time variable. However, the
4606 * user data will be transmitted if SCTP has attempted to transmit a
4607 * chunk before the life time expired.
4608 *
4609 * o destination transport address - specified as one of the destination
4610 * transport addresses of the peer endpoint to which this packet
4611 * should be sent. Whenever possible, SCTP should use this destination
4612 * transport address for sending the packets, instead of the current
4613 * primary path.
4614 *
4615 * o unorder flag - this flag, if present, indicates that the user
4616 * would like the data delivered in an unordered fashion to the peer
4617 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4618 * message).
4619 *
4620 * o no-bundle flag - instructs SCTP not to bundle this user data with
4621 * other outbound DATA chunks. SCTP MAY still bundle even when
4622 * this flag is present, when faced with network congestion.
4623 *
4624 * o payload protocol-id - A 32 bit unsigned integer that is to be
4625 * passed to the peer indicating the type of payload protocol data
4626 * being transmitted. This value is passed as opaque data by SCTP.
4627 *
4628 * The return value is the disposition.
4629 */
4630sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4631 const struct sctp_association *asoc,
4632 const sctp_subtype_t type,
4633 void *arg,
4634 sctp_cmd_seq_t *commands)
4635{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004636 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004638 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 return SCTP_DISPOSITION_CONSUME;
4640}
4641
4642/*
4643 * Process the SHUTDOWN primitive.
4644 *
4645 * Section: 10.1:
4646 * C) Shutdown
4647 *
4648 * Format: SHUTDOWN(association id)
4649 * -> result
4650 *
4651 * Gracefully closes an association. Any locally queued user data
4652 * will be delivered to the peer. The association will be terminated only
4653 * after the peer acknowledges all the SCTP packets sent. A success code
4654 * will be returned on successful termination of the association. If
4655 * attempting to terminate the association results in a failure, an error
4656 * code shall be returned.
4657 *
4658 * Mandatory attributes:
4659 *
4660 * o association id - local handle to the SCTP association
4661 *
4662 * Optional attributes:
4663 *
4664 * None.
4665 *
4666 * The return value is the disposition.
4667 */
4668sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4669 const struct sctp_endpoint *ep,
4670 const struct sctp_association *asoc,
4671 const sctp_subtype_t type,
4672 void *arg,
4673 sctp_cmd_seq_t *commands)
4674{
4675 int disposition;
4676
4677 /* From 9.2 Shutdown of an Association
4678 * Upon receipt of the SHUTDOWN primitive from its upper
4679 * layer, the endpoint enters SHUTDOWN-PENDING state and
4680 * remains there until all outstanding data has been
4681 * acknowledged by its peer. The endpoint accepts no new data
4682 * from its upper layer, but retransmits data to the far end
4683 * if necessary to fill gaps.
4684 */
4685 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4686 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 disposition = SCTP_DISPOSITION_CONSUME;
4689 if (sctp_outq_is_empty(&asoc->outqueue)) {
4690 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4691 arg, commands);
4692 }
4693 return disposition;
4694}
4695
4696/*
4697 * Process the ABORT primitive.
4698 *
4699 * Section: 10.1:
4700 * C) Abort
4701 *
4702 * Format: Abort(association id [, cause code])
4703 * -> result
4704 *
4705 * Ungracefully closes an association. Any locally queued user data
4706 * will be discarded and an ABORT chunk is sent to the peer. A success code
4707 * will be returned on successful abortion of the association. If
4708 * attempting to abort the association results in a failure, an error
4709 * code shall be returned.
4710 *
4711 * Mandatory attributes:
4712 *
4713 * o association id - local handle to the SCTP association
4714 *
4715 * Optional attributes:
4716 *
4717 * o cause code - reason of the abort to be passed to the peer
4718 *
4719 * None.
4720 *
4721 * The return value is the disposition.
4722 */
4723sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4724 const struct sctp_endpoint *ep,
4725 const struct sctp_association *asoc,
4726 const sctp_subtype_t type,
4727 void *arg,
4728 sctp_cmd_seq_t *commands)
4729{
4730 /* From 9.1 Abort of an Association
4731 * Upon receipt of the ABORT primitive from its upper
4732 * layer, the endpoint enters CLOSED state and
4733 * discard all outstanding data has been
4734 * acknowledged by its peer. The endpoint accepts no new data
4735 * from its upper layer, but retransmits data to the far end
4736 * if necessary to fill gaps.
4737 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004738 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 sctp_disposition_t retval;
4740
4741 retval = SCTP_DISPOSITION_CONSUME;
4742
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004743 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 /* Even if we can't send the ABORT due to low memory delete the
4746 * TCB. This is a departure from our typical NOMEM handling.
4747 */
4748
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004749 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4750 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 /* Delete the established association. */
4752 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004753 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4756 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4757
4758 return retval;
4759}
4760
4761/* We tried an illegal operation on an association which is closed. */
4762sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4763 const struct sctp_association *asoc,
4764 const sctp_subtype_t type,
4765 void *arg,
4766 sctp_cmd_seq_t *commands)
4767{
4768 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4769 return SCTP_DISPOSITION_CONSUME;
4770}
4771
4772/* We tried an illegal operation on an association which is shutting
4773 * down.
4774 */
4775sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4776 const struct sctp_association *asoc,
4777 const sctp_subtype_t type,
4778 void *arg,
4779 sctp_cmd_seq_t *commands)
4780{
4781 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4782 SCTP_ERROR(-ESHUTDOWN));
4783 return SCTP_DISPOSITION_CONSUME;
4784}
4785
4786/*
4787 * sctp_cookie_wait_prm_shutdown
4788 *
4789 * Section: 4 Note: 2
4790 * Verification Tag:
4791 * Inputs
4792 * (endpoint, asoc)
4793 *
4794 * The RFC does not explicitly address this issue, but is the route through the
4795 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4796 *
4797 * Outputs
4798 * (timers)
4799 */
4800sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4801 const struct sctp_endpoint *ep,
4802 const struct sctp_association *asoc,
4803 const sctp_subtype_t type,
4804 void *arg,
4805 sctp_cmd_seq_t *commands)
4806{
4807 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4808 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4809
4810 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4811 SCTP_STATE(SCTP_STATE_CLOSED));
4812
4813 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4814
4815 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4816
4817 return SCTP_DISPOSITION_DELETE_TCB;
4818}
4819
4820/*
4821 * sctp_cookie_echoed_prm_shutdown
4822 *
4823 * Section: 4 Note: 2
4824 * Verification Tag:
4825 * Inputs
4826 * (endpoint, asoc)
4827 *
4828 * The RFC does not explcitly address this issue, but is the route through the
4829 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4830 *
4831 * Outputs
4832 * (timers)
4833 */
4834sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4835 const struct sctp_endpoint *ep,
4836 const struct sctp_association *asoc,
4837 const sctp_subtype_t type,
4838 void *arg, sctp_cmd_seq_t *commands)
4839{
4840 /* There is a single T1 timer, so we should be able to use
4841 * common function with the COOKIE-WAIT state.
4842 */
4843 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4844}
4845
4846/*
4847 * sctp_sf_cookie_wait_prm_abort
4848 *
4849 * Section: 4 Note: 2
4850 * Verification Tag:
4851 * Inputs
4852 * (endpoint, asoc)
4853 *
4854 * The RFC does not explicitly address this issue, but is the route through the
4855 * state table when someone issues an abort while in COOKIE_WAIT state.
4856 *
4857 * Outputs
4858 * (timers)
4859 */
4860sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4861 const struct sctp_endpoint *ep,
4862 const struct sctp_association *asoc,
4863 const sctp_subtype_t type,
4864 void *arg,
4865 sctp_cmd_seq_t *commands)
4866{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004867 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 sctp_disposition_t retval;
4869
4870 /* Stop T1-init timer */
4871 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4872 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4873 retval = SCTP_DISPOSITION_CONSUME;
4874
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004875 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4878 SCTP_STATE(SCTP_STATE_CLOSED));
4879
4880 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4881
4882 /* Even if we can't send the ABORT due to low memory delete the
4883 * TCB. This is a departure from our typical NOMEM handling.
4884 */
4885
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004886 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4887 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 /* Delete the established association. */
4889 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004890 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
4892 return retval;
4893}
4894
4895/*
4896 * sctp_sf_cookie_echoed_prm_abort
4897 *
4898 * Section: 4 Note: 3
4899 * Verification Tag:
4900 * Inputs
4901 * (endpoint, asoc)
4902 *
4903 * The RFC does not explcitly address this issue, but is the route through the
4904 * state table when someone issues an abort while in COOKIE_ECHOED state.
4905 *
4906 * Outputs
4907 * (timers)
4908 */
4909sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4910 const struct sctp_endpoint *ep,
4911 const struct sctp_association *asoc,
4912 const sctp_subtype_t type,
4913 void *arg,
4914 sctp_cmd_seq_t *commands)
4915{
4916 /* There is a single T1 timer, so we should be able to use
4917 * common function with the COOKIE-WAIT state.
4918 */
4919 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4920}
4921
4922/*
4923 * sctp_sf_shutdown_pending_prm_abort
4924 *
4925 * Inputs
4926 * (endpoint, asoc)
4927 *
4928 * The RFC does not explicitly address this issue, but is the route through the
4929 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4930 *
4931 * Outputs
4932 * (timers)
4933 */
4934sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4935 const struct sctp_endpoint *ep,
4936 const struct sctp_association *asoc,
4937 const sctp_subtype_t type,
4938 void *arg,
4939 sctp_cmd_seq_t *commands)
4940{
4941 /* Stop the T5-shutdown guard timer. */
4942 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4943 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4944
4945 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4946}
4947
4948/*
4949 * sctp_sf_shutdown_sent_prm_abort
4950 *
4951 * Inputs
4952 * (endpoint, asoc)
4953 *
4954 * The RFC does not explicitly address this issue, but is the route through the
4955 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4956 *
4957 * Outputs
4958 * (timers)
4959 */
4960sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4961 const struct sctp_endpoint *ep,
4962 const struct sctp_association *asoc,
4963 const sctp_subtype_t type,
4964 void *arg,
4965 sctp_cmd_seq_t *commands)
4966{
4967 /* Stop the T2-shutdown timer. */
4968 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4969 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4970
4971 /* Stop the T5-shutdown guard timer. */
4972 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4973 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4974
4975 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4976}
4977
4978/*
4979 * sctp_sf_cookie_echoed_prm_abort
4980 *
4981 * Inputs
4982 * (endpoint, asoc)
4983 *
4984 * The RFC does not explcitly address this issue, but is the route through the
4985 * state table when someone issues an abort while in COOKIE_ECHOED state.
4986 *
4987 * Outputs
4988 * (timers)
4989 */
4990sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4991 const struct sctp_endpoint *ep,
4992 const struct sctp_association *asoc,
4993 const sctp_subtype_t type,
4994 void *arg,
4995 sctp_cmd_seq_t *commands)
4996{
4997 /* The same T2 timer, so we should be able to use
4998 * common function with the SHUTDOWN-SENT state.
4999 */
5000 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
5001}
5002
5003/*
5004 * Process the REQUESTHEARTBEAT primitive
5005 *
5006 * 10.1 ULP-to-SCTP
5007 * J) Request Heartbeat
5008 *
5009 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5010 *
5011 * -> result
5012 *
5013 * Instructs the local endpoint to perform a HeartBeat on the specified
5014 * destination transport address of the given association. The returned
5015 * result should indicate whether the transmission of the HEARTBEAT
5016 * chunk to the destination address is successful.
5017 *
5018 * Mandatory attributes:
5019 *
5020 * o association id - local handle to the SCTP association
5021 *
5022 * o destination transport address - the transport address of the
5023 * association on which a heartbeat should be issued.
5024 */
5025sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5026 const struct sctp_endpoint *ep,
5027 const struct sctp_association *asoc,
5028 const sctp_subtype_t type,
5029 void *arg,
5030 sctp_cmd_seq_t *commands)
5031{
Vlad Yasevichfb785252007-03-19 17:02:03 -07005032 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5033 (struct sctp_transport *)arg, commands))
5034 return SCTP_DISPOSITION_NOMEM;
5035
5036 /*
5037 * RFC 2960 (bis), section 8.3
5038 *
5039 * D) Request an on-demand HEARTBEAT on a specific destination
5040 * transport address of a given association.
5041 *
5042 * The endpoint should increment the respective error counter of
5043 * the destination transport address each time a HEARTBEAT is sent
5044 * to that address and not acknowledged within one RTO.
5045 *
5046 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005047 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005048 SCTP_TRANSPORT(arg));
5049 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050}
5051
5052/*
5053 * ADDIP Section 4.1 ASCONF Chunk Procedures
5054 * When an endpoint has an ASCONF signaled change to be sent to the
5055 * remote endpoint it should do A1 to A9
5056 */
5057sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5058 const struct sctp_association *asoc,
5059 const sctp_subtype_t type,
5060 void *arg,
5061 sctp_cmd_seq_t *commands)
5062{
5063 struct sctp_chunk *chunk = arg;
5064
5065 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5066 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5067 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5068 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5069 return SCTP_DISPOSITION_CONSUME;
5070}
5071
5072/*
5073 * Ignore the primitive event
5074 *
5075 * The return value is the disposition of the primitive.
5076 */
5077sctp_disposition_t sctp_sf_ignore_primitive(
5078 const struct sctp_endpoint *ep,
5079 const struct sctp_association *asoc,
5080 const sctp_subtype_t type,
5081 void *arg,
5082 sctp_cmd_seq_t *commands)
5083{
5084 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5085 return SCTP_DISPOSITION_DISCARD;
5086}
5087
5088/***************************************************************************
5089 * These are the state functions for the OTHER events.
5090 ***************************************************************************/
5091
5092/*
5093 * Start the shutdown negotiation.
5094 *
5095 * From Section 9.2:
5096 * Once all its outstanding data has been acknowledged, the endpoint
5097 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5098 * TSN Ack field the last sequential TSN it has received from the peer.
5099 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5100 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5101 * with the updated last sequential TSN received from its peer.
5102 *
5103 * The return value is the disposition.
5104 */
5105sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5106 const struct sctp_endpoint *ep,
5107 const struct sctp_association *asoc,
5108 const sctp_subtype_t type,
5109 void *arg,
5110 sctp_cmd_seq_t *commands)
5111{
5112 struct sctp_chunk *reply;
5113
5114 /* Once all its outstanding data has been acknowledged, the
5115 * endpoint shall send a SHUTDOWN chunk to its peer including
5116 * in the Cumulative TSN Ack field the last sequential TSN it
5117 * has received from the peer.
5118 */
5119 reply = sctp_make_shutdown(asoc, NULL);
5120 if (!reply)
5121 goto nomem;
5122
5123 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5124 * T2-shutdown timer.
5125 */
5126 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5127
5128 /* It shall then start the T2-shutdown timer */
5129 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5130 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5131
Wei Yongjun536428a2008-09-05 08:55:26 +08005132 /* RFC 4960 Section 9.2
5133 * The sender of the SHUTDOWN MAY also start an overall guard timer
5134 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5135 */
5136 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5137 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5138
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 if (asoc->autoclose)
5140 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5141 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5142
5143 /* and enter the SHUTDOWN-SENT state. */
5144 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5145 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5146
5147 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5148 *
5149 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005150 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 */
5152 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5153
5154 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5155
5156 return SCTP_DISPOSITION_CONSUME;
5157
5158nomem:
5159 return SCTP_DISPOSITION_NOMEM;
5160}
5161
5162/*
5163 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5164 *
5165 * From Section 9.2:
5166 *
5167 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5168 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5169 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5170 * endpoint must re-send the SHUTDOWN ACK.
5171 *
5172 * The return value is the disposition.
5173 */
5174sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5175 const struct sctp_endpoint *ep,
5176 const struct sctp_association *asoc,
5177 const sctp_subtype_t type,
5178 void *arg,
5179 sctp_cmd_seq_t *commands)
5180{
5181 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5182 struct sctp_chunk *reply;
5183
5184 /* There are 2 ways of getting here:
5185 * 1) called in response to a SHUTDOWN chunk
5186 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5187 *
5188 * For the case (2), the arg parameter is set to NULL. We need
5189 * to check that we have a chunk before accessing it's fields.
5190 */
5191 if (chunk) {
5192 if (!sctp_vtag_verify(chunk, asoc))
5193 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5194
5195 /* Make sure that the SHUTDOWN chunk has a valid length. */
5196 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5197 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5198 commands);
5199 }
5200
5201 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5202 * shall send a SHUTDOWN ACK ...
5203 */
5204 reply = sctp_make_shutdown_ack(asoc, chunk);
5205 if (!reply)
5206 goto nomem;
5207
5208 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5209 * the T2-shutdown timer.
5210 */
5211 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5212
5213 /* and start/restart a T2-shutdown timer of its own, */
5214 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5215 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5216
5217 if (asoc->autoclose)
5218 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5219 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5220
5221 /* Enter the SHUTDOWN-ACK-SENT state. */
5222 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5223 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5224
5225 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5226 *
5227 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005228 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 */
5230 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5231
5232 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5233
5234 return SCTP_DISPOSITION_CONSUME;
5235
5236nomem:
5237 return SCTP_DISPOSITION_NOMEM;
5238}
5239
5240/*
5241 * Ignore the event defined as other
5242 *
5243 * The return value is the disposition of the event.
5244 */
5245sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5246 const struct sctp_association *asoc,
5247 const sctp_subtype_t type,
5248 void *arg,
5249 sctp_cmd_seq_t *commands)
5250{
5251 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5252 return SCTP_DISPOSITION_DISCARD;
5253}
5254
5255/************************************************************
5256 * These are the state functions for handling timeout events.
5257 ************************************************************/
5258
5259/*
5260 * RTX Timeout
5261 *
5262 * Section: 6.3.3 Handle T3-rtx Expiration
5263 *
5264 * Whenever the retransmission timer T3-rtx expires for a destination
5265 * address, do the following:
5266 * [See below]
5267 *
5268 * The return value is the disposition of the chunk.
5269 */
5270sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5271 const struct sctp_association *asoc,
5272 const sctp_subtype_t type,
5273 void *arg,
5274 sctp_cmd_seq_t *commands)
5275{
5276 struct sctp_transport *transport = arg;
5277
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005278 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005281 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5282 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5284 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005285 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5287 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5288 return SCTP_DISPOSITION_DELETE_TCB;
5289 }
5290
5291 /* E1) For the destination address for which the timer
5292 * expires, adjust its ssthresh with rules defined in Section
5293 * 7.2.3 and set the cwnd <- MTU.
5294 */
5295
5296 /* E2) For the destination address for which the timer
5297 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5298 * maximum value discussed in rule C7 above (RTO.max) may be
5299 * used to provide an upper bound to this doubling operation.
5300 */
5301
5302 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5303 * outstanding DATA chunks for the address for which the
5304 * T3-rtx has expired will fit into a single packet, subject
5305 * to the MTU constraint for the path corresponding to the
5306 * destination transport address to which the retransmission
5307 * is being sent (this may be different from the address for
5308 * which the timer expires [see Section 6.4]). Call this
5309 * value K. Bundle and retransmit those K DATA chunks in a
5310 * single packet to the destination endpoint.
5311 *
5312 * Note: Any DATA chunks that were sent to the address for
5313 * which the T3-rtx timer expired but did not fit in one MTU
5314 * (rule E3 above), should be marked for retransmission and
5315 * sent as soon as cwnd allows (normally when a SACK arrives).
5316 */
5317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 /* Do some failure management (Section 8.2). */
5319 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5320
Vlad Yasevich1845a572007-02-21 02:06:19 -08005321 /* NB: Rules E4 and F1 are implicit in R1. */
5322 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 return SCTP_DISPOSITION_CONSUME;
5325}
5326
5327/*
5328 * Generate delayed SACK on timeout
5329 *
5330 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5331 *
5332 * The guidelines on delayed acknowledgement algorithm specified in
5333 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5334 * acknowledgement SHOULD be generated for at least every second packet
5335 * (not every second DATA chunk) received, and SHOULD be generated
5336 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5337 * some situations it may be beneficial for an SCTP transmitter to be
5338 * more conservative than the algorithms detailed in this document
5339 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5340 * the following algorithms allow.
5341 */
5342sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5343 const struct sctp_association *asoc,
5344 const sctp_subtype_t type,
5345 void *arg,
5346 sctp_cmd_seq_t *commands)
5347{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005348 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5350 return SCTP_DISPOSITION_CONSUME;
5351}
5352
5353/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005354 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 *
5356 * Section: 4 Note: 2
5357 * Verification Tag:
5358 * Inputs
5359 * (endpoint, asoc)
5360 *
5361 * RFC 2960 Section 4 Notes
5362 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5363 * and re-start the T1-init timer without changing state. This MUST
5364 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5365 * endpoint MUST abort the initialization process and report the
5366 * error to SCTP user.
5367 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005368 * Outputs
5369 * (timers, events)
5370 *
5371 */
5372sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5373 const struct sctp_association *asoc,
5374 const sctp_subtype_t type,
5375 void *arg,
5376 sctp_cmd_seq_t *commands)
5377{
5378 struct sctp_chunk *repl = NULL;
5379 struct sctp_bind_addr *bp;
5380 int attempts = asoc->init_err_counter + 1;
5381
5382 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005383 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005384
Vlad Yasevich81845c22006-01-30 15:59:54 -08005385 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005386 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5387 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5388 if (!repl)
5389 return SCTP_DISPOSITION_NOMEM;
5390
5391 /* Choose transport for INIT. */
5392 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5393 SCTP_CHUNK(repl));
5394
5395 /* Issue a sideeffect to do the needed accounting. */
5396 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5397 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5398
5399 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5400 } else {
5401 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5402 " max_init_attempts: %d\n",
5403 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005404 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5405 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005406 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005407 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005408 return SCTP_DISPOSITION_DELETE_TCB;
5409 }
5410
5411 return SCTP_DISPOSITION_CONSUME;
5412}
5413
5414/*
5415 * sctp_sf_t1_cookie_timer_expire
5416 *
5417 * Section: 4 Note: 2
5418 * Verification Tag:
5419 * Inputs
5420 * (endpoint, asoc)
5421 *
5422 * RFC 2960 Section 4 Notes
5423 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 * COOKIE ECHO and re-start the T1-cookie timer without changing
5425 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5426 * After that, the endpoint MUST abort the initialization process and
5427 * report the error to SCTP user.
5428 *
5429 * Outputs
5430 * (timers, events)
5431 *
5432 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005433sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 const struct sctp_association *asoc,
5435 const sctp_subtype_t type,
5436 void *arg,
5437 sctp_cmd_seq_t *commands)
5438{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005439 struct sctp_chunk *repl = NULL;
5440 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
Frank Filz3f7a87d2005-06-20 13:14:57 -07005442 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005443 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Vlad Yasevich81845c22006-01-30 15:59:54 -08005445 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005446 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005448 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005450 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5451 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005453 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5454 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5457 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005458 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5459 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005461 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return SCTP_DISPOSITION_DELETE_TCB;
5463 }
5464
5465 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466}
5467
5468/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5469 * with the updated last sequential TSN received from its peer.
5470 *
5471 * An endpoint should limit the number of retransmissions of the
5472 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5473 * If this threshold is exceeded the endpoint should destroy the TCB and
5474 * MUST report the peer endpoint unreachable to the upper layer (and
5475 * thus the association enters the CLOSED state). The reception of any
5476 * packet from its peer (i.e. as the peer sends all of its queued DATA
5477 * chunks) should clear the endpoint's retransmission count and restart
5478 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5479 * all of its queued DATA chunks that have not yet been sent.
5480 */
5481sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5482 const struct sctp_association *asoc,
5483 const sctp_subtype_t type,
5484 void *arg,
5485 sctp_cmd_seq_t *commands)
5486{
5487 struct sctp_chunk *reply = NULL;
5488
5489 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005490 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5491
Neil Horman58fbbed2008-02-29 11:40:56 -08005492 ((struct sctp_association *)asoc)->shutdown_retries++;
5493
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005495 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5496 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5498 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005499 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5501 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5502 return SCTP_DISPOSITION_DELETE_TCB;
5503 }
5504
5505 switch (asoc->state) {
5506 case SCTP_STATE_SHUTDOWN_SENT:
5507 reply = sctp_make_shutdown(asoc, NULL);
5508 break;
5509
5510 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5511 reply = sctp_make_shutdown_ack(asoc, NULL);
5512 break;
5513
5514 default:
5515 BUG();
5516 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518
5519 if (!reply)
5520 goto nomem;
5521
Wei Yongjun6345b192009-04-26 23:13:35 +08005522 /* Do some failure management (Section 8.2).
5523 * If we remove the transport an SHUTDOWN was last sent to, don't
5524 * do failure management.
5525 */
5526 if (asoc->shutdown_last_sent_to)
5527 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5528 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5531 * the T2-shutdown timer.
5532 */
5533 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5534
5535 /* Restart the T2-shutdown timer. */
5536 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5537 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5538 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5539 return SCTP_DISPOSITION_CONSUME;
5540
5541nomem:
5542 return SCTP_DISPOSITION_NOMEM;
5543}
5544
5545/*
5546 * ADDIP Section 4.1 ASCONF CHunk Procedures
5547 * If the T4 RTO timer expires the endpoint should do B1 to B5
5548 */
5549sctp_disposition_t sctp_sf_t4_timer_expire(
5550 const struct sctp_endpoint *ep,
5551 const struct sctp_association *asoc,
5552 const sctp_subtype_t type,
5553 void *arg,
5554 sctp_cmd_seq_t *commands)
5555{
5556 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5557 struct sctp_transport *transport = chunk->transport;
5558
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005559 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5562 * detection on the appropriate destination address as defined in
5563 * RFC2960 [5] section 8.1 and 8.2.
5564 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005565 if (transport)
5566 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5567 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
5569 /* Reconfig T4 timer and transport. */
5570 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5571
5572 /* ADDIP 4.1 B2) Increment the association error counters and perform
5573 * endpoint failure detection on the association as defined in
5574 * RFC2960 [5] section 8.1 and 8.2.
5575 * association error counter is incremented in SCTP_CMD_STRIKE.
5576 */
5577 if (asoc->overall_error_count >= asoc->max_retrans) {
5578 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5579 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005580 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5581 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005583 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjun3d5a0192008-10-09 14:32:24 -07005585 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return SCTP_DISPOSITION_ABORT;
5587 }
5588
5589 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5590 * the ASCONF chunk was sent by doubling the RTO timer value.
5591 * This is done in SCTP_CMD_STRIKE.
5592 */
5593
5594 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5595 * choose an alternate destination address (please refer to RFC2960
5596 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005597 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 * ASCONF sent.
5599 */
5600 sctp_chunk_hold(asoc->addip_last_asconf);
5601 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5602 SCTP_CHUNK(asoc->addip_last_asconf));
5603
5604 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5605 * destination is selected, then the RTO used will be that of the new
5606 * destination address.
5607 */
5608 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5609 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5610
5611 return SCTP_DISPOSITION_CONSUME;
5612}
5613
5614/* sctpimpguide-05 Section 2.12.2
5615 * The sender of the SHUTDOWN MAY also start an overall guard timer
5616 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5617 * At the expiration of this timer the sender SHOULD abort the association
5618 * by sending an ABORT chunk.
5619 */
5620sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5621 const struct sctp_association *asoc,
5622 const sctp_subtype_t type,
5623 void *arg,
5624 sctp_cmd_seq_t *commands)
5625{
5626 struct sctp_chunk *reply = NULL;
5627
5628 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005629 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
5631 reply = sctp_make_abort(asoc, NULL, 0);
5632 if (!reply)
5633 goto nomem;
5634
5635 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005636 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5637 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005639 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005641 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5642 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 return SCTP_DISPOSITION_DELETE_TCB;
5645nomem:
5646 return SCTP_DISPOSITION_NOMEM;
5647}
5648
5649/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5650 * the association is automatically closed by starting the shutdown process.
5651 * The work that needs to be done is same as when SHUTDOWN is initiated by
5652 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5653 */
5654sctp_disposition_t sctp_sf_autoclose_timer_expire(
5655 const struct sctp_endpoint *ep,
5656 const struct sctp_association *asoc,
5657 const sctp_subtype_t type,
5658 void *arg,
5659 sctp_cmd_seq_t *commands)
5660{
5661 int disposition;
5662
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005663 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 /* From 9.2 Shutdown of an Association
5666 * Upon receipt of the SHUTDOWN primitive from its upper
5667 * layer, the endpoint enters SHUTDOWN-PENDING state and
5668 * remains there until all outstanding data has been
5669 * acknowledged by its peer. The endpoint accepts no new data
5670 * from its upper layer, but retransmits data to the far end
5671 * if necessary to fill gaps.
5672 */
5673 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5674 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 disposition = SCTP_DISPOSITION_CONSUME;
5677 if (sctp_outq_is_empty(&asoc->outqueue)) {
5678 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5679 arg, commands);
5680 }
5681 return disposition;
5682}
5683
5684/*****************************************************************************
5685 * These are sa state functions which could apply to all types of events.
5686 ****************************************************************************/
5687
5688/*
5689 * This table entry is not implemented.
5690 *
5691 * Inputs
5692 * (endpoint, asoc, chunk)
5693 *
5694 * The return value is the disposition of the chunk.
5695 */
5696sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5697 const struct sctp_association *asoc,
5698 const sctp_subtype_t type,
5699 void *arg,
5700 sctp_cmd_seq_t *commands)
5701{
5702 return SCTP_DISPOSITION_NOT_IMPL;
5703}
5704
5705/*
5706 * This table entry represents a bug.
5707 *
5708 * Inputs
5709 * (endpoint, asoc, chunk)
5710 *
5711 * The return value is the disposition of the chunk.
5712 */
5713sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5714 const struct sctp_association *asoc,
5715 const sctp_subtype_t type,
5716 void *arg,
5717 sctp_cmd_seq_t *commands)
5718{
5719 return SCTP_DISPOSITION_BUG;
5720}
5721
5722/*
5723 * This table entry represents the firing of a timer in the wrong state.
5724 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5725 * when the association is in the wrong state. This event should
5726 * be ignored, so as to prevent any rearming of the timer.
5727 *
5728 * Inputs
5729 * (endpoint, asoc, chunk)
5730 *
5731 * The return value is the disposition of the chunk.
5732 */
5733sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5734 const struct sctp_association *asoc,
5735 const sctp_subtype_t type,
5736 void *arg,
5737 sctp_cmd_seq_t *commands)
5738{
5739 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5740 return SCTP_DISPOSITION_CONSUME;
5741}
5742
5743/********************************************************************
5744 * 2nd Level Abstractions
5745 ********************************************************************/
5746
5747/* Pull the SACK chunk based on the SACK header. */
5748static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5749{
5750 struct sctp_sackhdr *sack;
5751 unsigned int len;
5752 __u16 num_blocks;
5753 __u16 num_dup_tsns;
5754
5755 /* Protect ourselves from reading too far into
5756 * the skb from a bogus sender.
5757 */
5758 sack = (struct sctp_sackhdr *) chunk->skb->data;
5759
5760 num_blocks = ntohs(sack->num_gap_ack_blocks);
5761 num_dup_tsns = ntohs(sack->num_dup_tsns);
5762 len = sizeof(struct sctp_sackhdr);
5763 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5764 if (len > chunk->skb->len)
5765 return NULL;
5766
5767 skb_pull(chunk->skb, len);
5768
5769 return sack;
5770}
5771
5772/* Create an ABORT packet to be sent as a response, with the specified
5773 * error causes.
5774 */
5775static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5776 const struct sctp_association *asoc,
5777 struct sctp_chunk *chunk,
5778 const void *payload,
5779 size_t paylen)
5780{
5781 struct sctp_packet *packet;
5782 struct sctp_chunk *abort;
5783
5784 packet = sctp_ootb_pkt_new(asoc, chunk);
5785
5786 if (packet) {
5787 /* Make an ABORT.
5788 * The T bit will be set if the asoc is NULL.
5789 */
5790 abort = sctp_make_abort(asoc, chunk, paylen);
5791 if (!abort) {
5792 sctp_ootb_pkt_free(packet);
5793 return NULL;
5794 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005795
5796 /* Reflect vtag if T-Bit is set */
5797 if (sctp_test_T_bit(abort))
5798 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5799
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 /* Add specified error causes, i.e., payload, to the
5801 * end of the chunk.
5802 */
5803 sctp_addto_chunk(abort, paylen, payload);
5804
5805 /* Set the skb to the belonging sock for accounting. */
5806 abort->skb->sk = ep->base.sk;
5807
5808 sctp_packet_append_chunk(packet, abort);
5809
5810 }
5811
5812 return packet;
5813}
5814
5815/* Allocate a packet for responding in the OOTB conditions. */
5816static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5817 const struct sctp_chunk *chunk)
5818{
5819 struct sctp_packet *packet;
5820 struct sctp_transport *transport;
5821 __u16 sport;
5822 __u16 dport;
5823 __u32 vtag;
5824
5825 /* Get the source and destination port from the inbound packet. */
5826 sport = ntohs(chunk->sctp_hdr->dest);
5827 dport = ntohs(chunk->sctp_hdr->source);
5828
5829 /* The V-tag is going to be the same as the inbound packet if no
5830 * association exists, otherwise, use the peer's vtag.
5831 */
5832 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005833 /* Special case the INIT-ACK as there is no peer's vtag
5834 * yet.
5835 */
5836 switch(chunk->chunk_hdr->type) {
5837 case SCTP_CID_INIT_ACK:
5838 {
5839 sctp_initack_chunk_t *initack;
5840
5841 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5842 vtag = ntohl(initack->init_hdr.init_tag);
5843 break;
5844 }
5845 default:
5846 vtag = asoc->peer.i.init_tag;
5847 break;
5848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 } else {
5850 /* Special case the INIT and stale COOKIE_ECHO as there is no
5851 * vtag yet.
5852 */
5853 switch(chunk->chunk_hdr->type) {
5854 case SCTP_CID_INIT:
5855 {
5856 sctp_init_chunk_t *init;
5857
5858 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5859 vtag = ntohl(init->init_hdr.init_tag);
5860 break;
5861 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005862 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 vtag = ntohl(chunk->sctp_hdr->vtag);
5864 break;
5865 }
5866 }
5867
5868 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005869 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 if (!transport)
5871 goto nomem;
5872
5873 /* Cache a route for the transport with the chunk's destination as
5874 * the source address.
5875 */
Al Viro16b0a032006-11-20 17:13:38 -08005876 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877 sctp_sk(sctp_get_ctl_sock()));
5878
5879 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5880 packet = sctp_packet_config(packet, vtag, 0);
5881
5882 return packet;
5883
5884nomem:
5885 return NULL;
5886}
5887
5888/* Free the packet allocated earlier for responding in the OOTB condition. */
5889void sctp_ootb_pkt_free(struct sctp_packet *packet)
5890{
5891 sctp_transport_free(packet->transport);
5892}
5893
5894/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5895static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5896 const struct sctp_association *asoc,
5897 const struct sctp_chunk *chunk,
5898 sctp_cmd_seq_t *commands,
5899 struct sctp_chunk *err_chunk)
5900{
5901 struct sctp_packet *packet;
5902
5903 if (err_chunk) {
5904 packet = sctp_ootb_pkt_new(asoc, chunk);
5905 if (packet) {
5906 struct sctp_signed_cookie *cookie;
5907
5908 /* Override the OOTB vtag from the cookie. */
5909 cookie = chunk->subh.cookie_hdr;
5910 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005911
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 /* Set the skb to the belonging sock for accounting. */
5913 err_chunk->skb->sk = ep->base.sk;
5914 sctp_packet_append_chunk(packet, err_chunk);
5915 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5916 SCTP_PACKET(packet));
5917 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5918 } else
5919 sctp_chunk_free (err_chunk);
5920 }
5921}
5922
5923
5924/* Process a data chunk */
5925static int sctp_eat_data(const struct sctp_association *asoc,
5926 struct sctp_chunk *chunk,
5927 sctp_cmd_seq_t *commands)
5928{
5929 sctp_datahdr_t *data_hdr;
5930 struct sctp_chunk *err;
5931 size_t datalen;
5932 sctp_verb_t deliver;
5933 int tmp;
5934 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005935 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005936 struct sock *sk = asoc->base.sk;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04005937 u16 ssn;
5938 u16 sid;
5939 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
5941 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5942 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5943
5944 tsn = ntohl(data_hdr->tsn);
5945 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5946
5947 /* ASSERT: Now skb->data is really the user data. */
5948
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 /* Process ECN based congestion.
5950 *
5951 * Since the chunk structure is reused for all chunks within
5952 * a packet, we use ecn_ce_done to track if we've already
5953 * done CE processing for this packet.
5954 *
5955 * We need to do ECN processing even if we plan to discard the
5956 * chunk later.
5957 */
5958
5959 if (!chunk->ecn_ce_done) {
5960 struct sctp_af *af;
5961 chunk->ecn_ce_done = 1;
5962
5963 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005964 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965
5966 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5967 /* Do real work as sideffect. */
5968 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5969 SCTP_U32(tsn));
5970 }
5971 }
5972
5973 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5974 if (tmp < 0) {
5975 /* The TSN is too high--silently discard the chunk and
5976 * count on it getting retransmitted later.
5977 */
5978 return SCTP_IERROR_HIGH_TSN;
5979 } else if (tmp > 0) {
5980 /* This is a duplicate. Record it. */
5981 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5982 return SCTP_IERROR_DUP_TSN;
5983 }
5984
5985 /* This is a new TSN. */
5986
5987 /* Discard if there is no room in the receive window.
5988 * Actually, allow a little bit of overflow (up to a MTU).
5989 */
5990 datalen = ntohs(chunk->chunk_hdr->length);
5991 datalen -= sizeof(sctp_data_chunk_t);
5992
5993 deliver = SCTP_CMD_CHUNK_ULP;
5994
5995 /* Think about partial delivery. */
5996 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5997
5998 /* Even if we don't accept this chunk there is
5999 * memory pressure.
6000 */
6001 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6002 }
6003
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09006004 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 * seems a bit troublesome in that frag_point varies based on
6006 * PMTU. In cases, such as loopback, this might be a rather
6007 * large spill over.
6008 */
Neil Horman4d93df02007-08-15 16:07:44 -07006009 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6010 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
6012 /* If this is the next TSN, consider reneging to make
6013 * room. Note: Playing nice with a confused sender. A
6014 * malicious sender can still eat up all our buffer
6015 * space and in the future we may want to detect and
6016 * do more drastic reneging.
6017 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07006018 if (sctp_tsnmap_has_gap(map) &&
6019 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6021 deliver = SCTP_CMD_RENEGE;
6022 } else {
6023 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6024 "rwnd: %d\n", tsn, datalen,
6025 asoc->rwnd);
6026 return SCTP_IERROR_IGNORE_TSN;
6027 }
6028 }
6029
6030 /*
Neil Horman4d93df02007-08-15 16:07:44 -07006031 * Also try to renege to limit our memory usage in the event that
6032 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08006033 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07006034 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6035 * memory usage too much
6036 */
6037 if (*sk->sk_prot_creator->memory_pressure) {
6038 if (sctp_tsnmap_has_gap(map) &&
6039 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6040 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6041 deliver = SCTP_CMD_RENEGE;
6042 }
6043 }
6044
6045 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 * Section 3.3.10.9 No User Data (9)
6047 *
6048 * Cause of error
6049 * ---------------
6050 * No User Data: This error cause is returned to the originator of a
6051 * DATA chunk if a received DATA chunk has no user data.
6052 */
6053 if (unlikely(0 == datalen)) {
6054 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6055 if (err) {
6056 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6057 SCTP_CHUNK(err));
6058 }
6059 /* We are going to ABORT, so we might as well stop
6060 * processing the rest of the chunks in the packet.
6061 */
6062 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006063 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6064 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006066 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6068 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6069 return SCTP_IERROR_NO_DATA;
6070 }
6071
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006072 chunk->data_accepted = 1;
6073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6075 * if we renege and the chunk arrives again.
6076 */
6077 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6078 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006079 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006081 ordered = 1;
6082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083
6084 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6085 *
6086 * If an endpoint receive a DATA chunk with an invalid stream
6087 * identifier, it shall acknowledge the reception of the DATA chunk
6088 * following the normal procedure, immediately send an ERROR chunk
6089 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6090 * and discard the DATA chunk.
6091 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006092 sid = ntohs(data_hdr->stream);
6093 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006094 /* Mark tsn as received even though we drop it */
6095 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6098 &data_hdr->stream,
Vlad Yasevich6383cfb2009-11-23 15:53:56 -05006099 sizeof(data_hdr->stream),
6100 sizeof(u16));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 if (err)
6102 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6103 SCTP_CHUNK(err));
6104 return SCTP_IERROR_BAD_STREAM;
6105 }
6106
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006107 /* Check to see if the SSN is possible for this TSN.
6108 * The biggest gap we can record is 4K wide. Since SSNs wrap
6109 * at an unsigned short, there is no way that an SSN can
6110 * wrap and for a valid TSN. We can simply check if the current
6111 * SSN is smaller then the next expected one. If it is, it wrapped
6112 * and is invalid.
6113 */
6114 ssn = ntohs(data_hdr->ssn);
6115 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6116 return SCTP_IERROR_PROTO_VIOLATION;
6117 }
6118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 /* Send the data up to the user. Note: Schedule the
6120 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6121 * chunk needs the updated rwnd.
6122 */
6123 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6124
6125 return SCTP_IERROR_NO_ERROR;
6126}