blob: 8ee24c9dc7e94e1bfbdee47c305667a6548095bb [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
53#include <linux/types.h>
54#include <linux/kernel.h>
55#include <linux/ip.h>
56#include <linux/ipv6.h>
57#include <linux/net.h>
58#include <linux/inet.h>
59#include <net/sock.h>
60#include <net/inet_ecn.h>
61#include <linux/skbuff.h>
62#include <net/sctp/sctp.h>
63#include <net/sctp/sm.h>
64#include <net/sctp/structs.h>
65
66static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
67 const struct sctp_association *asoc,
68 struct sctp_chunk *chunk,
69 const void *payload,
70 size_t paylen);
71static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 sctp_cmd_seq_t *commands);
74static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
75 const struct sctp_chunk *chunk);
76static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
77 const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk,
79 sctp_cmd_seq_t *commands,
80 struct sctp_chunk *err_chunk);
81static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
82 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
86static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
87 const struct sctp_association *asoc,
88 const sctp_subtype_t type,
89 void *arg,
90 sctp_cmd_seq_t *commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -040091static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
92 const struct sctp_association *asoc,
93 const sctp_subtype_t type,
94 void *arg,
95 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
97
Adrian Bunk52c1da32005-06-23 22:05:33 -070098static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -080099 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700100 const struct sctp_association *asoc,
101 struct sctp_transport *transport);
102
Wei Yongjunaecedea2007-08-02 16:57:44 +0800103static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -0400104 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800105 const struct sctp_association *asoc,
106 void *arg,
107 sctp_cmd_seq_t *commands,
108 const __u8 *payload,
109 const size_t paylen);
110
Adrian Bunk52c1da32005-06-23 22:05:33 -0700111static sctp_disposition_t sctp_sf_violation_chunklen(
112 const struct sctp_endpoint *ep,
113 const struct sctp_association *asoc,
114 const sctp_subtype_t type,
115 void *arg,
116 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800118static sctp_disposition_t sctp_sf_violation_paramlen(
119 const struct sctp_endpoint *ep,
120 const struct sctp_association *asoc,
121 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700122 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123 sctp_cmd_seq_t *commands);
124
Wei Yongjunaecedea2007-08-02 16:57:44 +0800125static sctp_disposition_t sctp_sf_violation_ctsn(
126 const struct sctp_endpoint *ep,
127 const struct sctp_association *asoc,
128 const sctp_subtype_t type,
129 void *arg,
130 sctp_cmd_seq_t *commands);
131
Vlad Yasevichece25df2007-09-07 16:30:54 -0400132static sctp_disposition_t sctp_sf_violation_chunk(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700139static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
140 const struct sctp_association *asoc,
141 const sctp_subtype_t type,
142 struct sctp_chunk *chunk);
143
Vlad Yasevich75205f42007-12-20 14:12:59 -0800144static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
145 const struct sctp_association *asoc,
146 const sctp_subtype_t type,
147 void *arg,
148 sctp_cmd_seq_t *commands);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Small helper function that checks if the chunk length
151 * is of the appropriate length. The 'required_length' argument
152 * is set to be the size of a specific chunk we are testing.
153 * Return Values: 1 = Valid length
154 * 0 = Invalid length
155 *
156 */
157static inline int
158sctp_chunk_length_valid(struct sctp_chunk *chunk,
159 __u16 required_length)
160{
161 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
162
163 if (unlikely(chunk_length < required_length))
164 return 0;
165
166 return 1;
167}
168
169/**********************************************************
170 * These are the state functions for handling chunk events.
171 **********************************************************/
172
173/*
174 * Process the final SHUTDOWN COMPLETE.
175 *
176 * Section: 4 (C) (diagram), 9.2
177 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
178 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
179 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
180 * should stop the T2-shutdown timer and remove all knowledge of the
181 * association (and thus the association enters the CLOSED state).
182 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700183 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * C) Rules for packet carrying SHUTDOWN COMPLETE:
185 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700186 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
187 * if the Verification Tag field of the packet matches its own tag and
188 * the T bit is not set
189 * OR
190 * it is set to its peer's tag and the T bit is set in the Chunk
191 * Flags.
192 * Otherwise, the receiver MUST silently discard the packet
193 * and take no further action. An endpoint MUST ignore the
194 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Inputs
197 * (endpoint, asoc, chunk)
198 *
199 * Outputs
200 * (asoc, reply_msg, msg_up, timers, counters)
201 *
202 * The return value is the disposition of the chunk.
203 */
204sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
205 const struct sctp_association *asoc,
206 const sctp_subtype_t type,
207 void *arg,
208 sctp_cmd_seq_t *commands)
209{
210 struct sctp_chunk *chunk = arg;
211 struct sctp_ulpevent *ev;
212
Vlad Yasevichece25df2007-09-07 16:30:54 -0400213 if (!sctp_vtag_verify_either(chunk, asoc))
214 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* RFC 2960 6.10 Bundling
217 *
218 * An endpoint MUST NOT bundle INIT, INIT ACK or
219 * SHUTDOWN COMPLETE with any other chunks.
220 */
221 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400222 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Vlad Yasevichece25df2007-09-07 16:30:54 -0400224 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
225 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
226 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
227 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* RFC 2960 10.2 SCTP-to-ULP
230 *
231 * H) SHUTDOWN COMPLETE notification
232 *
233 * When SCTP completes the shutdown procedures (section 9.2) this
234 * notification is passed to the upper layer.
235 */
236 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700237 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700238 if (ev)
239 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900240 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
243 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
244 * not the chunk should be discarded. If the endpoint is in
245 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
246 * T2-shutdown timer and remove all knowledge of the
247 * association (and thus the association enters the CLOSED
248 * state).
249 */
250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
251 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
252
253 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
255
256 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
257 SCTP_STATE(SCTP_STATE_CLOSED));
258
259 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
260 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
261
262 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
263
264 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Respond to a normal INIT chunk.
269 * We are the side that is being asked for an association.
270 *
271 * Section: 5.1 Normal Establishment of an Association, B
272 * B) "Z" shall respond immediately with an INIT ACK chunk. The
273 * destination IP address of the INIT ACK MUST be set to the source
274 * IP address of the INIT to which this INIT ACK is responding. In
275 * the response, besides filling in other parameters, "Z" must set the
276 * Verification Tag field to Tag_A, and also provide its own
277 * Verification Tag (Tag_Z) in the Initiate Tag field.
278 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900279 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * Inputs
282 * (endpoint, asoc, chunk)
283 *
284 * Outputs
285 * (asoc, reply_msg, msg_up, timers, counters)
286 *
287 * The return value is the disposition of the chunk.
288 */
289sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
290 const struct sctp_association *asoc,
291 const sctp_subtype_t type,
292 void *arg,
293 sctp_cmd_seq_t *commands)
294{
295 struct sctp_chunk *chunk = arg;
296 struct sctp_chunk *repl;
297 struct sctp_association *new_asoc;
298 struct sctp_chunk *err_chunk;
299 struct sctp_packet *packet;
300 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int len;
302
303 /* 6.10 Bundling
304 * An endpoint MUST NOT bundle INIT, INIT ACK or
305 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * IG Section 2.11.2
308 * Furthermore, we require that the receiver of an INIT chunk MUST
309 * enforce these rules by silently discarding an arriving packet
310 * with an INIT chunk that is bundled with other chunks.
311 */
312 if (!chunk->singleton)
313 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
314
315 /* If the packet is an OOTB packet which is temporarily on the
316 * control endpoint, respond with an ABORT.
317 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800318 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
319 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900324 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326 if (chunk->sctp_hdr->vtag != 0)
327 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
328
329 /* Make sure that the INIT chunk has a valid length.
330 * Normally, this would cause an ABORT with a Protocol Violation
331 * error, but since we don't have an association, we'll
332 * just discard the packet.
333 */
334 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
335 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
336
Vlad Yasevichbec96402009-07-30 18:08:28 -0400337 /* If the INIT is coming toward a closing socket, we'll send back
338 * and ABORT. Essentially, this catches the race of INIT being
339 * backloged to the socket at the same time as the user isses close().
340 * Since the socket and all its associations are going away, we
341 * can treat this OOTB
342 */
343 if (sctp_sstate(ep->base.sk, CLOSING))
344 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Verify the INIT chunk before processing it. */
347 err_chunk = NULL;
348 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
349 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
350 &err_chunk)) {
351 /* This chunk contains fatal error. It is to be discarded.
352 * Send an ABORT, with causes if there is any.
353 */
354 if (err_chunk) {
355 packet = sctp_abort_pkt_new(ep, asoc, arg,
356 (__u8 *)(err_chunk->chunk_hdr) +
357 sizeof(sctp_chunkhdr_t),
358 ntohs(err_chunk->chunk_hdr->length) -
359 sizeof(sctp_chunkhdr_t));
360
361 sctp_chunk_free(err_chunk);
362
363 if (packet) {
364 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
365 SCTP_PACKET(packet));
366 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
367 return SCTP_DISPOSITION_CONSUME;
368 } else {
369 return SCTP_DISPOSITION_NOMEM;
370 }
371 } else {
372 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
373 commands);
374 }
375 }
376
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900377 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
379
380 /* Tag the variable length parameters. */
381 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
382
383 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
384 if (!new_asoc)
385 goto nomem;
386
Vlad Yasevich409b95a2009-11-10 08:57:34 +0000387 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
388 sctp_scope(sctp_source(chunk)),
389 GFP_ATOMIC) < 0)
390 goto nomem_init;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* The call, sctp_process_init(), can fail on memory allocation. */
393 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
394 sctp_source(chunk),
395 (sctp_init_chunk_t *)chunk->chunk_hdr,
396 GFP_ATOMIC))
397 goto nomem_init;
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
400
401 /* If there are errors need to be reported for unknown parameters,
402 * make sure to reserve enough room in the INIT ACK for them.
403 */
404 len = 0;
405 if (err_chunk)
406 len = ntohs(err_chunk->chunk_hdr->length) -
407 sizeof(sctp_chunkhdr_t);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
410 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700411 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 /* If there are errors need to be reported for unknown parameters,
414 * include them in the outgoing INIT ACK as "Unrecognized parameter"
415 * parameter.
416 */
417 if (err_chunk) {
418 /* Get the "Unrecognized parameter" parameter(s) out of the
419 * ERROR chunk generated by sctp_verify_init(). Since the
420 * error cause code for "unknown parameter" and the
421 * "Unrecognized parameter" type is the same, we can
422 * construct the parameters in INIT ACK by copying the
423 * ERROR causes over.
424 */
425 unk_param = (sctp_unrecognized_param_t *)
426 ((__u8 *)(err_chunk->chunk_hdr) +
427 sizeof(sctp_chunkhdr_t));
428 /* Replace the cause code with the "Unrecognized parameter"
429 * parameter type.
430 */
431 sctp_addto_chunk(repl, len, unk_param);
432 sctp_chunk_free(err_chunk);
433 }
434
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700435 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
438
439 /*
440 * Note: After sending out INIT ACK with the State Cookie parameter,
441 * "Z" MUST NOT allocate any resources, nor keep any states for the
442 * new association. Otherwise, "Z" will be vulnerable to resource
443 * attacks.
444 */
445 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
446
447 return SCTP_DISPOSITION_DELETE_TCB;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449nomem_init:
450 sctp_association_free(new_asoc);
451nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700452 if (err_chunk)
453 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return SCTP_DISPOSITION_NOMEM;
455}
456
457/*
458 * Respond to a normal INIT ACK chunk.
459 * We are the side that is initiating the association.
460 *
461 * Section: 5.1 Normal Establishment of an Association, C
462 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
463 * timer and leave COOKIE-WAIT state. "A" shall then send the State
464 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
465 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
466 *
467 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
468 * DATA chunks, but it MUST be the first chunk in the packet and
469 * until the COOKIE ACK is returned the sender MUST NOT send any
470 * other packets to the peer.
471 *
472 * Verification Tag: 3.3.3
473 * If the value of the Initiate Tag in a received INIT ACK chunk is
474 * found to be 0, the receiver MUST treat it as an error and close the
475 * association by transmitting an ABORT.
476 *
477 * Inputs
478 * (endpoint, asoc, chunk)
479 *
480 * Outputs
481 * (asoc, reply_msg, msg_up, timers, counters)
482 *
483 * The return value is the disposition of the chunk.
484 */
485sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
486 const struct sctp_association *asoc,
487 const sctp_subtype_t type,
488 void *arg,
489 sctp_cmd_seq_t *commands)
490{
491 struct sctp_chunk *chunk = arg;
492 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct sctp_chunk *err_chunk;
494 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (!sctp_vtag_verify(chunk, asoc))
497 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* 6.10 Bundling
500 * An endpoint MUST NOT bundle INIT, INIT ACK or
501 * SHUTDOWN COMPLETE with any other chunks.
502 */
503 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400504 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Vlad Yasevichece25df2007-09-07 16:30:54 -0400506 /* Make sure that the INIT-ACK chunk has a valid length */
507 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
508 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
509 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Grab the INIT header. */
511 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Verify the INIT chunk before processing it. */
514 err_chunk = NULL;
515 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
516 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
517 &err_chunk)) {
518
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800519 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800522 * Send an ABORT, with causes. If there are no causes,
523 * then there wasn't enough memory. Just terminate
524 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 */
526 if (err_chunk) {
527 packet = sctp_abort_pkt_new(ep, asoc, arg,
528 (__u8 *)(err_chunk->chunk_hdr) +
529 sizeof(sctp_chunkhdr_t),
530 ntohs(err_chunk->chunk_hdr->length) -
531 sizeof(sctp_chunkhdr_t));
532
533 sctp_chunk_free(err_chunk);
534
535 if (packet) {
536 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
537 SCTP_PACKET(packet));
538 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700539 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700542
543 /* SCTP-AUTH, Section 6.3:
544 * It should be noted that if the receiver wants to tear
545 * down an association in an authenticated way only, the
546 * handling of malformed packets should not result in
547 * tearing down the association.
548 *
549 * This means that if we only want to abort associations
550 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +0200551 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700552 * was malformed.
553 */
554 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
555 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
556
557 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700558 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
559 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
562 /* Tag the variable length parameters. Note that we never
563 * convert the parameters in an INIT chunk.
564 */
565 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
566
567 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
568
569 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
570 SCTP_PEER_INIT(initchunk));
571
Frank Filz3f7a87d2005-06-20 13:14:57 -0700572 /* Reset init error count upon receipt of INIT-ACK. */
573 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 /* 5.1 C) "A" shall stop the T1-init timer and leave
576 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
577 * timer, and enter the COOKIE-ECHOED state.
578 */
579 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
580 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
581 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
582 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
583 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
584 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
585
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700586 /* SCTP-AUTH: genereate the assocition shared keys so that
587 * we can potentially signe the COOKIE-ECHO.
588 */
589 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* 5.1 C) "A" shall then send the State Cookie received in the
592 * INIT ACK chunk in a COOKIE ECHO chunk, ...
593 */
594 /* If there is any errors to report, send the ERROR chunk generated
595 * for unknown parameters as well.
596 */
597 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
598 SCTP_CHUNK(err_chunk));
599
600 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
604 * Respond to a normal COOKIE ECHO chunk.
605 * We are the side that is being asked for an association.
606 *
607 * Section: 5.1 Normal Establishment of an Association, D
608 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
609 * with a COOKIE ACK chunk after building a TCB and moving to
610 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
611 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
612 * chunk MUST be the first chunk in the packet.
613 *
614 * IMPLEMENTATION NOTE: An implementation may choose to send the
615 * Communication Up notification to the SCTP user upon reception
616 * of a valid COOKIE ECHO chunk.
617 *
618 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
619 * D) Rules for packet carrying a COOKIE ECHO
620 *
621 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
622 * Initial Tag received in the INIT ACK.
623 *
624 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
625 *
626 * Inputs
627 * (endpoint, asoc, chunk)
628 *
629 * Outputs
630 * (asoc, reply_msg, msg_up, timers, counters)
631 *
632 * The return value is the disposition of the chunk.
633 */
634sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
635 const struct sctp_association *asoc,
636 const sctp_subtype_t type, void *arg,
637 sctp_cmd_seq_t *commands)
638{
639 struct sctp_chunk *chunk = arg;
640 struct sctp_association *new_asoc;
641 sctp_init_chunk_t *peer_init;
642 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700643 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 int error = 0;
645 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900646 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* If the packet is an OOTB packet which is temporarily on the
649 * control endpoint, respond with an ABORT.
650 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800651 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
652 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400653 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Make sure that the COOKIE_ECHO chunk has a valid length.
657 * In this case, we check that we have enough for at least a
658 * chunk header. More detailed verification is done
659 * in sctp_unpack_cookie().
660 */
661 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
662 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
663
Vlad Yasevich609ee462007-08-31 03:10:59 +0900664 /* If the endpoint is not listening or if the number of associations
665 * on the TCP-style socket exceed the max backlog, respond with an
666 * ABORT.
667 */
668 sk = ep->base.sk;
669 if (!sctp_sstate(sk, LISTENING) ||
670 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
671 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /* "Decode" the chunk. We have no optional parameters so we
674 * are in good shape.
675 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900676 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700678 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
679 sizeof(sctp_chunkhdr_t)))
680 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
683 * "Z" will reply with a COOKIE ACK chunk after building a TCB
684 * and moving to the ESTABLISHED state.
685 */
686 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
687 &err_chk_p);
688
689 /* FIXME:
690 * If the re-build failed, what is the proper error path
691 * from here?
692 *
693 * [We should abort the association. --piggy]
694 */
695 if (!new_asoc) {
696 /* FIXME: Several errors are possible. A bad cookie should
697 * be silently discarded, but think about logging it too.
698 */
699 switch (error) {
700 case -SCTP_IERROR_NOMEM:
701 goto nomem;
702
703 case -SCTP_IERROR_STALE_COOKIE:
704 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
705 err_chk_p);
706 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
707
708 case -SCTP_IERROR_BAD_SIG:
709 default:
710 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700715 /* Delay state machine commands until later.
716 *
717 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * the sctp_unpack_cookie() already.
719 */
720 /* This is a brand-new association, so these are not yet side
721 * effects--it is safe to run them here.
722 */
723 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
724
725 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
Al Viro6a1e5f32006-11-20 17:12:25 -0800726 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 peer_init, GFP_ATOMIC))
728 goto nomem_init;
729
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700730 /* SCTP-AUTH: Now that we've populate required fields in
731 * sctp_process_init, set up the assocaition shared keys as
732 * necessary so that we can potentially authenticate the ACK
733 */
734 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
735 if (error)
736 goto nomem_init;
737
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700738 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
739 * is supposed to be authenticated and we have to do delayed
740 * authentication. We've just recreated the association using
741 * the information in the cookie and now it's much easier to
742 * do the authentication.
743 */
744 if (chunk->auth_chunk) {
745 struct sctp_chunk auth;
746 sctp_ierror_t ret;
747
748 /* set-up our fake chunk so that we can process it */
749 auth.skb = chunk->auth_chunk;
750 auth.asoc = chunk->asoc;
751 auth.sctp_hdr = chunk->sctp_hdr;
752 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
753 sizeof(sctp_chunkhdr_t));
754 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
755 auth.transport = chunk->transport;
756
757 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
758
759 /* We can now safely free the auth_chunk clone */
760 kfree_skb(chunk->auth_chunk);
761
762 if (ret != SCTP_IERROR_NO_ERROR) {
763 sctp_association_free(new_asoc);
764 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
765 }
766 }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 repl = sctp_make_cookie_ack(new_asoc, chunk);
769 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700770 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* RFC 2960 5.1 Normal Establishment of an Association
773 *
774 * D) IMPLEMENTATION NOTE: An implementation may choose to
775 * send the Communication Up notification to the SCTP user
776 * upon reception of a valid COOKIE ECHO chunk.
777 */
778 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
779 new_asoc->c.sinit_num_ostreams,
780 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700781 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!ev)
783 goto nomem_ev;
784
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900785 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800786 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800788 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800790 if (new_asoc->peer.adaptation_ind) {
791 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700793 if (!ai_ev)
794 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700797 /* Add all the state machine commands now since we've created
798 * everything. This way we don't introduce memory corruptions
799 * during side-effect processing and correclty count established
800 * associations.
801 */
802 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
803 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
804 SCTP_STATE(SCTP_STATE_ESTABLISHED));
805 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
806 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
807 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
808
809 if (new_asoc->autoclose)
810 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
811 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
812
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700813 /* This will send the COOKIE ACK */
814 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
815
816 /* Queue the ASSOC_CHANGE event */
817 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
818
819 /* Send up the Adaptation Layer Indication event */
820 if (ai_ev)
821 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
822 SCTP_ULPEVENT(ai_ev));
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return SCTP_DISPOSITION_CONSUME;
825
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700826nomem_aiev:
827 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828nomem_ev:
829 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830nomem_init:
831 sctp_association_free(new_asoc);
832nomem:
833 return SCTP_DISPOSITION_NOMEM;
834}
835
836/*
837 * Respond to a normal COOKIE ACK chunk.
838 * We are the side that is being asked for an association.
839 *
840 * RFC 2960 5.1 Normal Establishment of an Association
841 *
842 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
843 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
844 * timer. It may also notify its ULP about the successful
845 * establishment of the association with a Communication Up
846 * notification (see Section 10).
847 *
848 * Verification Tag:
849 * Inputs
850 * (endpoint, asoc, chunk)
851 *
852 * Outputs
853 * (asoc, reply_msg, msg_up, timers, counters)
854 *
855 * The return value is the disposition of the chunk.
856 */
857sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
858 const struct sctp_association *asoc,
859 const sctp_subtype_t type, void *arg,
860 sctp_cmd_seq_t *commands)
861{
862 struct sctp_chunk *chunk = arg;
863 struct sctp_ulpevent *ev;
864
865 if (!sctp_vtag_verify(chunk, asoc))
866 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
867
868 /* Verify that the chunk length for the COOKIE-ACK is OK.
869 * If we don't do this, any bundled chunks may be junked.
870 */
871 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
872 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
873 commands);
874
875 /* Reset init error count upon receipt of COOKIE-ACK,
876 * to avoid problems with the managemement of this
877 * counter in stale cookie situations when a transition back
878 * from the COOKIE-ECHOED state to the COOKIE-WAIT
879 * state is performed.
880 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700881 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 /* RFC 2960 5.1 Normal Establishment of an Association
884 *
885 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
886 * from the COOKIE-ECHOED state to the ESTABLISHED state,
887 * stopping the T1-cookie timer.
888 */
889 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
890 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
891 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
892 SCTP_STATE(SCTP_STATE_ESTABLISHED));
893 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
894 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
895 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
896 if (asoc->autoclose)
897 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
898 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /* It may also notify its ULP about the successful
901 * establishment of the association with a Communication Up
902 * notification (see Section 10).
903 */
904 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
905 0, asoc->c.sinit_num_ostreams,
906 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700907 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 if (!ev)
910 goto nomem;
911
912 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
913
914 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800915 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800917 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800919 if (asoc->peer.adaptation_ind) {
920 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!ev)
922 goto nomem;
923
924 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
925 SCTP_ULPEVENT(ev));
926 }
927
928 return SCTP_DISPOSITION_CONSUME;
929nomem:
930 return SCTP_DISPOSITION_NOMEM;
931}
932
933/* Generate and sendout a heartbeat packet. */
934static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
935 const struct sctp_association *asoc,
936 const sctp_subtype_t type,
937 void *arg,
938 sctp_cmd_seq_t *commands)
939{
940 struct sctp_transport *transport = (struct sctp_transport *) arg;
941 struct sctp_chunk *reply;
942 sctp_sender_hb_info_t hbinfo;
943 size_t paylen = 0;
944
945 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
946 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
Al Viro63de08f2006-11-20 17:07:25 -0800947 hbinfo.daddr = transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 hbinfo.sent_at = jiffies;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700949 hbinfo.hb_nonce = transport->hb_nonce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 /* Send a heartbeat to our peer. */
952 paylen = sizeof(sctp_sender_hb_info_t);
953 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
954 if (!reply)
955 return SCTP_DISPOSITION_NOMEM;
956
957 /* Set rto_pending indicating that an RTT measurement
958 * is started with this heartbeat chunk.
959 */
960 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
961 SCTP_TRANSPORT(transport));
962
963 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
964 return SCTP_DISPOSITION_CONSUME;
965}
966
967/* Generate a HEARTBEAT packet on the given transport. */
968sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
969 const struct sctp_association *asoc,
970 const sctp_subtype_t type,
971 void *arg,
972 sctp_cmd_seq_t *commands)
973{
974 struct sctp_transport *transport = (struct sctp_transport *) arg;
975
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400976 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700977 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
978 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
980 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800981 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
983 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
984 return SCTP_DISPOSITION_DELETE_TCB;
985 }
986
987 /* Section 3.3.5.
988 * The Sender-specific Heartbeat Info field should normally include
989 * information about the sender's current time when this HEARTBEAT
990 * chunk is sent and the destination transport address to which this
991 * HEARTBEAT is sent (see Section 8.3).
992 */
993
Frank Filz52ccb8e2005-12-22 11:36:46 -0800994 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (SCTP_DISPOSITION_NOMEM ==
996 sctp_sf_heartbeat(ep, asoc, type, arg,
997 commands))
998 return SCTP_DISPOSITION_NOMEM;
999 /* Set transport error counter and association error counter
1000 * when sending heartbeat.
1001 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001002 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1003 SCTP_TRANSPORT(transport));
1004 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 SCTP_TRANSPORT(transport));
1006 }
1007 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1008 SCTP_TRANSPORT(transport));
1009
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001010 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013/*
1014 * Process an heartbeat request.
1015 *
1016 * Section: 8.3 Path Heartbeat
1017 * The receiver of the HEARTBEAT should immediately respond with a
1018 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1019 * from the received HEARTBEAT chunk.
1020 *
1021 * Verification Tag: 8.5 Verification Tag [Normal verification]
1022 * When receiving an SCTP packet, the endpoint MUST ensure that the
1023 * value in the Verification Tag field of the received SCTP packet
1024 * matches its own Tag. If the received Verification Tag value does not
1025 * match the receiver's own tag value, the receiver shall silently
1026 * discard the packet and shall not process it any further except for
1027 * those cases listed in Section 8.5.1 below.
1028 *
1029 * Inputs
1030 * (endpoint, asoc, chunk)
1031 *
1032 * Outputs
1033 * (asoc, reply_msg, msg_up, timers, counters)
1034 *
1035 * The return value is the disposition of the chunk.
1036 */
1037sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1038 const struct sctp_association *asoc,
1039 const sctp_subtype_t type,
1040 void *arg,
1041 sctp_cmd_seq_t *commands)
1042{
1043 struct sctp_chunk *chunk = arg;
1044 struct sctp_chunk *reply;
1045 size_t paylen = 0;
1046
1047 if (!sctp_vtag_verify(chunk, asoc))
1048 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1049
1050 /* Make sure that the HEARTBEAT chunk has a valid length. */
1051 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1052 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1053 commands);
1054
1055 /* 8.3 The receiver of the HEARTBEAT should immediately
1056 * respond with a HEARTBEAT ACK that contains the Heartbeat
1057 * Information field copied from the received HEARTBEAT chunk.
1058 */
1059 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1060 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001061 if (!pskb_pull(chunk->skb, paylen))
1062 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 reply = sctp_make_heartbeat_ack(asoc, chunk,
1065 chunk->subh.hb_hdr, paylen);
1066 if (!reply)
1067 goto nomem;
1068
1069 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1070 return SCTP_DISPOSITION_CONSUME;
1071
1072nomem:
1073 return SCTP_DISPOSITION_NOMEM;
1074}
1075
1076/*
1077 * Process the returning HEARTBEAT ACK.
1078 *
1079 * Section: 8.3 Path Heartbeat
1080 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1081 * should clear the error counter of the destination transport
1082 * address to which the HEARTBEAT was sent, and mark the destination
1083 * transport address as active if it is not so marked. The endpoint may
1084 * optionally report to the upper layer when an inactive destination
1085 * address is marked as active due to the reception of the latest
1086 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1087 * clear the association overall error count as well (as defined
1088 * in section 8.1).
1089 *
1090 * The receiver of the HEARTBEAT ACK should also perform an RTT
1091 * measurement for that destination transport address using the time
1092 * value carried in the HEARTBEAT ACK chunk.
1093 *
1094 * Verification Tag: 8.5 Verification Tag [Normal verification]
1095 *
1096 * Inputs
1097 * (endpoint, asoc, chunk)
1098 *
1099 * Outputs
1100 * (asoc, reply_msg, msg_up, timers, counters)
1101 *
1102 * The return value is the disposition of the chunk.
1103 */
1104sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1105 const struct sctp_association *asoc,
1106 const sctp_subtype_t type,
1107 void *arg,
1108 sctp_cmd_seq_t *commands)
1109{
1110 struct sctp_chunk *chunk = arg;
1111 union sctp_addr from_addr;
1112 struct sctp_transport *link;
1113 sctp_sender_hb_info_t *hbinfo;
1114 unsigned long max_interval;
1115
1116 if (!sctp_vtag_verify(chunk, asoc))
1117 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1118
1119 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001120 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1121 sizeof(sctp_sender_hb_info_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1123 commands);
1124
1125 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001126 /* Make sure that the length of the parameter is what we expect */
1127 if (ntohs(hbinfo->param_hdr.length) !=
1128 sizeof(sctp_sender_hb_info_t)) {
1129 return SCTP_DISPOSITION_DISCARD;
1130 }
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001133 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001136 if (unlikely(!link)) {
1137 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001138 if (net_ratelimit())
1139 printk(KERN_WARNING
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001140 "%s association %p could not find address %pI6\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001141 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001142 asoc,
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001143 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001144 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001145 if (net_ratelimit())
1146 printk(KERN_WARNING
Harvey Harrison21454aa2008-10-31 00:54:56 -07001147 "%s association %p could not find address %pI4\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001148 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001149 asoc,
Harvey Harrison21454aa2008-10-31 00:54:56 -07001150 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return SCTP_DISPOSITION_DISCARD;
1153 }
1154
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001155 /* Validate the 64-bit random nonce. */
1156 if (hbinfo->hb_nonce != link->hb_nonce)
1157 return SCTP_DISPOSITION_DISCARD;
1158
Frank Filz52ccb8e2005-12-22 11:36:46 -08001159 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /* Check if the timestamp looks valid. */
1162 if (time_after(hbinfo->sent_at, jiffies) ||
1163 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001164 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001166 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 return SCTP_DISPOSITION_DISCARD;
1168 }
1169
1170 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1171 * the HEARTBEAT should clear the error counter of the
1172 * destination transport address to which the HEARTBEAT was
1173 * sent and mark the destination transport address as active if
1174 * it is not so marked.
1175 */
1176 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1177
1178 return SCTP_DISPOSITION_CONSUME;
1179}
1180
1181/* Helper function to send out an abort for the restart
1182 * condition.
1183 */
1184static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1185 struct sctp_chunk *init,
1186 sctp_cmd_seq_t *commands)
1187{
1188 int len;
1189 struct sctp_packet *pkt;
1190 union sctp_addr_param *addrparm;
1191 struct sctp_errhdr *errhdr;
1192 struct sctp_endpoint *ep;
1193 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1194 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1195
1196 /* Build the error on the stack. We are way to malloc crazy
1197 * throughout the code today.
1198 */
1199 errhdr = (struct sctp_errhdr *)buffer;
1200 addrparm = (union sctp_addr_param *)errhdr->variable;
1201
1202 /* Copy into a parm format. */
1203 len = af->to_addr_param(ssa, addrparm);
1204 len += sizeof(sctp_errhdr_t);
1205
1206 errhdr->cause = SCTP_ERROR_RESTART;
1207 errhdr->length = htons(len);
1208
1209 /* Assign to the control socket. */
1210 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1211
1212 /* Association is NULL since this may be a restart attack and we
1213 * want to send back the attacker's vtag.
1214 */
1215 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1216
1217 if (!pkt)
1218 goto out;
1219 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1220
1221 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1222
1223 /* Discard the rest of the inbound packet. */
1224 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1225
1226out:
1227 /* Even if there is no memory, treat as a failure so
1228 * the packet will get dropped.
1229 */
1230 return 0;
1231}
1232
1233/* A restart is occurring, check to make sure no new addresses
1234 * are being added as we may be under a takeover attack.
1235 */
1236static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1237 const struct sctp_association *asoc,
1238 struct sctp_chunk *init,
1239 sctp_cmd_seq_t *commands)
1240{
1241 struct sctp_transport *new_addr, *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 int found;
1243
1244 /* Implementor's Guide - Sectin 5.2.2
1245 * ...
1246 * Before responding the endpoint MUST check to see if the
1247 * unexpected INIT adds new addresses to the association. If new
1248 * addresses are added to the association, the endpoint MUST respond
1249 * with an ABORT..
1250 */
1251
1252 /* Search through all current addresses and make sure
1253 * we aren't adding any new ones.
1254 */
1255 new_addr = NULL;
1256 found = 0;
1257
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001258 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1259 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 found = 0;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001261 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1262 transports) {
Al Viro5f242a12006-11-20 17:05:23 -08001263 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1264 &addr->ipaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 found = 1;
1266 break;
1267 }
1268 }
1269 if (!found)
1270 break;
1271 }
1272
1273 /* If a new address was added, ABORT the sender. */
1274 if (!found && new_addr) {
Al Viro7dd8a582006-11-20 17:23:25 -08001275 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
1277
1278 /* Return success if all addresses were found. */
1279 return found;
1280}
1281
1282/* Populate the verification/tie tags based on overlapping INIT
1283 * scenario.
1284 *
1285 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1286 */
1287static void sctp_tietags_populate(struct sctp_association *new_asoc,
1288 const struct sctp_association *asoc)
1289{
1290 switch (asoc->state) {
1291
1292 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1293
1294 case SCTP_STATE_COOKIE_WAIT:
1295 new_asoc->c.my_vtag = asoc->c.my_vtag;
1296 new_asoc->c.my_ttag = asoc->c.my_vtag;
1297 new_asoc->c.peer_ttag = 0;
1298 break;
1299
1300 case SCTP_STATE_COOKIE_ECHOED:
1301 new_asoc->c.my_vtag = asoc->c.my_vtag;
1302 new_asoc->c.my_ttag = asoc->c.my_vtag;
1303 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1304 break;
1305
1306 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1307 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1308 */
1309 default:
1310 new_asoc->c.my_ttag = asoc->c.my_vtag;
1311 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1312 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /* Other parameters for the endpoint SHOULD be copied from the
1316 * existing parameters of the association (e.g. number of
1317 * outbound streams) into the INIT ACK and cookie.
1318 */
1319 new_asoc->rwnd = asoc->rwnd;
1320 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1321 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1322 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1323}
1324
1325/*
1326 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1327 * handling action.
1328 *
1329 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1330 *
1331 * Returns value representing action to be taken. These action values
1332 * correspond to Action/Description values in RFC 2960, Table 2.
1333 */
1334static char sctp_tietags_compare(struct sctp_association *new_asoc,
1335 const struct sctp_association *asoc)
1336{
1337 /* In this case, the peer may have restarted. */
1338 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1339 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1340 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1341 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1342 return 'A';
1343
1344 /* Collision case B. */
1345 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1346 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1347 (0 == asoc->c.peer_vtag))) {
1348 return 'B';
1349 }
1350
1351 /* Collision case D. */
1352 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1353 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1354 return 'D';
1355
1356 /* Collision case C. */
1357 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1358 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1359 (0 == new_asoc->c.my_ttag) &&
1360 (0 == new_asoc->c.peer_ttag))
1361 return 'C';
1362
1363 /* No match to any of the special cases; discard this packet. */
1364 return 'E';
1365}
1366
1367/* Common helper routine for both duplicate and simulataneous INIT
1368 * chunk handling.
1369 */
1370static sctp_disposition_t sctp_sf_do_unexpected_init(
1371 const struct sctp_endpoint *ep,
1372 const struct sctp_association *asoc,
1373 const sctp_subtype_t type,
1374 void *arg, sctp_cmd_seq_t *commands)
1375{
1376 sctp_disposition_t retval;
1377 struct sctp_chunk *chunk = arg;
1378 struct sctp_chunk *repl;
1379 struct sctp_association *new_asoc;
1380 struct sctp_chunk *err_chunk;
1381 struct sctp_packet *packet;
1382 sctp_unrecognized_param_t *unk_param;
1383 int len;
1384
1385 /* 6.10 Bundling
1386 * An endpoint MUST NOT bundle INIT, INIT ACK or
1387 * SHUTDOWN COMPLETE with any other chunks.
1388 *
1389 * IG Section 2.11.2
1390 * Furthermore, we require that the receiver of an INIT chunk MUST
1391 * enforce these rules by silently discarding an arriving packet
1392 * with an INIT chunk that is bundled with other chunks.
1393 */
1394 if (!chunk->singleton)
1395 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1396
1397 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001398 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 */
1400 if (chunk->sctp_hdr->vtag != 0)
1401 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1402
1403 /* Make sure that the INIT chunk has a valid length.
1404 * In this case, we generate a protocol violation since we have
1405 * an association established.
1406 */
1407 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1408 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1409 commands);
1410 /* Grab the INIT header. */
1411 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1412
1413 /* Tag the variable length parameters. */
1414 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1415
1416 /* Verify the INIT chunk before processing it. */
1417 err_chunk = NULL;
1418 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1419 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1420 &err_chunk)) {
1421 /* This chunk contains fatal error. It is to be discarded.
1422 * Send an ABORT, with causes if there is any.
1423 */
1424 if (err_chunk) {
1425 packet = sctp_abort_pkt_new(ep, asoc, arg,
1426 (__u8 *)(err_chunk->chunk_hdr) +
1427 sizeof(sctp_chunkhdr_t),
1428 ntohs(err_chunk->chunk_hdr->length) -
1429 sizeof(sctp_chunkhdr_t));
1430
1431 if (packet) {
1432 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1433 SCTP_PACKET(packet));
1434 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1435 retval = SCTP_DISPOSITION_CONSUME;
1436 } else {
1437 retval = SCTP_DISPOSITION_NOMEM;
1438 }
1439 goto cleanup;
1440 } else {
1441 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1442 commands);
1443 }
1444 }
1445
1446 /*
1447 * Other parameters for the endpoint SHOULD be copied from the
1448 * existing parameters of the association (e.g. number of
1449 * outbound streams) into the INIT ACK and cookie.
1450 * FIXME: We are copying parameters from the endpoint not the
1451 * association.
1452 */
1453 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1454 if (!new_asoc)
1455 goto nomem;
1456
Vlad Yasevich409b95a2009-11-10 08:57:34 +00001457 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1458 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1459 goto nomem;
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 /* In the outbound INIT ACK the endpoint MUST copy its current
1462 * Verification Tag and Peers Verification tag into a reserved
1463 * place (local tie-tag and per tie-tag) within the state cookie.
1464 */
1465 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1466 sctp_source(chunk),
1467 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001468 GFP_ATOMIC))
1469 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /* Make sure no new addresses are being added during the
1472 * restart. Do not do this check for COOKIE-WAIT state,
1473 * since there are no peer addresses to check against.
1474 * Upon return an ABORT will have been sent if needed.
1475 */
1476 if (!sctp_state(asoc, COOKIE_WAIT)) {
1477 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1478 commands)) {
1479 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001480 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 }
1482 }
1483
1484 sctp_tietags_populate(new_asoc, asoc);
1485
1486 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1487
1488 /* If there are errors need to be reported for unknown parameters,
1489 * make sure to reserve enough room in the INIT ACK for them.
1490 */
1491 len = 0;
1492 if (err_chunk) {
1493 len = ntohs(err_chunk->chunk_hdr->length) -
1494 sizeof(sctp_chunkhdr_t);
1495 }
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1498 if (!repl)
1499 goto nomem;
1500
1501 /* If there are errors need to be reported for unknown parameters,
1502 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1503 * parameter.
1504 */
1505 if (err_chunk) {
1506 /* Get the "Unrecognized parameter" parameter(s) out of the
1507 * ERROR chunk generated by sctp_verify_init(). Since the
1508 * error cause code for "unknown parameter" and the
1509 * "Unrecognized parameter" type is the same, we can
1510 * construct the parameters in INIT ACK by copying the
1511 * ERROR causes over.
1512 */
1513 unk_param = (sctp_unrecognized_param_t *)
1514 ((__u8 *)(err_chunk->chunk_hdr) +
1515 sizeof(sctp_chunkhdr_t));
1516 /* Replace the cause code with the "Unrecognized parameter"
1517 * parameter type.
1518 */
1519 sctp_addto_chunk(repl, len, unk_param);
1520 }
1521
1522 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1523 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1524
1525 /*
1526 * Note: After sending out INIT ACK with the State Cookie parameter,
1527 * "Z" MUST NOT allocate any resources for this new association.
1528 * Otherwise, "Z" will be vulnerable to resource attacks.
1529 */
1530 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1531 retval = SCTP_DISPOSITION_CONSUME;
1532
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001533 return retval;
1534
1535nomem:
1536 retval = SCTP_DISPOSITION_NOMEM;
1537nomem_retval:
1538 if (new_asoc)
1539 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540cleanup:
1541 if (err_chunk)
1542 sctp_chunk_free(err_chunk);
1543 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
1546/*
1547 * Handle simultanous INIT.
1548 * This means we started an INIT and then we got an INIT request from
1549 * our peer.
1550 *
1551 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1552 * This usually indicates an initialization collision, i.e., each
1553 * endpoint is attempting, at about the same time, to establish an
1554 * association with the other endpoint.
1555 *
1556 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1557 * endpoint MUST respond with an INIT ACK using the same parameters it
1558 * sent in its original INIT chunk (including its Verification Tag,
1559 * unchanged). These original parameters are combined with those from the
1560 * newly received INIT chunk. The endpoint shall also generate a State
1561 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1562 * INIT to calculate the State Cookie.
1563 *
1564 * After that, the endpoint MUST NOT change its state, the T1-init
1565 * timer shall be left running and the corresponding TCB MUST NOT be
1566 * destroyed. The normal procedures for handling State Cookies when
1567 * a TCB exists will resolve the duplicate INITs to a single association.
1568 *
1569 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1570 * its Tie-Tags with the Tag information of itself and its peer (see
1571 * section 5.2.2 for a description of the Tie-Tags).
1572 *
1573 * Verification Tag: Not explicit, but an INIT can not have a valid
1574 * verification tag, so we skip the check.
1575 *
1576 * Inputs
1577 * (endpoint, asoc, chunk)
1578 *
1579 * Outputs
1580 * (asoc, reply_msg, msg_up, timers, counters)
1581 *
1582 * The return value is the disposition of the chunk.
1583 */
1584sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1585 const struct sctp_association *asoc,
1586 const sctp_subtype_t type,
1587 void *arg,
1588 sctp_cmd_seq_t *commands)
1589{
1590 /* Call helper to do the real work for both simulataneous and
1591 * duplicate INIT chunk handling.
1592 */
1593 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1594}
1595
1596/*
1597 * Handle duplicated INIT messages. These are usually delayed
1598 * restransmissions.
1599 *
1600 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1601 * COOKIE-ECHOED and COOKIE-WAIT
1602 *
1603 * Unless otherwise stated, upon reception of an unexpected INIT for
1604 * this association, the endpoint shall generate an INIT ACK with a
1605 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1606 * current Verification Tag and peer's Verification Tag into a reserved
1607 * place within the state cookie. We shall refer to these locations as
1608 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1609 * containing this INIT ACK MUST carry a Verification Tag value equal to
1610 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1611 * MUST contain a new Initiation Tag (randomly generated see Section
1612 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1613 * existing parameters of the association (e.g. number of outbound
1614 * streams) into the INIT ACK and cookie.
1615 *
1616 * After sending out the INIT ACK, the endpoint shall take no further
1617 * actions, i.e., the existing association, including its current state,
1618 * and the corresponding TCB MUST NOT be changed.
1619 *
1620 * Note: Only when a TCB exists and the association is not in a COOKIE-
1621 * WAIT state are the Tie-Tags populated. For a normal association INIT
1622 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1623 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1624 * State Cookie are populated as specified in section 5.2.1.
1625 *
1626 * Verification Tag: Not specified, but an INIT has no way of knowing
1627 * what the verification tag could be, so we ignore it.
1628 *
1629 * Inputs
1630 * (endpoint, asoc, chunk)
1631 *
1632 * Outputs
1633 * (asoc, reply_msg, msg_up, timers, counters)
1634 *
1635 * The return value is the disposition of the chunk.
1636 */
1637sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1638 const struct sctp_association *asoc,
1639 const sctp_subtype_t type,
1640 void *arg,
1641 sctp_cmd_seq_t *commands)
1642{
1643 /* Call helper to do the real work for both simulataneous and
1644 * duplicate INIT chunk handling.
1645 */
1646 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1647}
1648
1649
Vlad Yasevich610ab732007-01-15 19:18:30 -08001650/*
1651 * Unexpected INIT-ACK handler.
1652 *
1653 * Section 5.2.3
1654 * If an INIT ACK received by an endpoint in any state other than the
1655 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1656 * An unexpected INIT ACK usually indicates the processing of an old or
1657 * duplicated INIT chunk.
1658*/
1659sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1660 const struct sctp_association *asoc,
1661 const sctp_subtype_t type,
1662 void *arg, sctp_cmd_seq_t *commands)
1663{
1664 /* Per the above section, we'll discard the chunk if we have an
1665 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1666 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001667 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001668 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1669 else
1670 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1674 *
1675 * Section 5.2.4
1676 * A) In this case, the peer may have restarted.
1677 */
1678static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1679 const struct sctp_association *asoc,
1680 struct sctp_chunk *chunk,
1681 sctp_cmd_seq_t *commands,
1682 struct sctp_association *new_asoc)
1683{
1684 sctp_init_chunk_t *peer_init;
1685 struct sctp_ulpevent *ev;
1686 struct sctp_chunk *repl;
1687 struct sctp_chunk *err;
1688 sctp_disposition_t disposition;
1689
1690 /* new_asoc is a brand-new association, so these are not yet
1691 * side effects--it is safe to run them here.
1692 */
1693 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1694
1695 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1696 sctp_source(chunk), peer_init,
1697 GFP_ATOMIC))
1698 goto nomem;
1699
1700 /* Make sure no new addresses are being added during the
1701 * restart. Though this is a pretty complicated attack
1702 * since you'd have to get inside the cookie.
1703 */
1704 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1705 return SCTP_DISPOSITION_CONSUME;
1706 }
1707
1708 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1709 * the peer has restarted (Action A), it MUST NOT setup a new
1710 * association but instead resend the SHUTDOWN ACK and send an ERROR
1711 * chunk with a "Cookie Received while Shutting Down" error cause to
1712 * its peer.
1713 */
1714 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1715 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1716 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1717 chunk, commands);
1718 if (SCTP_DISPOSITION_NOMEM == disposition)
1719 goto nomem;
1720
1721 err = sctp_make_op_error(asoc, chunk,
1722 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1723 NULL, 0);
1724 if (err)
1725 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1726 SCTP_CHUNK(err));
1727
1728 return SCTP_DISPOSITION_CONSUME;
1729 }
1730
1731 /* For now, fail any unsent/unacked data. Consider the optional
1732 * choice of resending of this data.
1733 */
1734 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 repl = sctp_make_cookie_ack(new_asoc, chunk);
1737 if (!repl)
1738 goto nomem;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 /* Report association restart to upper layer. */
1741 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1742 new_asoc->c.sinit_num_ostreams,
1743 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001744 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 if (!ev)
1746 goto nomem_ev;
1747
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001748 /* Update the content of current association. */
1749 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1750 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1752 return SCTP_DISPOSITION_CONSUME;
1753
1754nomem_ev:
1755 sctp_chunk_free(repl);
1756nomem:
1757 return SCTP_DISPOSITION_NOMEM;
1758}
1759
1760/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1761 *
1762 * Section 5.2.4
1763 * B) In this case, both sides may be attempting to start an association
1764 * at about the same time but the peer endpoint started its INIT
1765 * after responding to the local endpoint's INIT
1766 */
1767/* This case represents an initialization collision. */
1768static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1769 const struct sctp_association *asoc,
1770 struct sctp_chunk *chunk,
1771 sctp_cmd_seq_t *commands,
1772 struct sctp_association *new_asoc)
1773{
1774 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 struct sctp_chunk *repl;
1776
1777 /* new_asoc is a brand-new association, so these are not yet
1778 * side effects--it is safe to run them here.
1779 */
1780 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1781 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1782 sctp_source(chunk), peer_init,
1783 GFP_ATOMIC))
1784 goto nomem;
1785
1786 /* Update the content of current association. */
1787 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1788 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1789 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1790 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1791 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1792
1793 repl = sctp_make_cookie_ack(new_asoc, chunk);
1794 if (!repl)
1795 goto nomem;
1796
1797 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 /* RFC 2960 5.1 Normal Establishment of an Association
1800 *
1801 * D) IMPLEMENTATION NOTE: An implementation may choose to
1802 * send the Communication Up notification to the SCTP user
1803 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001804 *
1805 * Sadly, this needs to be implemented as a side-effect, because
1806 * we are not guaranteed to have set the association id of the real
1807 * association and so these notifications need to be delayed until
1808 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Vlad Yasevich07d93962007-05-04 13:55:27 -07001811 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001814 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001816 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001817 *
1818 * This also needs to be done as a side effect for the same reason as
1819 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001821 if (asoc->peer.adaptation_ind)
1822 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 return SCTP_DISPOSITION_CONSUME;
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826nomem:
1827 return SCTP_DISPOSITION_NOMEM;
1828}
1829
1830/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1831 *
1832 * Section 5.2.4
1833 * C) In this case, the local endpoint's cookie has arrived late.
1834 * Before it arrived, the local endpoint sent an INIT and received an
1835 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1836 * but a new tag of its own.
1837 */
1838/* This case represents an initialization collision. */
1839static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1840 const struct sctp_association *asoc,
1841 struct sctp_chunk *chunk,
1842 sctp_cmd_seq_t *commands,
1843 struct sctp_association *new_asoc)
1844{
1845 /* The cookie should be silently discarded.
1846 * The endpoint SHOULD NOT change states and should leave
1847 * any timers running.
1848 */
1849 return SCTP_DISPOSITION_DISCARD;
1850}
1851
1852/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1853 *
1854 * Section 5.2.4
1855 *
1856 * D) When both local and remote tags match the endpoint should always
1857 * enter the ESTABLISHED state, if it has not already done so.
1858 */
1859/* This case represents an initialization collision. */
1860static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1861 const struct sctp_association *asoc,
1862 struct sctp_chunk *chunk,
1863 sctp_cmd_seq_t *commands,
1864 struct sctp_association *new_asoc)
1865{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001866 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 struct sctp_chunk *repl;
1868
1869 /* Clarification from Implementor's Guide:
1870 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001871 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1872 * It should stop any cookie timer that may be running and send
1873 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 */
1875
1876 /* Don't accidentally move back into established state. */
1877 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1878 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1879 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1880 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1881 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1882 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1883 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1884 SCTP_NULL());
1885
1886 /* RFC 2960 5.1 Normal Establishment of an Association
1887 *
1888 * D) IMPLEMENTATION NOTE: An implementation may choose
1889 * to send the Communication Up notification to the
1890 * SCTP user upon reception of a valid COOKIE
1891 * ECHO chunk.
1892 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001893 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001895 asoc->c.sinit_num_ostreams,
1896 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001897 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (!ev)
1899 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001902 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001904 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001906 if (asoc->peer.adaptation_ind) {
1907 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001909 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto nomem;
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 repl = sctp_make_cookie_ack(new_asoc, chunk);
1916 if (!repl)
1917 goto nomem;
1918
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001919 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1920
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001921 if (ev)
1922 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1923 SCTP_ULPEVENT(ev));
1924 if (ai_ev)
1925 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1926 SCTP_ULPEVENT(ai_ev));
1927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return SCTP_DISPOSITION_CONSUME;
1929
1930nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001931 if (ai_ev)
1932 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (ev)
1934 sctp_ulpevent_free(ev);
1935 return SCTP_DISPOSITION_NOMEM;
1936}
1937
1938/*
1939 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1940 * chunk was retransmitted and then delayed in the network.
1941 *
1942 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1943 *
1944 * Verification Tag: None. Do cookie validation.
1945 *
1946 * Inputs
1947 * (endpoint, asoc, chunk)
1948 *
1949 * Outputs
1950 * (asoc, reply_msg, msg_up, timers, counters)
1951 *
1952 * The return value is the disposition of the chunk.
1953 */
1954sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1955 const struct sctp_association *asoc,
1956 const sctp_subtype_t type,
1957 void *arg,
1958 sctp_cmd_seq_t *commands)
1959{
1960 sctp_disposition_t retval;
1961 struct sctp_chunk *chunk = arg;
1962 struct sctp_association *new_asoc;
1963 int error = 0;
1964 char action;
1965 struct sctp_chunk *err_chk_p;
1966
1967 /* Make sure that the chunk has a valid length from the protocol
1968 * perspective. In this case check to make sure we have at least
1969 * enough for the chunk header. Cookie length verification is
1970 * done later.
1971 */
1972 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1973 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1974 commands);
1975
1976 /* "Decode" the chunk. We have no optional parameters so we
1977 * are in good shape.
1978 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001979 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001980 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1981 sizeof(sctp_chunkhdr_t)))
1982 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1985 * of a duplicate COOKIE ECHO match the Verification Tags of the
1986 * current association, consider the State Cookie valid even if
1987 * the lifespan is exceeded.
1988 */
1989 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1990 &err_chk_p);
1991
1992 /* FIXME:
1993 * If the re-build failed, what is the proper error path
1994 * from here?
1995 *
1996 * [We should abort the association. --piggy]
1997 */
1998 if (!new_asoc) {
1999 /* FIXME: Several errors are possible. A bad cookie should
2000 * be silently discarded, but think about logging it too.
2001 */
2002 switch (error) {
2003 case -SCTP_IERROR_NOMEM:
2004 goto nomem;
2005
2006 case -SCTP_IERROR_STALE_COOKIE:
2007 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2008 err_chk_p);
2009 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2010 case -SCTP_IERROR_BAD_SIG:
2011 default:
2012 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015
2016 /* Compare the tie_tag in cookie with the verification tag of
2017 * current association.
2018 */
2019 action = sctp_tietags_compare(new_asoc, asoc);
2020
2021 switch (action) {
2022 case 'A': /* Association restart. */
2023 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2024 new_asoc);
2025 break;
2026
2027 case 'B': /* Collision case B. */
2028 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2029 new_asoc);
2030 break;
2031
2032 case 'C': /* Collision case C. */
2033 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2034 new_asoc);
2035 break;
2036
2037 case 'D': /* Collision case D. */
2038 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2039 new_asoc);
2040 break;
2041
2042 default: /* Discard packet for all others. */
2043 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2044 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* Delete the tempory new association. */
2048 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2049 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2050
2051 return retval;
2052
2053nomem:
2054 return SCTP_DISPOSITION_NOMEM;
2055}
2056
2057/*
2058 * Process an ABORT. (SHUTDOWN-PENDING state)
2059 *
2060 * See sctp_sf_do_9_1_abort().
2061 */
2062sctp_disposition_t sctp_sf_shutdown_pending_abort(
2063 const struct sctp_endpoint *ep,
2064 const struct sctp_association *asoc,
2065 const sctp_subtype_t type,
2066 void *arg,
2067 sctp_cmd_seq_t *commands)
2068{
2069 struct sctp_chunk *chunk = arg;
2070
2071 if (!sctp_vtag_verify_either(chunk, asoc))
2072 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2073
2074 /* Make sure that the ABORT chunk has a valid length.
2075 * Since this is an ABORT chunk, we have to discard it
2076 * because of the following text:
2077 * RFC 2960, Section 3.3.7
2078 * If an endpoint receives an ABORT with a format error or for an
2079 * association that doesn't exist, it MUST silently discard it.
2080 * Becasue the length is "invalid", we can't really discard just
2081 * as we do not know its true length. So, to be safe, discard the
2082 * packet.
2083 */
2084 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2085 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2086
Vlad Yasevich75205f42007-12-20 14:12:59 -08002087 /* ADD-IP: Special case for ABORT chunks
2088 * F4) One special consideration is that ABORT Chunks arriving
2089 * destined to the IP address being deleted MUST be
2090 * ignored (see Section 5.3.1 for further details).
2091 */
2092 if (SCTP_ADDR_DEL ==
2093 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2094 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2095
Vlad Yasevich75205f42007-12-20 14:12:59 -08002096 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097}
2098
2099/*
2100 * Process an ABORT. (SHUTDOWN-SENT state)
2101 *
2102 * See sctp_sf_do_9_1_abort().
2103 */
2104sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2105 const struct sctp_association *asoc,
2106 const sctp_subtype_t type,
2107 void *arg,
2108 sctp_cmd_seq_t *commands)
2109{
2110 struct sctp_chunk *chunk = arg;
2111
2112 if (!sctp_vtag_verify_either(chunk, asoc))
2113 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2114
2115 /* Make sure that the ABORT chunk has a valid length.
2116 * Since this is an ABORT chunk, we have to discard it
2117 * because of the following text:
2118 * RFC 2960, Section 3.3.7
2119 * If an endpoint receives an ABORT with a format error or for an
2120 * association that doesn't exist, it MUST silently discard it.
2121 * Becasue the length is "invalid", we can't really discard just
2122 * as we do not know its true length. So, to be safe, discard the
2123 * packet.
2124 */
2125 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2126 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2127
Vlad Yasevich75205f42007-12-20 14:12:59 -08002128 /* ADD-IP: Special case for ABORT chunks
2129 * F4) One special consideration is that ABORT Chunks arriving
2130 * destined to the IP address being deleted MUST be
2131 * ignored (see Section 5.3.1 for further details).
2132 */
2133 if (SCTP_ADDR_DEL ==
2134 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2135 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /* Stop the T2-shutdown timer. */
2138 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2139 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2140
2141 /* Stop the T5-shutdown guard timer. */
2142 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2143 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2144
Vlad Yasevich75205f42007-12-20 14:12:59 -08002145 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
2148/*
2149 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2150 *
2151 * See sctp_sf_do_9_1_abort().
2152 */
2153sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2154 const struct sctp_endpoint *ep,
2155 const struct sctp_association *asoc,
2156 const sctp_subtype_t type,
2157 void *arg,
2158 sctp_cmd_seq_t *commands)
2159{
2160 /* The same T2 timer, so we should be able to use
2161 * common function with the SHUTDOWN-SENT state.
2162 */
2163 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2164}
2165
2166/*
2167 * Handle an Error received in COOKIE_ECHOED state.
2168 *
2169 * Only handle the error type of stale COOKIE Error, the other errors will
2170 * be ignored.
2171 *
2172 * Inputs
2173 * (endpoint, asoc, chunk)
2174 *
2175 * Outputs
2176 * (asoc, reply_msg, msg_up, timers, counters)
2177 *
2178 * The return value is the disposition of the chunk.
2179 */
2180sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2181 const struct sctp_association *asoc,
2182 const sctp_subtype_t type,
2183 void *arg,
2184 sctp_cmd_seq_t *commands)
2185{
2186 struct sctp_chunk *chunk = arg;
2187 sctp_errhdr_t *err;
2188
2189 if (!sctp_vtag_verify(chunk, asoc))
2190 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2191
2192 /* Make sure that the ERROR chunk has a valid length.
2193 * The parameter walking depends on this as well.
2194 */
2195 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2196 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2197 commands);
2198
2199 /* Process the error here */
2200 /* FUTURE FIXME: When PR-SCTP related and other optional
2201 * parms are emitted, this will have to change to handle multiple
2202 * errors.
2203 */
2204 sctp_walk_errors(err, chunk->chunk_hdr) {
2205 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002206 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 arg, commands);
2208 }
2209
2210 /* It is possible to have malformed error causes, and that
2211 * will cause us to end the walk early. However, since
2212 * we are discarding the packet, there should be no adverse
2213 * affects.
2214 */
2215 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2216}
2217
2218/*
2219 * Handle a Stale COOKIE Error
2220 *
2221 * Section: 5.2.6 Handle Stale COOKIE Error
2222 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2223 * one of the following three alternatives.
2224 * ...
2225 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2226 * Preservative parameter requesting an extension to the lifetime of
2227 * the State Cookie. When calculating the time extension, an
2228 * implementation SHOULD use the RTT information measured based on the
2229 * previous COOKIE ECHO / ERROR exchange, and should add no more
2230 * than 1 second beyond the measured RTT, due to long State Cookie
2231 * lifetimes making the endpoint more subject to a replay attack.
2232 *
2233 * Verification Tag: Not explicit, but safe to ignore.
2234 *
2235 * Inputs
2236 * (endpoint, asoc, chunk)
2237 *
2238 * Outputs
2239 * (asoc, reply_msg, msg_up, timers, counters)
2240 *
2241 * The return value is the disposition of the chunk.
2242 */
2243static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2244 const struct sctp_association *asoc,
2245 const sctp_subtype_t type,
2246 void *arg,
2247 sctp_cmd_seq_t *commands)
2248{
2249 struct sctp_chunk *chunk = arg;
2250 time_t stale;
2251 sctp_cookie_preserve_param_t bht;
2252 sctp_errhdr_t *err;
2253 struct sctp_chunk *reply;
2254 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002255 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Vlad Yasevich81845c22006-01-30 15:59:54 -08002257 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002258 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2259 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002261 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 return SCTP_DISPOSITION_DELETE_TCB;
2263 }
2264
2265 err = (sctp_errhdr_t *)(chunk->skb->data);
2266
2267 /* When calculating the time extension, an implementation
2268 * SHOULD use the RTT information measured based on the
2269 * previous COOKIE ECHO / ERROR exchange, and should add no
2270 * more than 1 second beyond the measured RTT, due to long
2271 * State Cookie lifetimes making the endpoint more subject to
2272 * a replay attack.
2273 * Measure of Staleness's unit is usec. (1/1000000 sec)
2274 * Suggested Cookie Life-span Increment's unit is msec.
2275 * (1/1000 sec)
2276 * In general, if you use the suggested cookie life, the value
2277 * found in the field of measure of staleness should be doubled
2278 * to give ample time to retransmit the new cookie and thus
2279 * yield a higher probability of success on the reattempt.
2280 */
Al Viro34bcca22006-11-20 17:27:15 -08002281 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 stale = (stale * 2) / 1000;
2283
2284 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2285 bht.param_hdr.length = htons(sizeof(bht));
2286 bht.lifespan_increment = htonl(stale);
2287
2288 /* Build that new INIT chunk. */
2289 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2290 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2291 if (!reply)
2292 goto nomem;
2293
2294 sctp_addto_chunk(reply, sizeof(bht), &bht);
2295
2296 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2297 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2298
2299 /* Stop pending T3-rtx and heartbeat timers */
2300 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2301 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2302
2303 /* Delete non-primary peer ip addresses since we are transitioning
2304 * back to the COOKIE-WAIT state
2305 */
2306 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2307
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002308 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2309 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002311 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 SCTP_TRANSPORT(asoc->peer.primary_path));
2313
2314 /* Cast away the const modifier, as we want to just
2315 * rerun it through as a sideffect.
2316 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002317 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2320 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2321 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2322 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2323 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2324 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2325
2326 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2327
2328 return SCTP_DISPOSITION_CONSUME;
2329
2330nomem:
2331 return SCTP_DISPOSITION_NOMEM;
2332}
2333
2334/*
2335 * Process an ABORT.
2336 *
2337 * Section: 9.1
2338 * After checking the Verification Tag, the receiving endpoint shall
2339 * remove the association from its record, and shall report the
2340 * termination to its upper layer.
2341 *
2342 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2343 * B) Rules for packet carrying ABORT:
2344 *
2345 * - The endpoint shall always fill in the Verification Tag field of the
2346 * outbound packet with the destination endpoint's tag value if it
2347 * is known.
2348 *
2349 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2350 * MUST follow the procedure described in Section 8.4.
2351 *
2352 * - The receiver MUST accept the packet if the Verification Tag
2353 * matches either its own tag, OR the tag of its peer. Otherwise, the
2354 * receiver MUST silently discard the packet and take no further
2355 * action.
2356 *
2357 * Inputs
2358 * (endpoint, asoc, chunk)
2359 *
2360 * Outputs
2361 * (asoc, reply_msg, msg_up, timers, counters)
2362 *
2363 * The return value is the disposition of the chunk.
2364 */
2365sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2366 const struct sctp_association *asoc,
2367 const sctp_subtype_t type,
2368 void *arg,
2369 sctp_cmd_seq_t *commands)
2370{
2371 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 if (!sctp_vtag_verify_either(chunk, asoc))
2374 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2375
2376 /* Make sure that the ABORT chunk has a valid length.
2377 * Since this is an ABORT chunk, we have to discard it
2378 * because of the following text:
2379 * RFC 2960, Section 3.3.7
2380 * If an endpoint receives an ABORT with a format error or for an
2381 * association that doesn't exist, it MUST silently discard it.
2382 * Becasue the length is "invalid", we can't really discard just
2383 * as we do not know its true length. So, to be safe, discard the
2384 * packet.
2385 */
2386 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2387 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2388
Vlad Yasevich75205f42007-12-20 14:12:59 -08002389 /* ADD-IP: Special case for ABORT chunks
2390 * F4) One special consideration is that ABORT Chunks arriving
2391 * destined to the IP address being deleted MUST be
2392 * ignored (see Section 5.3.1 for further details).
2393 */
2394 if (SCTP_ADDR_DEL ==
2395 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2396 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2397
2398 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2399}
2400
2401static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2402 const struct sctp_association *asoc,
2403 const sctp_subtype_t type,
2404 void *arg,
2405 sctp_cmd_seq_t *commands)
2406{
2407 struct sctp_chunk *chunk = arg;
2408 unsigned len;
2409 __be16 error = SCTP_ERROR_NO_ERROR;
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /* See if we have an error cause code in the chunk. */
2412 len = ntohs(chunk->chunk_hdr->length);
2413 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2414 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2415
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002416 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002417 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002418 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2420 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2421
2422 return SCTP_DISPOSITION_ABORT;
2423}
2424
2425/*
2426 * Process an ABORT. (COOKIE-WAIT state)
2427 *
2428 * See sctp_sf_do_9_1_abort() above.
2429 */
2430sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2431 const struct sctp_association *asoc,
2432 const sctp_subtype_t type,
2433 void *arg,
2434 sctp_cmd_seq_t *commands)
2435{
2436 struct sctp_chunk *chunk = arg;
2437 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002438 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (!sctp_vtag_verify_either(chunk, asoc))
2441 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2442
2443 /* Make sure that the ABORT chunk has a valid length.
2444 * Since this is an ABORT chunk, we have to discard it
2445 * because of the following text:
2446 * RFC 2960, Section 3.3.7
2447 * If an endpoint receives an ABORT with a format error or for an
2448 * association that doesn't exist, it MUST silently discard it.
2449 * Becasue the length is "invalid", we can't really discard just
2450 * as we do not know its true length. So, to be safe, discard the
2451 * packet.
2452 */
2453 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2454 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2455
2456 /* See if we have an error cause code in the chunk. */
2457 len = ntohs(chunk->chunk_hdr->length);
2458 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2459 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2460
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002461 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2462 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
2465/*
2466 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2467 */
2468sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2469 const struct sctp_association *asoc,
2470 const sctp_subtype_t type,
2471 void *arg,
2472 sctp_cmd_seq_t *commands)
2473{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002474 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2475 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002476 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479/*
2480 * Process an ABORT. (COOKIE-ECHOED state)
2481 */
2482sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2483 const struct sctp_association *asoc,
2484 const sctp_subtype_t type,
2485 void *arg,
2486 sctp_cmd_seq_t *commands)
2487{
2488 /* There is a single T1 timer, so we should be able to use
2489 * common function with the COOKIE-WAIT state.
2490 */
2491 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2492}
2493
2494/*
2495 * Stop T1 timer and abort association with "INIT failed".
2496 *
2497 * This is common code called by several sctp_sf_*_abort() functions above.
2498 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002499static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002500 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002501 const struct sctp_association *asoc,
2502 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002504 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2506 SCTP_STATE(SCTP_STATE_CLOSED));
2507 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2508 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2509 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002510 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /* CMD_INIT_FAILED will DELETE_TCB. */
2512 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002513 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002514 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
2516
2517/*
2518 * sctp_sf_do_9_2_shut
2519 *
2520 * Section: 9.2
2521 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2522 * - enter the SHUTDOWN-RECEIVED state,
2523 *
2524 * - stop accepting new data from its SCTP user
2525 *
2526 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2527 * that all its outstanding DATA chunks have been received by the
2528 * SHUTDOWN sender.
2529 *
2530 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2531 * send a SHUTDOWN in response to a ULP request. And should discard
2532 * subsequent SHUTDOWN chunks.
2533 *
2534 * If there are still outstanding DATA chunks left, the SHUTDOWN
2535 * receiver shall continue to follow normal data transmission
2536 * procedures defined in Section 6 until all outstanding DATA chunks
2537 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2538 * new data from its SCTP user.
2539 *
2540 * Verification Tag: 8.5 Verification Tag [Normal verification]
2541 *
2542 * Inputs
2543 * (endpoint, asoc, chunk)
2544 *
2545 * Outputs
2546 * (asoc, reply_msg, msg_up, timers, counters)
2547 *
2548 * The return value is the disposition of the chunk.
2549 */
2550sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2551 const struct sctp_association *asoc,
2552 const sctp_subtype_t type,
2553 void *arg,
2554 sctp_cmd_seq_t *commands)
2555{
2556 struct sctp_chunk *chunk = arg;
2557 sctp_shutdownhdr_t *sdh;
2558 sctp_disposition_t disposition;
2559 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002560 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 if (!sctp_vtag_verify(chunk, asoc))
2563 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2564
2565 /* Make sure that the SHUTDOWN chunk has a valid length. */
2566 if (!sctp_chunk_length_valid(chunk,
2567 sizeof(struct sctp_shutdown_chunk_t)))
2568 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2569 commands);
2570
2571 /* Convert the elaborate header. */
2572 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2573 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2574 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002575 ctsn = ntohl(sdh->cum_tsn_ack);
2576
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002577 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2578 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2579 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2580 return SCTP_DISPOSITION_DISCARD;
2581 }
2582
Wei Yongjundf10eec2008-10-23 01:00:21 -07002583 /* If Cumulative TSN Ack beyond the max tsn currently
2584 * send, terminating the association and respond to the
2585 * sender with an ABORT.
2586 */
2587 if (!TSN_lt(ctsn, asoc->next_tsn))
2588 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002590 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2591 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2592 * inform the application that it should cease sending data.
2593 */
2594 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2595 if (!ev) {
2596 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002597 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002598 }
2599 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2602 * - enter the SHUTDOWN-RECEIVED state,
2603 * - stop accepting new data from its SCTP user
2604 *
2605 * [This is implicit in the new state.]
2606 */
2607 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2608 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2609 disposition = SCTP_DISPOSITION_CONSUME;
2610
2611 if (sctp_outq_is_empty(&asoc->outqueue)) {
2612 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2613 arg, commands);
2614 }
2615
2616 if (SCTP_DISPOSITION_NOMEM == disposition)
2617 goto out;
2618
2619 /* - verify, by checking the Cumulative TSN Ack field of the
2620 * chunk, that all its outstanding DATA chunks have been
2621 * received by the SHUTDOWN sender.
2622 */
2623 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002624 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626out:
2627 return disposition;
2628}
2629
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002630/*
2631 * sctp_sf_do_9_2_shut_ctsn
2632 *
2633 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2634 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2635 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2636 * MUST be processed.
2637 */
2638sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2639 const struct sctp_association *asoc,
2640 const sctp_subtype_t type,
2641 void *arg,
2642 sctp_cmd_seq_t *commands)
2643{
2644 struct sctp_chunk *chunk = arg;
2645 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002646 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002647
2648 if (!sctp_vtag_verify(chunk, asoc))
2649 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2650
2651 /* Make sure that the SHUTDOWN chunk has a valid length. */
2652 if (!sctp_chunk_length_valid(chunk,
2653 sizeof(struct sctp_shutdown_chunk_t)))
2654 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2655 commands);
2656
2657 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002658 ctsn = ntohl(sdh->cum_tsn_ack);
2659
2660 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2661 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2662 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2663 return SCTP_DISPOSITION_DISCARD;
2664 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002665
2666 /* If Cumulative TSN Ack beyond the max tsn currently
2667 * send, terminating the association and respond to the
2668 * sender with an ABORT.
2669 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002670 if (!TSN_lt(ctsn, asoc->next_tsn))
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002671 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2672
2673 /* verify, by checking the Cumulative TSN Ack field of the
2674 * chunk, that all its outstanding DATA chunks have been
2675 * received by the SHUTDOWN sender.
2676 */
2677 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2678 SCTP_BE32(sdh->cum_tsn_ack));
2679
2680 return SCTP_DISPOSITION_CONSUME;
2681}
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/* RFC 2960 9.2
2684 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2685 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2686 * transport addresses (either in the IP addresses or in the INIT chunk)
2687 * that belong to this association, it should discard the INIT chunk and
2688 * retransmit the SHUTDOWN ACK chunk.
2689 */
2690sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2691 const struct sctp_association *asoc,
2692 const sctp_subtype_t type,
2693 void *arg,
2694 sctp_cmd_seq_t *commands)
2695{
2696 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2697 struct sctp_chunk *reply;
2698
Vlad Yasevichece25df2007-09-07 16:30:54 -04002699 /* Make sure that the chunk has a valid length */
2700 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2701 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2702 commands);
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Since we are not going to really process this INIT, there
2705 * is no point in verifying chunk boundries. Just generate
2706 * the SHUTDOWN ACK.
2707 */
2708 reply = sctp_make_shutdown_ack(asoc, chunk);
2709 if (NULL == reply)
2710 goto nomem;
2711
2712 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2713 * the T2-SHUTDOWN timer.
2714 */
2715 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2716
2717 /* and restart the T2-shutdown timer. */
2718 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2719 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2720
2721 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2722
2723 return SCTP_DISPOSITION_CONSUME;
2724nomem:
2725 return SCTP_DISPOSITION_NOMEM;
2726}
2727
2728/*
2729 * sctp_sf_do_ecn_cwr
2730 *
2731 * Section: Appendix A: Explicit Congestion Notification
2732 *
2733 * CWR:
2734 *
2735 * RFC 2481 details a specific bit for a sender to send in the header of
2736 * its next outbound TCP segment to indicate to its peer that it has
2737 * reduced its congestion window. This is termed the CWR bit. For
2738 * SCTP the same indication is made by including the CWR chunk.
2739 * This chunk contains one data element, i.e. the TSN number that
2740 * was sent in the ECNE chunk. This element represents the lowest
2741 * TSN number in the datagram that was originally marked with the
2742 * CE bit.
2743 *
2744 * Verification Tag: 8.5 Verification Tag [Normal verification]
2745 * Inputs
2746 * (endpoint, asoc, chunk)
2747 *
2748 * Outputs
2749 * (asoc, reply_msg, msg_up, timers, counters)
2750 *
2751 * The return value is the disposition of the chunk.
2752 */
2753sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2754 const struct sctp_association *asoc,
2755 const sctp_subtype_t type,
2756 void *arg,
2757 sctp_cmd_seq_t *commands)
2758{
2759 sctp_cwrhdr_t *cwr;
2760 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002761 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762
2763 if (!sctp_vtag_verify(chunk, asoc))
2764 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2765
2766 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2767 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2768 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2771 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2772
Al Viro34bcca22006-11-20 17:27:15 -08002773 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002776 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /* Stop sending ECNE. */
2778 sctp_add_cmd_sf(commands,
2779 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002780 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
2782 return SCTP_DISPOSITION_CONSUME;
2783}
2784
2785/*
2786 * sctp_sf_do_ecne
2787 *
2788 * Section: Appendix A: Explicit Congestion Notification
2789 *
2790 * ECN-Echo
2791 *
2792 * RFC 2481 details a specific bit for a receiver to send back in its
2793 * TCP acknowledgements to notify the sender of the Congestion
2794 * Experienced (CE) bit having arrived from the network. For SCTP this
2795 * same indication is made by including the ECNE chunk. This chunk
2796 * contains one data element, i.e. the lowest TSN associated with the IP
2797 * datagram marked with the CE bit.....
2798 *
2799 * Verification Tag: 8.5 Verification Tag [Normal verification]
2800 * Inputs
2801 * (endpoint, asoc, chunk)
2802 *
2803 * Outputs
2804 * (asoc, reply_msg, msg_up, timers, counters)
2805 *
2806 * The return value is the disposition of the chunk.
2807 */
2808sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2809 const struct sctp_association *asoc,
2810 const sctp_subtype_t type,
2811 void *arg,
2812 sctp_cmd_seq_t *commands)
2813{
2814 sctp_ecnehdr_t *ecne;
2815 struct sctp_chunk *chunk = arg;
2816
2817 if (!sctp_vtag_verify(chunk, asoc))
2818 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2819
2820 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2821 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2822 commands);
2823
2824 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2825 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2826
2827 /* If this is a newer ECNE than the last CWR packet we sent out */
2828 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2829 SCTP_U32(ntohl(ecne->lowest_tsn)));
2830
2831 return SCTP_DISPOSITION_CONSUME;
2832}
2833
2834/*
2835 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2836 *
2837 * The SCTP endpoint MUST always acknowledge the reception of each valid
2838 * DATA chunk.
2839 *
2840 * The guidelines on delayed acknowledgement algorithm specified in
2841 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2842 * acknowledgement SHOULD be generated for at least every second packet
2843 * (not every second DATA chunk) received, and SHOULD be generated within
2844 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2845 * situations it may be beneficial for an SCTP transmitter to be more
2846 * conservative than the algorithms detailed in this document allow.
2847 * However, an SCTP transmitter MUST NOT be more aggressive than the
2848 * following algorithms allow.
2849 *
2850 * A SCTP receiver MUST NOT generate more than one SACK for every
2851 * incoming packet, other than to update the offered window as the
2852 * receiving application consumes new data.
2853 *
2854 * Verification Tag: 8.5 Verification Tag [Normal verification]
2855 *
2856 * Inputs
2857 * (endpoint, asoc, chunk)
2858 *
2859 * Outputs
2860 * (asoc, reply_msg, msg_up, timers, counters)
2861 *
2862 * The return value is the disposition of the chunk.
2863 */
2864sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2865 const struct sctp_association *asoc,
2866 const sctp_subtype_t type,
2867 void *arg,
2868 sctp_cmd_seq_t *commands)
2869{
2870 struct sctp_chunk *chunk = arg;
Wei Yongjun6dc76942009-11-23 15:53:53 -05002871 sctp_arg_t force = SCTP_NOFORCE();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 int error;
2873
2874 if (!sctp_vtag_verify(chunk, asoc)) {
2875 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2876 SCTP_NULL());
2877 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2881 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2882 commands);
2883
2884 error = sctp_eat_data(asoc, chunk, commands );
2885 switch (error) {
2886 case SCTP_IERROR_NO_ERROR:
2887 break;
2888 case SCTP_IERROR_HIGH_TSN:
2889 case SCTP_IERROR_BAD_STREAM:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002890 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 goto discard_noforce;
2892 case SCTP_IERROR_DUP_TSN:
2893 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002894 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 goto discard_force;
2896 case SCTP_IERROR_NO_DATA:
2897 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04002898 case SCTP_IERROR_PROTO_VIOLATION:
2899 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
2900 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 default:
2902 BUG();
2903 }
2904
Wei Yongjun6dc76942009-11-23 15:53:53 -05002905 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2906 force = SCTP_FORCE();
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (asoc->autoclose) {
2909 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2910 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2911 }
2912
2913 /* If this is the last chunk in a packet, we need to count it
2914 * toward sack generation. Note that we need to SACK every
2915 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2916 * THEM. We elect to NOT generate SACK's if the chunk fails
2917 * the verification tag test.
2918 *
2919 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2920 *
2921 * The SCTP endpoint MUST always acknowledge the reception of
2922 * each valid DATA chunk.
2923 *
2924 * The guidelines on delayed acknowledgement algorithm
2925 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2926 * Specifically, an acknowledgement SHOULD be generated for at
2927 * least every second packet (not every second DATA chunk)
2928 * received, and SHOULD be generated within 200 ms of the
2929 * arrival of any unacknowledged DATA chunk. In some
2930 * situations it may be beneficial for an SCTP transmitter to
2931 * be more conservative than the algorithms detailed in this
2932 * document allow. However, an SCTP transmitter MUST NOT be
2933 * more aggressive than the following algorithms allow.
2934 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002935 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002936 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return SCTP_DISPOSITION_CONSUME;
2939
2940discard_force:
2941 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2942 *
2943 * When a packet arrives with duplicate DATA chunk(s) and with
2944 * no new DATA chunk(s), the endpoint MUST immediately send a
2945 * SACK with no delay. If a packet arrives with duplicate
2946 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2947 * MAY immediately send a SACK. Normally receipt of duplicate
2948 * DATA chunks will occur when the original SACK chunk was lost
2949 * and the peer's RTO has expired. The duplicate TSN number(s)
2950 * SHOULD be reported in the SACK as duplicate.
2951 */
2952 /* In our case, we split the MAY SACK advice up whether or not
2953 * the last chunk is a duplicate.'
2954 */
2955 if (chunk->end_of_packet)
2956 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2957 return SCTP_DISPOSITION_DISCARD;
2958
2959discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002960 if (chunk->end_of_packet)
Wei Yongjun6dc76942009-11-23 15:53:53 -05002961 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 return SCTP_DISPOSITION_DISCARD;
2964consume:
2965 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
2969/*
2970 * sctp_sf_eat_data_fast_4_4
2971 *
2972 * Section: 4 (4)
2973 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2974 * DATA chunks without delay.
2975 *
2976 * Verification Tag: 8.5 Verification Tag [Normal verification]
2977 * Inputs
2978 * (endpoint, asoc, chunk)
2979 *
2980 * Outputs
2981 * (asoc, reply_msg, msg_up, timers, counters)
2982 *
2983 * The return value is the disposition of the chunk.
2984 */
2985sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2986 const struct sctp_association *asoc,
2987 const sctp_subtype_t type,
2988 void *arg,
2989 sctp_cmd_seq_t *commands)
2990{
2991 struct sctp_chunk *chunk = arg;
2992 int error;
2993
2994 if (!sctp_vtag_verify(chunk, asoc)) {
2995 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2996 SCTP_NULL());
2997 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2998 }
2999
3000 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3001 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3002 commands);
3003
3004 error = sctp_eat_data(asoc, chunk, commands );
3005 switch (error) {
3006 case SCTP_IERROR_NO_ERROR:
3007 case SCTP_IERROR_HIGH_TSN:
3008 case SCTP_IERROR_DUP_TSN:
3009 case SCTP_IERROR_IGNORE_TSN:
3010 case SCTP_IERROR_BAD_STREAM:
3011 break;
3012 case SCTP_IERROR_NO_DATA:
3013 goto consume;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04003014 case SCTP_IERROR_PROTO_VIOLATION:
3015 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
3016 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 default:
3018 BUG();
3019 }
3020
3021 /* Go a head and force a SACK, since we are shutting down. */
3022
3023 /* Implementor's Guide.
3024 *
3025 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3026 * respond to each received packet containing one or more DATA chunk(s)
3027 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3028 */
3029 if (chunk->end_of_packet) {
3030 /* We must delay the chunk creation since the cumulative
3031 * TSN has not been updated yet.
3032 */
3033 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3034 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3035 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3036 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3037 }
3038
3039consume:
3040 return SCTP_DISPOSITION_CONSUME;
3041}
3042
3043/*
3044 * Section: 6.2 Processing a Received SACK
3045 * D) Any time a SACK arrives, the endpoint performs the following:
3046 *
3047 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3048 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3049 * increasing, a SACK whose Cumulative TSN Ack is less than the
3050 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3051 *
3052 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3053 * of bytes still outstanding after processing the Cumulative TSN Ack
3054 * and the Gap Ack Blocks.
3055 *
3056 * iii) If the SACK is missing a TSN that was previously
3057 * acknowledged via a Gap Ack Block (e.g., the data receiver
3058 * reneged on the data), then mark the corresponding DATA chunk
3059 * as available for retransmit: Mark it as missing for fast
3060 * retransmit as described in Section 7.2.4 and if no retransmit
3061 * timer is running for the destination address to which the DATA
3062 * chunk was originally transmitted, then T3-rtx is started for
3063 * that destination address.
3064 *
3065 * Verification Tag: 8.5 Verification Tag [Normal verification]
3066 *
3067 * Inputs
3068 * (endpoint, asoc, chunk)
3069 *
3070 * Outputs
3071 * (asoc, reply_msg, msg_up, timers, counters)
3072 *
3073 * The return value is the disposition of the chunk.
3074 */
3075sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3076 const struct sctp_association *asoc,
3077 const sctp_subtype_t type,
3078 void *arg,
3079 sctp_cmd_seq_t *commands)
3080{
3081 struct sctp_chunk *chunk = arg;
3082 sctp_sackhdr_t *sackh;
3083 __u32 ctsn;
3084
3085 if (!sctp_vtag_verify(chunk, asoc))
3086 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3087
3088 /* Make sure that the SACK chunk has a valid length. */
3089 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3090 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3091 commands);
3092
3093 /* Pull the SACK chunk from the data buffer */
3094 sackh = sctp_sm_pull_sack(chunk);
3095 /* Was this a bogus SACK? */
3096 if (!sackh)
3097 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3098 chunk->subh.sack_hdr = sackh;
3099 ctsn = ntohl(sackh->cum_tsn_ack);
3100
3101 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3102 * Ack Point, then drop the SACK. Since Cumulative TSN
3103 * Ack is monotonically increasing, a SACK whose
3104 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3105 * Point indicates an out-of-order SACK.
3106 */
3107 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3108 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3109 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3110 return SCTP_DISPOSITION_DISCARD;
3111 }
3112
Wei Yongjunaecedea2007-08-02 16:57:44 +08003113 /* If Cumulative TSN Ack beyond the max tsn currently
3114 * send, terminating the association and respond to the
3115 * sender with an ABORT.
3116 */
3117 if (!TSN_lt(ctsn, asoc->next_tsn))
3118 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 /* Return this SACK for further processing. */
3121 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3122
3123 /* Note: We do the rest of the work on the PROCESS_SACK
3124 * sideeffect.
3125 */
3126 return SCTP_DISPOSITION_CONSUME;
3127}
3128
3129/*
3130 * Generate an ABORT in response to a packet.
3131 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003132 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003134 * 8) The receiver should respond to the sender of the OOTB packet with
3135 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3136 * MUST fill in the Verification Tag field of the outbound packet
3137 * with the value found in the Verification Tag field of the OOTB
3138 * packet and set the T-bit in the Chunk Flags to indicate that the
3139 * Verification Tag is reflected. After sending this ABORT, the
3140 * receiver of the OOTB packet shall discard the OOTB packet and take
3141 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 *
3143 * Verification Tag:
3144 *
3145 * The return value is the disposition of the chunk.
3146*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003147static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 const struct sctp_association *asoc,
3149 const sctp_subtype_t type,
3150 void *arg,
3151 sctp_cmd_seq_t *commands)
3152{
3153 struct sctp_packet *packet = NULL;
3154 struct sctp_chunk *chunk = arg;
3155 struct sctp_chunk *abort;
3156
3157 packet = sctp_ootb_pkt_new(asoc, chunk);
3158
3159 if (packet) {
3160 /* Make an ABORT. The T bit will be set if the asoc
3161 * is NULL.
3162 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003163 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 if (!abort) {
3165 sctp_ootb_pkt_free(packet);
3166 return SCTP_DISPOSITION_NOMEM;
3167 }
3168
Jerome Forissier047a2422005-04-28 11:58:43 -07003169 /* Reflect vtag if T-Bit is set */
3170 if (sctp_test_T_bit(abort))
3171 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /* Set the skb to the belonging sock for accounting. */
3174 abort->skb->sk = ep->base.sk;
3175
3176 sctp_packet_append_chunk(packet, abort);
3177
3178 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3179 SCTP_PACKET(packet));
3180
3181 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3182
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003183 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return SCTP_DISPOSITION_CONSUME;
3185 }
3186
3187 return SCTP_DISPOSITION_NOMEM;
3188}
3189
3190/*
3191 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3192 * event as ULP notification for each cause included in the chunk.
3193 *
3194 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3195 *
3196 * The return value is the disposition of the chunk.
3197*/
3198sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3199 const struct sctp_association *asoc,
3200 const sctp_subtype_t type,
3201 void *arg,
3202 sctp_cmd_seq_t *commands)
3203{
3204 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
3206 if (!sctp_vtag_verify(chunk, asoc))
3207 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3208
3209 /* Make sure that the ERROR chunk has a valid length. */
3210 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3211 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3212 commands);
3213
Wei Yongjun3df26782009-03-02 06:46:51 +00003214 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3215 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
3220/*
3221 * Process an inbound SHUTDOWN ACK.
3222 *
3223 * From Section 9.2:
3224 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3225 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3226 * peer, and remove all record of the association.
3227 *
3228 * The return value is the disposition.
3229 */
3230sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3231 const struct sctp_association *asoc,
3232 const sctp_subtype_t type,
3233 void *arg,
3234 sctp_cmd_seq_t *commands)
3235{
3236 struct sctp_chunk *chunk = arg;
3237 struct sctp_chunk *reply;
3238 struct sctp_ulpevent *ev;
3239
3240 if (!sctp_vtag_verify(chunk, asoc))
3241 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3242
3243 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3244 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3245 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3246 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 /* 10.2 H) SHUTDOWN COMPLETE notification
3248 *
3249 * When SCTP completes the shutdown procedures (section 9.2) this
3250 * notification is passed to the upper layer.
3251 */
3252 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003253 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if (!ev)
3255 goto nomem;
3256
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003257 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3258 reply = sctp_make_shutdown_complete(asoc, chunk);
3259 if (!reply)
3260 goto nomem_chunk;
3261
3262 /* Do all the commands now (after allocation), so that we
3263 * have consistent state if memory allocation failes
3264 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3266
3267 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3268 * stop the T2-shutdown timer,
3269 */
3270 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3271 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3272
3273 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3274 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3277 SCTP_STATE(SCTP_STATE_CLOSED));
3278 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3279 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3280 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3281
3282 /* ...and remove all record of the association. */
3283 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3284 return SCTP_DISPOSITION_DELETE_TCB;
3285
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003286nomem_chunk:
3287 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288nomem:
3289 return SCTP_DISPOSITION_NOMEM;
3290}
3291
3292/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003293 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3294 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3296 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3297 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3298 * packet must fill in the Verification Tag field of the outbound
3299 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003300 * set the T-bit in the Chunk Flags to indicate that the Verification
3301 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 *
3303 * 8) The receiver should respond to the sender of the OOTB packet with
3304 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3305 * MUST fill in the Verification Tag field of the outbound packet
3306 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003307 * packet and set the T-bit in the Chunk Flags to indicate that the
3308 * Verification Tag is reflected. After sending this ABORT, the
3309 * receiver of the OOTB packet shall discard the OOTB packet and take
3310 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 */
3312sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3313 const struct sctp_association *asoc,
3314 const sctp_subtype_t type,
3315 void *arg,
3316 sctp_cmd_seq_t *commands)
3317{
3318 struct sctp_chunk *chunk = arg;
3319 struct sk_buff *skb = chunk->skb;
3320 sctp_chunkhdr_t *ch;
3321 __u8 *ch_end;
3322 int ootb_shut_ack = 0;
3323
3324 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3325
3326 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3327 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003328 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003330 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3331 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Vlad Yasevichece25df2007-09-07 16:30:54 -04003333 /* Now that we know we at least have a chunk header,
3334 * do things that are type appropriate.
3335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3337 ootb_shut_ack = 1;
3338
3339 /* RFC 2960, Section 3.3.7
3340 * Moreover, under any circumstances, an endpoint that
3341 * receives an ABORT MUST NOT respond to that ABORT by
3342 * sending an ABORT of its own.
3343 */
3344 if (SCTP_CID_ABORT == ch->type)
3345 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003346
Vlad Yasevichece25df2007-09-07 16:30:54 -04003347 /* Report violation if chunk len overflows */
3348 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3349 if (ch_end > skb_tail_pointer(skb))
3350 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3351 commands);
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003354 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003357 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003359 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
3362/*
3363 * Handle an "Out of the blue" SHUTDOWN ACK.
3364 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003365 * Section: 8.4 5, sctpimpguide 2.41.
3366 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003368 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3369 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3370 * packet must fill in the Verification Tag field of the outbound
3371 * packet with the Verification Tag received in the SHUTDOWN ACK and
3372 * set the T-bit in the Chunk Flags to indicate that the Verification
3373 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 *
3375 * Inputs
3376 * (endpoint, asoc, type, arg, commands)
3377 *
3378 * Outputs
3379 * (sctp_disposition_t)
3380 *
3381 * The return value is the disposition of the chunk.
3382 */
3383static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3384 const struct sctp_association *asoc,
3385 const sctp_subtype_t type,
3386 void *arg,
3387 sctp_cmd_seq_t *commands)
3388{
3389 struct sctp_packet *packet = NULL;
3390 struct sctp_chunk *chunk = arg;
3391 struct sctp_chunk *shut;
3392
3393 packet = sctp_ootb_pkt_new(asoc, chunk);
3394
3395 if (packet) {
3396 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003397 * The T bit will be set if the asoc is NULL.
3398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 shut = sctp_make_shutdown_complete(asoc, chunk);
3400 if (!shut) {
3401 sctp_ootb_pkt_free(packet);
3402 return SCTP_DISPOSITION_NOMEM;
3403 }
3404
Jerome Forissier047a2422005-04-28 11:58:43 -07003405 /* Reflect vtag if T-Bit is set */
3406 if (sctp_test_T_bit(shut))
3407 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 /* Set the skb to the belonging sock for accounting. */
3410 shut->skb->sk = ep->base.sk;
3411
3412 sctp_packet_append_chunk(packet, shut);
3413
3414 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3415 SCTP_PACKET(packet));
3416
3417 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3418
3419 /* If the chunk length is invalid, we don't want to process
3420 * the reset of the packet.
3421 */
3422 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3423 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3424
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003425 /* We need to discard the rest of the packet to prevent
3426 * potential bomming attacks from additional bundled chunks.
3427 * This is documented in SCTP Threats ID.
3428 */
3429 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
3431
3432 return SCTP_DISPOSITION_NOMEM;
3433}
3434
3435/*
3436 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3437 *
3438 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3439 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3440 * procedures in section 8.4 SHOULD be followed, in other words it
3441 * should be treated as an Out Of The Blue packet.
3442 * [This means that we do NOT check the Verification Tag on these
3443 * chunks. --piggy ]
3444 *
3445 */
3446sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3447 const struct sctp_association *asoc,
3448 const sctp_subtype_t type,
3449 void *arg,
3450 sctp_cmd_seq_t *commands)
3451{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003452 struct sctp_chunk *chunk = arg;
3453
3454 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3455 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3456 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3457 commands);
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /* Although we do have an association in this case, it corresponds
3460 * to a restarted association. So the packet is treated as an OOTB
3461 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3462 * called with a NULL association.
3463 */
Wei Yongjun8190f892008-09-08 12:13:55 +08003464 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3467}
3468
3469/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3470sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3471 const struct sctp_association *asoc,
3472 const sctp_subtype_t type, void *arg,
3473 sctp_cmd_seq_t *commands)
3474{
3475 struct sctp_chunk *chunk = arg;
3476 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003477 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003479 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003481 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
3483 if (!sctp_vtag_verify(chunk, asoc)) {
3484 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3485 SCTP_NULL());
3486 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3487 }
3488
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003489 /* ADD-IP: Section 4.1.1
3490 * This chunk MUST be sent in an authenticated way by using
3491 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3492 * is received unauthenticated it MUST be silently discarded as
3493 * described in [I-D.ietf-tsvwg-sctp-auth].
3494 */
3495 if (!sctp_addip_noauth && !chunk->auth)
3496 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3499 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3500 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3501 commands);
3502
3503 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3504 serial = ntohl(hdr->serial);
3505
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003506 addr_param = (union sctp_addr_param *)hdr->params;
3507 length = ntohs(addr_param->p.length);
3508 if (length < sizeof(sctp_paramhdr_t))
Wei Yongjunba016672008-09-30 05:32:24 -07003509 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003510 (void *)addr_param, commands);
3511
3512 /* Verify the ASCONF chunk before processing it. */
3513 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003514 (sctp_paramhdr_t *)((void *)addr_param + length),
3515 (void *)chunk->chunk_end,
3516 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003517 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3518 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003519
Vlad Yasevicha08de642007-12-20 14:11:47 -08003520 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003522 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 */
3524 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003525 /* If this is the first instance of ASCONF in the packet,
3526 * we can clean our old ASCONF-ACKs.
3527 */
3528 if (!chunk->has_asconf)
3529 sctp_assoc_clean_asconf_ack_cache(asoc);
3530
3531 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3532 * expected, process the ASCONF as described below and after
3533 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3534 * the response packet and cache a copy of it (in the event it
3535 * later needs to be retransmitted).
3536 *
3537 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 */
3539 asconf_ack = sctp_process_asconf((struct sctp_association *)
3540 asoc, chunk);
3541 if (!asconf_ack)
3542 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003543 } else if (serial < asoc->peer.addip_serial + 1) {
3544 /* ADDIP 5.2 E2)
3545 * If the value found in the Sequence Number is less than the
3546 * ('Peer- Sequence-Number' + 1), simply skip to the next
3547 * ASCONF, and include in the outbound response packet
3548 * any previously cached ASCONF-ACK response that was
3549 * sent and saved that matches the Sequence Number of the
3550 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3551 * Chunk exists. This will occur when an older ASCONF
3552 * arrives out of order. In such a case, the receiver
3553 * should skip the ASCONF Chunk and not include ASCONF-ACK
3554 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003556 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3557 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return SCTP_DISPOSITION_DISCARD;
Vlad Yasevich31b02e12009-09-04 18:21:00 -04003559
3560 /* Reset the transport so that we select the correct one
3561 * this time around. This is to make sure that we don't
3562 * accidentally use a stale transport that's been removed.
3563 */
3564 asconf_ack->transport = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003566 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return SCTP_DISPOSITION_DISCARD;
3570 }
3571
Vlad Yasevicha08de642007-12-20 14:11:47 -08003572 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3573 * containing the ASCONF-ACK Chunks MUST be the source address of
3574 * the SCTP packet that held the ASCONF Chunks.
3575 *
3576 * To do this properly, we'll set the destination address of the chunk
3577 * and at the transmit time, will try look up the transport to use.
3578 * Since ASCONFs may be bundled, the correct transport may not be
3579 * created untill we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003581 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return SCTP_DISPOSITION_CONSUME;
3585}
3586
3587/*
3588 * ADDIP Section 4.3 General rules for address manipulation
3589 * When building TLV parameters for the ASCONF Chunk that will add or
3590 * delete IP addresses the D0 to D13 rules should be applied:
3591 */
3592sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3593 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003594 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 sctp_cmd_seq_t *commands)
3596{
3597 struct sctp_chunk *asconf_ack = arg;
3598 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3599 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003600 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 sctp_addiphdr_t *addip_hdr;
3602 __u32 sent_serial, rcvd_serial;
3603
3604 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3605 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3606 SCTP_NULL());
3607 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3608 }
3609
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003610 /* ADD-IP, Section 4.1.2:
3611 * This chunk MUST be sent in an authenticated way by using
3612 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3613 * is received unauthenticated it MUST be silently discarded as
3614 * described in [I-D.ietf-tsvwg-sctp-auth].
3615 */
3616 if (!sctp_addip_noauth && !asconf_ack->auth)
3617 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3618
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /* Make sure that the ADDIP chunk has a valid length. */
3620 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3621 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3622 commands);
3623
3624 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3625 rcvd_serial = ntohl(addip_hdr->serial);
3626
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003627 /* Verify the ASCONF-ACK chunk before processing it. */
3628 if (!sctp_verify_asconf(asoc,
3629 (sctp_paramhdr_t *)addip_hdr->params,
3630 (void *)asconf_ack->chunk_end,
3631 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003632 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3633 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 if (last_asconf) {
3636 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3637 sent_serial = ntohl(addip_hdr->serial);
3638 } else {
3639 sent_serial = asoc->addip_serial - 1;
3640 }
3641
3642 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3643 * equal to the next serial number to be used but no ASCONF chunk is
3644 * outstanding the endpoint MUST ABORT the association. Note that a
3645 * sequence number is greater than if it is no more than 2^^31-1
3646 * larger than the current sequence number (using serial arithmetic).
3647 */
3648 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3649 !(asoc->addip_last_asconf)) {
3650 abort = sctp_make_abort(asoc, asconf_ack,
3651 sizeof(sctp_errhdr_t));
3652 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003653 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3655 SCTP_CHUNK(abort));
3656 }
3657 /* We are going to ABORT, so we might as well stop
3658 * processing the rest of the chunks in the packet.
3659 */
3660 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3661 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3662 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003663 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003664 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003666 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3668 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3669 return SCTP_DISPOSITION_ABORT;
3670 }
3671
3672 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3673 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3674 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3675
3676 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3677 asconf_ack))
3678 return SCTP_DISPOSITION_CONSUME;
3679
3680 abort = sctp_make_abort(asoc, asconf_ack,
3681 sizeof(sctp_errhdr_t));
3682 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003683 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3685 SCTP_CHUNK(abort));
3686 }
3687 /* We are going to ABORT, so we might as well stop
3688 * processing the rest of the chunks in the packet.
3689 */
3690 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003691 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003692 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003694 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3696 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3697 return SCTP_DISPOSITION_ABORT;
3698 }
3699
3700 return SCTP_DISPOSITION_DISCARD;
3701}
3702
3703/*
3704 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3705 *
3706 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3707 * its cumulative TSN point to the value carried in the FORWARD TSN
3708 * chunk, and then MUST further advance its cumulative TSN point locally
3709 * if possible.
3710 * After the above processing, the data receiver MUST stop reporting any
3711 * missing TSNs earlier than or equal to the new cumulative TSN point.
3712 *
3713 * Verification Tag: 8.5 Verification Tag [Normal verification]
3714 *
3715 * The return value is the disposition of the chunk.
3716 */
3717sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3718 const struct sctp_association *asoc,
3719 const sctp_subtype_t type,
3720 void *arg,
3721 sctp_cmd_seq_t *commands)
3722{
3723 struct sctp_chunk *chunk = arg;
3724 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003725 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 __u16 len;
3727 __u32 tsn;
3728
3729 if (!sctp_vtag_verify(chunk, asoc)) {
3730 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3731 SCTP_NULL());
3732 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3733 }
3734
3735 /* Make sure that the FORWARD_TSN chunk has valid length. */
3736 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3737 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3738 commands);
3739
3740 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3741 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3742 len = ntohs(chunk->chunk_hdr->length);
3743 len -= sizeof(struct sctp_chunkhdr);
3744 skb_pull(chunk->skb, len);
3745
3746 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003747 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748
3749 /* The TSN is too high--silently discard the chunk and count on it
3750 * getting retransmitted later.
3751 */
3752 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3753 goto discard_noforce;
3754
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003755 /* Silently discard the chunk if stream-id is not valid */
3756 sctp_walk_fwdtsn(skip, chunk) {
3757 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3758 goto discard_noforce;
3759 }
3760
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3762 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003763 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003765
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 /* Count this as receiving DATA. */
3767 if (asoc->autoclose) {
3768 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3769 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3770 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003771
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003773 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 */
3775 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776
3777 return SCTP_DISPOSITION_CONSUME;
3778
3779discard_noforce:
3780 return SCTP_DISPOSITION_DISCARD;
3781}
3782
3783sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3784 const struct sctp_endpoint *ep,
3785 const struct sctp_association *asoc,
3786 const sctp_subtype_t type,
3787 void *arg,
3788 sctp_cmd_seq_t *commands)
3789{
3790 struct sctp_chunk *chunk = arg;
3791 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003792 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 __u16 len;
3794 __u32 tsn;
3795
3796 if (!sctp_vtag_verify(chunk, asoc)) {
3797 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3798 SCTP_NULL());
3799 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3800 }
3801
3802 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3803 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3804 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3805 commands);
3806
3807 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3808 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3809 len = ntohs(chunk->chunk_hdr->length);
3810 len -= sizeof(struct sctp_chunkhdr);
3811 skb_pull(chunk->skb, len);
3812
3813 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003814 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816 /* The TSN is too high--silently discard the chunk and count on it
3817 * getting retransmitted later.
3818 */
3819 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3820 goto gen_shutdown;
3821
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003822 /* Silently discard the chunk if stream-id is not valid */
3823 sctp_walk_fwdtsn(skip, chunk) {
3824 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3825 goto gen_shutdown;
3826 }
3827
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3829 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003830 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 /* Go a head and force a SACK, since we are shutting down. */
3834gen_shutdown:
3835 /* Implementor's Guide.
3836 *
3837 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3838 * respond to each received packet containing one or more DATA chunk(s)
3839 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3840 */
3841 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3842 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3843 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3844 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3845
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003846 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
3849/*
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003850 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3851 *
3852 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3853 * Identifier field. If this algorithm was not specified by the
3854 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3855 * during association setup, the AUTH chunk and all chunks after it MUST
3856 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3857 * defined in Section 4.1.
3858 *
3859 * If an endpoint with no shared key receives a Shared Key Identifier
3860 * other than 0, it MUST silently discard all authenticated chunks. If
3861 * the endpoint has at least one endpoint pair shared key for the peer,
3862 * it MUST use the key specified by the Shared Key Identifier if a
3863 * key has been configured for that Shared Key Identifier. If no
3864 * endpoint pair shared key has been configured for that Shared Key
3865 * Identifier, all authenticated chunks MUST be silently discarded.
3866 *
3867 * Verification Tag: 8.5 Verification Tag [Normal verification]
3868 *
3869 * The return value is the disposition of the chunk.
3870 */
3871static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3872 const struct sctp_association *asoc,
3873 const sctp_subtype_t type,
3874 struct sctp_chunk *chunk)
3875{
3876 struct sctp_authhdr *auth_hdr;
3877 struct sctp_hmac *hmac;
3878 unsigned int sig_len;
3879 __u16 key_id;
3880 __u8 *save_digest;
3881 __u8 *digest;
3882
3883 /* Pull in the auth header, so we can do some more verification */
3884 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3885 chunk->subh.auth_hdr = auth_hdr;
3886 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3887
3888 /* Make sure that we suport the HMAC algorithm from the auth
3889 * chunk.
3890 */
3891 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3892 return SCTP_IERROR_AUTH_BAD_HMAC;
3893
3894 /* Make sure that the provided shared key identifier has been
3895 * configured
3896 */
3897 key_id = ntohs(auth_hdr->shkey_id);
3898 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3899 return SCTP_IERROR_AUTH_BAD_KEYID;
3900
3901
3902 /* Make sure that the length of the signature matches what
3903 * we expect.
3904 */
3905 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3906 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3907 if (sig_len != hmac->hmac_len)
3908 return SCTP_IERROR_PROTO_VIOLATION;
3909
3910 /* Now that we've done validation checks, we can compute and
3911 * verify the hmac. The steps involved are:
3912 * 1. Save the digest from the chunk.
3913 * 2. Zero out the digest in the chunk.
3914 * 3. Compute the new digest
3915 * 4. Compare saved and new digests.
3916 */
3917 digest = auth_hdr->hmac;
3918 skb_pull(chunk->skb, sig_len);
3919
3920 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3921 if (!save_digest)
3922 goto nomem;
3923
3924 memset(digest, 0, sig_len);
3925
3926 sctp_auth_calculate_hmac(asoc, chunk->skb,
3927 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3928 GFP_ATOMIC);
3929
3930 /* Discard the packet if the digests do not match */
3931 if (memcmp(save_digest, digest, sig_len)) {
3932 kfree(save_digest);
3933 return SCTP_IERROR_BAD_SIG;
3934 }
3935
3936 kfree(save_digest);
3937 chunk->auth = 1;
3938
3939 return SCTP_IERROR_NO_ERROR;
3940nomem:
3941 return SCTP_IERROR_NOMEM;
3942}
3943
3944sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3945 const struct sctp_association *asoc,
3946 const sctp_subtype_t type,
3947 void *arg,
3948 sctp_cmd_seq_t *commands)
3949{
3950 struct sctp_authhdr *auth_hdr;
3951 struct sctp_chunk *chunk = arg;
3952 struct sctp_chunk *err_chunk;
3953 sctp_ierror_t error;
3954
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003955 /* Make sure that the peer has AUTH capable */
3956 if (!asoc->peer.auth_capable)
3957 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3958
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003959 if (!sctp_vtag_verify(chunk, asoc)) {
3960 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3961 SCTP_NULL());
3962 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3963 }
3964
3965 /* Make sure that the AUTH chunk has valid length. */
3966 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3967 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3968 commands);
3969
3970 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3971 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3972 switch (error) {
3973 case SCTP_IERROR_AUTH_BAD_HMAC:
3974 /* Generate the ERROR chunk and discard the rest
3975 * of the packet
3976 */
3977 err_chunk = sctp_make_op_error(asoc, chunk,
3978 SCTP_ERROR_UNSUP_HMAC,
3979 &auth_hdr->hmac_id,
3980 sizeof(__u16));
3981 if (err_chunk) {
3982 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3983 SCTP_CHUNK(err_chunk));
3984 }
3985 /* Fall Through */
3986 case SCTP_IERROR_AUTH_BAD_KEYID:
3987 case SCTP_IERROR_BAD_SIG:
3988 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3989 break;
3990 case SCTP_IERROR_PROTO_VIOLATION:
3991 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3992 commands);
3993 break;
3994 case SCTP_IERROR_NOMEM:
3995 return SCTP_DISPOSITION_NOMEM;
3996 default:
3997 break;
3998 }
3999
Vlad Yasevich65b07e52007-09-16 19:34:00 -07004000 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4001 struct sctp_ulpevent *ev;
4002
4003 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4004 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4005
4006 if (!ev)
4007 return -ENOMEM;
4008
4009 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4010 SCTP_ULPEVENT(ev));
4011 }
4012
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004013 return SCTP_DISPOSITION_CONSUME;
4014}
4015
4016/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 * Process an unknown chunk.
4018 *
4019 * Section: 3.2. Also, 2.1 in the implementor's guide.
4020 *
4021 * Chunk Types are encoded such that the highest-order two bits specify
4022 * the action that must be taken if the processing endpoint does not
4023 * recognize the Chunk Type.
4024 *
4025 * 00 - Stop processing this SCTP packet and discard it, do not process
4026 * any further chunks within it.
4027 *
4028 * 01 - Stop processing this SCTP packet and discard it, do not process
4029 * any further chunks within it, and report the unrecognized
4030 * chunk in an 'Unrecognized Chunk Type'.
4031 *
4032 * 10 - Skip this chunk and continue processing.
4033 *
4034 * 11 - Skip this chunk and continue processing, but report in an ERROR
4035 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4036 *
4037 * The return value is the disposition of the chunk.
4038 */
4039sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4040 const struct sctp_association *asoc,
4041 const sctp_subtype_t type,
4042 void *arg,
4043 sctp_cmd_seq_t *commands)
4044{
4045 struct sctp_chunk *unk_chunk = arg;
4046 struct sctp_chunk *err_chunk;
4047 sctp_chunkhdr_t *hdr;
4048
4049 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4050
4051 if (!sctp_vtag_verify(unk_chunk, asoc))
4052 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4053
4054 /* Make sure that the chunk has a valid length.
4055 * Since we don't know the chunk type, we use a general
4056 * chunkhdr structure to make a comparison.
4057 */
4058 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4059 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4060 commands);
4061
4062 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4063 case SCTP_CID_ACTION_DISCARD:
4064 /* Discard the packet. */
4065 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4066 break;
4067 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 /* Generate an ERROR chunk as response. */
4069 hdr = unk_chunk->chunk_hdr;
4070 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4071 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4072 WORD_ROUND(ntohs(hdr->length)));
4073 if (err_chunk) {
4074 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4075 SCTP_CHUNK(err_chunk));
4076 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004077
4078 /* Discard the packet. */
4079 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return SCTP_DISPOSITION_CONSUME;
4081 break;
4082 case SCTP_CID_ACTION_SKIP:
4083 /* Skip the chunk. */
4084 return SCTP_DISPOSITION_DISCARD;
4085 break;
4086 case SCTP_CID_ACTION_SKIP_ERR:
4087 /* Generate an ERROR chunk as response. */
4088 hdr = unk_chunk->chunk_hdr;
4089 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4090 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4091 WORD_ROUND(ntohs(hdr->length)));
4092 if (err_chunk) {
4093 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4094 SCTP_CHUNK(err_chunk));
4095 }
4096 /* Skip the chunk. */
4097 return SCTP_DISPOSITION_CONSUME;
4098 break;
4099 default:
4100 break;
4101 }
4102
4103 return SCTP_DISPOSITION_DISCARD;
4104}
4105
4106/*
4107 * Discard the chunk.
4108 *
4109 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4110 * [Too numerous to mention...]
4111 * Verification Tag: No verification needed.
4112 * Inputs
4113 * (endpoint, asoc, chunk)
4114 *
4115 * Outputs
4116 * (asoc, reply_msg, msg_up, timers, counters)
4117 *
4118 * The return value is the disposition of the chunk.
4119 */
4120sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4121 const struct sctp_association *asoc,
4122 const sctp_subtype_t type,
4123 void *arg,
4124 sctp_cmd_seq_t *commands)
4125{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004126 struct sctp_chunk *chunk = arg;
4127
4128 /* Make sure that the chunk has a valid length.
4129 * Since we don't know the chunk type, we use a general
4130 * chunkhdr structure to make a comparison.
4131 */
4132 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4133 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4134 commands);
4135
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4137 return SCTP_DISPOSITION_DISCARD;
4138}
4139
4140/*
4141 * Discard the whole packet.
4142 *
4143 * Section: 8.4 2)
4144 *
4145 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4146 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 *
4148 * Verification Tag: No verification necessary
4149 *
4150 * Inputs
4151 * (endpoint, asoc, chunk)
4152 *
4153 * Outputs
4154 * (asoc, reply_msg, msg_up, timers, counters)
4155 *
4156 * The return value is the disposition of the chunk.
4157 */
4158sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4159 const struct sctp_association *asoc,
4160 const sctp_subtype_t type,
4161 void *arg,
4162 sctp_cmd_seq_t *commands)
4163{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004164 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4166
4167 return SCTP_DISPOSITION_CONSUME;
4168}
4169
4170
4171/*
4172 * The other end is violating protocol.
4173 *
4174 * Section: Not specified
4175 * Verification Tag: Not specified
4176 * Inputs
4177 * (endpoint, asoc, chunk)
4178 *
4179 * Outputs
4180 * (asoc, reply_msg, msg_up, timers, counters)
4181 *
4182 * We simply tag the chunk as a violation. The state machine will log
4183 * the violation and continue.
4184 */
4185sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4186 const struct sctp_association *asoc,
4187 const sctp_subtype_t type,
4188 void *arg,
4189 sctp_cmd_seq_t *commands)
4190{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004191 struct sctp_chunk *chunk = arg;
4192
4193 /* Make sure that the chunk has a valid length. */
4194 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4195 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4196 commands);
4197
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 return SCTP_DISPOSITION_VIOLATION;
4199}
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004202 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004204static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004205 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004208 sctp_cmd_seq_t *commands,
4209 const __u8 *payload,
4210 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004212 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 struct sctp_chunk *chunk = arg;
4214 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004216 /* SCTP-AUTH, Section 6.3:
4217 * It should be noted that if the receiver wants to tear
4218 * down an association in an authenticated way only, the
4219 * handling of malformed packets should not result in
4220 * tearing down the association.
4221 *
4222 * This means that if we only want to abort associations
4223 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +02004224 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004225 * was malformed.
4226 */
4227 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4228 goto discard;
4229
Jesper Juhl9abed242007-11-11 23:57:49 +01004230 /* Make the abort chunk. */
4231 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4232 if (!abort)
4233 goto nomem;
4234
Vlad Yasevichece25df2007-09-07 16:30:54 -04004235 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004236 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4237 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4238 !asoc->peer.i.init_tag) {
4239 sctp_initack_chunk_t *initack;
4240
4241 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4242 if (!sctp_chunk_length_valid(chunk,
4243 sizeof(sctp_initack_chunk_t)))
4244 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4245 else {
4246 unsigned int inittag;
4247
4248 inittag = ntohl(initack->init_hdr.init_tag);
4249 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4250 SCTP_U32(inittag));
4251 }
4252 }
4253
Vlad Yasevichece25df2007-09-07 16:30:54 -04004254 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4255 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
Vlad Yasevichece25df2007-09-07 16:30:54 -04004257 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4258 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4259 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4260 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4261 SCTP_ERROR(ECONNREFUSED));
4262 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4263 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4264 } else {
4265 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4266 SCTP_ERROR(ECONNABORTED));
4267 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4268 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4269 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004272 packet = sctp_ootb_pkt_new(asoc, chunk);
4273
4274 if (!packet)
4275 goto nomem_pkt;
4276
4277 if (sctp_test_T_bit(abort))
4278 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4279
4280 abort->skb->sk = ep->base.sk;
4281
4282 sctp_packet_append_chunk(packet, abort);
4283
4284 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4285 SCTP_PACKET(packet));
4286
4287 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 }
4289
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004291
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004292discard:
4293 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return SCTP_DISPOSITION_ABORT;
4295
Vlad Yasevichece25df2007-09-07 16:30:54 -04004296nomem_pkt:
4297 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298nomem:
4299 return SCTP_DISPOSITION_NOMEM;
4300}
4301
Wei Yongjunaecedea2007-08-02 16:57:44 +08004302/*
4303 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004304 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004305 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004306 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004307 *
4308 * We inform the other end by sending an ABORT with a Protocol Violation
4309 * error code.
4310 *
4311 * Section: Not specified
4312 * Verification Tag: Nothing to do
4313 * Inputs
4314 * (endpoint, asoc, chunk)
4315 *
4316 * Outputs
4317 * (reply_msg, msg_up, counters)
4318 *
4319 * Generate an ABORT chunk and terminate the association.
4320 */
4321static sctp_disposition_t sctp_sf_violation_chunklen(
4322 const struct sctp_endpoint *ep,
4323 const struct sctp_association *asoc,
4324 const sctp_subtype_t type,
4325 void *arg,
4326 sctp_cmd_seq_t *commands)
4327{
Florian Westphal24448442008-03-23 22:46:34 -07004328 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004329
Vlad Yasevichece25df2007-09-07 16:30:54 -04004330 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004331 sizeof(err_str));
4332}
4333
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004334/*
4335 * Handle a protocol violation when the parameter length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004336 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004337 * given parameter can be.
4338 */
4339static sctp_disposition_t sctp_sf_violation_paramlen(
4340 const struct sctp_endpoint *ep,
4341 const struct sctp_association *asoc,
4342 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004343 void *arg, void *ext,
4344 sctp_cmd_seq_t *commands)
4345{
4346 struct sctp_chunk *chunk = arg;
4347 struct sctp_paramhdr *param = ext;
4348 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004349
Wei Yongjunba016672008-09-30 05:32:24 -07004350 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4351 goto discard;
4352
4353 /* Make the abort chunk. */
4354 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4355 if (!abort)
4356 goto nomem;
4357
4358 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4359 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4360
4361 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4362 SCTP_ERROR(ECONNABORTED));
4363 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4364 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4365 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004366 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004367
4368discard:
4369 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004370 return SCTP_DISPOSITION_ABORT;
4371nomem:
4372 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004373}
4374
Wei Yongjunaecedea2007-08-02 16:57:44 +08004375/* Handle a protocol violation when the peer trying to advance the
4376 * cumulative tsn ack to a point beyond the max tsn currently sent.
4377 *
4378 * We inform the other end by sending an ABORT with a Protocol Violation
4379 * error code.
4380 */
4381static sctp_disposition_t sctp_sf_violation_ctsn(
4382 const struct sctp_endpoint *ep,
4383 const struct sctp_association *asoc,
4384 const sctp_subtype_t type,
4385 void *arg,
4386 sctp_cmd_seq_t *commands)
4387{
Florian Westphal24448442008-03-23 22:46:34 -07004388 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004389
Vlad Yasevichece25df2007-09-07 16:30:54 -04004390 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004391 sizeof(err_str));
4392}
4393
Vlad Yasevichece25df2007-09-07 16:30:54 -04004394/* Handle protocol violation of an invalid chunk bundling. For example,
4395 * when we have an association and we recieve bundled INIT-ACK, or
4396 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4397 * statement from the specs. Additinally, there might be an attacker
4398 * on the path and we may not want to continue this communication.
4399 */
4400static sctp_disposition_t sctp_sf_violation_chunk(
4401 const struct sctp_endpoint *ep,
4402 const struct sctp_association *asoc,
4403 const sctp_subtype_t type,
4404 void *arg,
4405 sctp_cmd_seq_t *commands)
4406{
Florian Westphal24448442008-03-23 22:46:34 -07004407 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004408
4409 if (!asoc)
4410 return sctp_sf_violation(ep, asoc, type, arg, commands);
4411
4412 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4413 sizeof(err_str));
4414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415/***************************************************************************
4416 * These are the state functions for handling primitive (Section 10) events.
4417 ***************************************************************************/
4418/*
4419 * sctp_sf_do_prm_asoc
4420 *
4421 * Section: 10.1 ULP-to-SCTP
4422 * B) Associate
4423 *
4424 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4425 * outbound stream count)
4426 * -> association id [,destination transport addr list] [,outbound stream
4427 * count]
4428 *
4429 * This primitive allows the upper layer to initiate an association to a
4430 * specific peer endpoint.
4431 *
4432 * The peer endpoint shall be specified by one of the transport addresses
4433 * which defines the endpoint (see Section 1.4). If the local SCTP
4434 * instance has not been initialized, the ASSOCIATE is considered an
4435 * error.
4436 * [This is not relevant for the kernel implementation since we do all
4437 * initialization at boot time. It we hadn't initialized we wouldn't
4438 * get anywhere near this code.]
4439 *
4440 * An association id, which is a local handle to the SCTP association,
4441 * will be returned on successful establishment of the association. If
4442 * SCTP is not able to open an SCTP association with the peer endpoint,
4443 * an error is returned.
4444 * [In the kernel implementation, the struct sctp_association needs to
4445 * be created BEFORE causing this primitive to run.]
4446 *
4447 * Other association parameters may be returned, including the
4448 * complete destination transport addresses of the peer as well as the
4449 * outbound stream count of the local endpoint. One of the transport
4450 * address from the returned destination addresses will be selected by
4451 * the local endpoint as default primary path for sending SCTP packets
4452 * to this peer. The returned "destination transport addr list" can
4453 * be used by the ULP to change the default primary path or to force
4454 * sending a packet to a specific transport address. [All of this
4455 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4456 * function.]
4457 *
4458 * Mandatory attributes:
4459 *
4460 * o local SCTP instance name - obtained from the INITIALIZE operation.
4461 * [This is the argument asoc.]
4462 * o destination transport addr - specified as one of the transport
4463 * addresses of the peer endpoint with which the association is to be
4464 * established.
4465 * [This is asoc->peer.active_path.]
4466 * o outbound stream count - the number of outbound streams the ULP
4467 * would like to open towards this peer endpoint.
4468 * [BUG: This is not currently implemented.]
4469 * Optional attributes:
4470 *
4471 * None.
4472 *
4473 * The return value is a disposition.
4474 */
4475sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4476 const struct sctp_association *asoc,
4477 const sctp_subtype_t type,
4478 void *arg,
4479 sctp_cmd_seq_t *commands)
4480{
4481 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004482 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
4484 /* The comment below says that we enter COOKIE-WAIT AFTER
4485 * sending the INIT, but that doesn't actually work in our
4486 * implementation...
4487 */
4488 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4489 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4490
4491 /* RFC 2960 5.1 Normal Establishment of an Association
4492 *
4493 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4494 * must provide its Verification Tag (Tag_A) in the Initiate
4495 * Tag field. Tag_A SHOULD be a random number in the range of
4496 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4497 */
4498
4499 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4500 if (!repl)
4501 goto nomem;
4502
4503 /* Cast away the const modifier, as we want to just
4504 * rerun it through as a sideffect.
4505 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004506 my_asoc = (struct sctp_association *)asoc;
4507 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Frank Filz3f7a87d2005-06-20 13:14:57 -07004509 /* Choose transport for INIT. */
4510 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4511 SCTP_CHUNK(repl));
4512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 /* After sending the INIT, "A" starts the T1-init timer and
4514 * enters the COOKIE-WAIT state.
4515 */
4516 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4517 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4518 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4519 return SCTP_DISPOSITION_CONSUME;
4520
4521nomem:
4522 return SCTP_DISPOSITION_NOMEM;
4523}
4524
4525/*
4526 * Process the SEND primitive.
4527 *
4528 * Section: 10.1 ULP-to-SCTP
4529 * E) Send
4530 *
4531 * Format: SEND(association id, buffer address, byte count [,context]
4532 * [,stream id] [,life time] [,destination transport address]
4533 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4534 * -> result
4535 *
4536 * This is the main method to send user data via SCTP.
4537 *
4538 * Mandatory attributes:
4539 *
4540 * o association id - local handle to the SCTP association
4541 *
4542 * o buffer address - the location where the user message to be
4543 * transmitted is stored;
4544 *
4545 * o byte count - The size of the user data in number of bytes;
4546 *
4547 * Optional attributes:
4548 *
4549 * o context - an optional 32 bit integer that will be carried in the
4550 * sending failure notification to the ULP if the transportation of
4551 * this User Message fails.
4552 *
4553 * o stream id - to indicate which stream to send the data on. If not
4554 * specified, stream 0 will be used.
4555 *
4556 * o life time - specifies the life time of the user data. The user data
4557 * will not be sent by SCTP after the life time expires. This
4558 * parameter can be used to avoid efforts to transmit stale
4559 * user messages. SCTP notifies the ULP if the data cannot be
4560 * initiated to transport (i.e. sent to the destination via SCTP's
4561 * send primitive) within the life time variable. However, the
4562 * user data will be transmitted if SCTP has attempted to transmit a
4563 * chunk before the life time expired.
4564 *
4565 * o destination transport address - specified as one of the destination
4566 * transport addresses of the peer endpoint to which this packet
4567 * should be sent. Whenever possible, SCTP should use this destination
4568 * transport address for sending the packets, instead of the current
4569 * primary path.
4570 *
4571 * o unorder flag - this flag, if present, indicates that the user
4572 * would like the data delivered in an unordered fashion to the peer
4573 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4574 * message).
4575 *
4576 * o no-bundle flag - instructs SCTP not to bundle this user data with
4577 * other outbound DATA chunks. SCTP MAY still bundle even when
4578 * this flag is present, when faced with network congestion.
4579 *
4580 * o payload protocol-id - A 32 bit unsigned integer that is to be
4581 * passed to the peer indicating the type of payload protocol data
4582 * being transmitted. This value is passed as opaque data by SCTP.
4583 *
4584 * The return value is the disposition.
4585 */
4586sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4587 const struct sctp_association *asoc,
4588 const sctp_subtype_t type,
4589 void *arg,
4590 sctp_cmd_seq_t *commands)
4591{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004592 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004594 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 return SCTP_DISPOSITION_CONSUME;
4596}
4597
4598/*
4599 * Process the SHUTDOWN primitive.
4600 *
4601 * Section: 10.1:
4602 * C) Shutdown
4603 *
4604 * Format: SHUTDOWN(association id)
4605 * -> result
4606 *
4607 * Gracefully closes an association. Any locally queued user data
4608 * will be delivered to the peer. The association will be terminated only
4609 * after the peer acknowledges all the SCTP packets sent. A success code
4610 * will be returned on successful termination of the association. If
4611 * attempting to terminate the association results in a failure, an error
4612 * code shall be returned.
4613 *
4614 * Mandatory attributes:
4615 *
4616 * o association id - local handle to the SCTP association
4617 *
4618 * Optional attributes:
4619 *
4620 * None.
4621 *
4622 * The return value is the disposition.
4623 */
4624sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4625 const struct sctp_endpoint *ep,
4626 const struct sctp_association *asoc,
4627 const sctp_subtype_t type,
4628 void *arg,
4629 sctp_cmd_seq_t *commands)
4630{
4631 int disposition;
4632
4633 /* From 9.2 Shutdown of an Association
4634 * Upon receipt of the SHUTDOWN primitive from its upper
4635 * layer, the endpoint enters SHUTDOWN-PENDING state and
4636 * remains there until all outstanding data has been
4637 * acknowledged by its peer. The endpoint accepts no new data
4638 * from its upper layer, but retransmits data to the far end
4639 * if necessary to fill gaps.
4640 */
4641 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4642 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 disposition = SCTP_DISPOSITION_CONSUME;
4645 if (sctp_outq_is_empty(&asoc->outqueue)) {
4646 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4647 arg, commands);
4648 }
4649 return disposition;
4650}
4651
4652/*
4653 * Process the ABORT primitive.
4654 *
4655 * Section: 10.1:
4656 * C) Abort
4657 *
4658 * Format: Abort(association id [, cause code])
4659 * -> result
4660 *
4661 * Ungracefully closes an association. Any locally queued user data
4662 * will be discarded and an ABORT chunk is sent to the peer. A success code
4663 * will be returned on successful abortion of the association. If
4664 * attempting to abort the association results in a failure, an error
4665 * code shall be returned.
4666 *
4667 * Mandatory attributes:
4668 *
4669 * o association id - local handle to the SCTP association
4670 *
4671 * Optional attributes:
4672 *
4673 * o cause code - reason of the abort to be passed to the peer
4674 *
4675 * None.
4676 *
4677 * The return value is the disposition.
4678 */
4679sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4680 const struct sctp_endpoint *ep,
4681 const struct sctp_association *asoc,
4682 const sctp_subtype_t type,
4683 void *arg,
4684 sctp_cmd_seq_t *commands)
4685{
4686 /* From 9.1 Abort of an Association
4687 * Upon receipt of the ABORT primitive from its upper
4688 * layer, the endpoint enters CLOSED state and
4689 * discard all outstanding data has been
4690 * acknowledged by its peer. The endpoint accepts no new data
4691 * from its upper layer, but retransmits data to the far end
4692 * if necessary to fill gaps.
4693 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004694 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 sctp_disposition_t retval;
4696
4697 retval = SCTP_DISPOSITION_CONSUME;
4698
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004699 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 /* Even if we can't send the ABORT due to low memory delete the
4702 * TCB. This is a departure from our typical NOMEM handling.
4703 */
4704
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004705 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4706 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 /* Delete the established association. */
4708 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004709 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710
4711 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4712 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4713
4714 return retval;
4715}
4716
4717/* We tried an illegal operation on an association which is closed. */
4718sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4719 const struct sctp_association *asoc,
4720 const sctp_subtype_t type,
4721 void *arg,
4722 sctp_cmd_seq_t *commands)
4723{
4724 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4725 return SCTP_DISPOSITION_CONSUME;
4726}
4727
4728/* We tried an illegal operation on an association which is shutting
4729 * down.
4730 */
4731sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4732 const struct sctp_association *asoc,
4733 const sctp_subtype_t type,
4734 void *arg,
4735 sctp_cmd_seq_t *commands)
4736{
4737 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4738 SCTP_ERROR(-ESHUTDOWN));
4739 return SCTP_DISPOSITION_CONSUME;
4740}
4741
4742/*
4743 * sctp_cookie_wait_prm_shutdown
4744 *
4745 * Section: 4 Note: 2
4746 * Verification Tag:
4747 * Inputs
4748 * (endpoint, asoc)
4749 *
4750 * The RFC does not explicitly address this issue, but is the route through the
4751 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4752 *
4753 * Outputs
4754 * (timers)
4755 */
4756sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4757 const struct sctp_endpoint *ep,
4758 const struct sctp_association *asoc,
4759 const sctp_subtype_t type,
4760 void *arg,
4761 sctp_cmd_seq_t *commands)
4762{
4763 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4764 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4765
4766 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4767 SCTP_STATE(SCTP_STATE_CLOSED));
4768
4769 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4770
4771 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4772
4773 return SCTP_DISPOSITION_DELETE_TCB;
4774}
4775
4776/*
4777 * sctp_cookie_echoed_prm_shutdown
4778 *
4779 * Section: 4 Note: 2
4780 * Verification Tag:
4781 * Inputs
4782 * (endpoint, asoc)
4783 *
4784 * The RFC does not explcitly address this issue, but is the route through the
4785 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4786 *
4787 * Outputs
4788 * (timers)
4789 */
4790sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4791 const struct sctp_endpoint *ep,
4792 const struct sctp_association *asoc,
4793 const sctp_subtype_t type,
4794 void *arg, sctp_cmd_seq_t *commands)
4795{
4796 /* There is a single T1 timer, so we should be able to use
4797 * common function with the COOKIE-WAIT state.
4798 */
4799 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4800}
4801
4802/*
4803 * sctp_sf_cookie_wait_prm_abort
4804 *
4805 * Section: 4 Note: 2
4806 * Verification Tag:
4807 * Inputs
4808 * (endpoint, asoc)
4809 *
4810 * The RFC does not explicitly address this issue, but is the route through the
4811 * state table when someone issues an abort while in COOKIE_WAIT state.
4812 *
4813 * Outputs
4814 * (timers)
4815 */
4816sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4817 const struct sctp_endpoint *ep,
4818 const struct sctp_association *asoc,
4819 const sctp_subtype_t type,
4820 void *arg,
4821 sctp_cmd_seq_t *commands)
4822{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004823 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 sctp_disposition_t retval;
4825
4826 /* Stop T1-init timer */
4827 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4828 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4829 retval = SCTP_DISPOSITION_CONSUME;
4830
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004831 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
4833 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4834 SCTP_STATE(SCTP_STATE_CLOSED));
4835
4836 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4837
4838 /* Even if we can't send the ABORT due to low memory delete the
4839 * TCB. This is a departure from our typical NOMEM handling.
4840 */
4841
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004842 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4843 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 /* Delete the established association. */
4845 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004846 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847
4848 return retval;
4849}
4850
4851/*
4852 * sctp_sf_cookie_echoed_prm_abort
4853 *
4854 * Section: 4 Note: 3
4855 * Verification Tag:
4856 * Inputs
4857 * (endpoint, asoc)
4858 *
4859 * The RFC does not explcitly address this issue, but is the route through the
4860 * state table when someone issues an abort while in COOKIE_ECHOED state.
4861 *
4862 * Outputs
4863 * (timers)
4864 */
4865sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4866 const struct sctp_endpoint *ep,
4867 const struct sctp_association *asoc,
4868 const sctp_subtype_t type,
4869 void *arg,
4870 sctp_cmd_seq_t *commands)
4871{
4872 /* There is a single T1 timer, so we should be able to use
4873 * common function with the COOKIE-WAIT state.
4874 */
4875 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4876}
4877
4878/*
4879 * sctp_sf_shutdown_pending_prm_abort
4880 *
4881 * Inputs
4882 * (endpoint, asoc)
4883 *
4884 * The RFC does not explicitly address this issue, but is the route through the
4885 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4886 *
4887 * Outputs
4888 * (timers)
4889 */
4890sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4891 const struct sctp_endpoint *ep,
4892 const struct sctp_association *asoc,
4893 const sctp_subtype_t type,
4894 void *arg,
4895 sctp_cmd_seq_t *commands)
4896{
4897 /* Stop the T5-shutdown guard timer. */
4898 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4899 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4900
4901 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4902}
4903
4904/*
4905 * sctp_sf_shutdown_sent_prm_abort
4906 *
4907 * Inputs
4908 * (endpoint, asoc)
4909 *
4910 * The RFC does not explicitly address this issue, but is the route through the
4911 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4912 *
4913 * Outputs
4914 * (timers)
4915 */
4916sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4917 const struct sctp_endpoint *ep,
4918 const struct sctp_association *asoc,
4919 const sctp_subtype_t type,
4920 void *arg,
4921 sctp_cmd_seq_t *commands)
4922{
4923 /* Stop the T2-shutdown timer. */
4924 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4925 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4926
4927 /* Stop the T5-shutdown guard timer. */
4928 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4929 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4930
4931 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4932}
4933
4934/*
4935 * sctp_sf_cookie_echoed_prm_abort
4936 *
4937 * Inputs
4938 * (endpoint, asoc)
4939 *
4940 * The RFC does not explcitly address this issue, but is the route through the
4941 * state table when someone issues an abort while in COOKIE_ECHOED state.
4942 *
4943 * Outputs
4944 * (timers)
4945 */
4946sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4947 const struct sctp_endpoint *ep,
4948 const struct sctp_association *asoc,
4949 const sctp_subtype_t type,
4950 void *arg,
4951 sctp_cmd_seq_t *commands)
4952{
4953 /* The same T2 timer, so we should be able to use
4954 * common function with the SHUTDOWN-SENT state.
4955 */
4956 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4957}
4958
4959/*
4960 * Process the REQUESTHEARTBEAT primitive
4961 *
4962 * 10.1 ULP-to-SCTP
4963 * J) Request Heartbeat
4964 *
4965 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4966 *
4967 * -> result
4968 *
4969 * Instructs the local endpoint to perform a HeartBeat on the specified
4970 * destination transport address of the given association. The returned
4971 * result should indicate whether the transmission of the HEARTBEAT
4972 * chunk to the destination address is successful.
4973 *
4974 * Mandatory attributes:
4975 *
4976 * o association id - local handle to the SCTP association
4977 *
4978 * o destination transport address - the transport address of the
4979 * association on which a heartbeat should be issued.
4980 */
4981sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4982 const struct sctp_endpoint *ep,
4983 const struct sctp_association *asoc,
4984 const sctp_subtype_t type,
4985 void *arg,
4986 sctp_cmd_seq_t *commands)
4987{
Vlad Yasevichfb785252007-03-19 17:02:03 -07004988 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4989 (struct sctp_transport *)arg, commands))
4990 return SCTP_DISPOSITION_NOMEM;
4991
4992 /*
4993 * RFC 2960 (bis), section 8.3
4994 *
4995 * D) Request an on-demand HEARTBEAT on a specific destination
4996 * transport address of a given association.
4997 *
4998 * The endpoint should increment the respective error counter of
4999 * the destination transport address each time a HEARTBEAT is sent
5000 * to that address and not acknowledged within one RTO.
5001 *
5002 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00005003 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07005004 SCTP_TRANSPORT(arg));
5005 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006}
5007
5008/*
5009 * ADDIP Section 4.1 ASCONF Chunk Procedures
5010 * When an endpoint has an ASCONF signaled change to be sent to the
5011 * remote endpoint it should do A1 to A9
5012 */
5013sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5014 const struct sctp_association *asoc,
5015 const sctp_subtype_t type,
5016 void *arg,
5017 sctp_cmd_seq_t *commands)
5018{
5019 struct sctp_chunk *chunk = arg;
5020
5021 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5022 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5023 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5024 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5025 return SCTP_DISPOSITION_CONSUME;
5026}
5027
5028/*
5029 * Ignore the primitive event
5030 *
5031 * The return value is the disposition of the primitive.
5032 */
5033sctp_disposition_t sctp_sf_ignore_primitive(
5034 const struct sctp_endpoint *ep,
5035 const struct sctp_association *asoc,
5036 const sctp_subtype_t type,
5037 void *arg,
5038 sctp_cmd_seq_t *commands)
5039{
5040 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5041 return SCTP_DISPOSITION_DISCARD;
5042}
5043
5044/***************************************************************************
5045 * These are the state functions for the OTHER events.
5046 ***************************************************************************/
5047
5048/*
5049 * Start the shutdown negotiation.
5050 *
5051 * From Section 9.2:
5052 * Once all its outstanding data has been acknowledged, the endpoint
5053 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5054 * TSN Ack field the last sequential TSN it has received from the peer.
5055 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5056 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5057 * with the updated last sequential TSN received from its peer.
5058 *
5059 * The return value is the disposition.
5060 */
5061sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5062 const struct sctp_endpoint *ep,
5063 const struct sctp_association *asoc,
5064 const sctp_subtype_t type,
5065 void *arg,
5066 sctp_cmd_seq_t *commands)
5067{
5068 struct sctp_chunk *reply;
5069
5070 /* Once all its outstanding data has been acknowledged, the
5071 * endpoint shall send a SHUTDOWN chunk to its peer including
5072 * in the Cumulative TSN Ack field the last sequential TSN it
5073 * has received from the peer.
5074 */
5075 reply = sctp_make_shutdown(asoc, NULL);
5076 if (!reply)
5077 goto nomem;
5078
5079 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5080 * T2-shutdown timer.
5081 */
5082 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5083
5084 /* It shall then start the T2-shutdown timer */
5085 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5086 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5087
Wei Yongjun536428a2008-09-05 08:55:26 +08005088 /* RFC 4960 Section 9.2
5089 * The sender of the SHUTDOWN MAY also start an overall guard timer
5090 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5091 */
5092 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5093 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 if (asoc->autoclose)
5096 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5097 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5098
5099 /* and enter the SHUTDOWN-SENT state. */
5100 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5101 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5102
5103 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5104 *
5105 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005106 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 */
5108 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5109
5110 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5111
5112 return SCTP_DISPOSITION_CONSUME;
5113
5114nomem:
5115 return SCTP_DISPOSITION_NOMEM;
5116}
5117
5118/*
5119 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5120 *
5121 * From Section 9.2:
5122 *
5123 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5124 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5125 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5126 * endpoint must re-send the SHUTDOWN ACK.
5127 *
5128 * The return value is the disposition.
5129 */
5130sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5131 const struct sctp_endpoint *ep,
5132 const struct sctp_association *asoc,
5133 const sctp_subtype_t type,
5134 void *arg,
5135 sctp_cmd_seq_t *commands)
5136{
5137 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5138 struct sctp_chunk *reply;
5139
5140 /* There are 2 ways of getting here:
5141 * 1) called in response to a SHUTDOWN chunk
5142 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5143 *
5144 * For the case (2), the arg parameter is set to NULL. We need
5145 * to check that we have a chunk before accessing it's fields.
5146 */
5147 if (chunk) {
5148 if (!sctp_vtag_verify(chunk, asoc))
5149 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5150
5151 /* Make sure that the SHUTDOWN chunk has a valid length. */
5152 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5153 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5154 commands);
5155 }
5156
5157 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5158 * shall send a SHUTDOWN ACK ...
5159 */
5160 reply = sctp_make_shutdown_ack(asoc, chunk);
5161 if (!reply)
5162 goto nomem;
5163
5164 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5165 * the T2-shutdown timer.
5166 */
5167 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5168
5169 /* and start/restart a T2-shutdown timer of its own, */
5170 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5171 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5172
5173 if (asoc->autoclose)
5174 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5175 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5176
5177 /* Enter the SHUTDOWN-ACK-SENT state. */
5178 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5179 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5180
5181 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5182 *
5183 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005184 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 */
5186 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5187
5188 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5189
5190 return SCTP_DISPOSITION_CONSUME;
5191
5192nomem:
5193 return SCTP_DISPOSITION_NOMEM;
5194}
5195
5196/*
5197 * Ignore the event defined as other
5198 *
5199 * The return value is the disposition of the event.
5200 */
5201sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5202 const struct sctp_association *asoc,
5203 const sctp_subtype_t type,
5204 void *arg,
5205 sctp_cmd_seq_t *commands)
5206{
5207 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5208 return SCTP_DISPOSITION_DISCARD;
5209}
5210
5211/************************************************************
5212 * These are the state functions for handling timeout events.
5213 ************************************************************/
5214
5215/*
5216 * RTX Timeout
5217 *
5218 * Section: 6.3.3 Handle T3-rtx Expiration
5219 *
5220 * Whenever the retransmission timer T3-rtx expires for a destination
5221 * address, do the following:
5222 * [See below]
5223 *
5224 * The return value is the disposition of the chunk.
5225 */
5226sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5227 const struct sctp_association *asoc,
5228 const sctp_subtype_t type,
5229 void *arg,
5230 sctp_cmd_seq_t *commands)
5231{
5232 struct sctp_transport *transport = arg;
5233
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005234 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5235
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005237 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5238 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5240 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005241 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5243 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5244 return SCTP_DISPOSITION_DELETE_TCB;
5245 }
5246
5247 /* E1) For the destination address for which the timer
5248 * expires, adjust its ssthresh with rules defined in Section
5249 * 7.2.3 and set the cwnd <- MTU.
5250 */
5251
5252 /* E2) For the destination address for which the timer
5253 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5254 * maximum value discussed in rule C7 above (RTO.max) may be
5255 * used to provide an upper bound to this doubling operation.
5256 */
5257
5258 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5259 * outstanding DATA chunks for the address for which the
5260 * T3-rtx has expired will fit into a single packet, subject
5261 * to the MTU constraint for the path corresponding to the
5262 * destination transport address to which the retransmission
5263 * is being sent (this may be different from the address for
5264 * which the timer expires [see Section 6.4]). Call this
5265 * value K. Bundle and retransmit those K DATA chunks in a
5266 * single packet to the destination endpoint.
5267 *
5268 * Note: Any DATA chunks that were sent to the address for
5269 * which the T3-rtx timer expired but did not fit in one MTU
5270 * (rule E3 above), should be marked for retransmission and
5271 * sent as soon as cwnd allows (normally when a SACK arrives).
5272 */
5273
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 /* Do some failure management (Section 8.2). */
5275 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5276
Vlad Yasevich1845a572007-02-21 02:06:19 -08005277 /* NB: Rules E4 and F1 are implicit in R1. */
5278 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 return SCTP_DISPOSITION_CONSUME;
5281}
5282
5283/*
5284 * Generate delayed SACK on timeout
5285 *
5286 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5287 *
5288 * The guidelines on delayed acknowledgement algorithm specified in
5289 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5290 * acknowledgement SHOULD be generated for at least every second packet
5291 * (not every second DATA chunk) received, and SHOULD be generated
5292 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5293 * some situations it may be beneficial for an SCTP transmitter to be
5294 * more conservative than the algorithms detailed in this document
5295 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5296 * the following algorithms allow.
5297 */
5298sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5299 const struct sctp_association *asoc,
5300 const sctp_subtype_t type,
5301 void *arg,
5302 sctp_cmd_seq_t *commands)
5303{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005304 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5306 return SCTP_DISPOSITION_CONSUME;
5307}
5308
5309/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005310 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 *
5312 * Section: 4 Note: 2
5313 * Verification Tag:
5314 * Inputs
5315 * (endpoint, asoc)
5316 *
5317 * RFC 2960 Section 4 Notes
5318 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5319 * and re-start the T1-init timer without changing state. This MUST
5320 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5321 * endpoint MUST abort the initialization process and report the
5322 * error to SCTP user.
5323 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005324 * Outputs
5325 * (timers, events)
5326 *
5327 */
5328sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5329 const struct sctp_association *asoc,
5330 const sctp_subtype_t type,
5331 void *arg,
5332 sctp_cmd_seq_t *commands)
5333{
5334 struct sctp_chunk *repl = NULL;
5335 struct sctp_bind_addr *bp;
5336 int attempts = asoc->init_err_counter + 1;
5337
5338 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005339 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005340
Vlad Yasevich81845c22006-01-30 15:59:54 -08005341 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005342 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5343 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5344 if (!repl)
5345 return SCTP_DISPOSITION_NOMEM;
5346
5347 /* Choose transport for INIT. */
5348 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5349 SCTP_CHUNK(repl));
5350
5351 /* Issue a sideeffect to do the needed accounting. */
5352 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5353 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5354
5355 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5356 } else {
5357 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5358 " max_init_attempts: %d\n",
5359 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005360 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5361 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005362 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005363 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005364 return SCTP_DISPOSITION_DELETE_TCB;
5365 }
5366
5367 return SCTP_DISPOSITION_CONSUME;
5368}
5369
5370/*
5371 * sctp_sf_t1_cookie_timer_expire
5372 *
5373 * Section: 4 Note: 2
5374 * Verification Tag:
5375 * Inputs
5376 * (endpoint, asoc)
5377 *
5378 * RFC 2960 Section 4 Notes
5379 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 * COOKIE ECHO and re-start the T1-cookie timer without changing
5381 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5382 * After that, the endpoint MUST abort the initialization process and
5383 * report the error to SCTP user.
5384 *
5385 * Outputs
5386 * (timers, events)
5387 *
5388 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005389sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 const struct sctp_association *asoc,
5391 const sctp_subtype_t type,
5392 void *arg,
5393 sctp_cmd_seq_t *commands)
5394{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005395 struct sctp_chunk *repl = NULL;
5396 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397
Frank Filz3f7a87d2005-06-20 13:14:57 -07005398 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005399 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400
Vlad Yasevich81845c22006-01-30 15:59:54 -08005401 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005402 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005404 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005406 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5407 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005409 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5410 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5411
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5413 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005414 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5415 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005417 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 return SCTP_DISPOSITION_DELETE_TCB;
5419 }
5420
5421 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
5424/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5425 * with the updated last sequential TSN received from its peer.
5426 *
5427 * An endpoint should limit the number of retransmissions of the
5428 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5429 * If this threshold is exceeded the endpoint should destroy the TCB and
5430 * MUST report the peer endpoint unreachable to the upper layer (and
5431 * thus the association enters the CLOSED state). The reception of any
5432 * packet from its peer (i.e. as the peer sends all of its queued DATA
5433 * chunks) should clear the endpoint's retransmission count and restart
5434 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5435 * all of its queued DATA chunks that have not yet been sent.
5436 */
5437sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5438 const struct sctp_association *asoc,
5439 const sctp_subtype_t type,
5440 void *arg,
5441 sctp_cmd_seq_t *commands)
5442{
5443 struct sctp_chunk *reply = NULL;
5444
5445 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005446 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5447
Neil Horman58fbbed2008-02-29 11:40:56 -08005448 ((struct sctp_association *)asoc)->shutdown_retries++;
5449
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005451 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5452 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5454 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005455 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5457 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5458 return SCTP_DISPOSITION_DELETE_TCB;
5459 }
5460
5461 switch (asoc->state) {
5462 case SCTP_STATE_SHUTDOWN_SENT:
5463 reply = sctp_make_shutdown(asoc, NULL);
5464 break;
5465
5466 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5467 reply = sctp_make_shutdown_ack(asoc, NULL);
5468 break;
5469
5470 default:
5471 BUG();
5472 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 if (!reply)
5476 goto nomem;
5477
Wei Yongjun6345b192009-04-26 23:13:35 +08005478 /* Do some failure management (Section 8.2).
5479 * If we remove the transport an SHUTDOWN was last sent to, don't
5480 * do failure management.
5481 */
5482 if (asoc->shutdown_last_sent_to)
5483 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5484 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485
5486 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5487 * the T2-shutdown timer.
5488 */
5489 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5490
5491 /* Restart the T2-shutdown timer. */
5492 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5493 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5494 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5495 return SCTP_DISPOSITION_CONSUME;
5496
5497nomem:
5498 return SCTP_DISPOSITION_NOMEM;
5499}
5500
5501/*
5502 * ADDIP Section 4.1 ASCONF CHunk Procedures
5503 * If the T4 RTO timer expires the endpoint should do B1 to B5
5504 */
5505sctp_disposition_t sctp_sf_t4_timer_expire(
5506 const struct sctp_endpoint *ep,
5507 const struct sctp_association *asoc,
5508 const sctp_subtype_t type,
5509 void *arg,
5510 sctp_cmd_seq_t *commands)
5511{
5512 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5513 struct sctp_transport *transport = chunk->transport;
5514
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005515 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5518 * detection on the appropriate destination address as defined in
5519 * RFC2960 [5] section 8.1 and 8.2.
5520 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005521 if (transport)
5522 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5523 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524
5525 /* Reconfig T4 timer and transport. */
5526 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5527
5528 /* ADDIP 4.1 B2) Increment the association error counters and perform
5529 * endpoint failure detection on the association as defined in
5530 * RFC2960 [5] section 8.1 and 8.2.
5531 * association error counter is incremented in SCTP_CMD_STRIKE.
5532 */
5533 if (asoc->overall_error_count >= asoc->max_retrans) {
5534 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5535 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005536 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5537 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005539 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjun3d5a0192008-10-09 14:32:24 -07005541 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 return SCTP_DISPOSITION_ABORT;
5543 }
5544
5545 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5546 * the ASCONF chunk was sent by doubling the RTO timer value.
5547 * This is done in SCTP_CMD_STRIKE.
5548 */
5549
5550 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5551 * choose an alternate destination address (please refer to RFC2960
5552 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005553 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 * ASCONF sent.
5555 */
5556 sctp_chunk_hold(asoc->addip_last_asconf);
5557 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5558 SCTP_CHUNK(asoc->addip_last_asconf));
5559
5560 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5561 * destination is selected, then the RTO used will be that of the new
5562 * destination address.
5563 */
5564 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5565 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5566
5567 return SCTP_DISPOSITION_CONSUME;
5568}
5569
5570/* sctpimpguide-05 Section 2.12.2
5571 * The sender of the SHUTDOWN MAY also start an overall guard timer
5572 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5573 * At the expiration of this timer the sender SHOULD abort the association
5574 * by sending an ABORT chunk.
5575 */
5576sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5577 const struct sctp_association *asoc,
5578 const sctp_subtype_t type,
5579 void *arg,
5580 sctp_cmd_seq_t *commands)
5581{
5582 struct sctp_chunk *reply = NULL;
5583
5584 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005585 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
5587 reply = sctp_make_abort(asoc, NULL, 0);
5588 if (!reply)
5589 goto nomem;
5590
5591 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005592 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5593 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005595 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005597 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5598 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5599
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 return SCTP_DISPOSITION_DELETE_TCB;
5601nomem:
5602 return SCTP_DISPOSITION_NOMEM;
5603}
5604
5605/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5606 * the association is automatically closed by starting the shutdown process.
5607 * The work that needs to be done is same as when SHUTDOWN is initiated by
5608 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5609 */
5610sctp_disposition_t sctp_sf_autoclose_timer_expire(
5611 const struct sctp_endpoint *ep,
5612 const struct sctp_association *asoc,
5613 const sctp_subtype_t type,
5614 void *arg,
5615 sctp_cmd_seq_t *commands)
5616{
5617 int disposition;
5618
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005619 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5620
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 /* From 9.2 Shutdown of an Association
5622 * Upon receipt of the SHUTDOWN primitive from its upper
5623 * layer, the endpoint enters SHUTDOWN-PENDING state and
5624 * remains there until all outstanding data has been
5625 * acknowledged by its peer. The endpoint accepts no new data
5626 * from its upper layer, but retransmits data to the far end
5627 * if necessary to fill gaps.
5628 */
5629 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5630 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5631
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 disposition = SCTP_DISPOSITION_CONSUME;
5633 if (sctp_outq_is_empty(&asoc->outqueue)) {
5634 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5635 arg, commands);
5636 }
5637 return disposition;
5638}
5639
5640/*****************************************************************************
5641 * These are sa state functions which could apply to all types of events.
5642 ****************************************************************************/
5643
5644/*
5645 * This table entry is not implemented.
5646 *
5647 * Inputs
5648 * (endpoint, asoc, chunk)
5649 *
5650 * The return value is the disposition of the chunk.
5651 */
5652sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5653 const struct sctp_association *asoc,
5654 const sctp_subtype_t type,
5655 void *arg,
5656 sctp_cmd_seq_t *commands)
5657{
5658 return SCTP_DISPOSITION_NOT_IMPL;
5659}
5660
5661/*
5662 * This table entry represents a bug.
5663 *
5664 * Inputs
5665 * (endpoint, asoc, chunk)
5666 *
5667 * The return value is the disposition of the chunk.
5668 */
5669sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5670 const struct sctp_association *asoc,
5671 const sctp_subtype_t type,
5672 void *arg,
5673 sctp_cmd_seq_t *commands)
5674{
5675 return SCTP_DISPOSITION_BUG;
5676}
5677
5678/*
5679 * This table entry represents the firing of a timer in the wrong state.
5680 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5681 * when the association is in the wrong state. This event should
5682 * be ignored, so as to prevent any rearming of the timer.
5683 *
5684 * Inputs
5685 * (endpoint, asoc, chunk)
5686 *
5687 * The return value is the disposition of the chunk.
5688 */
5689sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5690 const struct sctp_association *asoc,
5691 const sctp_subtype_t type,
5692 void *arg,
5693 sctp_cmd_seq_t *commands)
5694{
5695 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5696 return SCTP_DISPOSITION_CONSUME;
5697}
5698
5699/********************************************************************
5700 * 2nd Level Abstractions
5701 ********************************************************************/
5702
5703/* Pull the SACK chunk based on the SACK header. */
5704static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5705{
5706 struct sctp_sackhdr *sack;
5707 unsigned int len;
5708 __u16 num_blocks;
5709 __u16 num_dup_tsns;
5710
5711 /* Protect ourselves from reading too far into
5712 * the skb from a bogus sender.
5713 */
5714 sack = (struct sctp_sackhdr *) chunk->skb->data;
5715
5716 num_blocks = ntohs(sack->num_gap_ack_blocks);
5717 num_dup_tsns = ntohs(sack->num_dup_tsns);
5718 len = sizeof(struct sctp_sackhdr);
5719 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5720 if (len > chunk->skb->len)
5721 return NULL;
5722
5723 skb_pull(chunk->skb, len);
5724
5725 return sack;
5726}
5727
5728/* Create an ABORT packet to be sent as a response, with the specified
5729 * error causes.
5730 */
5731static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5732 const struct sctp_association *asoc,
5733 struct sctp_chunk *chunk,
5734 const void *payload,
5735 size_t paylen)
5736{
5737 struct sctp_packet *packet;
5738 struct sctp_chunk *abort;
5739
5740 packet = sctp_ootb_pkt_new(asoc, chunk);
5741
5742 if (packet) {
5743 /* Make an ABORT.
5744 * The T bit will be set if the asoc is NULL.
5745 */
5746 abort = sctp_make_abort(asoc, chunk, paylen);
5747 if (!abort) {
5748 sctp_ootb_pkt_free(packet);
5749 return NULL;
5750 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005751
5752 /* Reflect vtag if T-Bit is set */
5753 if (sctp_test_T_bit(abort))
5754 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 /* Add specified error causes, i.e., payload, to the
5757 * end of the chunk.
5758 */
5759 sctp_addto_chunk(abort, paylen, payload);
5760
5761 /* Set the skb to the belonging sock for accounting. */
5762 abort->skb->sk = ep->base.sk;
5763
5764 sctp_packet_append_chunk(packet, abort);
5765
5766 }
5767
5768 return packet;
5769}
5770
5771/* Allocate a packet for responding in the OOTB conditions. */
5772static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5773 const struct sctp_chunk *chunk)
5774{
5775 struct sctp_packet *packet;
5776 struct sctp_transport *transport;
5777 __u16 sport;
5778 __u16 dport;
5779 __u32 vtag;
5780
5781 /* Get the source and destination port from the inbound packet. */
5782 sport = ntohs(chunk->sctp_hdr->dest);
5783 dport = ntohs(chunk->sctp_hdr->source);
5784
5785 /* The V-tag is going to be the same as the inbound packet if no
5786 * association exists, otherwise, use the peer's vtag.
5787 */
5788 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005789 /* Special case the INIT-ACK as there is no peer's vtag
5790 * yet.
5791 */
5792 switch(chunk->chunk_hdr->type) {
5793 case SCTP_CID_INIT_ACK:
5794 {
5795 sctp_initack_chunk_t *initack;
5796
5797 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5798 vtag = ntohl(initack->init_hdr.init_tag);
5799 break;
5800 }
5801 default:
5802 vtag = asoc->peer.i.init_tag;
5803 break;
5804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 } else {
5806 /* Special case the INIT and stale COOKIE_ECHO as there is no
5807 * vtag yet.
5808 */
5809 switch(chunk->chunk_hdr->type) {
5810 case SCTP_CID_INIT:
5811 {
5812 sctp_init_chunk_t *init;
5813
5814 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5815 vtag = ntohl(init->init_hdr.init_tag);
5816 break;
5817 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005818 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 vtag = ntohl(chunk->sctp_hdr->vtag);
5820 break;
5821 }
5822 }
5823
5824 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005825 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826 if (!transport)
5827 goto nomem;
5828
5829 /* Cache a route for the transport with the chunk's destination as
5830 * the source address.
5831 */
Al Viro16b0a032006-11-20 17:13:38 -08005832 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 sctp_sk(sctp_get_ctl_sock()));
5834
5835 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5836 packet = sctp_packet_config(packet, vtag, 0);
5837
5838 return packet;
5839
5840nomem:
5841 return NULL;
5842}
5843
5844/* Free the packet allocated earlier for responding in the OOTB condition. */
5845void sctp_ootb_pkt_free(struct sctp_packet *packet)
5846{
5847 sctp_transport_free(packet->transport);
5848}
5849
5850/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5851static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5852 const struct sctp_association *asoc,
5853 const struct sctp_chunk *chunk,
5854 sctp_cmd_seq_t *commands,
5855 struct sctp_chunk *err_chunk)
5856{
5857 struct sctp_packet *packet;
5858
5859 if (err_chunk) {
5860 packet = sctp_ootb_pkt_new(asoc, chunk);
5861 if (packet) {
5862 struct sctp_signed_cookie *cookie;
5863
5864 /* Override the OOTB vtag from the cookie. */
5865 cookie = chunk->subh.cookie_hdr;
5866 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 /* Set the skb to the belonging sock for accounting. */
5869 err_chunk->skb->sk = ep->base.sk;
5870 sctp_packet_append_chunk(packet, err_chunk);
5871 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5872 SCTP_PACKET(packet));
5873 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5874 } else
5875 sctp_chunk_free (err_chunk);
5876 }
5877}
5878
5879
5880/* Process a data chunk */
5881static int sctp_eat_data(const struct sctp_association *asoc,
5882 struct sctp_chunk *chunk,
5883 sctp_cmd_seq_t *commands)
5884{
5885 sctp_datahdr_t *data_hdr;
5886 struct sctp_chunk *err;
5887 size_t datalen;
5888 sctp_verb_t deliver;
5889 int tmp;
5890 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005891 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005892 struct sock *sk = asoc->base.sk;
Vlad Yasevichf1751c52009-09-04 18:21:03 -04005893 u16 ssn;
5894 u16 sid;
5895 u8 ordered = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
5897 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5898 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5899
5900 tsn = ntohl(data_hdr->tsn);
5901 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5902
5903 /* ASSERT: Now skb->data is really the user data. */
5904
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 /* Process ECN based congestion.
5906 *
5907 * Since the chunk structure is reused for all chunks within
5908 * a packet, we use ecn_ce_done to track if we've already
5909 * done CE processing for this packet.
5910 *
5911 * We need to do ECN processing even if we plan to discard the
5912 * chunk later.
5913 */
5914
5915 if (!chunk->ecn_ce_done) {
5916 struct sctp_af *af;
5917 chunk->ecn_ce_done = 1;
5918
5919 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005920 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
5922 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5923 /* Do real work as sideffect. */
5924 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5925 SCTP_U32(tsn));
5926 }
5927 }
5928
5929 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5930 if (tmp < 0) {
5931 /* The TSN is too high--silently discard the chunk and
5932 * count on it getting retransmitted later.
5933 */
5934 return SCTP_IERROR_HIGH_TSN;
5935 } else if (tmp > 0) {
5936 /* This is a duplicate. Record it. */
5937 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5938 return SCTP_IERROR_DUP_TSN;
5939 }
5940
5941 /* This is a new TSN. */
5942
5943 /* Discard if there is no room in the receive window.
5944 * Actually, allow a little bit of overflow (up to a MTU).
5945 */
5946 datalen = ntohs(chunk->chunk_hdr->length);
5947 datalen -= sizeof(sctp_data_chunk_t);
5948
5949 deliver = SCTP_CMD_CHUNK_ULP;
5950
5951 /* Think about partial delivery. */
5952 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5953
5954 /* Even if we don't accept this chunk there is
5955 * memory pressure.
5956 */
5957 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5958 }
5959
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005960 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 * seems a bit troublesome in that frag_point varies based on
5962 * PMTU. In cases, such as loopback, this might be a rather
5963 * large spill over.
5964 */
Neil Horman4d93df02007-08-15 16:07:44 -07005965 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5966 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
5968 /* If this is the next TSN, consider reneging to make
5969 * room. Note: Playing nice with a confused sender. A
5970 * malicious sender can still eat up all our buffer
5971 * space and in the future we may want to detect and
5972 * do more drastic reneging.
5973 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005974 if (sctp_tsnmap_has_gap(map) &&
5975 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5977 deliver = SCTP_CMD_RENEGE;
5978 } else {
5979 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5980 "rwnd: %d\n", tsn, datalen,
5981 asoc->rwnd);
5982 return SCTP_IERROR_IGNORE_TSN;
5983 }
5984 }
5985
5986 /*
Neil Horman4d93df02007-08-15 16:07:44 -07005987 * Also try to renege to limit our memory usage in the event that
5988 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08005989 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07005990 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5991 * memory usage too much
5992 */
5993 if (*sk->sk_prot_creator->memory_pressure) {
5994 if (sctp_tsnmap_has_gap(map) &&
5995 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5996 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5997 deliver = SCTP_CMD_RENEGE;
5998 }
5999 }
6000
6001 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 * Section 3.3.10.9 No User Data (9)
6003 *
6004 * Cause of error
6005 * ---------------
6006 * No User Data: This error cause is returned to the originator of a
6007 * DATA chunk if a received DATA chunk has no user data.
6008 */
6009 if (unlikely(0 == datalen)) {
6010 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6011 if (err) {
6012 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6013 SCTP_CHUNK(err));
6014 }
6015 /* We are going to ABORT, so we might as well stop
6016 * processing the rest of the chunks in the packet.
6017 */
6018 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07006019 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6020 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006022 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6024 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6025 return SCTP_IERROR_NO_DATA;
6026 }
6027
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006028 chunk->data_accepted = 1;
6029
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6031 * if we renege and the chunk arrives again.
6032 */
6033 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6034 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006035 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006037 ordered = 1;
6038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
6040 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6041 *
6042 * If an endpoint receive a DATA chunk with an invalid stream
6043 * identifier, it shall acknowledge the reception of the DATA chunk
6044 * following the normal procedure, immediately send an ERROR chunk
6045 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6046 * and discard the DATA chunk.
6047 */
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006048 sid = ntohs(data_hdr->stream);
6049 if (sid >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006050 /* Mark tsn as received even though we drop it */
6051 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6052
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6054 &data_hdr->stream,
6055 sizeof(data_hdr->stream));
6056 if (err)
6057 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6058 SCTP_CHUNK(err));
6059 return SCTP_IERROR_BAD_STREAM;
6060 }
6061
Vlad Yasevichf1751c52009-09-04 18:21:03 -04006062 /* Check to see if the SSN is possible for this TSN.
6063 * The biggest gap we can record is 4K wide. Since SSNs wrap
6064 * at an unsigned short, there is no way that an SSN can
6065 * wrap and for a valid TSN. We can simply check if the current
6066 * SSN is smaller then the next expected one. If it is, it wrapped
6067 * and is invalid.
6068 */
6069 ssn = ntohs(data_hdr->ssn);
6070 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6071 return SCTP_IERROR_PROTO_VIOLATION;
6072 }
6073
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 /* Send the data up to the user. Note: Schedule the
6075 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6076 * chunk needs the updated rwnd.
6077 */
6078 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6079
6080 return SCTP_IERROR_NO_ERROR;
6081}