blob: 3ef97499df0ddb1631098b505efb2de2775dca23 [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,
122 void *arg,
123 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 */
318 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
319 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900322 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324 if (chunk->sctp_hdr->vtag != 0)
325 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
326
327 /* Make sure that the INIT chunk has a valid length.
328 * Normally, this would cause an ABORT with a Protocol Violation
329 * error, but since we don't have an association, we'll
330 * just discard the packet.
331 */
332 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
333 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
334
335 /* Verify the INIT chunk before processing it. */
336 err_chunk = NULL;
337 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
338 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
339 &err_chunk)) {
340 /* This chunk contains fatal error. It is to be discarded.
341 * Send an ABORT, with causes if there is any.
342 */
343 if (err_chunk) {
344 packet = sctp_abort_pkt_new(ep, asoc, arg,
345 (__u8 *)(err_chunk->chunk_hdr) +
346 sizeof(sctp_chunkhdr_t),
347 ntohs(err_chunk->chunk_hdr->length) -
348 sizeof(sctp_chunkhdr_t));
349
350 sctp_chunk_free(err_chunk);
351
352 if (packet) {
353 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
354 SCTP_PACKET(packet));
355 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
356 return SCTP_DISPOSITION_CONSUME;
357 } else {
358 return SCTP_DISPOSITION_NOMEM;
359 }
360 } else {
361 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
362 commands);
363 }
364 }
365
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900366 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
368
369 /* Tag the variable length parameters. */
370 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
371
372 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
373 if (!new_asoc)
374 goto nomem;
375
376 /* The call, sctp_process_init(), can fail on memory allocation. */
377 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
378 sctp_source(chunk),
379 (sctp_init_chunk_t *)chunk->chunk_hdr,
380 GFP_ATOMIC))
381 goto nomem_init;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
384
385 /* If there are errors need to be reported for unknown parameters,
386 * make sure to reserve enough room in the INIT ACK for them.
387 */
388 len = 0;
389 if (err_chunk)
390 len = ntohs(err_chunk->chunk_hdr->length) -
391 sizeof(sctp_chunkhdr_t);
392
393 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700394 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
397 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700398 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* If there are errors need to be reported for unknown parameters,
401 * include them in the outgoing INIT ACK as "Unrecognized parameter"
402 * parameter.
403 */
404 if (err_chunk) {
405 /* Get the "Unrecognized parameter" parameter(s) out of the
406 * ERROR chunk generated by sctp_verify_init(). Since the
407 * error cause code for "unknown parameter" and the
408 * "Unrecognized parameter" type is the same, we can
409 * construct the parameters in INIT ACK by copying the
410 * ERROR causes over.
411 */
412 unk_param = (sctp_unrecognized_param_t *)
413 ((__u8 *)(err_chunk->chunk_hdr) +
414 sizeof(sctp_chunkhdr_t));
415 /* Replace the cause code with the "Unrecognized parameter"
416 * parameter type.
417 */
418 sctp_addto_chunk(repl, len, unk_param);
419 sctp_chunk_free(err_chunk);
420 }
421
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700422 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
425
426 /*
427 * Note: After sending out INIT ACK with the State Cookie parameter,
428 * "Z" MUST NOT allocate any resources, nor keep any states for the
429 * new association. Otherwise, "Z" will be vulnerable to resource
430 * attacks.
431 */
432 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
433
434 return SCTP_DISPOSITION_DELETE_TCB;
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436nomem_init:
437 sctp_association_free(new_asoc);
438nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700439 if (err_chunk)
440 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return SCTP_DISPOSITION_NOMEM;
442}
443
444/*
445 * Respond to a normal INIT ACK chunk.
446 * We are the side that is initiating the association.
447 *
448 * Section: 5.1 Normal Establishment of an Association, C
449 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
450 * timer and leave COOKIE-WAIT state. "A" shall then send the State
451 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
452 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
453 *
454 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
455 * DATA chunks, but it MUST be the first chunk in the packet and
456 * until the COOKIE ACK is returned the sender MUST NOT send any
457 * other packets to the peer.
458 *
459 * Verification Tag: 3.3.3
460 * If the value of the Initiate Tag in a received INIT ACK chunk is
461 * found to be 0, the receiver MUST treat it as an error and close the
462 * association by transmitting an ABORT.
463 *
464 * Inputs
465 * (endpoint, asoc, chunk)
466 *
467 * Outputs
468 * (asoc, reply_msg, msg_up, timers, counters)
469 *
470 * The return value is the disposition of the chunk.
471 */
472sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
473 const struct sctp_association *asoc,
474 const sctp_subtype_t type,
475 void *arg,
476 sctp_cmd_seq_t *commands)
477{
478 struct sctp_chunk *chunk = arg;
479 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct sctp_chunk *err_chunk;
481 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 if (!sctp_vtag_verify(chunk, asoc))
484 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 /* 6.10 Bundling
487 * An endpoint MUST NOT bundle INIT, INIT ACK or
488 * SHUTDOWN COMPLETE with any other chunks.
489 */
490 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400491 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Vlad Yasevichece25df2007-09-07 16:30:54 -0400493 /* Make sure that the INIT-ACK chunk has a valid length */
494 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
495 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
496 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* Grab the INIT header. */
498 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Verify the INIT chunk before processing it. */
501 err_chunk = NULL;
502 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
503 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
504 &err_chunk)) {
505
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800506 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800509 * Send an ABORT, with causes. If there are no causes,
510 * then there wasn't enough memory. Just terminate
511 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513 if (err_chunk) {
514 packet = sctp_abort_pkt_new(ep, asoc, arg,
515 (__u8 *)(err_chunk->chunk_hdr) +
516 sizeof(sctp_chunkhdr_t),
517 ntohs(err_chunk->chunk_hdr->length) -
518 sizeof(sctp_chunkhdr_t));
519
520 sctp_chunk_free(err_chunk);
521
522 if (packet) {
523 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
524 SCTP_PACKET(packet));
525 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700526 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700529
530 /* SCTP-AUTH, Section 6.3:
531 * It should be noted that if the receiver wants to tear
532 * down an association in an authenticated way only, the
533 * handling of malformed packets should not result in
534 * tearing down the association.
535 *
536 * This means that if we only want to abort associations
537 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +0200538 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700539 * was malformed.
540 */
541 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
542 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
543
544 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700545 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
546 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
549 /* Tag the variable length parameters. Note that we never
550 * convert the parameters in an INIT chunk.
551 */
552 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
553
554 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
555
556 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
557 SCTP_PEER_INIT(initchunk));
558
Frank Filz3f7a87d2005-06-20 13:14:57 -0700559 /* Reset init error count upon receipt of INIT-ACK. */
560 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* 5.1 C) "A" shall stop the T1-init timer and leave
563 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
564 * timer, and enter the COOKIE-ECHOED state.
565 */
566 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
567 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
568 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
569 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
570 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
571 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
572
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700573 /* SCTP-AUTH: genereate the assocition shared keys so that
574 * we can potentially signe the COOKIE-ECHO.
575 */
576 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 /* 5.1 C) "A" shall then send the State Cookie received in the
579 * INIT ACK chunk in a COOKIE ECHO chunk, ...
580 */
581 /* If there is any errors to report, send the ERROR chunk generated
582 * for unknown parameters as well.
583 */
584 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
585 SCTP_CHUNK(err_chunk));
586
587 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * Respond to a normal COOKIE ECHO chunk.
592 * We are the side that is being asked for an association.
593 *
594 * Section: 5.1 Normal Establishment of an Association, D
595 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
596 * with a COOKIE ACK chunk after building a TCB and moving to
597 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
598 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
599 * chunk MUST be the first chunk in the packet.
600 *
601 * IMPLEMENTATION NOTE: An implementation may choose to send the
602 * Communication Up notification to the SCTP user upon reception
603 * of a valid COOKIE ECHO chunk.
604 *
605 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
606 * D) Rules for packet carrying a COOKIE ECHO
607 *
608 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
609 * Initial Tag received in the INIT ACK.
610 *
611 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
612 *
613 * Inputs
614 * (endpoint, asoc, chunk)
615 *
616 * Outputs
617 * (asoc, reply_msg, msg_up, timers, counters)
618 *
619 * The return value is the disposition of the chunk.
620 */
621sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
622 const struct sctp_association *asoc,
623 const sctp_subtype_t type, void *arg,
624 sctp_cmd_seq_t *commands)
625{
626 struct sctp_chunk *chunk = arg;
627 struct sctp_association *new_asoc;
628 sctp_init_chunk_t *peer_init;
629 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700630 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 int error = 0;
632 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900633 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* If the packet is an OOTB packet which is temporarily on the
636 * control endpoint, respond with an ABORT.
637 */
638 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400639 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* Make sure that the COOKIE_ECHO chunk has a valid length.
642 * In this case, we check that we have enough for at least a
643 * chunk header. More detailed verification is done
644 * in sctp_unpack_cookie().
645 */
646 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
647 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
648
Vlad Yasevich609ee462007-08-31 03:10:59 +0900649 /* If the endpoint is not listening or if the number of associations
650 * on the TCP-style socket exceed the max backlog, respond with an
651 * ABORT.
652 */
653 sk = ep->base.sk;
654 if (!sctp_sstate(sk, LISTENING) ||
655 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
656 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 /* "Decode" the chunk. We have no optional parameters so we
659 * are in good shape.
660 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900661 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700663 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
664 sizeof(sctp_chunkhdr_t)))
665 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
668 * "Z" will reply with a COOKIE ACK chunk after building a TCB
669 * and moving to the ESTABLISHED state.
670 */
671 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
672 &err_chk_p);
673
674 /* FIXME:
675 * If the re-build failed, what is the proper error path
676 * from here?
677 *
678 * [We should abort the association. --piggy]
679 */
680 if (!new_asoc) {
681 /* FIXME: Several errors are possible. A bad cookie should
682 * be silently discarded, but think about logging it too.
683 */
684 switch (error) {
685 case -SCTP_IERROR_NOMEM:
686 goto nomem;
687
688 case -SCTP_IERROR_STALE_COOKIE:
689 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
690 err_chk_p);
691 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
692
693 case -SCTP_IERROR_BAD_SIG:
694 default:
695 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700700 /* Delay state machine commands until later.
701 *
702 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * the sctp_unpack_cookie() already.
704 */
705 /* This is a brand-new association, so these are not yet side
706 * effects--it is safe to run them here.
707 */
708 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
709
710 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
Al Viro6a1e5f32006-11-20 17:12:25 -0800711 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 peer_init, GFP_ATOMIC))
713 goto nomem_init;
714
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700715 /* SCTP-AUTH: Now that we've populate required fields in
716 * sctp_process_init, set up the assocaition shared keys as
717 * necessary so that we can potentially authenticate the ACK
718 */
719 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
720 if (error)
721 goto nomem_init;
722
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700723 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
724 * is supposed to be authenticated and we have to do delayed
725 * authentication. We've just recreated the association using
726 * the information in the cookie and now it's much easier to
727 * do the authentication.
728 */
729 if (chunk->auth_chunk) {
730 struct sctp_chunk auth;
731 sctp_ierror_t ret;
732
733 /* set-up our fake chunk so that we can process it */
734 auth.skb = chunk->auth_chunk;
735 auth.asoc = chunk->asoc;
736 auth.sctp_hdr = chunk->sctp_hdr;
737 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
738 sizeof(sctp_chunkhdr_t));
739 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
740 auth.transport = chunk->transport;
741
742 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
743
744 /* We can now safely free the auth_chunk clone */
745 kfree_skb(chunk->auth_chunk);
746
747 if (ret != SCTP_IERROR_NO_ERROR) {
748 sctp_association_free(new_asoc);
749 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
750 }
751 }
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 repl = sctp_make_cookie_ack(new_asoc, chunk);
754 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700755 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /* RFC 2960 5.1 Normal Establishment of an Association
758 *
759 * D) IMPLEMENTATION NOTE: An implementation may choose to
760 * send the Communication Up notification to the SCTP user
761 * upon reception of a valid COOKIE ECHO chunk.
762 */
763 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
764 new_asoc->c.sinit_num_ostreams,
765 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700766 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!ev)
768 goto nomem_ev;
769
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900770 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800771 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800773 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800775 if (new_asoc->peer.adaptation_ind) {
776 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700778 if (!ai_ev)
779 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700782 /* Add all the state machine commands now since we've created
783 * everything. This way we don't introduce memory corruptions
784 * during side-effect processing and correclty count established
785 * associations.
786 */
787 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
788 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
789 SCTP_STATE(SCTP_STATE_ESTABLISHED));
790 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
791 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
792 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
793
794 if (new_asoc->autoclose)
795 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
796 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
797
798 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
799
800 /* This will send the COOKIE ACK */
801 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
802
803 /* Queue the ASSOC_CHANGE event */
804 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
805
806 /* Send up the Adaptation Layer Indication event */
807 if (ai_ev)
808 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
809 SCTP_ULPEVENT(ai_ev));
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 return SCTP_DISPOSITION_CONSUME;
812
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700813nomem_aiev:
814 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815nomem_ev:
816 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817nomem_init:
818 sctp_association_free(new_asoc);
819nomem:
820 return SCTP_DISPOSITION_NOMEM;
821}
822
823/*
824 * Respond to a normal COOKIE ACK chunk.
825 * We are the side that is being asked for an association.
826 *
827 * RFC 2960 5.1 Normal Establishment of an Association
828 *
829 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
830 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
831 * timer. It may also notify its ULP about the successful
832 * establishment of the association with a Communication Up
833 * notification (see Section 10).
834 *
835 * Verification Tag:
836 * Inputs
837 * (endpoint, asoc, chunk)
838 *
839 * Outputs
840 * (asoc, reply_msg, msg_up, timers, counters)
841 *
842 * The return value is the disposition of the chunk.
843 */
844sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
845 const struct sctp_association *asoc,
846 const sctp_subtype_t type, void *arg,
847 sctp_cmd_seq_t *commands)
848{
849 struct sctp_chunk *chunk = arg;
850 struct sctp_ulpevent *ev;
851
852 if (!sctp_vtag_verify(chunk, asoc))
853 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
854
855 /* Verify that the chunk length for the COOKIE-ACK is OK.
856 * If we don't do this, any bundled chunks may be junked.
857 */
858 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
859 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
860 commands);
861
862 /* Reset init error count upon receipt of COOKIE-ACK,
863 * to avoid problems with the managemement of this
864 * counter in stale cookie situations when a transition back
865 * from the COOKIE-ECHOED state to the COOKIE-WAIT
866 * state is performed.
867 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700868 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /* RFC 2960 5.1 Normal Establishment of an Association
871 *
872 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
873 * from the COOKIE-ECHOED state to the ESTABLISHED state,
874 * stopping the T1-cookie timer.
875 */
876 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
877 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
878 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
879 SCTP_STATE(SCTP_STATE_ESTABLISHED));
880 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
881 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
882 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
883 if (asoc->autoclose)
884 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
885 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
886 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
887
888 /* It may also notify its ULP about the successful
889 * establishment of the association with a Communication Up
890 * notification (see Section 10).
891 */
892 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
893 0, asoc->c.sinit_num_ostreams,
894 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700895 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 if (!ev)
898 goto nomem;
899
900 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
901
902 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800903 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800905 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800907 if (asoc->peer.adaptation_ind) {
908 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!ev)
910 goto nomem;
911
912 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
913 SCTP_ULPEVENT(ev));
914 }
915
916 return SCTP_DISPOSITION_CONSUME;
917nomem:
918 return SCTP_DISPOSITION_NOMEM;
919}
920
921/* Generate and sendout a heartbeat packet. */
922static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
923 const struct sctp_association *asoc,
924 const sctp_subtype_t type,
925 void *arg,
926 sctp_cmd_seq_t *commands)
927{
928 struct sctp_transport *transport = (struct sctp_transport *) arg;
929 struct sctp_chunk *reply;
930 sctp_sender_hb_info_t hbinfo;
931 size_t paylen = 0;
932
933 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
934 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
Al Viro63de08f2006-11-20 17:07:25 -0800935 hbinfo.daddr = transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 hbinfo.sent_at = jiffies;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700937 hbinfo.hb_nonce = transport->hb_nonce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* Send a heartbeat to our peer. */
940 paylen = sizeof(sctp_sender_hb_info_t);
941 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
942 if (!reply)
943 return SCTP_DISPOSITION_NOMEM;
944
945 /* Set rto_pending indicating that an RTT measurement
946 * is started with this heartbeat chunk.
947 */
948 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
949 SCTP_TRANSPORT(transport));
950
951 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
952 return SCTP_DISPOSITION_CONSUME;
953}
954
955/* Generate a HEARTBEAT packet on the given transport. */
956sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
957 const struct sctp_association *asoc,
958 const sctp_subtype_t type,
959 void *arg,
960 sctp_cmd_seq_t *commands)
961{
962 struct sctp_transport *transport = (struct sctp_transport *) arg;
963
Vlad Yasevichfd102792007-11-15 12:13:32 -0500964 if (asoc->overall_error_count > asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700965 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
966 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
968 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800969 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
971 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
972 return SCTP_DISPOSITION_DELETE_TCB;
973 }
974
975 /* Section 3.3.5.
976 * The Sender-specific Heartbeat Info field should normally include
977 * information about the sender's current time when this HEARTBEAT
978 * chunk is sent and the destination transport address to which this
979 * HEARTBEAT is sent (see Section 8.3).
980 */
981
Frank Filz52ccb8e2005-12-22 11:36:46 -0800982 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (SCTP_DISPOSITION_NOMEM ==
984 sctp_sf_heartbeat(ep, asoc, type, arg,
985 commands))
986 return SCTP_DISPOSITION_NOMEM;
987 /* Set transport error counter and association error counter
988 * when sending heartbeat.
989 */
990 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
991 SCTP_TRANSPORT(transport));
992 }
993 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
994 SCTP_TRANSPORT(transport));
995
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900996 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999/*
1000 * Process an heartbeat request.
1001 *
1002 * Section: 8.3 Path Heartbeat
1003 * The receiver of the HEARTBEAT should immediately respond with a
1004 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1005 * from the received HEARTBEAT chunk.
1006 *
1007 * Verification Tag: 8.5 Verification Tag [Normal verification]
1008 * When receiving an SCTP packet, the endpoint MUST ensure that the
1009 * value in the Verification Tag field of the received SCTP packet
1010 * matches its own Tag. If the received Verification Tag value does not
1011 * match the receiver's own tag value, the receiver shall silently
1012 * discard the packet and shall not process it any further except for
1013 * those cases listed in Section 8.5.1 below.
1014 *
1015 * Inputs
1016 * (endpoint, asoc, chunk)
1017 *
1018 * Outputs
1019 * (asoc, reply_msg, msg_up, timers, counters)
1020 *
1021 * The return value is the disposition of the chunk.
1022 */
1023sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1024 const struct sctp_association *asoc,
1025 const sctp_subtype_t type,
1026 void *arg,
1027 sctp_cmd_seq_t *commands)
1028{
1029 struct sctp_chunk *chunk = arg;
1030 struct sctp_chunk *reply;
1031 size_t paylen = 0;
1032
1033 if (!sctp_vtag_verify(chunk, asoc))
1034 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1035
1036 /* Make sure that the HEARTBEAT chunk has a valid length. */
1037 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1038 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1039 commands);
1040
1041 /* 8.3 The receiver of the HEARTBEAT should immediately
1042 * respond with a HEARTBEAT ACK that contains the Heartbeat
1043 * Information field copied from the received HEARTBEAT chunk.
1044 */
1045 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1046 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001047 if (!pskb_pull(chunk->skb, paylen))
1048 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 reply = sctp_make_heartbeat_ack(asoc, chunk,
1051 chunk->subh.hb_hdr, paylen);
1052 if (!reply)
1053 goto nomem;
1054
1055 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1056 return SCTP_DISPOSITION_CONSUME;
1057
1058nomem:
1059 return SCTP_DISPOSITION_NOMEM;
1060}
1061
1062/*
1063 * Process the returning HEARTBEAT ACK.
1064 *
1065 * Section: 8.3 Path Heartbeat
1066 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1067 * should clear the error counter of the destination transport
1068 * address to which the HEARTBEAT was sent, and mark the destination
1069 * transport address as active if it is not so marked. The endpoint may
1070 * optionally report to the upper layer when an inactive destination
1071 * address is marked as active due to the reception of the latest
1072 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1073 * clear the association overall error count as well (as defined
1074 * in section 8.1).
1075 *
1076 * The receiver of the HEARTBEAT ACK should also perform an RTT
1077 * measurement for that destination transport address using the time
1078 * value carried in the HEARTBEAT ACK chunk.
1079 *
1080 * Verification Tag: 8.5 Verification Tag [Normal verification]
1081 *
1082 * Inputs
1083 * (endpoint, asoc, chunk)
1084 *
1085 * Outputs
1086 * (asoc, reply_msg, msg_up, timers, counters)
1087 *
1088 * The return value is the disposition of the chunk.
1089 */
1090sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1091 const struct sctp_association *asoc,
1092 const sctp_subtype_t type,
1093 void *arg,
1094 sctp_cmd_seq_t *commands)
1095{
1096 struct sctp_chunk *chunk = arg;
1097 union sctp_addr from_addr;
1098 struct sctp_transport *link;
1099 sctp_sender_hb_info_t *hbinfo;
1100 unsigned long max_interval;
1101
1102 if (!sctp_vtag_verify(chunk, asoc))
1103 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1104
1105 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1106 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1107 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1108 commands);
1109
1110 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001111 /* Make sure that the length of the parameter is what we expect */
1112 if (ntohs(hbinfo->param_hdr.length) !=
1113 sizeof(sctp_sender_hb_info_t)) {
1114 return SCTP_DISPOSITION_DISCARD;
1115 }
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001118 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001121 if (unlikely(!link)) {
1122 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001123 if (net_ratelimit())
1124 printk(KERN_WARNING
1125 "%s association %p could not find address "
1126 NIP6_FMT "\n",
1127 __FUNCTION__,
1128 asoc,
1129 NIP6(from_addr.v6.sin6_addr));
Frank Filz3f7a87d2005-06-20 13:14:57 -07001130 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001131 if (net_ratelimit())
1132 printk(KERN_WARNING
1133 "%s association %p could not find address "
1134 NIPQUAD_FMT "\n",
1135 __FUNCTION__,
1136 asoc,
1137 NIPQUAD(from_addr.v4.sin_addr.s_addr));
Frank Filz3f7a87d2005-06-20 13:14:57 -07001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return SCTP_DISPOSITION_DISCARD;
1140 }
1141
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001142 /* Validate the 64-bit random nonce. */
1143 if (hbinfo->hb_nonce != link->hb_nonce)
1144 return SCTP_DISPOSITION_DISCARD;
1145
Frank Filz52ccb8e2005-12-22 11:36:46 -08001146 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 /* Check if the timestamp looks valid. */
1149 if (time_after(hbinfo->sent_at, jiffies) ||
1150 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001151 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 "received for transport: %p\n",
1153 __FUNCTION__, link);
1154 return SCTP_DISPOSITION_DISCARD;
1155 }
1156
1157 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1158 * the HEARTBEAT should clear the error counter of the
1159 * destination transport address to which the HEARTBEAT was
1160 * sent and mark the destination transport address as active if
1161 * it is not so marked.
1162 */
1163 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1164
1165 return SCTP_DISPOSITION_CONSUME;
1166}
1167
1168/* Helper function to send out an abort for the restart
1169 * condition.
1170 */
1171static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1172 struct sctp_chunk *init,
1173 sctp_cmd_seq_t *commands)
1174{
1175 int len;
1176 struct sctp_packet *pkt;
1177 union sctp_addr_param *addrparm;
1178 struct sctp_errhdr *errhdr;
1179 struct sctp_endpoint *ep;
1180 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1181 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1182
1183 /* Build the error on the stack. We are way to malloc crazy
1184 * throughout the code today.
1185 */
1186 errhdr = (struct sctp_errhdr *)buffer;
1187 addrparm = (union sctp_addr_param *)errhdr->variable;
1188
1189 /* Copy into a parm format. */
1190 len = af->to_addr_param(ssa, addrparm);
1191 len += sizeof(sctp_errhdr_t);
1192
1193 errhdr->cause = SCTP_ERROR_RESTART;
1194 errhdr->length = htons(len);
1195
1196 /* Assign to the control socket. */
1197 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1198
1199 /* Association is NULL since this may be a restart attack and we
1200 * want to send back the attacker's vtag.
1201 */
1202 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1203
1204 if (!pkt)
1205 goto out;
1206 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1207
1208 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1209
1210 /* Discard the rest of the inbound packet. */
1211 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1212
1213out:
1214 /* Even if there is no memory, treat as a failure so
1215 * the packet will get dropped.
1216 */
1217 return 0;
1218}
1219
1220/* A restart is occurring, check to make sure no new addresses
1221 * are being added as we may be under a takeover attack.
1222 */
1223static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1224 const struct sctp_association *asoc,
1225 struct sctp_chunk *init,
1226 sctp_cmd_seq_t *commands)
1227{
1228 struct sctp_transport *new_addr, *addr;
1229 struct list_head *pos, *pos2;
1230 int found;
1231
1232 /* Implementor's Guide - Sectin 5.2.2
1233 * ...
1234 * Before responding the endpoint MUST check to see if the
1235 * unexpected INIT adds new addresses to the association. If new
1236 * addresses are added to the association, the endpoint MUST respond
1237 * with an ABORT..
1238 */
1239
1240 /* Search through all current addresses and make sure
1241 * we aren't adding any new ones.
1242 */
1243 new_addr = NULL;
1244 found = 0;
1245
1246 list_for_each(pos, &new_asoc->peer.transport_addr_list) {
1247 new_addr = list_entry(pos, struct sctp_transport, transports);
1248 found = 0;
1249 list_for_each(pos2, &asoc->peer.transport_addr_list) {
1250 addr = list_entry(pos2, struct sctp_transport,
1251 transports);
Al Viro5f242a12006-11-20 17:05:23 -08001252 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1253 &addr->ipaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 found = 1;
1255 break;
1256 }
1257 }
1258 if (!found)
1259 break;
1260 }
1261
1262 /* If a new address was added, ABORT the sender. */
1263 if (!found && new_addr) {
Al Viro7dd8a582006-11-20 17:23:25 -08001264 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
1267 /* Return success if all addresses were found. */
1268 return found;
1269}
1270
1271/* Populate the verification/tie tags based on overlapping INIT
1272 * scenario.
1273 *
1274 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1275 */
1276static void sctp_tietags_populate(struct sctp_association *new_asoc,
1277 const struct sctp_association *asoc)
1278{
1279 switch (asoc->state) {
1280
1281 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1282
1283 case SCTP_STATE_COOKIE_WAIT:
1284 new_asoc->c.my_vtag = asoc->c.my_vtag;
1285 new_asoc->c.my_ttag = asoc->c.my_vtag;
1286 new_asoc->c.peer_ttag = 0;
1287 break;
1288
1289 case SCTP_STATE_COOKIE_ECHOED:
1290 new_asoc->c.my_vtag = asoc->c.my_vtag;
1291 new_asoc->c.my_ttag = asoc->c.my_vtag;
1292 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1293 break;
1294
1295 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1296 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1297 */
1298 default:
1299 new_asoc->c.my_ttag = asoc->c.my_vtag;
1300 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1301 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* Other parameters for the endpoint SHOULD be copied from the
1305 * existing parameters of the association (e.g. number of
1306 * outbound streams) into the INIT ACK and cookie.
1307 */
1308 new_asoc->rwnd = asoc->rwnd;
1309 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1310 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1311 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1312}
1313
1314/*
1315 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1316 * handling action.
1317 *
1318 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1319 *
1320 * Returns value representing action to be taken. These action values
1321 * correspond to Action/Description values in RFC 2960, Table 2.
1322 */
1323static char sctp_tietags_compare(struct sctp_association *new_asoc,
1324 const struct sctp_association *asoc)
1325{
1326 /* In this case, the peer may have restarted. */
1327 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1328 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1329 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1330 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1331 return 'A';
1332
1333 /* Collision case B. */
1334 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1335 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1336 (0 == asoc->c.peer_vtag))) {
1337 return 'B';
1338 }
1339
1340 /* Collision case D. */
1341 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1342 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1343 return 'D';
1344
1345 /* Collision case C. */
1346 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1347 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1348 (0 == new_asoc->c.my_ttag) &&
1349 (0 == new_asoc->c.peer_ttag))
1350 return 'C';
1351
1352 /* No match to any of the special cases; discard this packet. */
1353 return 'E';
1354}
1355
1356/* Common helper routine for both duplicate and simulataneous INIT
1357 * chunk handling.
1358 */
1359static sctp_disposition_t sctp_sf_do_unexpected_init(
1360 const struct sctp_endpoint *ep,
1361 const struct sctp_association *asoc,
1362 const sctp_subtype_t type,
1363 void *arg, sctp_cmd_seq_t *commands)
1364{
1365 sctp_disposition_t retval;
1366 struct sctp_chunk *chunk = arg;
1367 struct sctp_chunk *repl;
1368 struct sctp_association *new_asoc;
1369 struct sctp_chunk *err_chunk;
1370 struct sctp_packet *packet;
1371 sctp_unrecognized_param_t *unk_param;
1372 int len;
1373
1374 /* 6.10 Bundling
1375 * An endpoint MUST NOT bundle INIT, INIT ACK or
1376 * SHUTDOWN COMPLETE with any other chunks.
1377 *
1378 * IG Section 2.11.2
1379 * Furthermore, we require that the receiver of an INIT chunk MUST
1380 * enforce these rules by silently discarding an arriving packet
1381 * with an INIT chunk that is bundled with other chunks.
1382 */
1383 if (!chunk->singleton)
1384 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1385
1386 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001387 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
1389 if (chunk->sctp_hdr->vtag != 0)
1390 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1391
1392 /* Make sure that the INIT chunk has a valid length.
1393 * In this case, we generate a protocol violation since we have
1394 * an association established.
1395 */
1396 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1397 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1398 commands);
1399 /* Grab the INIT header. */
1400 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1401
1402 /* Tag the variable length parameters. */
1403 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1404
1405 /* Verify the INIT chunk before processing it. */
1406 err_chunk = NULL;
1407 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1408 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1409 &err_chunk)) {
1410 /* This chunk contains fatal error. It is to be discarded.
1411 * Send an ABORT, with causes if there is any.
1412 */
1413 if (err_chunk) {
1414 packet = sctp_abort_pkt_new(ep, asoc, arg,
1415 (__u8 *)(err_chunk->chunk_hdr) +
1416 sizeof(sctp_chunkhdr_t),
1417 ntohs(err_chunk->chunk_hdr->length) -
1418 sizeof(sctp_chunkhdr_t));
1419
1420 if (packet) {
1421 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1422 SCTP_PACKET(packet));
1423 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1424 retval = SCTP_DISPOSITION_CONSUME;
1425 } else {
1426 retval = SCTP_DISPOSITION_NOMEM;
1427 }
1428 goto cleanup;
1429 } else {
1430 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1431 commands);
1432 }
1433 }
1434
1435 /*
1436 * Other parameters for the endpoint SHOULD be copied from the
1437 * existing parameters of the association (e.g. number of
1438 * outbound streams) into the INIT ACK and cookie.
1439 * FIXME: We are copying parameters from the endpoint not the
1440 * association.
1441 */
1442 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1443 if (!new_asoc)
1444 goto nomem;
1445
1446 /* In the outbound INIT ACK the endpoint MUST copy its current
1447 * Verification Tag and Peers Verification tag into a reserved
1448 * place (local tie-tag and per tie-tag) within the state cookie.
1449 */
1450 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1451 sctp_source(chunk),
1452 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001453 GFP_ATOMIC))
1454 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* Make sure no new addresses are being added during the
1457 * restart. Do not do this check for COOKIE-WAIT state,
1458 * since there are no peer addresses to check against.
1459 * Upon return an ABORT will have been sent if needed.
1460 */
1461 if (!sctp_state(asoc, COOKIE_WAIT)) {
1462 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1463 commands)) {
1464 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001465 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 }
1468
1469 sctp_tietags_populate(new_asoc, asoc);
1470
1471 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1472
1473 /* If there are errors need to be reported for unknown parameters,
1474 * make sure to reserve enough room in the INIT ACK for them.
1475 */
1476 len = 0;
1477 if (err_chunk) {
1478 len = ntohs(err_chunk->chunk_hdr->length) -
1479 sizeof(sctp_chunkhdr_t);
1480 }
1481
1482 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1483 goto nomem;
1484
1485 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1486 if (!repl)
1487 goto nomem;
1488
1489 /* If there are errors need to be reported for unknown parameters,
1490 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1491 * parameter.
1492 */
1493 if (err_chunk) {
1494 /* Get the "Unrecognized parameter" parameter(s) out of the
1495 * ERROR chunk generated by sctp_verify_init(). Since the
1496 * error cause code for "unknown parameter" and the
1497 * "Unrecognized parameter" type is the same, we can
1498 * construct the parameters in INIT ACK by copying the
1499 * ERROR causes over.
1500 */
1501 unk_param = (sctp_unrecognized_param_t *)
1502 ((__u8 *)(err_chunk->chunk_hdr) +
1503 sizeof(sctp_chunkhdr_t));
1504 /* Replace the cause code with the "Unrecognized parameter"
1505 * parameter type.
1506 */
1507 sctp_addto_chunk(repl, len, unk_param);
1508 }
1509
1510 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1511 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1512
1513 /*
1514 * Note: After sending out INIT ACK with the State Cookie parameter,
1515 * "Z" MUST NOT allocate any resources for this new association.
1516 * Otherwise, "Z" will be vulnerable to resource attacks.
1517 */
1518 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1519 retval = SCTP_DISPOSITION_CONSUME;
1520
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001521 return retval;
1522
1523nomem:
1524 retval = SCTP_DISPOSITION_NOMEM;
1525nomem_retval:
1526 if (new_asoc)
1527 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528cleanup:
1529 if (err_chunk)
1530 sctp_chunk_free(err_chunk);
1531 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532}
1533
1534/*
1535 * Handle simultanous INIT.
1536 * This means we started an INIT and then we got an INIT request from
1537 * our peer.
1538 *
1539 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1540 * This usually indicates an initialization collision, i.e., each
1541 * endpoint is attempting, at about the same time, to establish an
1542 * association with the other endpoint.
1543 *
1544 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1545 * endpoint MUST respond with an INIT ACK using the same parameters it
1546 * sent in its original INIT chunk (including its Verification Tag,
1547 * unchanged). These original parameters are combined with those from the
1548 * newly received INIT chunk. The endpoint shall also generate a State
1549 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1550 * INIT to calculate the State Cookie.
1551 *
1552 * After that, the endpoint MUST NOT change its state, the T1-init
1553 * timer shall be left running and the corresponding TCB MUST NOT be
1554 * destroyed. The normal procedures for handling State Cookies when
1555 * a TCB exists will resolve the duplicate INITs to a single association.
1556 *
1557 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1558 * its Tie-Tags with the Tag information of itself and its peer (see
1559 * section 5.2.2 for a description of the Tie-Tags).
1560 *
1561 * Verification Tag: Not explicit, but an INIT can not have a valid
1562 * verification tag, so we skip the check.
1563 *
1564 * Inputs
1565 * (endpoint, asoc, chunk)
1566 *
1567 * Outputs
1568 * (asoc, reply_msg, msg_up, timers, counters)
1569 *
1570 * The return value is the disposition of the chunk.
1571 */
1572sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1573 const struct sctp_association *asoc,
1574 const sctp_subtype_t type,
1575 void *arg,
1576 sctp_cmd_seq_t *commands)
1577{
1578 /* Call helper to do the real work for both simulataneous and
1579 * duplicate INIT chunk handling.
1580 */
1581 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1582}
1583
1584/*
1585 * Handle duplicated INIT messages. These are usually delayed
1586 * restransmissions.
1587 *
1588 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1589 * COOKIE-ECHOED and COOKIE-WAIT
1590 *
1591 * Unless otherwise stated, upon reception of an unexpected INIT for
1592 * this association, the endpoint shall generate an INIT ACK with a
1593 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1594 * current Verification Tag and peer's Verification Tag into a reserved
1595 * place within the state cookie. We shall refer to these locations as
1596 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1597 * containing this INIT ACK MUST carry a Verification Tag value equal to
1598 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1599 * MUST contain a new Initiation Tag (randomly generated see Section
1600 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1601 * existing parameters of the association (e.g. number of outbound
1602 * streams) into the INIT ACK and cookie.
1603 *
1604 * After sending out the INIT ACK, the endpoint shall take no further
1605 * actions, i.e., the existing association, including its current state,
1606 * and the corresponding TCB MUST NOT be changed.
1607 *
1608 * Note: Only when a TCB exists and the association is not in a COOKIE-
1609 * WAIT state are the Tie-Tags populated. For a normal association INIT
1610 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1611 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1612 * State Cookie are populated as specified in section 5.2.1.
1613 *
1614 * Verification Tag: Not specified, but an INIT has no way of knowing
1615 * what the verification tag could be, so we ignore it.
1616 *
1617 * Inputs
1618 * (endpoint, asoc, chunk)
1619 *
1620 * Outputs
1621 * (asoc, reply_msg, msg_up, timers, counters)
1622 *
1623 * The return value is the disposition of the chunk.
1624 */
1625sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1626 const struct sctp_association *asoc,
1627 const sctp_subtype_t type,
1628 void *arg,
1629 sctp_cmd_seq_t *commands)
1630{
1631 /* Call helper to do the real work for both simulataneous and
1632 * duplicate INIT chunk handling.
1633 */
1634 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1635}
1636
1637
Vlad Yasevich610ab732007-01-15 19:18:30 -08001638/*
1639 * Unexpected INIT-ACK handler.
1640 *
1641 * Section 5.2.3
1642 * If an INIT ACK received by an endpoint in any state other than the
1643 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1644 * An unexpected INIT ACK usually indicates the processing of an old or
1645 * duplicated INIT chunk.
1646*/
1647sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1648 const struct sctp_association *asoc,
1649 const sctp_subtype_t type,
1650 void *arg, sctp_cmd_seq_t *commands)
1651{
1652 /* Per the above section, we'll discard the chunk if we have an
1653 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1654 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001655 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001656 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1657 else
1658 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1659}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1662 *
1663 * Section 5.2.4
1664 * A) In this case, the peer may have restarted.
1665 */
1666static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1667 const struct sctp_association *asoc,
1668 struct sctp_chunk *chunk,
1669 sctp_cmd_seq_t *commands,
1670 struct sctp_association *new_asoc)
1671{
1672 sctp_init_chunk_t *peer_init;
1673 struct sctp_ulpevent *ev;
1674 struct sctp_chunk *repl;
1675 struct sctp_chunk *err;
1676 sctp_disposition_t disposition;
1677
1678 /* new_asoc is a brand-new association, so these are not yet
1679 * side effects--it is safe to run them here.
1680 */
1681 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1682
1683 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1684 sctp_source(chunk), peer_init,
1685 GFP_ATOMIC))
1686 goto nomem;
1687
1688 /* Make sure no new addresses are being added during the
1689 * restart. Though this is a pretty complicated attack
1690 * since you'd have to get inside the cookie.
1691 */
1692 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1693 return SCTP_DISPOSITION_CONSUME;
1694 }
1695
1696 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1697 * the peer has restarted (Action A), it MUST NOT setup a new
1698 * association but instead resend the SHUTDOWN ACK and send an ERROR
1699 * chunk with a "Cookie Received while Shutting Down" error cause to
1700 * its peer.
1701 */
1702 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1703 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1704 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1705 chunk, commands);
1706 if (SCTP_DISPOSITION_NOMEM == disposition)
1707 goto nomem;
1708
1709 err = sctp_make_op_error(asoc, chunk,
1710 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1711 NULL, 0);
1712 if (err)
1713 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1714 SCTP_CHUNK(err));
1715
1716 return SCTP_DISPOSITION_CONSUME;
1717 }
1718
1719 /* For now, fail any unsent/unacked data. Consider the optional
1720 * choice of resending of this data.
1721 */
1722 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 repl = sctp_make_cookie_ack(new_asoc, chunk);
1725 if (!repl)
1726 goto nomem;
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* Report association restart to upper layer. */
1729 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1730 new_asoc->c.sinit_num_ostreams,
1731 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001732 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (!ev)
1734 goto nomem_ev;
1735
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001736 /* Update the content of current association. */
1737 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1738 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1740 return SCTP_DISPOSITION_CONSUME;
1741
1742nomem_ev:
1743 sctp_chunk_free(repl);
1744nomem:
1745 return SCTP_DISPOSITION_NOMEM;
1746}
1747
1748/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1749 *
1750 * Section 5.2.4
1751 * B) In this case, both sides may be attempting to start an association
1752 * at about the same time but the peer endpoint started its INIT
1753 * after responding to the local endpoint's INIT
1754 */
1755/* This case represents an initialization collision. */
1756static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1757 const struct sctp_association *asoc,
1758 struct sctp_chunk *chunk,
1759 sctp_cmd_seq_t *commands,
1760 struct sctp_association *new_asoc)
1761{
1762 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 struct sctp_chunk *repl;
1764
1765 /* new_asoc is a brand-new association, so these are not yet
1766 * side effects--it is safe to run them here.
1767 */
1768 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1769 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1770 sctp_source(chunk), peer_init,
1771 GFP_ATOMIC))
1772 goto nomem;
1773
1774 /* Update the content of current association. */
1775 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1776 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1777 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1778 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1779 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1780
1781 repl = sctp_make_cookie_ack(new_asoc, chunk);
1782 if (!repl)
1783 goto nomem;
1784
1785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1786 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1787
1788 /* RFC 2960 5.1 Normal Establishment of an Association
1789 *
1790 * D) IMPLEMENTATION NOTE: An implementation may choose to
1791 * send the Communication Up notification to the SCTP user
1792 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001793 *
1794 * Sadly, this needs to be implemented as a side-effect, because
1795 * we are not guaranteed to have set the association id of the real
1796 * association and so these notifications need to be delayed until
1797 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Vlad Yasevich07d93962007-05-04 13:55:27 -07001800 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001803 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001805 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001806 *
1807 * This also needs to be done as a side effect for the same reason as
1808 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001810 if (asoc->peer.adaptation_ind)
1811 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 return SCTP_DISPOSITION_CONSUME;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815nomem:
1816 return SCTP_DISPOSITION_NOMEM;
1817}
1818
1819/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1820 *
1821 * Section 5.2.4
1822 * C) In this case, the local endpoint's cookie has arrived late.
1823 * Before it arrived, the local endpoint sent an INIT and received an
1824 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1825 * but a new tag of its own.
1826 */
1827/* This case represents an initialization collision. */
1828static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1829 const struct sctp_association *asoc,
1830 struct sctp_chunk *chunk,
1831 sctp_cmd_seq_t *commands,
1832 struct sctp_association *new_asoc)
1833{
1834 /* The cookie should be silently discarded.
1835 * The endpoint SHOULD NOT change states and should leave
1836 * any timers running.
1837 */
1838 return SCTP_DISPOSITION_DISCARD;
1839}
1840
1841/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1842 *
1843 * Section 5.2.4
1844 *
1845 * D) When both local and remote tags match the endpoint should always
1846 * enter the ESTABLISHED state, if it has not already done so.
1847 */
1848/* This case represents an initialization collision. */
1849static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1850 const struct sctp_association *asoc,
1851 struct sctp_chunk *chunk,
1852 sctp_cmd_seq_t *commands,
1853 struct sctp_association *new_asoc)
1854{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001855 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct sctp_chunk *repl;
1857
1858 /* Clarification from Implementor's Guide:
1859 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001860 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1861 * It should stop any cookie timer that may be running and send
1862 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
1864
1865 /* Don't accidentally move back into established state. */
1866 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1867 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1868 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1869 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1870 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1871 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1872 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1873 SCTP_NULL());
1874
1875 /* RFC 2960 5.1 Normal Establishment of an Association
1876 *
1877 * D) IMPLEMENTATION NOTE: An implementation may choose
1878 * to send the Communication Up notification to the
1879 * SCTP user upon reception of a valid COOKIE
1880 * ECHO chunk.
1881 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001882 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001884 asoc->c.sinit_num_ostreams,
1885 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001886 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 if (!ev)
1888 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001891 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001893 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001895 if (asoc->peer.adaptation_ind) {
1896 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001898 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 goto nomem;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
1902 }
1903 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1904
1905 repl = sctp_make_cookie_ack(new_asoc, chunk);
1906 if (!repl)
1907 goto nomem;
1908
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001909 if (ev)
1910 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1911 SCTP_ULPEVENT(ev));
1912 if (ai_ev)
1913 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1914 SCTP_ULPEVENT(ai_ev));
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1917 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1918
1919 return SCTP_DISPOSITION_CONSUME;
1920
1921nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001922 if (ai_ev)
1923 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (ev)
1925 sctp_ulpevent_free(ev);
1926 return SCTP_DISPOSITION_NOMEM;
1927}
1928
1929/*
1930 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1931 * chunk was retransmitted and then delayed in the network.
1932 *
1933 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1934 *
1935 * Verification Tag: None. Do cookie validation.
1936 *
1937 * Inputs
1938 * (endpoint, asoc, chunk)
1939 *
1940 * Outputs
1941 * (asoc, reply_msg, msg_up, timers, counters)
1942 *
1943 * The return value is the disposition of the chunk.
1944 */
1945sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1946 const struct sctp_association *asoc,
1947 const sctp_subtype_t type,
1948 void *arg,
1949 sctp_cmd_seq_t *commands)
1950{
1951 sctp_disposition_t retval;
1952 struct sctp_chunk *chunk = arg;
1953 struct sctp_association *new_asoc;
1954 int error = 0;
1955 char action;
1956 struct sctp_chunk *err_chk_p;
1957
1958 /* Make sure that the chunk has a valid length from the protocol
1959 * perspective. In this case check to make sure we have at least
1960 * enough for the chunk header. Cookie length verification is
1961 * done later.
1962 */
1963 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1964 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1965 commands);
1966
1967 /* "Decode" the chunk. We have no optional parameters so we
1968 * are in good shape.
1969 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001970 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001971 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1972 sizeof(sctp_chunkhdr_t)))
1973 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1976 * of a duplicate COOKIE ECHO match the Verification Tags of the
1977 * current association, consider the State Cookie valid even if
1978 * the lifespan is exceeded.
1979 */
1980 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1981 &err_chk_p);
1982
1983 /* FIXME:
1984 * If the re-build failed, what is the proper error path
1985 * from here?
1986 *
1987 * [We should abort the association. --piggy]
1988 */
1989 if (!new_asoc) {
1990 /* FIXME: Several errors are possible. A bad cookie should
1991 * be silently discarded, but think about logging it too.
1992 */
1993 switch (error) {
1994 case -SCTP_IERROR_NOMEM:
1995 goto nomem;
1996
1997 case -SCTP_IERROR_STALE_COOKIE:
1998 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
1999 err_chk_p);
2000 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2001 case -SCTP_IERROR_BAD_SIG:
2002 default:
2003 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006
2007 /* Compare the tie_tag in cookie with the verification tag of
2008 * current association.
2009 */
2010 action = sctp_tietags_compare(new_asoc, asoc);
2011
2012 switch (action) {
2013 case 'A': /* Association restart. */
2014 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2015 new_asoc);
2016 break;
2017
2018 case 'B': /* Collision case B. */
2019 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2020 new_asoc);
2021 break;
2022
2023 case 'C': /* Collision case C. */
2024 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2025 new_asoc);
2026 break;
2027
2028 case 'D': /* Collision case D. */
2029 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2030 new_asoc);
2031 break;
2032
2033 default: /* Discard packet for all others. */
2034 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2035 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* Delete the tempory new association. */
2039 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2040 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2041
2042 return retval;
2043
2044nomem:
2045 return SCTP_DISPOSITION_NOMEM;
2046}
2047
2048/*
2049 * Process an ABORT. (SHUTDOWN-PENDING state)
2050 *
2051 * See sctp_sf_do_9_1_abort().
2052 */
2053sctp_disposition_t sctp_sf_shutdown_pending_abort(
2054 const struct sctp_endpoint *ep,
2055 const struct sctp_association *asoc,
2056 const sctp_subtype_t type,
2057 void *arg,
2058 sctp_cmd_seq_t *commands)
2059{
2060 struct sctp_chunk *chunk = arg;
2061
2062 if (!sctp_vtag_verify_either(chunk, asoc))
2063 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2064
2065 /* Make sure that the ABORT chunk has a valid length.
2066 * Since this is an ABORT chunk, we have to discard it
2067 * because of the following text:
2068 * RFC 2960, Section 3.3.7
2069 * If an endpoint receives an ABORT with a format error or for an
2070 * association that doesn't exist, it MUST silently discard it.
2071 * Becasue the length is "invalid", we can't really discard just
2072 * as we do not know its true length. So, to be safe, discard the
2073 * packet.
2074 */
2075 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2076 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2077
Vlad Yasevich75205f42007-12-20 14:12:59 -08002078 /* ADD-IP: Special case for ABORT chunks
2079 * F4) One special consideration is that ABORT Chunks arriving
2080 * destined to the IP address being deleted MUST be
2081 * ignored (see Section 5.3.1 for further details).
2082 */
2083 if (SCTP_ADDR_DEL ==
2084 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2085 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /* Stop the T5-shutdown guard timer. */
2088 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2089 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2090
Vlad Yasevich75205f42007-12-20 14:12:59 -08002091 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094/*
2095 * Process an ABORT. (SHUTDOWN-SENT state)
2096 *
2097 * See sctp_sf_do_9_1_abort().
2098 */
2099sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2100 const struct sctp_association *asoc,
2101 const sctp_subtype_t type,
2102 void *arg,
2103 sctp_cmd_seq_t *commands)
2104{
2105 struct sctp_chunk *chunk = arg;
2106
2107 if (!sctp_vtag_verify_either(chunk, asoc))
2108 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2109
2110 /* Make sure that the ABORT chunk has a valid length.
2111 * Since this is an ABORT chunk, we have to discard it
2112 * because of the following text:
2113 * RFC 2960, Section 3.3.7
2114 * If an endpoint receives an ABORT with a format error or for an
2115 * association that doesn't exist, it MUST silently discard it.
2116 * Becasue the length is "invalid", we can't really discard just
2117 * as we do not know its true length. So, to be safe, discard the
2118 * packet.
2119 */
2120 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2121 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2122
Vlad Yasevich75205f42007-12-20 14:12:59 -08002123 /* ADD-IP: Special case for ABORT chunks
2124 * F4) One special consideration is that ABORT Chunks arriving
2125 * destined to the IP address being deleted MUST be
2126 * ignored (see Section 5.3.1 for further details).
2127 */
2128 if (SCTP_ADDR_DEL ==
2129 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2130 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* Stop the T2-shutdown timer. */
2133 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2134 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2135
2136 /* Stop the T5-shutdown guard timer. */
2137 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2138 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2139
Vlad Yasevich75205f42007-12-20 14:12:59 -08002140 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
2143/*
2144 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2145 *
2146 * See sctp_sf_do_9_1_abort().
2147 */
2148sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2149 const struct sctp_endpoint *ep,
2150 const struct sctp_association *asoc,
2151 const sctp_subtype_t type,
2152 void *arg,
2153 sctp_cmd_seq_t *commands)
2154{
2155 /* The same T2 timer, so we should be able to use
2156 * common function with the SHUTDOWN-SENT state.
2157 */
2158 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2159}
2160
2161/*
2162 * Handle an Error received in COOKIE_ECHOED state.
2163 *
2164 * Only handle the error type of stale COOKIE Error, the other errors will
2165 * be ignored.
2166 *
2167 * Inputs
2168 * (endpoint, asoc, chunk)
2169 *
2170 * Outputs
2171 * (asoc, reply_msg, msg_up, timers, counters)
2172 *
2173 * The return value is the disposition of the chunk.
2174 */
2175sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2176 const struct sctp_association *asoc,
2177 const sctp_subtype_t type,
2178 void *arg,
2179 sctp_cmd_seq_t *commands)
2180{
2181 struct sctp_chunk *chunk = arg;
2182 sctp_errhdr_t *err;
2183
2184 if (!sctp_vtag_verify(chunk, asoc))
2185 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2186
2187 /* Make sure that the ERROR chunk has a valid length.
2188 * The parameter walking depends on this as well.
2189 */
2190 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2191 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2192 commands);
2193
2194 /* Process the error here */
2195 /* FUTURE FIXME: When PR-SCTP related and other optional
2196 * parms are emitted, this will have to change to handle multiple
2197 * errors.
2198 */
2199 sctp_walk_errors(err, chunk->chunk_hdr) {
2200 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002201 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 arg, commands);
2203 }
2204
2205 /* It is possible to have malformed error causes, and that
2206 * will cause us to end the walk early. However, since
2207 * we are discarding the packet, there should be no adverse
2208 * affects.
2209 */
2210 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2211}
2212
2213/*
2214 * Handle a Stale COOKIE Error
2215 *
2216 * Section: 5.2.6 Handle Stale COOKIE Error
2217 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2218 * one of the following three alternatives.
2219 * ...
2220 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2221 * Preservative parameter requesting an extension to the lifetime of
2222 * the State Cookie. When calculating the time extension, an
2223 * implementation SHOULD use the RTT information measured based on the
2224 * previous COOKIE ECHO / ERROR exchange, and should add no more
2225 * than 1 second beyond the measured RTT, due to long State Cookie
2226 * lifetimes making the endpoint more subject to a replay attack.
2227 *
2228 * Verification Tag: Not explicit, but safe to ignore.
2229 *
2230 * Inputs
2231 * (endpoint, asoc, chunk)
2232 *
2233 * Outputs
2234 * (asoc, reply_msg, msg_up, timers, counters)
2235 *
2236 * The return value is the disposition of the chunk.
2237 */
2238static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2239 const struct sctp_association *asoc,
2240 const sctp_subtype_t type,
2241 void *arg,
2242 sctp_cmd_seq_t *commands)
2243{
2244 struct sctp_chunk *chunk = arg;
2245 time_t stale;
2246 sctp_cookie_preserve_param_t bht;
2247 sctp_errhdr_t *err;
2248 struct sctp_chunk *reply;
2249 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002250 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Vlad Yasevich81845c22006-01-30 15:59:54 -08002252 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002253 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2254 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002256 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return SCTP_DISPOSITION_DELETE_TCB;
2258 }
2259
2260 err = (sctp_errhdr_t *)(chunk->skb->data);
2261
2262 /* When calculating the time extension, an implementation
2263 * SHOULD use the RTT information measured based on the
2264 * previous COOKIE ECHO / ERROR exchange, and should add no
2265 * more than 1 second beyond the measured RTT, due to long
2266 * State Cookie lifetimes making the endpoint more subject to
2267 * a replay attack.
2268 * Measure of Staleness's unit is usec. (1/1000000 sec)
2269 * Suggested Cookie Life-span Increment's unit is msec.
2270 * (1/1000 sec)
2271 * In general, if you use the suggested cookie life, the value
2272 * found in the field of measure of staleness should be doubled
2273 * to give ample time to retransmit the new cookie and thus
2274 * yield a higher probability of success on the reattempt.
2275 */
Al Viro34bcca22006-11-20 17:27:15 -08002276 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 stale = (stale * 2) / 1000;
2278
2279 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2280 bht.param_hdr.length = htons(sizeof(bht));
2281 bht.lifespan_increment = htonl(stale);
2282
2283 /* Build that new INIT chunk. */
2284 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2285 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2286 if (!reply)
2287 goto nomem;
2288
2289 sctp_addto_chunk(reply, sizeof(bht), &bht);
2290
2291 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2292 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2293
2294 /* Stop pending T3-rtx and heartbeat timers */
2295 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2296 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2297
2298 /* Delete non-primary peer ip addresses since we are transitioning
2299 * back to the COOKIE-WAIT state
2300 */
2301 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2302
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002303 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2304 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002306 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 SCTP_TRANSPORT(asoc->peer.primary_path));
2308
2309 /* Cast away the const modifier, as we want to just
2310 * rerun it through as a sideffect.
2311 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002312 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2315 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2316 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2317 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2318 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2319 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2320
2321 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2322
2323 return SCTP_DISPOSITION_CONSUME;
2324
2325nomem:
2326 return SCTP_DISPOSITION_NOMEM;
2327}
2328
2329/*
2330 * Process an ABORT.
2331 *
2332 * Section: 9.1
2333 * After checking the Verification Tag, the receiving endpoint shall
2334 * remove the association from its record, and shall report the
2335 * termination to its upper layer.
2336 *
2337 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2338 * B) Rules for packet carrying ABORT:
2339 *
2340 * - The endpoint shall always fill in the Verification Tag field of the
2341 * outbound packet with the destination endpoint's tag value if it
2342 * is known.
2343 *
2344 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2345 * MUST follow the procedure described in Section 8.4.
2346 *
2347 * - The receiver MUST accept the packet if the Verification Tag
2348 * matches either its own tag, OR the tag of its peer. Otherwise, the
2349 * receiver MUST silently discard the packet and take no further
2350 * action.
2351 *
2352 * Inputs
2353 * (endpoint, asoc, chunk)
2354 *
2355 * Outputs
2356 * (asoc, reply_msg, msg_up, timers, counters)
2357 *
2358 * The return value is the disposition of the chunk.
2359 */
2360sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2361 const struct sctp_association *asoc,
2362 const sctp_subtype_t type,
2363 void *arg,
2364 sctp_cmd_seq_t *commands)
2365{
2366 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
2368 if (!sctp_vtag_verify_either(chunk, asoc))
2369 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2370
2371 /* Make sure that the ABORT chunk has a valid length.
2372 * Since this is an ABORT chunk, we have to discard it
2373 * because of the following text:
2374 * RFC 2960, Section 3.3.7
2375 * If an endpoint receives an ABORT with a format error or for an
2376 * association that doesn't exist, it MUST silently discard it.
2377 * Becasue the length is "invalid", we can't really discard just
2378 * as we do not know its true length. So, to be safe, discard the
2379 * packet.
2380 */
2381 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2382 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2383
Vlad Yasevich75205f42007-12-20 14:12:59 -08002384 /* ADD-IP: Special case for ABORT chunks
2385 * F4) One special consideration is that ABORT Chunks arriving
2386 * destined to the IP address being deleted MUST be
2387 * ignored (see Section 5.3.1 for further details).
2388 */
2389 if (SCTP_ADDR_DEL ==
2390 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2391 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2392
2393 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2394}
2395
2396static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2397 const struct sctp_association *asoc,
2398 const sctp_subtype_t type,
2399 void *arg,
2400 sctp_cmd_seq_t *commands)
2401{
2402 struct sctp_chunk *chunk = arg;
2403 unsigned len;
2404 __be16 error = SCTP_ERROR_NO_ERROR;
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* See if we have an error cause code in the chunk. */
2407 len = ntohs(chunk->chunk_hdr->length);
2408 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2409 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2410
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002411 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002412 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002413 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2415 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2416
2417 return SCTP_DISPOSITION_ABORT;
2418}
2419
2420/*
2421 * Process an ABORT. (COOKIE-WAIT state)
2422 *
2423 * See sctp_sf_do_9_1_abort() above.
2424 */
2425sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2426 const struct sctp_association *asoc,
2427 const sctp_subtype_t type,
2428 void *arg,
2429 sctp_cmd_seq_t *commands)
2430{
2431 struct sctp_chunk *chunk = arg;
2432 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002433 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 if (!sctp_vtag_verify_either(chunk, asoc))
2436 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2437
2438 /* Make sure that the ABORT chunk has a valid length.
2439 * Since this is an ABORT chunk, we have to discard it
2440 * because of the following text:
2441 * RFC 2960, Section 3.3.7
2442 * If an endpoint receives an ABORT with a format error or for an
2443 * association that doesn't exist, it MUST silently discard it.
2444 * Becasue the length is "invalid", we can't really discard just
2445 * as we do not know its true length. So, to be safe, discard the
2446 * packet.
2447 */
2448 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2449 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2450
2451 /* See if we have an error cause code in the chunk. */
2452 len = ntohs(chunk->chunk_hdr->length);
2453 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2454 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2455
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002456 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2457 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
2460/*
2461 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2462 */
2463sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2464 const struct sctp_association *asoc,
2465 const sctp_subtype_t type,
2466 void *arg,
2467 sctp_cmd_seq_t *commands)
2468{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002469 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2470 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002471 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
2474/*
2475 * Process an ABORT. (COOKIE-ECHOED state)
2476 */
2477sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2478 const struct sctp_association *asoc,
2479 const sctp_subtype_t type,
2480 void *arg,
2481 sctp_cmd_seq_t *commands)
2482{
2483 /* There is a single T1 timer, so we should be able to use
2484 * common function with the COOKIE-WAIT state.
2485 */
2486 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2487}
2488
2489/*
2490 * Stop T1 timer and abort association with "INIT failed".
2491 *
2492 * This is common code called by several sctp_sf_*_abort() functions above.
2493 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002494static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002495 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002496 const struct sctp_association *asoc,
2497 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002499 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2501 SCTP_STATE(SCTP_STATE_CLOSED));
2502 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2503 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2504 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002505 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 /* CMD_INIT_FAILED will DELETE_TCB. */
2507 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002508 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002509 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
2512/*
2513 * sctp_sf_do_9_2_shut
2514 *
2515 * Section: 9.2
2516 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2517 * - enter the SHUTDOWN-RECEIVED state,
2518 *
2519 * - stop accepting new data from its SCTP user
2520 *
2521 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2522 * that all its outstanding DATA chunks have been received by the
2523 * SHUTDOWN sender.
2524 *
2525 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2526 * send a SHUTDOWN in response to a ULP request. And should discard
2527 * subsequent SHUTDOWN chunks.
2528 *
2529 * If there are still outstanding DATA chunks left, the SHUTDOWN
2530 * receiver shall continue to follow normal data transmission
2531 * procedures defined in Section 6 until all outstanding DATA chunks
2532 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2533 * new data from its SCTP user.
2534 *
2535 * Verification Tag: 8.5 Verification Tag [Normal verification]
2536 *
2537 * Inputs
2538 * (endpoint, asoc, chunk)
2539 *
2540 * Outputs
2541 * (asoc, reply_msg, msg_up, timers, counters)
2542 *
2543 * The return value is the disposition of the chunk.
2544 */
2545sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2546 const struct sctp_association *asoc,
2547 const sctp_subtype_t type,
2548 void *arg,
2549 sctp_cmd_seq_t *commands)
2550{
2551 struct sctp_chunk *chunk = arg;
2552 sctp_shutdownhdr_t *sdh;
2553 sctp_disposition_t disposition;
2554 struct sctp_ulpevent *ev;
2555
2556 if (!sctp_vtag_verify(chunk, asoc))
2557 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2558
2559 /* Make sure that the SHUTDOWN chunk has a valid length. */
2560 if (!sctp_chunk_length_valid(chunk,
2561 sizeof(struct sctp_shutdown_chunk_t)))
2562 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2563 commands);
2564
2565 /* Convert the elaborate header. */
2566 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2567 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2568 chunk->subh.shutdown_hdr = sdh;
2569
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002570 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2571 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2572 * inform the application that it should cease sending data.
2573 */
2574 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2575 if (!ev) {
2576 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002577 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002578 }
2579 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2582 * - enter the SHUTDOWN-RECEIVED state,
2583 * - stop accepting new data from its SCTP user
2584 *
2585 * [This is implicit in the new state.]
2586 */
2587 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2588 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2589 disposition = SCTP_DISPOSITION_CONSUME;
2590
2591 if (sctp_outq_is_empty(&asoc->outqueue)) {
2592 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2593 arg, commands);
2594 }
2595
2596 if (SCTP_DISPOSITION_NOMEM == disposition)
2597 goto out;
2598
2599 /* - verify, by checking the Cumulative TSN Ack field of the
2600 * chunk, that all its outstanding DATA chunks have been
2601 * received by the SHUTDOWN sender.
2602 */
2603 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002604 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606out:
2607 return disposition;
2608}
2609
2610/* RFC 2960 9.2
2611 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2612 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2613 * transport addresses (either in the IP addresses or in the INIT chunk)
2614 * that belong to this association, it should discard the INIT chunk and
2615 * retransmit the SHUTDOWN ACK chunk.
2616 */
2617sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2618 const struct sctp_association *asoc,
2619 const sctp_subtype_t type,
2620 void *arg,
2621 sctp_cmd_seq_t *commands)
2622{
2623 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2624 struct sctp_chunk *reply;
2625
Vlad Yasevichece25df2007-09-07 16:30:54 -04002626 /* Make sure that the chunk has a valid length */
2627 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2628 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2629 commands);
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 /* Since we are not going to really process this INIT, there
2632 * is no point in verifying chunk boundries. Just generate
2633 * the SHUTDOWN ACK.
2634 */
2635 reply = sctp_make_shutdown_ack(asoc, chunk);
2636 if (NULL == reply)
2637 goto nomem;
2638
2639 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2640 * the T2-SHUTDOWN timer.
2641 */
2642 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2643
2644 /* and restart the T2-shutdown timer. */
2645 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2646 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2647
2648 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2649
2650 return SCTP_DISPOSITION_CONSUME;
2651nomem:
2652 return SCTP_DISPOSITION_NOMEM;
2653}
2654
2655/*
2656 * sctp_sf_do_ecn_cwr
2657 *
2658 * Section: Appendix A: Explicit Congestion Notification
2659 *
2660 * CWR:
2661 *
2662 * RFC 2481 details a specific bit for a sender to send in the header of
2663 * its next outbound TCP segment to indicate to its peer that it has
2664 * reduced its congestion window. This is termed the CWR bit. For
2665 * SCTP the same indication is made by including the CWR chunk.
2666 * This chunk contains one data element, i.e. the TSN number that
2667 * was sent in the ECNE chunk. This element represents the lowest
2668 * TSN number in the datagram that was originally marked with the
2669 * CE bit.
2670 *
2671 * Verification Tag: 8.5 Verification Tag [Normal verification]
2672 * Inputs
2673 * (endpoint, asoc, chunk)
2674 *
2675 * Outputs
2676 * (asoc, reply_msg, msg_up, timers, counters)
2677 *
2678 * The return value is the disposition of the chunk.
2679 */
2680sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2681 const struct sctp_association *asoc,
2682 const sctp_subtype_t type,
2683 void *arg,
2684 sctp_cmd_seq_t *commands)
2685{
2686 sctp_cwrhdr_t *cwr;
2687 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002688 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 if (!sctp_vtag_verify(chunk, asoc))
2691 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2692
2693 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2694 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2695 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2698 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2699
Al Viro34bcca22006-11-20 17:27:15 -08002700 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002703 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* Stop sending ECNE. */
2705 sctp_add_cmd_sf(commands,
2706 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002707 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709 return SCTP_DISPOSITION_CONSUME;
2710}
2711
2712/*
2713 * sctp_sf_do_ecne
2714 *
2715 * Section: Appendix A: Explicit Congestion Notification
2716 *
2717 * ECN-Echo
2718 *
2719 * RFC 2481 details a specific bit for a receiver to send back in its
2720 * TCP acknowledgements to notify the sender of the Congestion
2721 * Experienced (CE) bit having arrived from the network. For SCTP this
2722 * same indication is made by including the ECNE chunk. This chunk
2723 * contains one data element, i.e. the lowest TSN associated with the IP
2724 * datagram marked with the CE bit.....
2725 *
2726 * Verification Tag: 8.5 Verification Tag [Normal verification]
2727 * Inputs
2728 * (endpoint, asoc, chunk)
2729 *
2730 * Outputs
2731 * (asoc, reply_msg, msg_up, timers, counters)
2732 *
2733 * The return value is the disposition of the chunk.
2734 */
2735sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2736 const struct sctp_association *asoc,
2737 const sctp_subtype_t type,
2738 void *arg,
2739 sctp_cmd_seq_t *commands)
2740{
2741 sctp_ecnehdr_t *ecne;
2742 struct sctp_chunk *chunk = arg;
2743
2744 if (!sctp_vtag_verify(chunk, asoc))
2745 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2746
2747 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2748 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2749 commands);
2750
2751 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2752 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2753
2754 /* If this is a newer ECNE than the last CWR packet we sent out */
2755 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2756 SCTP_U32(ntohl(ecne->lowest_tsn)));
2757
2758 return SCTP_DISPOSITION_CONSUME;
2759}
2760
2761/*
2762 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2763 *
2764 * The SCTP endpoint MUST always acknowledge the reception of each valid
2765 * DATA chunk.
2766 *
2767 * The guidelines on delayed acknowledgement algorithm specified in
2768 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2769 * acknowledgement SHOULD be generated for at least every second packet
2770 * (not every second DATA chunk) received, and SHOULD be generated within
2771 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2772 * situations it may be beneficial for an SCTP transmitter to be more
2773 * conservative than the algorithms detailed in this document allow.
2774 * However, an SCTP transmitter MUST NOT be more aggressive than the
2775 * following algorithms allow.
2776 *
2777 * A SCTP receiver MUST NOT generate more than one SACK for every
2778 * incoming packet, other than to update the offered window as the
2779 * receiving application consumes new data.
2780 *
2781 * Verification Tag: 8.5 Verification Tag [Normal verification]
2782 *
2783 * Inputs
2784 * (endpoint, asoc, chunk)
2785 *
2786 * Outputs
2787 * (asoc, reply_msg, msg_up, timers, counters)
2788 *
2789 * The return value is the disposition of the chunk.
2790 */
2791sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2792 const struct sctp_association *asoc,
2793 const sctp_subtype_t type,
2794 void *arg,
2795 sctp_cmd_seq_t *commands)
2796{
2797 struct sctp_chunk *chunk = arg;
2798 int error;
2799
2800 if (!sctp_vtag_verify(chunk, asoc)) {
2801 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2802 SCTP_NULL());
2803 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2807 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2808 commands);
2809
2810 error = sctp_eat_data(asoc, chunk, commands );
2811 switch (error) {
2812 case SCTP_IERROR_NO_ERROR:
2813 break;
2814 case SCTP_IERROR_HIGH_TSN:
2815 case SCTP_IERROR_BAD_STREAM:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002816 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 goto discard_noforce;
2818 case SCTP_IERROR_DUP_TSN:
2819 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002820 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 goto discard_force;
2822 case SCTP_IERROR_NO_DATA:
2823 goto consume;
2824 default:
2825 BUG();
2826 }
2827
2828 if (asoc->autoclose) {
2829 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2830 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2831 }
2832
2833 /* If this is the last chunk in a packet, we need to count it
2834 * toward sack generation. Note that we need to SACK every
2835 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2836 * THEM. We elect to NOT generate SACK's if the chunk fails
2837 * the verification tag test.
2838 *
2839 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2840 *
2841 * The SCTP endpoint MUST always acknowledge the reception of
2842 * each valid DATA chunk.
2843 *
2844 * The guidelines on delayed acknowledgement algorithm
2845 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2846 * Specifically, an acknowledgement SHOULD be generated for at
2847 * least every second packet (not every second DATA chunk)
2848 * received, and SHOULD be generated within 200 ms of the
2849 * arrival of any unacknowledged DATA chunk. In some
2850 * situations it may be beneficial for an SCTP transmitter to
2851 * be more conservative than the algorithms detailed in this
2852 * document allow. However, an SCTP transmitter MUST NOT be
2853 * more aggressive than the following algorithms allow.
2854 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002855 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2857
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return SCTP_DISPOSITION_CONSUME;
2859
2860discard_force:
2861 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2862 *
2863 * When a packet arrives with duplicate DATA chunk(s) and with
2864 * no new DATA chunk(s), the endpoint MUST immediately send a
2865 * SACK with no delay. If a packet arrives with duplicate
2866 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2867 * MAY immediately send a SACK. Normally receipt of duplicate
2868 * DATA chunks will occur when the original SACK chunk was lost
2869 * and the peer's RTO has expired. The duplicate TSN number(s)
2870 * SHOULD be reported in the SACK as duplicate.
2871 */
2872 /* In our case, we split the MAY SACK advice up whether or not
2873 * the last chunk is a duplicate.'
2874 */
2875 if (chunk->end_of_packet)
2876 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2877 return SCTP_DISPOSITION_DISCARD;
2878
2879discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002880 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return SCTP_DISPOSITION_DISCARD;
2884consume:
2885 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887}
2888
2889/*
2890 * sctp_sf_eat_data_fast_4_4
2891 *
2892 * Section: 4 (4)
2893 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2894 * DATA chunks without delay.
2895 *
2896 * Verification Tag: 8.5 Verification Tag [Normal verification]
2897 * Inputs
2898 * (endpoint, asoc, chunk)
2899 *
2900 * Outputs
2901 * (asoc, reply_msg, msg_up, timers, counters)
2902 *
2903 * The return value is the disposition of the chunk.
2904 */
2905sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2906 const struct sctp_association *asoc,
2907 const sctp_subtype_t type,
2908 void *arg,
2909 sctp_cmd_seq_t *commands)
2910{
2911 struct sctp_chunk *chunk = arg;
2912 int error;
2913
2914 if (!sctp_vtag_verify(chunk, asoc)) {
2915 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2916 SCTP_NULL());
2917 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2918 }
2919
2920 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2921 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2922 commands);
2923
2924 error = sctp_eat_data(asoc, chunk, commands );
2925 switch (error) {
2926 case SCTP_IERROR_NO_ERROR:
2927 case SCTP_IERROR_HIGH_TSN:
2928 case SCTP_IERROR_DUP_TSN:
2929 case SCTP_IERROR_IGNORE_TSN:
2930 case SCTP_IERROR_BAD_STREAM:
2931 break;
2932 case SCTP_IERROR_NO_DATA:
2933 goto consume;
2934 default:
2935 BUG();
2936 }
2937
2938 /* Go a head and force a SACK, since we are shutting down. */
2939
2940 /* Implementor's Guide.
2941 *
2942 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
2943 * respond to each received packet containing one or more DATA chunk(s)
2944 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
2945 */
2946 if (chunk->end_of_packet) {
2947 /* We must delay the chunk creation since the cumulative
2948 * TSN has not been updated yet.
2949 */
2950 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
2951 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2952 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2953 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2954 }
2955
2956consume:
2957 return SCTP_DISPOSITION_CONSUME;
2958}
2959
2960/*
2961 * Section: 6.2 Processing a Received SACK
2962 * D) Any time a SACK arrives, the endpoint performs the following:
2963 *
2964 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
2965 * then drop the SACK. Since Cumulative TSN Ack is monotonically
2966 * increasing, a SACK whose Cumulative TSN Ack is less than the
2967 * Cumulative TSN Ack Point indicates an out-of-order SACK.
2968 *
2969 * ii) Set rwnd equal to the newly received a_rwnd minus the number
2970 * of bytes still outstanding after processing the Cumulative TSN Ack
2971 * and the Gap Ack Blocks.
2972 *
2973 * iii) If the SACK is missing a TSN that was previously
2974 * acknowledged via a Gap Ack Block (e.g., the data receiver
2975 * reneged on the data), then mark the corresponding DATA chunk
2976 * as available for retransmit: Mark it as missing for fast
2977 * retransmit as described in Section 7.2.4 and if no retransmit
2978 * timer is running for the destination address to which the DATA
2979 * chunk was originally transmitted, then T3-rtx is started for
2980 * that destination address.
2981 *
2982 * Verification Tag: 8.5 Verification Tag [Normal verification]
2983 *
2984 * Inputs
2985 * (endpoint, asoc, chunk)
2986 *
2987 * Outputs
2988 * (asoc, reply_msg, msg_up, timers, counters)
2989 *
2990 * The return value is the disposition of the chunk.
2991 */
2992sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
2993 const struct sctp_association *asoc,
2994 const sctp_subtype_t type,
2995 void *arg,
2996 sctp_cmd_seq_t *commands)
2997{
2998 struct sctp_chunk *chunk = arg;
2999 sctp_sackhdr_t *sackh;
3000 __u32 ctsn;
3001
3002 if (!sctp_vtag_verify(chunk, asoc))
3003 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3004
3005 /* Make sure that the SACK chunk has a valid length. */
3006 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3007 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3008 commands);
3009
3010 /* Pull the SACK chunk from the data buffer */
3011 sackh = sctp_sm_pull_sack(chunk);
3012 /* Was this a bogus SACK? */
3013 if (!sackh)
3014 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3015 chunk->subh.sack_hdr = sackh;
3016 ctsn = ntohl(sackh->cum_tsn_ack);
3017
3018 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3019 * Ack Point, then drop the SACK. Since Cumulative TSN
3020 * Ack is monotonically increasing, a SACK whose
3021 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3022 * Point indicates an out-of-order SACK.
3023 */
3024 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3025 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3026 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3027 return SCTP_DISPOSITION_DISCARD;
3028 }
3029
Wei Yongjunaecedea2007-08-02 16:57:44 +08003030 /* If Cumulative TSN Ack beyond the max tsn currently
3031 * send, terminating the association and respond to the
3032 * sender with an ABORT.
3033 */
3034 if (!TSN_lt(ctsn, asoc->next_tsn))
3035 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 /* Return this SACK for further processing. */
3038 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3039
3040 /* Note: We do the rest of the work on the PROCESS_SACK
3041 * sideeffect.
3042 */
3043 return SCTP_DISPOSITION_CONSUME;
3044}
3045
3046/*
3047 * Generate an ABORT in response to a packet.
3048 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003049 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003051 * 8) The receiver should respond to the sender of the OOTB packet with
3052 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3053 * MUST fill in the Verification Tag field of the outbound packet
3054 * with the value found in the Verification Tag field of the OOTB
3055 * packet and set the T-bit in the Chunk Flags to indicate that the
3056 * Verification Tag is reflected. After sending this ABORT, the
3057 * receiver of the OOTB packet shall discard the OOTB packet and take
3058 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 *
3060 * Verification Tag:
3061 *
3062 * The return value is the disposition of the chunk.
3063*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003064static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 const struct sctp_association *asoc,
3066 const sctp_subtype_t type,
3067 void *arg,
3068 sctp_cmd_seq_t *commands)
3069{
3070 struct sctp_packet *packet = NULL;
3071 struct sctp_chunk *chunk = arg;
3072 struct sctp_chunk *abort;
3073
3074 packet = sctp_ootb_pkt_new(asoc, chunk);
3075
3076 if (packet) {
3077 /* Make an ABORT. The T bit will be set if the asoc
3078 * is NULL.
3079 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003080 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 if (!abort) {
3082 sctp_ootb_pkt_free(packet);
3083 return SCTP_DISPOSITION_NOMEM;
3084 }
3085
Jerome Forissier047a2422005-04-28 11:58:43 -07003086 /* Reflect vtag if T-Bit is set */
3087 if (sctp_test_T_bit(abort))
3088 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 /* Set the skb to the belonging sock for accounting. */
3091 abort->skb->sk = ep->base.sk;
3092
3093 sctp_packet_append_chunk(packet, abort);
3094
3095 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3096 SCTP_PACKET(packet));
3097
3098 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3099
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003100 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 return SCTP_DISPOSITION_CONSUME;
3102 }
3103
3104 return SCTP_DISPOSITION_NOMEM;
3105}
3106
3107/*
3108 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3109 * event as ULP notification for each cause included in the chunk.
3110 *
3111 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3112 *
3113 * The return value is the disposition of the chunk.
3114*/
3115sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3116 const struct sctp_association *asoc,
3117 const sctp_subtype_t type,
3118 void *arg,
3119 sctp_cmd_seq_t *commands)
3120{
3121 struct sctp_chunk *chunk = arg;
3122 struct sctp_ulpevent *ev;
3123
3124 if (!sctp_vtag_verify(chunk, asoc))
3125 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3126
3127 /* Make sure that the ERROR chunk has a valid length. */
3128 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3129 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3130 commands);
3131
3132 while (chunk->chunk_end > chunk->skb->data) {
3133 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
3134 GFP_ATOMIC);
3135 if (!ev)
3136 goto nomem;
3137
3138 if (!sctp_add_cmd(commands, SCTP_CMD_EVENT_ULP,
3139 SCTP_ULPEVENT(ev))) {
3140 sctp_ulpevent_free(ev);
3141 goto nomem;
3142 }
3143
3144 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003145 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
3147 return SCTP_DISPOSITION_CONSUME;
3148
3149nomem:
3150 return SCTP_DISPOSITION_NOMEM;
3151}
3152
3153/*
3154 * Process an inbound SHUTDOWN ACK.
3155 *
3156 * From Section 9.2:
3157 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3158 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3159 * peer, and remove all record of the association.
3160 *
3161 * The return value is the disposition.
3162 */
3163sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3164 const struct sctp_association *asoc,
3165 const sctp_subtype_t type,
3166 void *arg,
3167 sctp_cmd_seq_t *commands)
3168{
3169 struct sctp_chunk *chunk = arg;
3170 struct sctp_chunk *reply;
3171 struct sctp_ulpevent *ev;
3172
3173 if (!sctp_vtag_verify(chunk, asoc))
3174 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3175
3176 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3177 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3178 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3179 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 /* 10.2 H) SHUTDOWN COMPLETE notification
3181 *
3182 * When SCTP completes the shutdown procedures (section 9.2) this
3183 * notification is passed to the upper layer.
3184 */
3185 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003186 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (!ev)
3188 goto nomem;
3189
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003190 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3191 reply = sctp_make_shutdown_complete(asoc, chunk);
3192 if (!reply)
3193 goto nomem_chunk;
3194
3195 /* Do all the commands now (after allocation), so that we
3196 * have consistent state if memory allocation failes
3197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3199
3200 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3201 * stop the T2-shutdown timer,
3202 */
3203 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3204 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3205
3206 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3207 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3210 SCTP_STATE(SCTP_STATE_CLOSED));
3211 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3212 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3213 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3214
3215 /* ...and remove all record of the association. */
3216 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3217 return SCTP_DISPOSITION_DELETE_TCB;
3218
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003219nomem_chunk:
3220 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221nomem:
3222 return SCTP_DISPOSITION_NOMEM;
3223}
3224
3225/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003226 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3227 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3229 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3230 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3231 * packet must fill in the Verification Tag field of the outbound
3232 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003233 * set the T-bit in the Chunk Flags to indicate that the Verification
3234 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 *
3236 * 8) The receiver should respond to the sender of the OOTB packet with
3237 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3238 * MUST fill in the Verification Tag field of the outbound packet
3239 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003240 * packet and set the T-bit in the Chunk Flags to indicate that the
3241 * Verification Tag is reflected. After sending this ABORT, the
3242 * receiver of the OOTB packet shall discard the OOTB packet and take
3243 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 */
3245sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3246 const struct sctp_association *asoc,
3247 const sctp_subtype_t type,
3248 void *arg,
3249 sctp_cmd_seq_t *commands)
3250{
3251 struct sctp_chunk *chunk = arg;
3252 struct sk_buff *skb = chunk->skb;
3253 sctp_chunkhdr_t *ch;
3254 __u8 *ch_end;
3255 int ootb_shut_ack = 0;
3256
3257 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3258
3259 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3260 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003261 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003263 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3264 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Vlad Yasevichece25df2007-09-07 16:30:54 -04003266 /* Now that we know we at least have a chunk header,
3267 * do things that are type appropriate.
3268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3270 ootb_shut_ack = 1;
3271
3272 /* RFC 2960, Section 3.3.7
3273 * Moreover, under any circumstances, an endpoint that
3274 * receives an ABORT MUST NOT respond to that ABORT by
3275 * sending an ABORT of its own.
3276 */
3277 if (SCTP_CID_ABORT == ch->type)
3278 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003279
Vlad Yasevichece25df2007-09-07 16:30:54 -04003280 /* Report violation if chunk len overflows */
3281 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3282 if (ch_end > skb_tail_pointer(skb))
3283 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3284 commands);
3285
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003287 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003290 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003292 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293}
3294
3295/*
3296 * Handle an "Out of the blue" SHUTDOWN ACK.
3297 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003298 * Section: 8.4 5, sctpimpguide 2.41.
3299 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003301 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3302 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3303 * packet must fill in the Verification Tag field of the outbound
3304 * packet with the Verification Tag received in the SHUTDOWN ACK and
3305 * set the T-bit in the Chunk Flags to indicate that the Verification
3306 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 *
3308 * Inputs
3309 * (endpoint, asoc, type, arg, commands)
3310 *
3311 * Outputs
3312 * (sctp_disposition_t)
3313 *
3314 * The return value is the disposition of the chunk.
3315 */
3316static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3317 const struct sctp_association *asoc,
3318 const sctp_subtype_t type,
3319 void *arg,
3320 sctp_cmd_seq_t *commands)
3321{
3322 struct sctp_packet *packet = NULL;
3323 struct sctp_chunk *chunk = arg;
3324 struct sctp_chunk *shut;
3325
3326 packet = sctp_ootb_pkt_new(asoc, chunk);
3327
3328 if (packet) {
3329 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003330 * The T bit will be set if the asoc is NULL.
3331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 shut = sctp_make_shutdown_complete(asoc, chunk);
3333 if (!shut) {
3334 sctp_ootb_pkt_free(packet);
3335 return SCTP_DISPOSITION_NOMEM;
3336 }
3337
Jerome Forissier047a2422005-04-28 11:58:43 -07003338 /* Reflect vtag if T-Bit is set */
3339 if (sctp_test_T_bit(shut))
3340 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 /* Set the skb to the belonging sock for accounting. */
3343 shut->skb->sk = ep->base.sk;
3344
3345 sctp_packet_append_chunk(packet, shut);
3346
3347 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3348 SCTP_PACKET(packet));
3349
3350 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3351
3352 /* If the chunk length is invalid, we don't want to process
3353 * the reset of the packet.
3354 */
3355 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3356 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3357
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003358 /* We need to discard the rest of the packet to prevent
3359 * potential bomming attacks from additional bundled chunks.
3360 * This is documented in SCTP Threats ID.
3361 */
3362 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
3364
3365 return SCTP_DISPOSITION_NOMEM;
3366}
3367
3368/*
3369 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3370 *
3371 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3372 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3373 * procedures in section 8.4 SHOULD be followed, in other words it
3374 * should be treated as an Out Of The Blue packet.
3375 * [This means that we do NOT check the Verification Tag on these
3376 * chunks. --piggy ]
3377 *
3378 */
3379sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3380 const struct sctp_association *asoc,
3381 const sctp_subtype_t type,
3382 void *arg,
3383 sctp_cmd_seq_t *commands)
3384{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003385 struct sctp_chunk *chunk = arg;
3386
3387 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3388 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3389 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3390 commands);
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /* Although we do have an association in this case, it corresponds
3393 * to a restarted association. So the packet is treated as an OOTB
3394 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3395 * called with a NULL association.
3396 */
3397 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3398}
3399
3400/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3401sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3402 const struct sctp_association *asoc,
3403 const sctp_subtype_t type, void *arg,
3404 sctp_cmd_seq_t *commands)
3405{
3406 struct sctp_chunk *chunk = arg;
3407 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003408 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003410 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003412 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 if (!sctp_vtag_verify(chunk, asoc)) {
3415 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3416 SCTP_NULL());
3417 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3418 }
3419
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003420 /* ADD-IP: Section 4.1.1
3421 * This chunk MUST be sent in an authenticated way by using
3422 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3423 * is received unauthenticated it MUST be silently discarded as
3424 * described in [I-D.ietf-tsvwg-sctp-auth].
3425 */
3426 if (!sctp_addip_noauth && !chunk->auth)
3427 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3430 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3431 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3432 commands);
3433
3434 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3435 serial = ntohl(hdr->serial);
3436
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003437 addr_param = (union sctp_addr_param *)hdr->params;
3438 length = ntohs(addr_param->p.length);
3439 if (length < sizeof(sctp_paramhdr_t))
3440 return sctp_sf_violation_paramlen(ep, asoc, type,
3441 (void *)addr_param, commands);
3442
3443 /* Verify the ASCONF chunk before processing it. */
3444 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003445 (sctp_paramhdr_t *)((void *)addr_param + length),
3446 (void *)chunk->chunk_end,
3447 &err_param))
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003448 return sctp_sf_violation_paramlen(ep, asoc, type,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003449 (void *)&err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003450
Vlad Yasevicha08de642007-12-20 14:11:47 -08003451 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003453 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 */
3455 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003456 /* If this is the first instance of ASCONF in the packet,
3457 * we can clean our old ASCONF-ACKs.
3458 */
3459 if (!chunk->has_asconf)
3460 sctp_assoc_clean_asconf_ack_cache(asoc);
3461
3462 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3463 * expected, process the ASCONF as described below and after
3464 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3465 * the response packet and cache a copy of it (in the event it
3466 * later needs to be retransmitted).
3467 *
3468 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 */
3470 asconf_ack = sctp_process_asconf((struct sctp_association *)
3471 asoc, chunk);
3472 if (!asconf_ack)
3473 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003474 } else if (serial < asoc->peer.addip_serial + 1) {
3475 /* ADDIP 5.2 E2)
3476 * If the value found in the Sequence Number is less than the
3477 * ('Peer- Sequence-Number' + 1), simply skip to the next
3478 * ASCONF, and include in the outbound response packet
3479 * any previously cached ASCONF-ACK response that was
3480 * sent and saved that matches the Sequence Number of the
3481 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3482 * Chunk exists. This will occur when an older ASCONF
3483 * arrives out of order. In such a case, the receiver
3484 * should skip the ASCONF Chunk and not include ASCONF-ACK
3485 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003487 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3488 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 return SCTP_DISPOSITION_DISCARD;
3490 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003491 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 return SCTP_DISPOSITION_DISCARD;
3495 }
3496
Vlad Yasevicha08de642007-12-20 14:11:47 -08003497 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3498 * containing the ASCONF-ACK Chunks MUST be the source address of
3499 * the SCTP packet that held the ASCONF Chunks.
3500 *
3501 * To do this properly, we'll set the destination address of the chunk
3502 * and at the transmit time, will try look up the transport to use.
3503 * Since ASCONFs may be bundled, the correct transport may not be
3504 * created untill we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003506 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 return SCTP_DISPOSITION_CONSUME;
3510}
3511
3512/*
3513 * ADDIP Section 4.3 General rules for address manipulation
3514 * When building TLV parameters for the ASCONF Chunk that will add or
3515 * delete IP addresses the D0 to D13 rules should be applied:
3516 */
3517sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3518 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003519 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 sctp_cmd_seq_t *commands)
3521{
3522 struct sctp_chunk *asconf_ack = arg;
3523 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3524 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003525 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 sctp_addiphdr_t *addip_hdr;
3527 __u32 sent_serial, rcvd_serial;
3528
3529 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3530 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3531 SCTP_NULL());
3532 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3533 }
3534
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003535 /* ADD-IP, Section 4.1.2:
3536 * This chunk MUST be sent in an authenticated way by using
3537 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3538 * is received unauthenticated it MUST be silently discarded as
3539 * described in [I-D.ietf-tsvwg-sctp-auth].
3540 */
3541 if (!sctp_addip_noauth && !asconf_ack->auth)
3542 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 /* Make sure that the ADDIP chunk has a valid length. */
3545 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3546 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3547 commands);
3548
3549 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3550 rcvd_serial = ntohl(addip_hdr->serial);
3551
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003552 /* Verify the ASCONF-ACK chunk before processing it. */
3553 if (!sctp_verify_asconf(asoc,
3554 (sctp_paramhdr_t *)addip_hdr->params,
3555 (void *)asconf_ack->chunk_end,
3556 &err_param))
3557 return sctp_sf_violation_paramlen(ep, asoc, type,
3558 (void *)&err_param, commands);
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 if (last_asconf) {
3561 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3562 sent_serial = ntohl(addip_hdr->serial);
3563 } else {
3564 sent_serial = asoc->addip_serial - 1;
3565 }
3566
3567 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3568 * equal to the next serial number to be used but no ASCONF chunk is
3569 * outstanding the endpoint MUST ABORT the association. Note that a
3570 * sequence number is greater than if it is no more than 2^^31-1
3571 * larger than the current sequence number (using serial arithmetic).
3572 */
3573 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3574 !(asoc->addip_last_asconf)) {
3575 abort = sctp_make_abort(asoc, asconf_ack,
3576 sizeof(sctp_errhdr_t));
3577 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003578 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3580 SCTP_CHUNK(abort));
3581 }
3582 /* We are going to ABORT, so we might as well stop
3583 * processing the rest of the chunks in the packet.
3584 */
3585 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3586 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3587 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003588 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003589 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003591 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3593 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3594 return SCTP_DISPOSITION_ABORT;
3595 }
3596
3597 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3598 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3599 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3600
3601 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3602 asconf_ack))
3603 return SCTP_DISPOSITION_CONSUME;
3604
3605 abort = sctp_make_abort(asoc, asconf_ack,
3606 sizeof(sctp_errhdr_t));
3607 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003608 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3610 SCTP_CHUNK(abort));
3611 }
3612 /* We are going to ABORT, so we might as well stop
3613 * processing the rest of the chunks in the packet.
3614 */
3615 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003616 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003617 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003619 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3621 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3622 return SCTP_DISPOSITION_ABORT;
3623 }
3624
3625 return SCTP_DISPOSITION_DISCARD;
3626}
3627
3628/*
3629 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3630 *
3631 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3632 * its cumulative TSN point to the value carried in the FORWARD TSN
3633 * chunk, and then MUST further advance its cumulative TSN point locally
3634 * if possible.
3635 * After the above processing, the data receiver MUST stop reporting any
3636 * missing TSNs earlier than or equal to the new cumulative TSN point.
3637 *
3638 * Verification Tag: 8.5 Verification Tag [Normal verification]
3639 *
3640 * The return value is the disposition of the chunk.
3641 */
3642sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3643 const struct sctp_association *asoc,
3644 const sctp_subtype_t type,
3645 void *arg,
3646 sctp_cmd_seq_t *commands)
3647{
3648 struct sctp_chunk *chunk = arg;
3649 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3650 __u16 len;
3651 __u32 tsn;
3652
3653 if (!sctp_vtag_verify(chunk, asoc)) {
3654 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3655 SCTP_NULL());
3656 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3657 }
3658
3659 /* Make sure that the FORWARD_TSN chunk has valid length. */
3660 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3661 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3662 commands);
3663
3664 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3665 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3666 len = ntohs(chunk->chunk_hdr->length);
3667 len -= sizeof(struct sctp_chunkhdr);
3668 skb_pull(chunk->skb, len);
3669
3670 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3671 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
3672
3673 /* The TSN is too high--silently discard the chunk and count on it
3674 * getting retransmitted later.
3675 */
3676 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3677 goto discard_noforce;
3678
3679 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3680 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003681 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /* Count this as receiving DATA. */
3685 if (asoc->autoclose) {
3686 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3687 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3688 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003691 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 */
3693 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694
3695 return SCTP_DISPOSITION_CONSUME;
3696
3697discard_noforce:
3698 return SCTP_DISPOSITION_DISCARD;
3699}
3700
3701sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3702 const struct sctp_endpoint *ep,
3703 const struct sctp_association *asoc,
3704 const sctp_subtype_t type,
3705 void *arg,
3706 sctp_cmd_seq_t *commands)
3707{
3708 struct sctp_chunk *chunk = arg;
3709 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3710 __u16 len;
3711 __u32 tsn;
3712
3713 if (!sctp_vtag_verify(chunk, asoc)) {
3714 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3715 SCTP_NULL());
3716 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3717 }
3718
3719 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3720 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3721 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3722 commands);
3723
3724 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3725 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3726 len = ntohs(chunk->chunk_hdr->length);
3727 len -= sizeof(struct sctp_chunkhdr);
3728 skb_pull(chunk->skb, len);
3729
3730 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3731 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
3732
3733 /* The TSN is too high--silently discard the chunk and count on it
3734 * getting retransmitted later.
3735 */
3736 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3737 goto gen_shutdown;
3738
3739 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3740 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003741 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 /* Go a head and force a SACK, since we are shutting down. */
3745gen_shutdown:
3746 /* Implementor's Guide.
3747 *
3748 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3749 * respond to each received packet containing one or more DATA chunk(s)
3750 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3751 */
3752 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3753 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3754 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3755 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3756
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003757 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758}
3759
3760/*
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003761 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3762 *
3763 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3764 * Identifier field. If this algorithm was not specified by the
3765 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3766 * during association setup, the AUTH chunk and all chunks after it MUST
3767 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3768 * defined in Section 4.1.
3769 *
3770 * If an endpoint with no shared key receives a Shared Key Identifier
3771 * other than 0, it MUST silently discard all authenticated chunks. If
3772 * the endpoint has at least one endpoint pair shared key for the peer,
3773 * it MUST use the key specified by the Shared Key Identifier if a
3774 * key has been configured for that Shared Key Identifier. If no
3775 * endpoint pair shared key has been configured for that Shared Key
3776 * Identifier, all authenticated chunks MUST be silently discarded.
3777 *
3778 * Verification Tag: 8.5 Verification Tag [Normal verification]
3779 *
3780 * The return value is the disposition of the chunk.
3781 */
3782static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3783 const struct sctp_association *asoc,
3784 const sctp_subtype_t type,
3785 struct sctp_chunk *chunk)
3786{
3787 struct sctp_authhdr *auth_hdr;
3788 struct sctp_hmac *hmac;
3789 unsigned int sig_len;
3790 __u16 key_id;
3791 __u8 *save_digest;
3792 __u8 *digest;
3793
3794 /* Pull in the auth header, so we can do some more verification */
3795 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3796 chunk->subh.auth_hdr = auth_hdr;
3797 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3798
3799 /* Make sure that we suport the HMAC algorithm from the auth
3800 * chunk.
3801 */
3802 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3803 return SCTP_IERROR_AUTH_BAD_HMAC;
3804
3805 /* Make sure that the provided shared key identifier has been
3806 * configured
3807 */
3808 key_id = ntohs(auth_hdr->shkey_id);
3809 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3810 return SCTP_IERROR_AUTH_BAD_KEYID;
3811
3812
3813 /* Make sure that the length of the signature matches what
3814 * we expect.
3815 */
3816 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3817 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3818 if (sig_len != hmac->hmac_len)
3819 return SCTP_IERROR_PROTO_VIOLATION;
3820
3821 /* Now that we've done validation checks, we can compute and
3822 * verify the hmac. The steps involved are:
3823 * 1. Save the digest from the chunk.
3824 * 2. Zero out the digest in the chunk.
3825 * 3. Compute the new digest
3826 * 4. Compare saved and new digests.
3827 */
3828 digest = auth_hdr->hmac;
3829 skb_pull(chunk->skb, sig_len);
3830
3831 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3832 if (!save_digest)
3833 goto nomem;
3834
3835 memset(digest, 0, sig_len);
3836
3837 sctp_auth_calculate_hmac(asoc, chunk->skb,
3838 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3839 GFP_ATOMIC);
3840
3841 /* Discard the packet if the digests do not match */
3842 if (memcmp(save_digest, digest, sig_len)) {
3843 kfree(save_digest);
3844 return SCTP_IERROR_BAD_SIG;
3845 }
3846
3847 kfree(save_digest);
3848 chunk->auth = 1;
3849
3850 return SCTP_IERROR_NO_ERROR;
3851nomem:
3852 return SCTP_IERROR_NOMEM;
3853}
3854
3855sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3856 const struct sctp_association *asoc,
3857 const sctp_subtype_t type,
3858 void *arg,
3859 sctp_cmd_seq_t *commands)
3860{
3861 struct sctp_authhdr *auth_hdr;
3862 struct sctp_chunk *chunk = arg;
3863 struct sctp_chunk *err_chunk;
3864 sctp_ierror_t error;
3865
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003866 /* Make sure that the peer has AUTH capable */
3867 if (!asoc->peer.auth_capable)
3868 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3869
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003870 if (!sctp_vtag_verify(chunk, asoc)) {
3871 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3872 SCTP_NULL());
3873 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3874 }
3875
3876 /* Make sure that the AUTH chunk has valid length. */
3877 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3878 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3879 commands);
3880
3881 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3882 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3883 switch (error) {
3884 case SCTP_IERROR_AUTH_BAD_HMAC:
3885 /* Generate the ERROR chunk and discard the rest
3886 * of the packet
3887 */
3888 err_chunk = sctp_make_op_error(asoc, chunk,
3889 SCTP_ERROR_UNSUP_HMAC,
3890 &auth_hdr->hmac_id,
3891 sizeof(__u16));
3892 if (err_chunk) {
3893 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3894 SCTP_CHUNK(err_chunk));
3895 }
3896 /* Fall Through */
3897 case SCTP_IERROR_AUTH_BAD_KEYID:
3898 case SCTP_IERROR_BAD_SIG:
3899 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3900 break;
3901 case SCTP_IERROR_PROTO_VIOLATION:
3902 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3903 commands);
3904 break;
3905 case SCTP_IERROR_NOMEM:
3906 return SCTP_DISPOSITION_NOMEM;
3907 default:
3908 break;
3909 }
3910
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003911 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
3912 struct sctp_ulpevent *ev;
3913
3914 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
3915 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
3916
3917 if (!ev)
3918 return -ENOMEM;
3919
3920 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3921 SCTP_ULPEVENT(ev));
3922 }
3923
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003924 return SCTP_DISPOSITION_CONSUME;
3925}
3926
3927/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 * Process an unknown chunk.
3929 *
3930 * Section: 3.2. Also, 2.1 in the implementor's guide.
3931 *
3932 * Chunk Types are encoded such that the highest-order two bits specify
3933 * the action that must be taken if the processing endpoint does not
3934 * recognize the Chunk Type.
3935 *
3936 * 00 - Stop processing this SCTP packet and discard it, do not process
3937 * any further chunks within it.
3938 *
3939 * 01 - Stop processing this SCTP packet and discard it, do not process
3940 * any further chunks within it, and report the unrecognized
3941 * chunk in an 'Unrecognized Chunk Type'.
3942 *
3943 * 10 - Skip this chunk and continue processing.
3944 *
3945 * 11 - Skip this chunk and continue processing, but report in an ERROR
3946 * Chunk using the 'Unrecognized Chunk Type' cause of error.
3947 *
3948 * The return value is the disposition of the chunk.
3949 */
3950sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
3951 const struct sctp_association *asoc,
3952 const sctp_subtype_t type,
3953 void *arg,
3954 sctp_cmd_seq_t *commands)
3955{
3956 struct sctp_chunk *unk_chunk = arg;
3957 struct sctp_chunk *err_chunk;
3958 sctp_chunkhdr_t *hdr;
3959
3960 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
3961
3962 if (!sctp_vtag_verify(unk_chunk, asoc))
3963 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3964
3965 /* Make sure that the chunk has a valid length.
3966 * Since we don't know the chunk type, we use a general
3967 * chunkhdr structure to make a comparison.
3968 */
3969 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
3970 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3971 commands);
3972
3973 switch (type.chunk & SCTP_CID_ACTION_MASK) {
3974 case SCTP_CID_ACTION_DISCARD:
3975 /* Discard the packet. */
3976 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3977 break;
3978 case SCTP_CID_ACTION_DISCARD_ERR:
3979 /* Discard the packet. */
3980 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3981
3982 /* Generate an ERROR chunk as response. */
3983 hdr = unk_chunk->chunk_hdr;
3984 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3985 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3986 WORD_ROUND(ntohs(hdr->length)));
3987 if (err_chunk) {
3988 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3989 SCTP_CHUNK(err_chunk));
3990 }
3991 return SCTP_DISPOSITION_CONSUME;
3992 break;
3993 case SCTP_CID_ACTION_SKIP:
3994 /* Skip the chunk. */
3995 return SCTP_DISPOSITION_DISCARD;
3996 break;
3997 case SCTP_CID_ACTION_SKIP_ERR:
3998 /* Generate an ERROR chunk as response. */
3999 hdr = unk_chunk->chunk_hdr;
4000 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4001 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4002 WORD_ROUND(ntohs(hdr->length)));
4003 if (err_chunk) {
4004 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4005 SCTP_CHUNK(err_chunk));
4006 }
4007 /* Skip the chunk. */
4008 return SCTP_DISPOSITION_CONSUME;
4009 break;
4010 default:
4011 break;
4012 }
4013
4014 return SCTP_DISPOSITION_DISCARD;
4015}
4016
4017/*
4018 * Discard the chunk.
4019 *
4020 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4021 * [Too numerous to mention...]
4022 * Verification Tag: No verification needed.
4023 * Inputs
4024 * (endpoint, asoc, chunk)
4025 *
4026 * Outputs
4027 * (asoc, reply_msg, msg_up, timers, counters)
4028 *
4029 * The return value is the disposition of the chunk.
4030 */
4031sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4032 const struct sctp_association *asoc,
4033 const sctp_subtype_t type,
4034 void *arg,
4035 sctp_cmd_seq_t *commands)
4036{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004037 struct sctp_chunk *chunk = arg;
4038
4039 /* Make sure that the chunk has a valid length.
4040 * Since we don't know the chunk type, we use a general
4041 * chunkhdr structure to make a comparison.
4042 */
4043 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4044 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4045 commands);
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4048 return SCTP_DISPOSITION_DISCARD;
4049}
4050
4051/*
4052 * Discard the whole packet.
4053 *
4054 * Section: 8.4 2)
4055 *
4056 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4057 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 *
4059 * Verification Tag: No verification necessary
4060 *
4061 * Inputs
4062 * (endpoint, asoc, chunk)
4063 *
4064 * Outputs
4065 * (asoc, reply_msg, msg_up, timers, counters)
4066 *
4067 * The return value is the disposition of the chunk.
4068 */
4069sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4070 const struct sctp_association *asoc,
4071 const sctp_subtype_t type,
4072 void *arg,
4073 sctp_cmd_seq_t *commands)
4074{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004075 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4077
4078 return SCTP_DISPOSITION_CONSUME;
4079}
4080
4081
4082/*
4083 * The other end is violating protocol.
4084 *
4085 * Section: Not specified
4086 * Verification Tag: Not specified
4087 * Inputs
4088 * (endpoint, asoc, chunk)
4089 *
4090 * Outputs
4091 * (asoc, reply_msg, msg_up, timers, counters)
4092 *
4093 * We simply tag the chunk as a violation. The state machine will log
4094 * the violation and continue.
4095 */
4096sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4097 const struct sctp_association *asoc,
4098 const sctp_subtype_t type,
4099 void *arg,
4100 sctp_cmd_seq_t *commands)
4101{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004102 struct sctp_chunk *chunk = arg;
4103
4104 /* Make sure that the chunk has a valid length. */
4105 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4106 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4107 commands);
4108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 return SCTP_DISPOSITION_VIOLATION;
4110}
4111
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004113 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004115static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004116 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004119 sctp_cmd_seq_t *commands,
4120 const __u8 *payload,
4121 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004123 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct sctp_chunk *chunk = arg;
4125 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004127 /* SCTP-AUTH, Section 6.3:
4128 * It should be noted that if the receiver wants to tear
4129 * down an association in an authenticated way only, the
4130 * handling of malformed packets should not result in
4131 * tearing down the association.
4132 *
4133 * This means that if we only want to abort associations
4134 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +02004135 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004136 * was malformed.
4137 */
4138 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4139 goto discard;
4140
Jesper Juhl9abed242007-11-11 23:57:49 +01004141 /* Make the abort chunk. */
4142 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4143 if (!abort)
4144 goto nomem;
4145
Vlad Yasevichece25df2007-09-07 16:30:54 -04004146 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004147 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4148 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4149 !asoc->peer.i.init_tag) {
4150 sctp_initack_chunk_t *initack;
4151
4152 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4153 if (!sctp_chunk_length_valid(chunk,
4154 sizeof(sctp_initack_chunk_t)))
4155 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4156 else {
4157 unsigned int inittag;
4158
4159 inittag = ntohl(initack->init_hdr.init_tag);
4160 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4161 SCTP_U32(inittag));
4162 }
4163 }
4164
Vlad Yasevichece25df2007-09-07 16:30:54 -04004165 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4166 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Vlad Yasevichece25df2007-09-07 16:30:54 -04004168 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4169 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4170 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4171 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4172 SCTP_ERROR(ECONNREFUSED));
4173 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4174 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4175 } else {
4176 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4177 SCTP_ERROR(ECONNABORTED));
4178 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4179 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4180 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004183 packet = sctp_ootb_pkt_new(asoc, chunk);
4184
4185 if (!packet)
4186 goto nomem_pkt;
4187
4188 if (sctp_test_T_bit(abort))
4189 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4190
4191 abort->skb->sk = ep->base.sk;
4192
4193 sctp_packet_append_chunk(packet, abort);
4194
4195 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4196 SCTP_PACKET(packet));
4197
4198 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
4200
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004201discard:
Vlad Yasevichece25df2007-09-07 16:30:54 -04004202 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
4204 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004205
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return SCTP_DISPOSITION_ABORT;
4207
Vlad Yasevichece25df2007-09-07 16:30:54 -04004208nomem_pkt:
4209 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210nomem:
4211 return SCTP_DISPOSITION_NOMEM;
4212}
4213
Wei Yongjunaecedea2007-08-02 16:57:44 +08004214/*
4215 * Handle a protocol violation when the chunk length is invalid.
4216 * "Invalid" length is identified as smaller then the minimal length a
4217 * given chunk can be. For example, a SACK chunk has invalid length
4218 * if it's length is set to be smaller then the size of sctp_sack_chunk_t.
4219 *
4220 * We inform the other end by sending an ABORT with a Protocol Violation
4221 * error code.
4222 *
4223 * Section: Not specified
4224 * Verification Tag: Nothing to do
4225 * Inputs
4226 * (endpoint, asoc, chunk)
4227 *
4228 * Outputs
4229 * (reply_msg, msg_up, counters)
4230 *
4231 * Generate an ABORT chunk and terminate the association.
4232 */
4233static sctp_disposition_t sctp_sf_violation_chunklen(
4234 const struct sctp_endpoint *ep,
4235 const struct sctp_association *asoc,
4236 const sctp_subtype_t type,
4237 void *arg,
4238 sctp_cmd_seq_t *commands)
4239{
4240 char err_str[]="The following chunk had invalid length:";
4241
Vlad Yasevichece25df2007-09-07 16:30:54 -04004242 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004243 sizeof(err_str));
4244}
4245
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004246/*
4247 * Handle a protocol violation when the parameter length is invalid.
4248 * "Invalid" length is identified as smaller then the minimal length a
4249 * given parameter can be.
4250 */
4251static sctp_disposition_t sctp_sf_violation_paramlen(
4252 const struct sctp_endpoint *ep,
4253 const struct sctp_association *asoc,
4254 const sctp_subtype_t type,
4255 void *arg,
4256 sctp_cmd_seq_t *commands) {
4257 char err_str[] = "The following parameter had invalid length:";
4258
4259 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4260 sizeof(err_str));
4261}
4262
Wei Yongjunaecedea2007-08-02 16:57:44 +08004263/* Handle a protocol violation when the peer trying to advance the
4264 * cumulative tsn ack to a point beyond the max tsn currently sent.
4265 *
4266 * We inform the other end by sending an ABORT with a Protocol Violation
4267 * error code.
4268 */
4269static sctp_disposition_t sctp_sf_violation_ctsn(
4270 const struct sctp_endpoint *ep,
4271 const struct sctp_association *asoc,
4272 const sctp_subtype_t type,
4273 void *arg,
4274 sctp_cmd_seq_t *commands)
4275{
4276 char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4277
Vlad Yasevichece25df2007-09-07 16:30:54 -04004278 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004279 sizeof(err_str));
4280}
4281
Vlad Yasevichece25df2007-09-07 16:30:54 -04004282/* Handle protocol violation of an invalid chunk bundling. For example,
4283 * when we have an association and we recieve bundled INIT-ACK, or
4284 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4285 * statement from the specs. Additinally, there might be an attacker
4286 * on the path and we may not want to continue this communication.
4287 */
4288static sctp_disposition_t sctp_sf_violation_chunk(
4289 const struct sctp_endpoint *ep,
4290 const struct sctp_association *asoc,
4291 const sctp_subtype_t type,
4292 void *arg,
4293 sctp_cmd_seq_t *commands)
4294{
4295 char err_str[]="The following chunk violates protocol:";
4296
4297 if (!asoc)
4298 return sctp_sf_violation(ep, asoc, type, arg, commands);
4299
4300 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4301 sizeof(err_str));
4302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303/***************************************************************************
4304 * These are the state functions for handling primitive (Section 10) events.
4305 ***************************************************************************/
4306/*
4307 * sctp_sf_do_prm_asoc
4308 *
4309 * Section: 10.1 ULP-to-SCTP
4310 * B) Associate
4311 *
4312 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4313 * outbound stream count)
4314 * -> association id [,destination transport addr list] [,outbound stream
4315 * count]
4316 *
4317 * This primitive allows the upper layer to initiate an association to a
4318 * specific peer endpoint.
4319 *
4320 * The peer endpoint shall be specified by one of the transport addresses
4321 * which defines the endpoint (see Section 1.4). If the local SCTP
4322 * instance has not been initialized, the ASSOCIATE is considered an
4323 * error.
4324 * [This is not relevant for the kernel implementation since we do all
4325 * initialization at boot time. It we hadn't initialized we wouldn't
4326 * get anywhere near this code.]
4327 *
4328 * An association id, which is a local handle to the SCTP association,
4329 * will be returned on successful establishment of the association. If
4330 * SCTP is not able to open an SCTP association with the peer endpoint,
4331 * an error is returned.
4332 * [In the kernel implementation, the struct sctp_association needs to
4333 * be created BEFORE causing this primitive to run.]
4334 *
4335 * Other association parameters may be returned, including the
4336 * complete destination transport addresses of the peer as well as the
4337 * outbound stream count of the local endpoint. One of the transport
4338 * address from the returned destination addresses will be selected by
4339 * the local endpoint as default primary path for sending SCTP packets
4340 * to this peer. The returned "destination transport addr list" can
4341 * be used by the ULP to change the default primary path or to force
4342 * sending a packet to a specific transport address. [All of this
4343 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4344 * function.]
4345 *
4346 * Mandatory attributes:
4347 *
4348 * o local SCTP instance name - obtained from the INITIALIZE operation.
4349 * [This is the argument asoc.]
4350 * o destination transport addr - specified as one of the transport
4351 * addresses of the peer endpoint with which the association is to be
4352 * established.
4353 * [This is asoc->peer.active_path.]
4354 * o outbound stream count - the number of outbound streams the ULP
4355 * would like to open towards this peer endpoint.
4356 * [BUG: This is not currently implemented.]
4357 * Optional attributes:
4358 *
4359 * None.
4360 *
4361 * The return value is a disposition.
4362 */
4363sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4364 const struct sctp_association *asoc,
4365 const sctp_subtype_t type,
4366 void *arg,
4367 sctp_cmd_seq_t *commands)
4368{
4369 struct sctp_chunk *repl;
4370
4371 /* The comment below says that we enter COOKIE-WAIT AFTER
4372 * sending the INIT, but that doesn't actually work in our
4373 * implementation...
4374 */
4375 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4376 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4377
4378 /* RFC 2960 5.1 Normal Establishment of an Association
4379 *
4380 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4381 * must provide its Verification Tag (Tag_A) in the Initiate
4382 * Tag field. Tag_A SHOULD be a random number in the range of
4383 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4384 */
4385
4386 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4387 if (!repl)
4388 goto nomem;
4389
4390 /* Cast away the const modifier, as we want to just
4391 * rerun it through as a sideffect.
4392 */
4393 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC,
4394 SCTP_ASOC((struct sctp_association *) asoc));
4395
Frank Filz3f7a87d2005-06-20 13:14:57 -07004396 /* Choose transport for INIT. */
4397 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4398 SCTP_CHUNK(repl));
4399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 /* After sending the INIT, "A" starts the T1-init timer and
4401 * enters the COOKIE-WAIT state.
4402 */
4403 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4404 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4405 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4406 return SCTP_DISPOSITION_CONSUME;
4407
4408nomem:
4409 return SCTP_DISPOSITION_NOMEM;
4410}
4411
4412/*
4413 * Process the SEND primitive.
4414 *
4415 * Section: 10.1 ULP-to-SCTP
4416 * E) Send
4417 *
4418 * Format: SEND(association id, buffer address, byte count [,context]
4419 * [,stream id] [,life time] [,destination transport address]
4420 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4421 * -> result
4422 *
4423 * This is the main method to send user data via SCTP.
4424 *
4425 * Mandatory attributes:
4426 *
4427 * o association id - local handle to the SCTP association
4428 *
4429 * o buffer address - the location where the user message to be
4430 * transmitted is stored;
4431 *
4432 * o byte count - The size of the user data in number of bytes;
4433 *
4434 * Optional attributes:
4435 *
4436 * o context - an optional 32 bit integer that will be carried in the
4437 * sending failure notification to the ULP if the transportation of
4438 * this User Message fails.
4439 *
4440 * o stream id - to indicate which stream to send the data on. If not
4441 * specified, stream 0 will be used.
4442 *
4443 * o life time - specifies the life time of the user data. The user data
4444 * will not be sent by SCTP after the life time expires. This
4445 * parameter can be used to avoid efforts to transmit stale
4446 * user messages. SCTP notifies the ULP if the data cannot be
4447 * initiated to transport (i.e. sent to the destination via SCTP's
4448 * send primitive) within the life time variable. However, the
4449 * user data will be transmitted if SCTP has attempted to transmit a
4450 * chunk before the life time expired.
4451 *
4452 * o destination transport address - specified as one of the destination
4453 * transport addresses of the peer endpoint to which this packet
4454 * should be sent. Whenever possible, SCTP should use this destination
4455 * transport address for sending the packets, instead of the current
4456 * primary path.
4457 *
4458 * o unorder flag - this flag, if present, indicates that the user
4459 * would like the data delivered in an unordered fashion to the peer
4460 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4461 * message).
4462 *
4463 * o no-bundle flag - instructs SCTP not to bundle this user data with
4464 * other outbound DATA chunks. SCTP MAY still bundle even when
4465 * this flag is present, when faced with network congestion.
4466 *
4467 * o payload protocol-id - A 32 bit unsigned integer that is to be
4468 * passed to the peer indicating the type of payload protocol data
4469 * being transmitted. This value is passed as opaque data by SCTP.
4470 *
4471 * The return value is the disposition.
4472 */
4473sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4474 const struct sctp_association *asoc,
4475 const sctp_subtype_t type,
4476 void *arg,
4477 sctp_cmd_seq_t *commands)
4478{
4479 struct sctp_chunk *chunk = arg;
4480
4481 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
4482 return SCTP_DISPOSITION_CONSUME;
4483}
4484
4485/*
4486 * Process the SHUTDOWN primitive.
4487 *
4488 * Section: 10.1:
4489 * C) Shutdown
4490 *
4491 * Format: SHUTDOWN(association id)
4492 * -> result
4493 *
4494 * Gracefully closes an association. Any locally queued user data
4495 * will be delivered to the peer. The association will be terminated only
4496 * after the peer acknowledges all the SCTP packets sent. A success code
4497 * will be returned on successful termination of the association. If
4498 * attempting to terminate the association results in a failure, an error
4499 * code shall be returned.
4500 *
4501 * Mandatory attributes:
4502 *
4503 * o association id - local handle to the SCTP association
4504 *
4505 * Optional attributes:
4506 *
4507 * None.
4508 *
4509 * The return value is the disposition.
4510 */
4511sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4512 const struct sctp_endpoint *ep,
4513 const struct sctp_association *asoc,
4514 const sctp_subtype_t type,
4515 void *arg,
4516 sctp_cmd_seq_t *commands)
4517{
4518 int disposition;
4519
4520 /* From 9.2 Shutdown of an Association
4521 * Upon receipt of the SHUTDOWN primitive from its upper
4522 * layer, the endpoint enters SHUTDOWN-PENDING state and
4523 * remains there until all outstanding data has been
4524 * acknowledged by its peer. The endpoint accepts no new data
4525 * from its upper layer, but retransmits data to the far end
4526 * if necessary to fill gaps.
4527 */
4528 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4529 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4530
4531 /* sctpimpguide-05 Section 2.12.2
4532 * The sender of the SHUTDOWN MAY also start an overall guard timer
4533 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4534 */
4535 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4536 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4537
4538 disposition = SCTP_DISPOSITION_CONSUME;
4539 if (sctp_outq_is_empty(&asoc->outqueue)) {
4540 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4541 arg, commands);
4542 }
4543 return disposition;
4544}
4545
4546/*
4547 * Process the ABORT primitive.
4548 *
4549 * Section: 10.1:
4550 * C) Abort
4551 *
4552 * Format: Abort(association id [, cause code])
4553 * -> result
4554 *
4555 * Ungracefully closes an association. Any locally queued user data
4556 * will be discarded and an ABORT chunk is sent to the peer. A success code
4557 * will be returned on successful abortion of the association. If
4558 * attempting to abort the association results in a failure, an error
4559 * code shall be returned.
4560 *
4561 * Mandatory attributes:
4562 *
4563 * o association id - local handle to the SCTP association
4564 *
4565 * Optional attributes:
4566 *
4567 * o cause code - reason of the abort to be passed to the peer
4568 *
4569 * None.
4570 *
4571 * The return value is the disposition.
4572 */
4573sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4574 const struct sctp_endpoint *ep,
4575 const struct sctp_association *asoc,
4576 const sctp_subtype_t type,
4577 void *arg,
4578 sctp_cmd_seq_t *commands)
4579{
4580 /* From 9.1 Abort of an Association
4581 * Upon receipt of the ABORT primitive from its upper
4582 * layer, the endpoint enters CLOSED state and
4583 * discard all outstanding data has been
4584 * acknowledged by its peer. The endpoint accepts no new data
4585 * from its upper layer, but retransmits data to the far end
4586 * if necessary to fill gaps.
4587 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004588 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 sctp_disposition_t retval;
4590
4591 retval = SCTP_DISPOSITION_CONSUME;
4592
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004593 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
4595 /* Even if we can't send the ABORT due to low memory delete the
4596 * TCB. This is a departure from our typical NOMEM handling.
4597 */
4598
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004599 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4600 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 /* Delete the established association. */
4602 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004603 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4606 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4607
4608 return retval;
4609}
4610
4611/* We tried an illegal operation on an association which is closed. */
4612sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4613 const struct sctp_association *asoc,
4614 const sctp_subtype_t type,
4615 void *arg,
4616 sctp_cmd_seq_t *commands)
4617{
4618 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4619 return SCTP_DISPOSITION_CONSUME;
4620}
4621
4622/* We tried an illegal operation on an association which is shutting
4623 * down.
4624 */
4625sctp_disposition_t sctp_sf_error_shutdown(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 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4632 SCTP_ERROR(-ESHUTDOWN));
4633 return SCTP_DISPOSITION_CONSUME;
4634}
4635
4636/*
4637 * sctp_cookie_wait_prm_shutdown
4638 *
4639 * Section: 4 Note: 2
4640 * Verification Tag:
4641 * Inputs
4642 * (endpoint, asoc)
4643 *
4644 * The RFC does not explicitly address this issue, but is the route through the
4645 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4646 *
4647 * Outputs
4648 * (timers)
4649 */
4650sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4651 const struct sctp_endpoint *ep,
4652 const struct sctp_association *asoc,
4653 const sctp_subtype_t type,
4654 void *arg,
4655 sctp_cmd_seq_t *commands)
4656{
4657 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4658 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4659
4660 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4661 SCTP_STATE(SCTP_STATE_CLOSED));
4662
4663 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4664
4665 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4666
4667 return SCTP_DISPOSITION_DELETE_TCB;
4668}
4669
4670/*
4671 * sctp_cookie_echoed_prm_shutdown
4672 *
4673 * Section: 4 Note: 2
4674 * Verification Tag:
4675 * Inputs
4676 * (endpoint, asoc)
4677 *
4678 * The RFC does not explcitly address this issue, but is the route through the
4679 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4680 *
4681 * Outputs
4682 * (timers)
4683 */
4684sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4685 const struct sctp_endpoint *ep,
4686 const struct sctp_association *asoc,
4687 const sctp_subtype_t type,
4688 void *arg, sctp_cmd_seq_t *commands)
4689{
4690 /* There is a single T1 timer, so we should be able to use
4691 * common function with the COOKIE-WAIT state.
4692 */
4693 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4694}
4695
4696/*
4697 * sctp_sf_cookie_wait_prm_abort
4698 *
4699 * Section: 4 Note: 2
4700 * Verification Tag:
4701 * Inputs
4702 * (endpoint, asoc)
4703 *
4704 * The RFC does not explicitly address this issue, but is the route through the
4705 * state table when someone issues an abort while in COOKIE_WAIT state.
4706 *
4707 * Outputs
4708 * (timers)
4709 */
4710sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4711 const struct sctp_endpoint *ep,
4712 const struct sctp_association *asoc,
4713 const sctp_subtype_t type,
4714 void *arg,
4715 sctp_cmd_seq_t *commands)
4716{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004717 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 sctp_disposition_t retval;
4719
4720 /* Stop T1-init timer */
4721 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4722 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4723 retval = SCTP_DISPOSITION_CONSUME;
4724
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004725 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4728 SCTP_STATE(SCTP_STATE_CLOSED));
4729
4730 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4731
4732 /* Even if we can't send the ABORT due to low memory delete the
4733 * TCB. This is a departure from our typical NOMEM handling.
4734 */
4735
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004736 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4737 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 /* Delete the established association. */
4739 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004740 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 return retval;
4743}
4744
4745/*
4746 * sctp_sf_cookie_echoed_prm_abort
4747 *
4748 * Section: 4 Note: 3
4749 * Verification Tag:
4750 * Inputs
4751 * (endpoint, asoc)
4752 *
4753 * The RFC does not explcitly address this issue, but is the route through the
4754 * state table when someone issues an abort while in COOKIE_ECHOED state.
4755 *
4756 * Outputs
4757 * (timers)
4758 */
4759sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4760 const struct sctp_endpoint *ep,
4761 const struct sctp_association *asoc,
4762 const sctp_subtype_t type,
4763 void *arg,
4764 sctp_cmd_seq_t *commands)
4765{
4766 /* There is a single T1 timer, so we should be able to use
4767 * common function with the COOKIE-WAIT state.
4768 */
4769 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4770}
4771
4772/*
4773 * sctp_sf_shutdown_pending_prm_abort
4774 *
4775 * Inputs
4776 * (endpoint, asoc)
4777 *
4778 * The RFC does not explicitly address this issue, but is the route through the
4779 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4780 *
4781 * Outputs
4782 * (timers)
4783 */
4784sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4785 const struct sctp_endpoint *ep,
4786 const struct sctp_association *asoc,
4787 const sctp_subtype_t type,
4788 void *arg,
4789 sctp_cmd_seq_t *commands)
4790{
4791 /* Stop the T5-shutdown guard timer. */
4792 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4793 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4794
4795 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4796}
4797
4798/*
4799 * sctp_sf_shutdown_sent_prm_abort
4800 *
4801 * Inputs
4802 * (endpoint, asoc)
4803 *
4804 * The RFC does not explicitly address this issue, but is the route through the
4805 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4806 *
4807 * Outputs
4808 * (timers)
4809 */
4810sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4811 const struct sctp_endpoint *ep,
4812 const struct sctp_association *asoc,
4813 const sctp_subtype_t type,
4814 void *arg,
4815 sctp_cmd_seq_t *commands)
4816{
4817 /* Stop the T2-shutdown timer. */
4818 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4819 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4820
4821 /* Stop the T5-shutdown guard timer. */
4822 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4823 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4824
4825 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4826}
4827
4828/*
4829 * sctp_sf_cookie_echoed_prm_abort
4830 *
4831 * Inputs
4832 * (endpoint, asoc)
4833 *
4834 * The RFC does not explcitly address this issue, but is the route through the
4835 * state table when someone issues an abort while in COOKIE_ECHOED state.
4836 *
4837 * Outputs
4838 * (timers)
4839 */
4840sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4841 const struct sctp_endpoint *ep,
4842 const struct sctp_association *asoc,
4843 const sctp_subtype_t type,
4844 void *arg,
4845 sctp_cmd_seq_t *commands)
4846{
4847 /* The same T2 timer, so we should be able to use
4848 * common function with the SHUTDOWN-SENT state.
4849 */
4850 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4851}
4852
4853/*
4854 * Process the REQUESTHEARTBEAT primitive
4855 *
4856 * 10.1 ULP-to-SCTP
4857 * J) Request Heartbeat
4858 *
4859 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4860 *
4861 * -> result
4862 *
4863 * Instructs the local endpoint to perform a HeartBeat on the specified
4864 * destination transport address of the given association. The returned
4865 * result should indicate whether the transmission of the HEARTBEAT
4866 * chunk to the destination address is successful.
4867 *
4868 * Mandatory attributes:
4869 *
4870 * o association id - local handle to the SCTP association
4871 *
4872 * o destination transport address - the transport address of the
4873 * association on which a heartbeat should be issued.
4874 */
4875sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4876 const struct sctp_endpoint *ep,
4877 const struct sctp_association *asoc,
4878 const sctp_subtype_t type,
4879 void *arg,
4880 sctp_cmd_seq_t *commands)
4881{
Vlad Yasevichfb785252007-03-19 17:02:03 -07004882 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4883 (struct sctp_transport *)arg, commands))
4884 return SCTP_DISPOSITION_NOMEM;
4885
4886 /*
4887 * RFC 2960 (bis), section 8.3
4888 *
4889 * D) Request an on-demand HEARTBEAT on a specific destination
4890 * transport address of a given association.
4891 *
4892 * The endpoint should increment the respective error counter of
4893 * the destination transport address each time a HEARTBEAT is sent
4894 * to that address and not acknowledged within one RTO.
4895 *
4896 */
4897 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
4898 SCTP_TRANSPORT(arg));
4899 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900}
4901
4902/*
4903 * ADDIP Section 4.1 ASCONF Chunk Procedures
4904 * When an endpoint has an ASCONF signaled change to be sent to the
4905 * remote endpoint it should do A1 to A9
4906 */
4907sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4908 const struct sctp_association *asoc,
4909 const sctp_subtype_t type,
4910 void *arg,
4911 sctp_cmd_seq_t *commands)
4912{
4913 struct sctp_chunk *chunk = arg;
4914
4915 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
4916 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4917 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4918 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
4919 return SCTP_DISPOSITION_CONSUME;
4920}
4921
4922/*
4923 * Ignore the primitive event
4924 *
4925 * The return value is the disposition of the primitive.
4926 */
4927sctp_disposition_t sctp_sf_ignore_primitive(
4928 const struct sctp_endpoint *ep,
4929 const struct sctp_association *asoc,
4930 const sctp_subtype_t type,
4931 void *arg,
4932 sctp_cmd_seq_t *commands)
4933{
4934 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
4935 return SCTP_DISPOSITION_DISCARD;
4936}
4937
4938/***************************************************************************
4939 * These are the state functions for the OTHER events.
4940 ***************************************************************************/
4941
4942/*
4943 * Start the shutdown negotiation.
4944 *
4945 * From Section 9.2:
4946 * Once all its outstanding data has been acknowledged, the endpoint
4947 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
4948 * TSN Ack field the last sequential TSN it has received from the peer.
4949 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
4950 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
4951 * with the updated last sequential TSN received from its peer.
4952 *
4953 * The return value is the disposition.
4954 */
4955sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
4956 const struct sctp_endpoint *ep,
4957 const struct sctp_association *asoc,
4958 const sctp_subtype_t type,
4959 void *arg,
4960 sctp_cmd_seq_t *commands)
4961{
4962 struct sctp_chunk *reply;
4963
4964 /* Once all its outstanding data has been acknowledged, the
4965 * endpoint shall send a SHUTDOWN chunk to its peer including
4966 * in the Cumulative TSN Ack field the last sequential TSN it
4967 * has received from the peer.
4968 */
4969 reply = sctp_make_shutdown(asoc, NULL);
4970 if (!reply)
4971 goto nomem;
4972
4973 /* Set the transport for the SHUTDOWN chunk and the timeout for the
4974 * T2-shutdown timer.
4975 */
4976 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4977
4978 /* It shall then start the T2-shutdown timer */
4979 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4980 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4981
4982 if (asoc->autoclose)
4983 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4984 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4985
4986 /* and enter the SHUTDOWN-SENT state. */
4987 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4988 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
4989
4990 /* sctp-implguide 2.10 Issues with Heartbeating and failover
4991 *
4992 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004993 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 */
4995 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
4996
4997 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4998
4999 return SCTP_DISPOSITION_CONSUME;
5000
5001nomem:
5002 return SCTP_DISPOSITION_NOMEM;
5003}
5004
5005/*
5006 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5007 *
5008 * From Section 9.2:
5009 *
5010 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5011 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5012 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5013 * endpoint must re-send the SHUTDOWN ACK.
5014 *
5015 * The return value is the disposition.
5016 */
5017sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5018 const struct sctp_endpoint *ep,
5019 const struct sctp_association *asoc,
5020 const sctp_subtype_t type,
5021 void *arg,
5022 sctp_cmd_seq_t *commands)
5023{
5024 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5025 struct sctp_chunk *reply;
5026
5027 /* There are 2 ways of getting here:
5028 * 1) called in response to a SHUTDOWN chunk
5029 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5030 *
5031 * For the case (2), the arg parameter is set to NULL. We need
5032 * to check that we have a chunk before accessing it's fields.
5033 */
5034 if (chunk) {
5035 if (!sctp_vtag_verify(chunk, asoc))
5036 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5037
5038 /* Make sure that the SHUTDOWN chunk has a valid length. */
5039 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5040 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5041 commands);
5042 }
5043
5044 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5045 * shall send a SHUTDOWN ACK ...
5046 */
5047 reply = sctp_make_shutdown_ack(asoc, chunk);
5048 if (!reply)
5049 goto nomem;
5050
5051 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5052 * the T2-shutdown timer.
5053 */
5054 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5055
5056 /* and start/restart a T2-shutdown timer of its own, */
5057 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5058 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5059
5060 if (asoc->autoclose)
5061 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5062 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5063
5064 /* Enter the SHUTDOWN-ACK-SENT state. */
5065 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5066 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5067
5068 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5069 *
5070 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005071 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5074
5075 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5076
5077 return SCTP_DISPOSITION_CONSUME;
5078
5079nomem:
5080 return SCTP_DISPOSITION_NOMEM;
5081}
5082
5083/*
5084 * Ignore the event defined as other
5085 *
5086 * The return value is the disposition of the event.
5087 */
5088sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5089 const struct sctp_association *asoc,
5090 const sctp_subtype_t type,
5091 void *arg,
5092 sctp_cmd_seq_t *commands)
5093{
5094 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5095 return SCTP_DISPOSITION_DISCARD;
5096}
5097
5098/************************************************************
5099 * These are the state functions for handling timeout events.
5100 ************************************************************/
5101
5102/*
5103 * RTX Timeout
5104 *
5105 * Section: 6.3.3 Handle T3-rtx Expiration
5106 *
5107 * Whenever the retransmission timer T3-rtx expires for a destination
5108 * address, do the following:
5109 * [See below]
5110 *
5111 * The return value is the disposition of the chunk.
5112 */
5113sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5114 const struct sctp_association *asoc,
5115 const sctp_subtype_t type,
5116 void *arg,
5117 sctp_cmd_seq_t *commands)
5118{
5119 struct sctp_transport *transport = arg;
5120
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005121 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005124 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5125 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5127 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005128 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5130 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5131 return SCTP_DISPOSITION_DELETE_TCB;
5132 }
5133
5134 /* E1) For the destination address for which the timer
5135 * expires, adjust its ssthresh with rules defined in Section
5136 * 7.2.3 and set the cwnd <- MTU.
5137 */
5138
5139 /* E2) For the destination address for which the timer
5140 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5141 * maximum value discussed in rule C7 above (RTO.max) may be
5142 * used to provide an upper bound to this doubling operation.
5143 */
5144
5145 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5146 * outstanding DATA chunks for the address for which the
5147 * T3-rtx has expired will fit into a single packet, subject
5148 * to the MTU constraint for the path corresponding to the
5149 * destination transport address to which the retransmission
5150 * is being sent (this may be different from the address for
5151 * which the timer expires [see Section 6.4]). Call this
5152 * value K. Bundle and retransmit those K DATA chunks in a
5153 * single packet to the destination endpoint.
5154 *
5155 * Note: Any DATA chunks that were sent to the address for
5156 * which the T3-rtx timer expired but did not fit in one MTU
5157 * (rule E3 above), should be marked for retransmission and
5158 * sent as soon as cwnd allows (normally when a SACK arrives).
5159 */
5160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 /* Do some failure management (Section 8.2). */
5162 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5163
Vlad Yasevich1845a572007-02-21 02:06:19 -08005164 /* NB: Rules E4 and F1 are implicit in R1. */
5165 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5166
Linus Torvalds1da177e2005-04-16 15:20:36 -07005167 return SCTP_DISPOSITION_CONSUME;
5168}
5169
5170/*
5171 * Generate delayed SACK on timeout
5172 *
5173 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5174 *
5175 * The guidelines on delayed acknowledgement algorithm specified in
5176 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5177 * acknowledgement SHOULD be generated for at least every second packet
5178 * (not every second DATA chunk) received, and SHOULD be generated
5179 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5180 * some situations it may be beneficial for an SCTP transmitter to be
5181 * more conservative than the algorithms detailed in this document
5182 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5183 * the following algorithms allow.
5184 */
5185sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5186 const struct sctp_association *asoc,
5187 const sctp_subtype_t type,
5188 void *arg,
5189 sctp_cmd_seq_t *commands)
5190{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005191 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5193 return SCTP_DISPOSITION_CONSUME;
5194}
5195
5196/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005197 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 *
5199 * Section: 4 Note: 2
5200 * Verification Tag:
5201 * Inputs
5202 * (endpoint, asoc)
5203 *
5204 * RFC 2960 Section 4 Notes
5205 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5206 * and re-start the T1-init timer without changing state. This MUST
5207 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5208 * endpoint MUST abort the initialization process and report the
5209 * error to SCTP user.
5210 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005211 * Outputs
5212 * (timers, events)
5213 *
5214 */
5215sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5216 const struct sctp_association *asoc,
5217 const sctp_subtype_t type,
5218 void *arg,
5219 sctp_cmd_seq_t *commands)
5220{
5221 struct sctp_chunk *repl = NULL;
5222 struct sctp_bind_addr *bp;
5223 int attempts = asoc->init_err_counter + 1;
5224
5225 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005226 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005227
Vlad Yasevich81845c22006-01-30 15:59:54 -08005228 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005229 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5230 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5231 if (!repl)
5232 return SCTP_DISPOSITION_NOMEM;
5233
5234 /* Choose transport for INIT. */
5235 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5236 SCTP_CHUNK(repl));
5237
5238 /* Issue a sideeffect to do the needed accounting. */
5239 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5240 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5241
5242 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5243 } else {
5244 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5245 " max_init_attempts: %d\n",
5246 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005247 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5248 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005249 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005250 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005251 return SCTP_DISPOSITION_DELETE_TCB;
5252 }
5253
5254 return SCTP_DISPOSITION_CONSUME;
5255}
5256
5257/*
5258 * sctp_sf_t1_cookie_timer_expire
5259 *
5260 * Section: 4 Note: 2
5261 * Verification Tag:
5262 * Inputs
5263 * (endpoint, asoc)
5264 *
5265 * RFC 2960 Section 4 Notes
5266 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 * COOKIE ECHO and re-start the T1-cookie timer without changing
5268 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5269 * After that, the endpoint MUST abort the initialization process and
5270 * report the error to SCTP user.
5271 *
5272 * Outputs
5273 * (timers, events)
5274 *
5275 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005276sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 const struct sctp_association *asoc,
5278 const sctp_subtype_t type,
5279 void *arg,
5280 sctp_cmd_seq_t *commands)
5281{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005282 struct sctp_chunk *repl = NULL;
5283 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
Frank Filz3f7a87d2005-06-20 13:14:57 -07005285 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005286 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287
Vlad Yasevich81845c22006-01-30 15:59:54 -08005288 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005289 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005291 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005294 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5295 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5296
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5298 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005299 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5300 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005302 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 return SCTP_DISPOSITION_DELETE_TCB;
5304 }
5305
5306 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307}
5308
5309/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5310 * with the updated last sequential TSN received from its peer.
5311 *
5312 * An endpoint should limit the number of retransmissions of the
5313 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5314 * If this threshold is exceeded the endpoint should destroy the TCB and
5315 * MUST report the peer endpoint unreachable to the upper layer (and
5316 * thus the association enters the CLOSED state). The reception of any
5317 * packet from its peer (i.e. as the peer sends all of its queued DATA
5318 * chunks) should clear the endpoint's retransmission count and restart
5319 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5320 * all of its queued DATA chunks that have not yet been sent.
5321 */
5322sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5323 const struct sctp_association *asoc,
5324 const sctp_subtype_t type,
5325 void *arg,
5326 sctp_cmd_seq_t *commands)
5327{
5328 struct sctp_chunk *reply = NULL;
5329
5330 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005331 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5332
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005334 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5335 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5337 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005338 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5340 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5341 return SCTP_DISPOSITION_DELETE_TCB;
5342 }
5343
5344 switch (asoc->state) {
5345 case SCTP_STATE_SHUTDOWN_SENT:
5346 reply = sctp_make_shutdown(asoc, NULL);
5347 break;
5348
5349 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5350 reply = sctp_make_shutdown_ack(asoc, NULL);
5351 break;
5352
5353 default:
5354 BUG();
5355 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 if (!reply)
5359 goto nomem;
5360
5361 /* Do some failure management (Section 8.2). */
5362 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5363 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5364
5365 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5366 * the T2-shutdown timer.
5367 */
5368 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5369
5370 /* Restart the T2-shutdown timer. */
5371 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5372 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5373 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5374 return SCTP_DISPOSITION_CONSUME;
5375
5376nomem:
5377 return SCTP_DISPOSITION_NOMEM;
5378}
5379
5380/*
5381 * ADDIP Section 4.1 ASCONF CHunk Procedures
5382 * If the T4 RTO timer expires the endpoint should do B1 to B5
5383 */
5384sctp_disposition_t sctp_sf_t4_timer_expire(
5385 const struct sctp_endpoint *ep,
5386 const struct sctp_association *asoc,
5387 const sctp_subtype_t type,
5388 void *arg,
5389 sctp_cmd_seq_t *commands)
5390{
5391 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5392 struct sctp_transport *transport = chunk->transport;
5393
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005394 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5397 * detection on the appropriate destination address as defined in
5398 * RFC2960 [5] section 8.1 and 8.2.
5399 */
5400 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5401
5402 /* Reconfig T4 timer and transport. */
5403 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5404
5405 /* ADDIP 4.1 B2) Increment the association error counters and perform
5406 * endpoint failure detection on the association as defined in
5407 * RFC2960 [5] section 8.1 and 8.2.
5408 * association error counter is incremented in SCTP_CMD_STRIKE.
5409 */
5410 if (asoc->overall_error_count >= asoc->max_retrans) {
5411 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5412 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005413 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5414 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005416 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5418 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
5419 return SCTP_DISPOSITION_ABORT;
5420 }
5421
5422 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5423 * the ASCONF chunk was sent by doubling the RTO timer value.
5424 * This is done in SCTP_CMD_STRIKE.
5425 */
5426
5427 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5428 * choose an alternate destination address (please refer to RFC2960
5429 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005430 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 * ASCONF sent.
5432 */
5433 sctp_chunk_hold(asoc->addip_last_asconf);
5434 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5435 SCTP_CHUNK(asoc->addip_last_asconf));
5436
5437 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5438 * destination is selected, then the RTO used will be that of the new
5439 * destination address.
5440 */
5441 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5442 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5443
5444 return SCTP_DISPOSITION_CONSUME;
5445}
5446
5447/* sctpimpguide-05 Section 2.12.2
5448 * The sender of the SHUTDOWN MAY also start an overall guard timer
5449 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5450 * At the expiration of this timer the sender SHOULD abort the association
5451 * by sending an ABORT chunk.
5452 */
5453sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5454 const struct sctp_association *asoc,
5455 const sctp_subtype_t type,
5456 void *arg,
5457 sctp_cmd_seq_t *commands)
5458{
5459 struct sctp_chunk *reply = NULL;
5460
5461 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005462 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
5464 reply = sctp_make_abort(asoc, NULL, 0);
5465 if (!reply)
5466 goto nomem;
5467
5468 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005469 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5470 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005472 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473
5474 return SCTP_DISPOSITION_DELETE_TCB;
5475nomem:
5476 return SCTP_DISPOSITION_NOMEM;
5477}
5478
5479/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5480 * the association is automatically closed by starting the shutdown process.
5481 * The work that needs to be done is same as when SHUTDOWN is initiated by
5482 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5483 */
5484sctp_disposition_t sctp_sf_autoclose_timer_expire(
5485 const struct sctp_endpoint *ep,
5486 const struct sctp_association *asoc,
5487 const sctp_subtype_t type,
5488 void *arg,
5489 sctp_cmd_seq_t *commands)
5490{
5491 int disposition;
5492
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005493 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 /* From 9.2 Shutdown of an Association
5496 * Upon receipt of the SHUTDOWN primitive from its upper
5497 * layer, the endpoint enters SHUTDOWN-PENDING state and
5498 * remains there until all outstanding data has been
5499 * acknowledged by its peer. The endpoint accepts no new data
5500 * from its upper layer, but retransmits data to the far end
5501 * if necessary to fill gaps.
5502 */
5503 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5504 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5505
5506 /* sctpimpguide-05 Section 2.12.2
5507 * The sender of the SHUTDOWN MAY also start an overall guard timer
5508 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5511 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5512 disposition = SCTP_DISPOSITION_CONSUME;
5513 if (sctp_outq_is_empty(&asoc->outqueue)) {
5514 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5515 arg, commands);
5516 }
5517 return disposition;
5518}
5519
5520/*****************************************************************************
5521 * These are sa state functions which could apply to all types of events.
5522 ****************************************************************************/
5523
5524/*
5525 * This table entry is not implemented.
5526 *
5527 * Inputs
5528 * (endpoint, asoc, chunk)
5529 *
5530 * The return value is the disposition of the chunk.
5531 */
5532sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5533 const struct sctp_association *asoc,
5534 const sctp_subtype_t type,
5535 void *arg,
5536 sctp_cmd_seq_t *commands)
5537{
5538 return SCTP_DISPOSITION_NOT_IMPL;
5539}
5540
5541/*
5542 * This table entry represents a bug.
5543 *
5544 * Inputs
5545 * (endpoint, asoc, chunk)
5546 *
5547 * The return value is the disposition of the chunk.
5548 */
5549sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5550 const struct sctp_association *asoc,
5551 const sctp_subtype_t type,
5552 void *arg,
5553 sctp_cmd_seq_t *commands)
5554{
5555 return SCTP_DISPOSITION_BUG;
5556}
5557
5558/*
5559 * This table entry represents the firing of a timer in the wrong state.
5560 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5561 * when the association is in the wrong state. This event should
5562 * be ignored, so as to prevent any rearming of the timer.
5563 *
5564 * Inputs
5565 * (endpoint, asoc, chunk)
5566 *
5567 * The return value is the disposition of the chunk.
5568 */
5569sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5570 const struct sctp_association *asoc,
5571 const sctp_subtype_t type,
5572 void *arg,
5573 sctp_cmd_seq_t *commands)
5574{
5575 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5576 return SCTP_DISPOSITION_CONSUME;
5577}
5578
5579/********************************************************************
5580 * 2nd Level Abstractions
5581 ********************************************************************/
5582
5583/* Pull the SACK chunk based on the SACK header. */
5584static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5585{
5586 struct sctp_sackhdr *sack;
5587 unsigned int len;
5588 __u16 num_blocks;
5589 __u16 num_dup_tsns;
5590
5591 /* Protect ourselves from reading too far into
5592 * the skb from a bogus sender.
5593 */
5594 sack = (struct sctp_sackhdr *) chunk->skb->data;
5595
5596 num_blocks = ntohs(sack->num_gap_ack_blocks);
5597 num_dup_tsns = ntohs(sack->num_dup_tsns);
5598 len = sizeof(struct sctp_sackhdr);
5599 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5600 if (len > chunk->skb->len)
5601 return NULL;
5602
5603 skb_pull(chunk->skb, len);
5604
5605 return sack;
5606}
5607
5608/* Create an ABORT packet to be sent as a response, with the specified
5609 * error causes.
5610 */
5611static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5612 const struct sctp_association *asoc,
5613 struct sctp_chunk *chunk,
5614 const void *payload,
5615 size_t paylen)
5616{
5617 struct sctp_packet *packet;
5618 struct sctp_chunk *abort;
5619
5620 packet = sctp_ootb_pkt_new(asoc, chunk);
5621
5622 if (packet) {
5623 /* Make an ABORT.
5624 * The T bit will be set if the asoc is NULL.
5625 */
5626 abort = sctp_make_abort(asoc, chunk, paylen);
5627 if (!abort) {
5628 sctp_ootb_pkt_free(packet);
5629 return NULL;
5630 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005631
5632 /* Reflect vtag if T-Bit is set */
5633 if (sctp_test_T_bit(abort))
5634 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 /* Add specified error causes, i.e., payload, to the
5637 * end of the chunk.
5638 */
5639 sctp_addto_chunk(abort, paylen, payload);
5640
5641 /* Set the skb to the belonging sock for accounting. */
5642 abort->skb->sk = ep->base.sk;
5643
5644 sctp_packet_append_chunk(packet, abort);
5645
5646 }
5647
5648 return packet;
5649}
5650
5651/* Allocate a packet for responding in the OOTB conditions. */
5652static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5653 const struct sctp_chunk *chunk)
5654{
5655 struct sctp_packet *packet;
5656 struct sctp_transport *transport;
5657 __u16 sport;
5658 __u16 dport;
5659 __u32 vtag;
5660
5661 /* Get the source and destination port from the inbound packet. */
5662 sport = ntohs(chunk->sctp_hdr->dest);
5663 dport = ntohs(chunk->sctp_hdr->source);
5664
5665 /* The V-tag is going to be the same as the inbound packet if no
5666 * association exists, otherwise, use the peer's vtag.
5667 */
5668 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005669 /* Special case the INIT-ACK as there is no peer's vtag
5670 * yet.
5671 */
5672 switch(chunk->chunk_hdr->type) {
5673 case SCTP_CID_INIT_ACK:
5674 {
5675 sctp_initack_chunk_t *initack;
5676
5677 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5678 vtag = ntohl(initack->init_hdr.init_tag);
5679 break;
5680 }
5681 default:
5682 vtag = asoc->peer.i.init_tag;
5683 break;
5684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 } else {
5686 /* Special case the INIT and stale COOKIE_ECHO as there is no
5687 * vtag yet.
5688 */
5689 switch(chunk->chunk_hdr->type) {
5690 case SCTP_CID_INIT:
5691 {
5692 sctp_init_chunk_t *init;
5693
5694 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5695 vtag = ntohl(init->init_hdr.init_tag);
5696 break;
5697 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005698 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 vtag = ntohl(chunk->sctp_hdr->vtag);
5700 break;
5701 }
5702 }
5703
5704 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005705 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 if (!transport)
5707 goto nomem;
5708
5709 /* Cache a route for the transport with the chunk's destination as
5710 * the source address.
5711 */
Al Viro16b0a032006-11-20 17:13:38 -08005712 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 sctp_sk(sctp_get_ctl_sock()));
5714
5715 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5716 packet = sctp_packet_config(packet, vtag, 0);
5717
5718 return packet;
5719
5720nomem:
5721 return NULL;
5722}
5723
5724/* Free the packet allocated earlier for responding in the OOTB condition. */
5725void sctp_ootb_pkt_free(struct sctp_packet *packet)
5726{
5727 sctp_transport_free(packet->transport);
5728}
5729
5730/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5731static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5732 const struct sctp_association *asoc,
5733 const struct sctp_chunk *chunk,
5734 sctp_cmd_seq_t *commands,
5735 struct sctp_chunk *err_chunk)
5736{
5737 struct sctp_packet *packet;
5738
5739 if (err_chunk) {
5740 packet = sctp_ootb_pkt_new(asoc, chunk);
5741 if (packet) {
5742 struct sctp_signed_cookie *cookie;
5743
5744 /* Override the OOTB vtag from the cookie. */
5745 cookie = chunk->subh.cookie_hdr;
5746 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005747
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 /* Set the skb to the belonging sock for accounting. */
5749 err_chunk->skb->sk = ep->base.sk;
5750 sctp_packet_append_chunk(packet, err_chunk);
5751 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5752 SCTP_PACKET(packet));
5753 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5754 } else
5755 sctp_chunk_free (err_chunk);
5756 }
5757}
5758
5759
5760/* Process a data chunk */
5761static int sctp_eat_data(const struct sctp_association *asoc,
5762 struct sctp_chunk *chunk,
5763 sctp_cmd_seq_t *commands)
5764{
5765 sctp_datahdr_t *data_hdr;
5766 struct sctp_chunk *err;
5767 size_t datalen;
5768 sctp_verb_t deliver;
5769 int tmp;
5770 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005771 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005772 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5775 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5776
5777 tsn = ntohl(data_hdr->tsn);
5778 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5779
5780 /* ASSERT: Now skb->data is really the user data. */
5781
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 /* Process ECN based congestion.
5783 *
5784 * Since the chunk structure is reused for all chunks within
5785 * a packet, we use ecn_ce_done to track if we've already
5786 * done CE processing for this packet.
5787 *
5788 * We need to do ECN processing even if we plan to discard the
5789 * chunk later.
5790 */
5791
5792 if (!chunk->ecn_ce_done) {
5793 struct sctp_af *af;
5794 chunk->ecn_ce_done = 1;
5795
5796 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005797 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5800 /* Do real work as sideffect. */
5801 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5802 SCTP_U32(tsn));
5803 }
5804 }
5805
5806 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5807 if (tmp < 0) {
5808 /* The TSN is too high--silently discard the chunk and
5809 * count on it getting retransmitted later.
5810 */
5811 return SCTP_IERROR_HIGH_TSN;
5812 } else if (tmp > 0) {
5813 /* This is a duplicate. Record it. */
5814 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5815 return SCTP_IERROR_DUP_TSN;
5816 }
5817
5818 /* This is a new TSN. */
5819
5820 /* Discard if there is no room in the receive window.
5821 * Actually, allow a little bit of overflow (up to a MTU).
5822 */
5823 datalen = ntohs(chunk->chunk_hdr->length);
5824 datalen -= sizeof(sctp_data_chunk_t);
5825
5826 deliver = SCTP_CMD_CHUNK_ULP;
5827
5828 /* Think about partial delivery. */
5829 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5830
5831 /* Even if we don't accept this chunk there is
5832 * memory pressure.
5833 */
5834 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5835 }
5836
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005837 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 * seems a bit troublesome in that frag_point varies based on
5839 * PMTU. In cases, such as loopback, this might be a rather
5840 * large spill over.
5841 */
Neil Horman4d93df02007-08-15 16:07:44 -07005842 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5843 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844
5845 /* If this is the next TSN, consider reneging to make
5846 * room. Note: Playing nice with a confused sender. A
5847 * malicious sender can still eat up all our buffer
5848 * space and in the future we may want to detect and
5849 * do more drastic reneging.
5850 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005851 if (sctp_tsnmap_has_gap(map) &&
5852 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5854 deliver = SCTP_CMD_RENEGE;
5855 } else {
5856 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5857 "rwnd: %d\n", tsn, datalen,
5858 asoc->rwnd);
5859 return SCTP_IERROR_IGNORE_TSN;
5860 }
5861 }
5862
5863 /*
Neil Horman4d93df02007-08-15 16:07:44 -07005864 * Also try to renege to limit our memory usage in the event that
5865 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08005866 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07005867 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5868 * memory usage too much
5869 */
5870 if (*sk->sk_prot_creator->memory_pressure) {
5871 if (sctp_tsnmap_has_gap(map) &&
5872 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5873 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5874 deliver = SCTP_CMD_RENEGE;
5875 }
5876 }
5877
5878 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 * Section 3.3.10.9 No User Data (9)
5880 *
5881 * Cause of error
5882 * ---------------
5883 * No User Data: This error cause is returned to the originator of a
5884 * DATA chunk if a received DATA chunk has no user data.
5885 */
5886 if (unlikely(0 == datalen)) {
5887 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5888 if (err) {
5889 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5890 SCTP_CHUNK(err));
5891 }
5892 /* We are going to ABORT, so we might as well stop
5893 * processing the rest of the chunks in the packet.
5894 */
5895 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005896 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5897 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005899 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5901 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5902 return SCTP_IERROR_NO_DATA;
5903 }
5904
5905 /* If definately accepting the DATA chunk, record its TSN, otherwise
5906 * wait for renege processing.
5907 */
5908 if (SCTP_CMD_CHUNK_ULP == deliver)
5909 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
5910
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07005911 chunk->data_accepted = 1;
5912
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 /* Note: Some chunks may get overcounted (if we drop) or overcounted
5914 * if we renege and the chunk arrives again.
5915 */
5916 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
5917 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
5918 else
5919 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
5920
5921 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
5922 *
5923 * If an endpoint receive a DATA chunk with an invalid stream
5924 * identifier, it shall acknowledge the reception of the DATA chunk
5925 * following the normal procedure, immediately send an ERROR chunk
5926 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
5927 * and discard the DATA chunk.
5928 */
5929 if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
5930 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
5931 &data_hdr->stream,
5932 sizeof(data_hdr->stream));
5933 if (err)
5934 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5935 SCTP_CHUNK(err));
5936 return SCTP_IERROR_BAD_STREAM;
5937 }
5938
5939 /* Send the data up to the user. Note: Schedule the
5940 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
5941 * chunk needs the updated rwnd.
5942 */
5943 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
5944
5945 return SCTP_IERROR_NO_ERROR;
5946}