blob: ea3a34cbe47001008553658ebaa987fbcb568dc3 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
28 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 *
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
35 *
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
53#include <linux/types.h>
54#include <linux/kernel.h>
55#include <linux/ip.h>
56#include <linux/ipv6.h>
57#include <linux/net.h>
58#include <linux/inet.h>
59#include <net/sock.h>
60#include <net/inet_ecn.h>
61#include <linux/skbuff.h>
62#include <net/sctp/sctp.h>
63#include <net/sctp/sm.h>
64#include <net/sctp/structs.h>
65
66static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
67 const struct sctp_association *asoc,
68 struct sctp_chunk *chunk,
69 const void *payload,
70 size_t paylen);
71static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 sctp_cmd_seq_t *commands);
74static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
75 const struct sctp_chunk *chunk);
76static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
77 const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk,
79 sctp_cmd_seq_t *commands,
80 struct sctp_chunk *err_chunk);
81static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
82 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
86static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
87 const struct sctp_association *asoc,
88 const sctp_subtype_t type,
89 void *arg,
90 sctp_cmd_seq_t *commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -040091static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
92 const struct sctp_association *asoc,
93 const sctp_subtype_t type,
94 void *arg,
95 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
97
Adrian Bunk52c1da32005-06-23 22:05:33 -070098static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -080099 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700100 const struct sctp_association *asoc,
101 struct sctp_transport *transport);
102
Wei Yongjunaecedea2007-08-02 16:57:44 +0800103static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -0400104 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800105 const struct sctp_association *asoc,
106 void *arg,
107 sctp_cmd_seq_t *commands,
108 const __u8 *payload,
109 const size_t paylen);
110
Adrian Bunk52c1da32005-06-23 22:05:33 -0700111static sctp_disposition_t sctp_sf_violation_chunklen(
112 const struct sctp_endpoint *ep,
113 const struct sctp_association *asoc,
114 const sctp_subtype_t type,
115 void *arg,
116 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800118static sctp_disposition_t sctp_sf_violation_paramlen(
119 const struct sctp_endpoint *ep,
120 const struct sctp_association *asoc,
121 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700122 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123 sctp_cmd_seq_t *commands);
124
Wei Yongjunaecedea2007-08-02 16:57:44 +0800125static sctp_disposition_t sctp_sf_violation_ctsn(
126 const struct sctp_endpoint *ep,
127 const struct sctp_association *asoc,
128 const sctp_subtype_t type,
129 void *arg,
130 sctp_cmd_seq_t *commands);
131
Vlad Yasevichece25df2007-09-07 16:30:54 -0400132static sctp_disposition_t sctp_sf_violation_chunk(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700139static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
140 const struct sctp_association *asoc,
141 const sctp_subtype_t type,
142 struct sctp_chunk *chunk);
143
Vlad Yasevich75205f42007-12-20 14:12:59 -0800144static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
145 const struct sctp_association *asoc,
146 const sctp_subtype_t type,
147 void *arg,
148 sctp_cmd_seq_t *commands);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Small helper function that checks if the chunk length
151 * is of the appropriate length. The 'required_length' argument
152 * is set to be the size of a specific chunk we are testing.
153 * Return Values: 1 = Valid length
154 * 0 = Invalid length
155 *
156 */
157static inline int
158sctp_chunk_length_valid(struct sctp_chunk *chunk,
159 __u16 required_length)
160{
161 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
162
163 if (unlikely(chunk_length < required_length))
164 return 0;
165
166 return 1;
167}
168
169/**********************************************************
170 * These are the state functions for handling chunk events.
171 **********************************************************/
172
173/*
174 * Process the final SHUTDOWN COMPLETE.
175 *
176 * Section: 4 (C) (diagram), 9.2
177 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
178 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
179 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
180 * should stop the T2-shutdown timer and remove all knowledge of the
181 * association (and thus the association enters the CLOSED state).
182 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700183 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * C) Rules for packet carrying SHUTDOWN COMPLETE:
185 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700186 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
187 * if the Verification Tag field of the packet matches its own tag and
188 * the T bit is not set
189 * OR
190 * it is set to its peer's tag and the T bit is set in the Chunk
191 * Flags.
192 * Otherwise, the receiver MUST silently discard the packet
193 * and take no further action. An endpoint MUST ignore the
194 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Inputs
197 * (endpoint, asoc, chunk)
198 *
199 * Outputs
200 * (asoc, reply_msg, msg_up, timers, counters)
201 *
202 * The return value is the disposition of the chunk.
203 */
204sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
205 const struct sctp_association *asoc,
206 const sctp_subtype_t type,
207 void *arg,
208 sctp_cmd_seq_t *commands)
209{
210 struct sctp_chunk *chunk = arg;
211 struct sctp_ulpevent *ev;
212
Vlad Yasevichece25df2007-09-07 16:30:54 -0400213 if (!sctp_vtag_verify_either(chunk, asoc))
214 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* RFC 2960 6.10 Bundling
217 *
218 * An endpoint MUST NOT bundle INIT, INIT ACK or
219 * SHUTDOWN COMPLETE with any other chunks.
220 */
221 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400222 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Vlad Yasevichece25df2007-09-07 16:30:54 -0400224 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
225 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
226 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
227 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* RFC 2960 10.2 SCTP-to-ULP
230 *
231 * H) SHUTDOWN COMPLETE notification
232 *
233 * When SCTP completes the shutdown procedures (section 9.2) this
234 * notification is passed to the upper layer.
235 */
236 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700237 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700238 if (ev)
239 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900240 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
243 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
244 * not the chunk should be discarded. If the endpoint is in
245 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
246 * T2-shutdown timer and remove all knowledge of the
247 * association (and thus the association enters the CLOSED
248 * state).
249 */
250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
251 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
252
253 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
255
256 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
257 SCTP_STATE(SCTP_STATE_CLOSED));
258
259 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
260 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
261
262 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
263
264 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Respond to a normal INIT chunk.
269 * We are the side that is being asked for an association.
270 *
271 * Section: 5.1 Normal Establishment of an Association, B
272 * B) "Z" shall respond immediately with an INIT ACK chunk. The
273 * destination IP address of the INIT ACK MUST be set to the source
274 * IP address of the INIT to which this INIT ACK is responding. In
275 * the response, besides filling in other parameters, "Z" must set the
276 * Verification Tag field to Tag_A, and also provide its own
277 * Verification Tag (Tag_Z) in the Initiate Tag field.
278 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900279 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * Inputs
282 * (endpoint, asoc, chunk)
283 *
284 * Outputs
285 * (asoc, reply_msg, msg_up, timers, counters)
286 *
287 * The return value is the disposition of the chunk.
288 */
289sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
290 const struct sctp_association *asoc,
291 const sctp_subtype_t type,
292 void *arg,
293 sctp_cmd_seq_t *commands)
294{
295 struct sctp_chunk *chunk = arg;
296 struct sctp_chunk *repl;
297 struct sctp_association *new_asoc;
298 struct sctp_chunk *err_chunk;
299 struct sctp_packet *packet;
300 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int len;
302
303 /* 6.10 Bundling
304 * An endpoint MUST NOT bundle INIT, INIT ACK or
305 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * IG Section 2.11.2
308 * Furthermore, we require that the receiver of an INIT chunk MUST
309 * enforce these rules by silently discarding an arriving packet
310 * with an INIT chunk that is bundled with other chunks.
311 */
312 if (!chunk->singleton)
313 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
314
315 /* If the packet is an OOTB packet which is temporarily on the
316 * control endpoint, respond with an ABORT.
317 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800318 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
319 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900324 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326 if (chunk->sctp_hdr->vtag != 0)
327 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
328
329 /* Make sure that the INIT chunk has a valid length.
330 * Normally, this would cause an ABORT with a Protocol Violation
331 * error, but since we don't have an association, we'll
332 * just discard the packet.
333 */
334 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
335 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
336
337 /* Verify the INIT chunk before processing it. */
338 err_chunk = NULL;
339 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
340 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
341 &err_chunk)) {
342 /* This chunk contains fatal error. It is to be discarded.
343 * Send an ABORT, with causes if there is any.
344 */
345 if (err_chunk) {
346 packet = sctp_abort_pkt_new(ep, asoc, arg,
347 (__u8 *)(err_chunk->chunk_hdr) +
348 sizeof(sctp_chunkhdr_t),
349 ntohs(err_chunk->chunk_hdr->length) -
350 sizeof(sctp_chunkhdr_t));
351
352 sctp_chunk_free(err_chunk);
353
354 if (packet) {
355 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
356 SCTP_PACKET(packet));
357 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
358 return SCTP_DISPOSITION_CONSUME;
359 } else {
360 return SCTP_DISPOSITION_NOMEM;
361 }
362 } else {
363 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
364 commands);
365 }
366 }
367
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900368 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
370
371 /* Tag the variable length parameters. */
372 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
373
374 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
375 if (!new_asoc)
376 goto nomem;
377
378 /* The call, sctp_process_init(), can fail on memory allocation. */
379 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
380 sctp_source(chunk),
381 (sctp_init_chunk_t *)chunk->chunk_hdr,
382 GFP_ATOMIC))
383 goto nomem_init;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
386
387 /* If there are errors need to be reported for unknown parameters,
388 * make sure to reserve enough room in the INIT ACK for them.
389 */
390 len = 0;
391 if (err_chunk)
392 len = ntohs(err_chunk->chunk_hdr->length) -
393 sizeof(sctp_chunkhdr_t);
394
395 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700396 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
399 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700400 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* If there are errors need to be reported for unknown parameters,
403 * include them in the outgoing INIT ACK as "Unrecognized parameter"
404 * parameter.
405 */
406 if (err_chunk) {
407 /* Get the "Unrecognized parameter" parameter(s) out of the
408 * ERROR chunk generated by sctp_verify_init(). Since the
409 * error cause code for "unknown parameter" and the
410 * "Unrecognized parameter" type is the same, we can
411 * construct the parameters in INIT ACK by copying the
412 * ERROR causes over.
413 */
414 unk_param = (sctp_unrecognized_param_t *)
415 ((__u8 *)(err_chunk->chunk_hdr) +
416 sizeof(sctp_chunkhdr_t));
417 /* Replace the cause code with the "Unrecognized parameter"
418 * parameter type.
419 */
420 sctp_addto_chunk(repl, len, unk_param);
421 sctp_chunk_free(err_chunk);
422 }
423
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700424 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
427
428 /*
429 * Note: After sending out INIT ACK with the State Cookie parameter,
430 * "Z" MUST NOT allocate any resources, nor keep any states for the
431 * new association. Otherwise, "Z" will be vulnerable to resource
432 * attacks.
433 */
434 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
435
436 return SCTP_DISPOSITION_DELETE_TCB;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438nomem_init:
439 sctp_association_free(new_asoc);
440nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700441 if (err_chunk)
442 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return SCTP_DISPOSITION_NOMEM;
444}
445
446/*
447 * Respond to a normal INIT ACK chunk.
448 * We are the side that is initiating the association.
449 *
450 * Section: 5.1 Normal Establishment of an Association, C
451 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
452 * timer and leave COOKIE-WAIT state. "A" shall then send the State
453 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
454 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
455 *
456 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
457 * DATA chunks, but it MUST be the first chunk in the packet and
458 * until the COOKIE ACK is returned the sender MUST NOT send any
459 * other packets to the peer.
460 *
461 * Verification Tag: 3.3.3
462 * If the value of the Initiate Tag in a received INIT ACK chunk is
463 * found to be 0, the receiver MUST treat it as an error and close the
464 * association by transmitting an ABORT.
465 *
466 * Inputs
467 * (endpoint, asoc, chunk)
468 *
469 * Outputs
470 * (asoc, reply_msg, msg_up, timers, counters)
471 *
472 * The return value is the disposition of the chunk.
473 */
474sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
475 const struct sctp_association *asoc,
476 const sctp_subtype_t type,
477 void *arg,
478 sctp_cmd_seq_t *commands)
479{
480 struct sctp_chunk *chunk = arg;
481 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct sctp_chunk *err_chunk;
483 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 if (!sctp_vtag_verify(chunk, asoc))
486 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 /* 6.10 Bundling
489 * An endpoint MUST NOT bundle INIT, INIT ACK or
490 * SHUTDOWN COMPLETE with any other chunks.
491 */
492 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400493 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Vlad Yasevichece25df2007-09-07 16:30:54 -0400495 /* Make sure that the INIT-ACK chunk has a valid length */
496 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
497 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
498 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Grab the INIT header. */
500 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /* Verify the INIT chunk before processing it. */
503 err_chunk = NULL;
504 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
505 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
506 &err_chunk)) {
507
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800508 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800511 * Send an ABORT, with causes. If there are no causes,
512 * then there wasn't enough memory. Just terminate
513 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
515 if (err_chunk) {
516 packet = sctp_abort_pkt_new(ep, asoc, arg,
517 (__u8 *)(err_chunk->chunk_hdr) +
518 sizeof(sctp_chunkhdr_t),
519 ntohs(err_chunk->chunk_hdr->length) -
520 sizeof(sctp_chunkhdr_t));
521
522 sctp_chunk_free(err_chunk);
523
524 if (packet) {
525 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
526 SCTP_PACKET(packet));
527 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700528 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700531
532 /* SCTP-AUTH, Section 6.3:
533 * It should be noted that if the receiver wants to tear
534 * down an association in an authenticated way only, the
535 * handling of malformed packets should not result in
536 * tearing down the association.
537 *
538 * This means that if we only want to abort associations
539 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +0200540 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700541 * was malformed.
542 */
543 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
544 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
545
546 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700547 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
548 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
551 /* Tag the variable length parameters. Note that we never
552 * convert the parameters in an INIT chunk.
553 */
554 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
555
556 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
557
558 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
559 SCTP_PEER_INIT(initchunk));
560
Frank Filz3f7a87d2005-06-20 13:14:57 -0700561 /* Reset init error count upon receipt of INIT-ACK. */
562 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /* 5.1 C) "A" shall stop the T1-init timer and leave
565 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
566 * timer, and enter the COOKIE-ECHOED state.
567 */
568 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
569 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
570 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
571 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
572 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
573 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
574
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700575 /* SCTP-AUTH: genereate the assocition shared keys so that
576 * we can potentially signe the COOKIE-ECHO.
577 */
578 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /* 5.1 C) "A" shall then send the State Cookie received in the
581 * INIT ACK chunk in a COOKIE ECHO chunk, ...
582 */
583 /* If there is any errors to report, send the ERROR chunk generated
584 * for unknown parameters as well.
585 */
586 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
587 SCTP_CHUNK(err_chunk));
588
589 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/*
593 * Respond to a normal COOKIE ECHO chunk.
594 * We are the side that is being asked for an association.
595 *
596 * Section: 5.1 Normal Establishment of an Association, D
597 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
598 * with a COOKIE ACK chunk after building a TCB and moving to
599 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
600 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
601 * chunk MUST be the first chunk in the packet.
602 *
603 * IMPLEMENTATION NOTE: An implementation may choose to send the
604 * Communication Up notification to the SCTP user upon reception
605 * of a valid COOKIE ECHO chunk.
606 *
607 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
608 * D) Rules for packet carrying a COOKIE ECHO
609 *
610 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
611 * Initial Tag received in the INIT ACK.
612 *
613 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
614 *
615 * Inputs
616 * (endpoint, asoc, chunk)
617 *
618 * Outputs
619 * (asoc, reply_msg, msg_up, timers, counters)
620 *
621 * The return value is the disposition of the chunk.
622 */
623sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
624 const struct sctp_association *asoc,
625 const sctp_subtype_t type, void *arg,
626 sctp_cmd_seq_t *commands)
627{
628 struct sctp_chunk *chunk = arg;
629 struct sctp_association *new_asoc;
630 sctp_init_chunk_t *peer_init;
631 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700632 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 int error = 0;
634 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900635 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 /* If the packet is an OOTB packet which is temporarily on the
638 * control endpoint, respond with an ABORT.
639 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800640 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
641 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400642 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 /* Make sure that the COOKIE_ECHO chunk has a valid length.
646 * In this case, we check that we have enough for at least a
647 * chunk header. More detailed verification is done
648 * in sctp_unpack_cookie().
649 */
650 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
651 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
652
Vlad Yasevich609ee462007-08-31 03:10:59 +0900653 /* If the endpoint is not listening or if the number of associations
654 * on the TCP-style socket exceed the max backlog, respond with an
655 * ABORT.
656 */
657 sk = ep->base.sk;
658 if (!sctp_sstate(sk, LISTENING) ||
659 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
660 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /* "Decode" the chunk. We have no optional parameters so we
663 * are in good shape.
664 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900665 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700667 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
668 sizeof(sctp_chunkhdr_t)))
669 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
672 * "Z" will reply with a COOKIE ACK chunk after building a TCB
673 * and moving to the ESTABLISHED state.
674 */
675 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
676 &err_chk_p);
677
678 /* FIXME:
679 * If the re-build failed, what is the proper error path
680 * from here?
681 *
682 * [We should abort the association. --piggy]
683 */
684 if (!new_asoc) {
685 /* FIXME: Several errors are possible. A bad cookie should
686 * be silently discarded, but think about logging it too.
687 */
688 switch (error) {
689 case -SCTP_IERROR_NOMEM:
690 goto nomem;
691
692 case -SCTP_IERROR_STALE_COOKIE:
693 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
694 err_chk_p);
695 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
696
697 case -SCTP_IERROR_BAD_SIG:
698 default:
699 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700704 /* Delay state machine commands until later.
705 *
706 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * the sctp_unpack_cookie() already.
708 */
709 /* This is a brand-new association, so these are not yet side
710 * effects--it is safe to run them here.
711 */
712 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
713
714 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
Al Viro6a1e5f32006-11-20 17:12:25 -0800715 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 peer_init, GFP_ATOMIC))
717 goto nomem_init;
718
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700719 /* SCTP-AUTH: Now that we've populate required fields in
720 * sctp_process_init, set up the assocaition shared keys as
721 * necessary so that we can potentially authenticate the ACK
722 */
723 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
724 if (error)
725 goto nomem_init;
726
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700727 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
728 * is supposed to be authenticated and we have to do delayed
729 * authentication. We've just recreated the association using
730 * the information in the cookie and now it's much easier to
731 * do the authentication.
732 */
733 if (chunk->auth_chunk) {
734 struct sctp_chunk auth;
735 sctp_ierror_t ret;
736
737 /* set-up our fake chunk so that we can process it */
738 auth.skb = chunk->auth_chunk;
739 auth.asoc = chunk->asoc;
740 auth.sctp_hdr = chunk->sctp_hdr;
741 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
742 sizeof(sctp_chunkhdr_t));
743 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
744 auth.transport = chunk->transport;
745
746 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
747
748 /* We can now safely free the auth_chunk clone */
749 kfree_skb(chunk->auth_chunk);
750
751 if (ret != SCTP_IERROR_NO_ERROR) {
752 sctp_association_free(new_asoc);
753 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
754 }
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 repl = sctp_make_cookie_ack(new_asoc, chunk);
758 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700759 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 /* RFC 2960 5.1 Normal Establishment of an Association
762 *
763 * D) IMPLEMENTATION NOTE: An implementation may choose to
764 * send the Communication Up notification to the SCTP user
765 * upon reception of a valid COOKIE ECHO chunk.
766 */
767 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
768 new_asoc->c.sinit_num_ostreams,
769 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700770 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (!ev)
772 goto nomem_ev;
773
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900774 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800775 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800777 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800779 if (new_asoc->peer.adaptation_ind) {
780 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700782 if (!ai_ev)
783 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700786 /* Add all the state machine commands now since we've created
787 * everything. This way we don't introduce memory corruptions
788 * during side-effect processing and correclty count established
789 * associations.
790 */
791 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
792 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
793 SCTP_STATE(SCTP_STATE_ESTABLISHED));
794 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
795 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
796 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
797
798 if (new_asoc->autoclose)
799 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
800 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
801
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700802 /* This will send the COOKIE ACK */
803 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
804
805 /* Queue the ASSOC_CHANGE event */
806 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
807
808 /* Send up the Adaptation Layer Indication event */
809 if (ai_ev)
810 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
811 SCTP_ULPEVENT(ai_ev));
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return SCTP_DISPOSITION_CONSUME;
814
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700815nomem_aiev:
816 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817nomem_ev:
818 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819nomem_init:
820 sctp_association_free(new_asoc);
821nomem:
822 return SCTP_DISPOSITION_NOMEM;
823}
824
825/*
826 * Respond to a normal COOKIE ACK chunk.
827 * We are the side that is being asked for an association.
828 *
829 * RFC 2960 5.1 Normal Establishment of an Association
830 *
831 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
832 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
833 * timer. It may also notify its ULP about the successful
834 * establishment of the association with a Communication Up
835 * notification (see Section 10).
836 *
837 * Verification Tag:
838 * Inputs
839 * (endpoint, asoc, chunk)
840 *
841 * Outputs
842 * (asoc, reply_msg, msg_up, timers, counters)
843 *
844 * The return value is the disposition of the chunk.
845 */
846sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
847 const struct sctp_association *asoc,
848 const sctp_subtype_t type, void *arg,
849 sctp_cmd_seq_t *commands)
850{
851 struct sctp_chunk *chunk = arg;
852 struct sctp_ulpevent *ev;
853
854 if (!sctp_vtag_verify(chunk, asoc))
855 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
856
857 /* Verify that the chunk length for the COOKIE-ACK is OK.
858 * If we don't do this, any bundled chunks may be junked.
859 */
860 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
861 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
862 commands);
863
864 /* Reset init error count upon receipt of COOKIE-ACK,
865 * to avoid problems with the managemement of this
866 * counter in stale cookie situations when a transition back
867 * from the COOKIE-ECHOED state to the COOKIE-WAIT
868 * state is performed.
869 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700870 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 /* RFC 2960 5.1 Normal Establishment of an Association
873 *
874 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
875 * from the COOKIE-ECHOED state to the ESTABLISHED state,
876 * stopping the T1-cookie timer.
877 */
878 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
879 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
880 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
881 SCTP_STATE(SCTP_STATE_ESTABLISHED));
882 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
883 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
884 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
885 if (asoc->autoclose)
886 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
887 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 /* It may also notify its ULP about the successful
890 * establishment of the association with a Communication Up
891 * notification (see Section 10).
892 */
893 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
894 0, asoc->c.sinit_num_ostreams,
895 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700896 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (!ev)
899 goto nomem;
900
901 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
902
903 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800904 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800906 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800908 if (asoc->peer.adaptation_ind) {
909 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!ev)
911 goto nomem;
912
913 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
914 SCTP_ULPEVENT(ev));
915 }
916
917 return SCTP_DISPOSITION_CONSUME;
918nomem:
919 return SCTP_DISPOSITION_NOMEM;
920}
921
922/* Generate and sendout a heartbeat packet. */
923static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
924 const struct sctp_association *asoc,
925 const sctp_subtype_t type,
926 void *arg,
927 sctp_cmd_seq_t *commands)
928{
929 struct sctp_transport *transport = (struct sctp_transport *) arg;
930 struct sctp_chunk *reply;
931 sctp_sender_hb_info_t hbinfo;
932 size_t paylen = 0;
933
934 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
935 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
Al Viro63de08f2006-11-20 17:07:25 -0800936 hbinfo.daddr = transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 hbinfo.sent_at = jiffies;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700938 hbinfo.hb_nonce = transport->hb_nonce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /* Send a heartbeat to our peer. */
941 paylen = sizeof(sctp_sender_hb_info_t);
942 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
943 if (!reply)
944 return SCTP_DISPOSITION_NOMEM;
945
946 /* Set rto_pending indicating that an RTT measurement
947 * is started with this heartbeat chunk.
948 */
949 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
950 SCTP_TRANSPORT(transport));
951
952 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
953 return SCTP_DISPOSITION_CONSUME;
954}
955
956/* Generate a HEARTBEAT packet on the given transport. */
957sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
958 const struct sctp_association *asoc,
959 const sctp_subtype_t type,
960 void *arg,
961 sctp_cmd_seq_t *commands)
962{
963 struct sctp_transport *transport = (struct sctp_transport *) arg;
964
Vlad Yasevichfd102792007-11-15 12:13:32 -0500965 if (asoc->overall_error_count > asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700966 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
967 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
969 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800970 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
972 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
973 return SCTP_DISPOSITION_DELETE_TCB;
974 }
975
976 /* Section 3.3.5.
977 * The Sender-specific Heartbeat Info field should normally include
978 * information about the sender's current time when this HEARTBEAT
979 * chunk is sent and the destination transport address to which this
980 * HEARTBEAT is sent (see Section 8.3).
981 */
982
Frank Filz52ccb8e2005-12-22 11:36:46 -0800983 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 if (SCTP_DISPOSITION_NOMEM ==
985 sctp_sf_heartbeat(ep, asoc, type, arg,
986 commands))
987 return SCTP_DISPOSITION_NOMEM;
988 /* Set transport error counter and association error counter
989 * when sending heartbeat.
990 */
991 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
992 SCTP_TRANSPORT(transport));
993 }
994 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
995 SCTP_TRANSPORT(transport));
996
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900997 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/*
1001 * Process an heartbeat request.
1002 *
1003 * Section: 8.3 Path Heartbeat
1004 * The receiver of the HEARTBEAT should immediately respond with a
1005 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1006 * from the received HEARTBEAT chunk.
1007 *
1008 * Verification Tag: 8.5 Verification Tag [Normal verification]
1009 * When receiving an SCTP packet, the endpoint MUST ensure that the
1010 * value in the Verification Tag field of the received SCTP packet
1011 * matches its own Tag. If the received Verification Tag value does not
1012 * match the receiver's own tag value, the receiver shall silently
1013 * discard the packet and shall not process it any further except for
1014 * those cases listed in Section 8.5.1 below.
1015 *
1016 * Inputs
1017 * (endpoint, asoc, chunk)
1018 *
1019 * Outputs
1020 * (asoc, reply_msg, msg_up, timers, counters)
1021 *
1022 * The return value is the disposition of the chunk.
1023 */
1024sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1025 const struct sctp_association *asoc,
1026 const sctp_subtype_t type,
1027 void *arg,
1028 sctp_cmd_seq_t *commands)
1029{
1030 struct sctp_chunk *chunk = arg;
1031 struct sctp_chunk *reply;
1032 size_t paylen = 0;
1033
1034 if (!sctp_vtag_verify(chunk, asoc))
1035 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1036
1037 /* Make sure that the HEARTBEAT chunk has a valid length. */
1038 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1039 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1040 commands);
1041
1042 /* 8.3 The receiver of the HEARTBEAT should immediately
1043 * respond with a HEARTBEAT ACK that contains the Heartbeat
1044 * Information field copied from the received HEARTBEAT chunk.
1045 */
1046 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1047 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001048 if (!pskb_pull(chunk->skb, paylen))
1049 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 reply = sctp_make_heartbeat_ack(asoc, chunk,
1052 chunk->subh.hb_hdr, paylen);
1053 if (!reply)
1054 goto nomem;
1055
1056 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1057 return SCTP_DISPOSITION_CONSUME;
1058
1059nomem:
1060 return SCTP_DISPOSITION_NOMEM;
1061}
1062
1063/*
1064 * Process the returning HEARTBEAT ACK.
1065 *
1066 * Section: 8.3 Path Heartbeat
1067 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1068 * should clear the error counter of the destination transport
1069 * address to which the HEARTBEAT was sent, and mark the destination
1070 * transport address as active if it is not so marked. The endpoint may
1071 * optionally report to the upper layer when an inactive destination
1072 * address is marked as active due to the reception of the latest
1073 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1074 * clear the association overall error count as well (as defined
1075 * in section 8.1).
1076 *
1077 * The receiver of the HEARTBEAT ACK should also perform an RTT
1078 * measurement for that destination transport address using the time
1079 * value carried in the HEARTBEAT ACK chunk.
1080 *
1081 * Verification Tag: 8.5 Verification Tag [Normal verification]
1082 *
1083 * Inputs
1084 * (endpoint, asoc, chunk)
1085 *
1086 * Outputs
1087 * (asoc, reply_msg, msg_up, timers, counters)
1088 *
1089 * The return value is the disposition of the chunk.
1090 */
1091sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1092 const struct sctp_association *asoc,
1093 const sctp_subtype_t type,
1094 void *arg,
1095 sctp_cmd_seq_t *commands)
1096{
1097 struct sctp_chunk *chunk = arg;
1098 union sctp_addr from_addr;
1099 struct sctp_transport *link;
1100 sctp_sender_hb_info_t *hbinfo;
1101 unsigned long max_interval;
1102
1103 if (!sctp_vtag_verify(chunk, asoc))
1104 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1105
1106 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1107 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1108 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1109 commands);
1110
1111 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001112 /* Make sure that the length of the parameter is what we expect */
1113 if (ntohs(hbinfo->param_hdr.length) !=
1114 sizeof(sctp_sender_hb_info_t)) {
1115 return SCTP_DISPOSITION_DISCARD;
1116 }
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001119 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001122 if (unlikely(!link)) {
1123 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001124 if (net_ratelimit())
1125 printk(KERN_WARNING
1126 "%s association %p could not find address "
1127 NIP6_FMT "\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001128 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001129 asoc,
1130 NIP6(from_addr.v6.sin6_addr));
Frank Filz3f7a87d2005-06-20 13:14:57 -07001131 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001132 if (net_ratelimit())
1133 printk(KERN_WARNING
1134 "%s association %p could not find address "
1135 NIPQUAD_FMT "\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001136 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001137 asoc,
1138 NIPQUAD(from_addr.v4.sin_addr.s_addr));
Frank Filz3f7a87d2005-06-20 13:14:57 -07001139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return SCTP_DISPOSITION_DISCARD;
1141 }
1142
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001143 /* Validate the 64-bit random nonce. */
1144 if (hbinfo->hb_nonce != link->hb_nonce)
1145 return SCTP_DISPOSITION_DISCARD;
1146
Frank Filz52ccb8e2005-12-22 11:36:46 -08001147 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /* Check if the timestamp looks valid. */
1150 if (time_after(hbinfo->sent_at, jiffies) ||
1151 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001152 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001154 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 return SCTP_DISPOSITION_DISCARD;
1156 }
1157
1158 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1159 * the HEARTBEAT should clear the error counter of the
1160 * destination transport address to which the HEARTBEAT was
1161 * sent and mark the destination transport address as active if
1162 * it is not so marked.
1163 */
1164 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1165
1166 return SCTP_DISPOSITION_CONSUME;
1167}
1168
1169/* Helper function to send out an abort for the restart
1170 * condition.
1171 */
1172static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1173 struct sctp_chunk *init,
1174 sctp_cmd_seq_t *commands)
1175{
1176 int len;
1177 struct sctp_packet *pkt;
1178 union sctp_addr_param *addrparm;
1179 struct sctp_errhdr *errhdr;
1180 struct sctp_endpoint *ep;
1181 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1182 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1183
1184 /* Build the error on the stack. We are way to malloc crazy
1185 * throughout the code today.
1186 */
1187 errhdr = (struct sctp_errhdr *)buffer;
1188 addrparm = (union sctp_addr_param *)errhdr->variable;
1189
1190 /* Copy into a parm format. */
1191 len = af->to_addr_param(ssa, addrparm);
1192 len += sizeof(sctp_errhdr_t);
1193
1194 errhdr->cause = SCTP_ERROR_RESTART;
1195 errhdr->length = htons(len);
1196
1197 /* Assign to the control socket. */
1198 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1199
1200 /* Association is NULL since this may be a restart attack and we
1201 * want to send back the attacker's vtag.
1202 */
1203 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1204
1205 if (!pkt)
1206 goto out;
1207 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1208
1209 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1210
1211 /* Discard the rest of the inbound packet. */
1212 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1213
1214out:
1215 /* Even if there is no memory, treat as a failure so
1216 * the packet will get dropped.
1217 */
1218 return 0;
1219}
1220
1221/* A restart is occurring, check to make sure no new addresses
1222 * are being added as we may be under a takeover attack.
1223 */
1224static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1225 const struct sctp_association *asoc,
1226 struct sctp_chunk *init,
1227 sctp_cmd_seq_t *commands)
1228{
1229 struct sctp_transport *new_addr, *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 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
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001246 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1247 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 found = 0;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001249 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1250 transports) {
Al Viro5f242a12006-11-20 17:05:23 -08001251 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1252 &addr->ipaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 found = 1;
1254 break;
1255 }
1256 }
1257 if (!found)
1258 break;
1259 }
1260
1261 /* If a new address was added, ABORT the sender. */
1262 if (!found && new_addr) {
Al Viro7dd8a582006-11-20 17:23:25 -08001263 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
1266 /* Return success if all addresses were found. */
1267 return found;
1268}
1269
1270/* Populate the verification/tie tags based on overlapping INIT
1271 * scenario.
1272 *
1273 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1274 */
1275static void sctp_tietags_populate(struct sctp_association *new_asoc,
1276 const struct sctp_association *asoc)
1277{
1278 switch (asoc->state) {
1279
1280 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1281
1282 case SCTP_STATE_COOKIE_WAIT:
1283 new_asoc->c.my_vtag = asoc->c.my_vtag;
1284 new_asoc->c.my_ttag = asoc->c.my_vtag;
1285 new_asoc->c.peer_ttag = 0;
1286 break;
1287
1288 case SCTP_STATE_COOKIE_ECHOED:
1289 new_asoc->c.my_vtag = asoc->c.my_vtag;
1290 new_asoc->c.my_ttag = asoc->c.my_vtag;
1291 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1292 break;
1293
1294 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1295 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1296 */
1297 default:
1298 new_asoc->c.my_ttag = asoc->c.my_vtag;
1299 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1300 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* Other parameters for the endpoint SHOULD be copied from the
1304 * existing parameters of the association (e.g. number of
1305 * outbound streams) into the INIT ACK and cookie.
1306 */
1307 new_asoc->rwnd = asoc->rwnd;
1308 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1309 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1310 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1311}
1312
1313/*
1314 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1315 * handling action.
1316 *
1317 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1318 *
1319 * Returns value representing action to be taken. These action values
1320 * correspond to Action/Description values in RFC 2960, Table 2.
1321 */
1322static char sctp_tietags_compare(struct sctp_association *new_asoc,
1323 const struct sctp_association *asoc)
1324{
1325 /* In this case, the peer may have restarted. */
1326 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1327 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1328 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1329 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1330 return 'A';
1331
1332 /* Collision case B. */
1333 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1334 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1335 (0 == asoc->c.peer_vtag))) {
1336 return 'B';
1337 }
1338
1339 /* Collision case D. */
1340 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1341 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1342 return 'D';
1343
1344 /* Collision case C. */
1345 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1346 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1347 (0 == new_asoc->c.my_ttag) &&
1348 (0 == new_asoc->c.peer_ttag))
1349 return 'C';
1350
1351 /* No match to any of the special cases; discard this packet. */
1352 return 'E';
1353}
1354
1355/* Common helper routine for both duplicate and simulataneous INIT
1356 * chunk handling.
1357 */
1358static sctp_disposition_t sctp_sf_do_unexpected_init(
1359 const struct sctp_endpoint *ep,
1360 const struct sctp_association *asoc,
1361 const sctp_subtype_t type,
1362 void *arg, sctp_cmd_seq_t *commands)
1363{
1364 sctp_disposition_t retval;
1365 struct sctp_chunk *chunk = arg;
1366 struct sctp_chunk *repl;
1367 struct sctp_association *new_asoc;
1368 struct sctp_chunk *err_chunk;
1369 struct sctp_packet *packet;
1370 sctp_unrecognized_param_t *unk_param;
1371 int len;
1372
1373 /* 6.10 Bundling
1374 * An endpoint MUST NOT bundle INIT, INIT ACK or
1375 * SHUTDOWN COMPLETE with any other chunks.
1376 *
1377 * IG Section 2.11.2
1378 * Furthermore, we require that the receiver of an INIT chunk MUST
1379 * enforce these rules by silently discarding an arriving packet
1380 * with an INIT chunk that is bundled with other chunks.
1381 */
1382 if (!chunk->singleton)
1383 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1384
1385 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001386 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 */
1388 if (chunk->sctp_hdr->vtag != 0)
1389 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1390
1391 /* Make sure that the INIT chunk has a valid length.
1392 * In this case, we generate a protocol violation since we have
1393 * an association established.
1394 */
1395 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1396 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1397 commands);
1398 /* Grab the INIT header. */
1399 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1400
1401 /* Tag the variable length parameters. */
1402 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1403
1404 /* Verify the INIT chunk before processing it. */
1405 err_chunk = NULL;
1406 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1407 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1408 &err_chunk)) {
1409 /* This chunk contains fatal error. It is to be discarded.
1410 * Send an ABORT, with causes if there is any.
1411 */
1412 if (err_chunk) {
1413 packet = sctp_abort_pkt_new(ep, asoc, arg,
1414 (__u8 *)(err_chunk->chunk_hdr) +
1415 sizeof(sctp_chunkhdr_t),
1416 ntohs(err_chunk->chunk_hdr->length) -
1417 sizeof(sctp_chunkhdr_t));
1418
1419 if (packet) {
1420 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1421 SCTP_PACKET(packet));
1422 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1423 retval = SCTP_DISPOSITION_CONSUME;
1424 } else {
1425 retval = SCTP_DISPOSITION_NOMEM;
1426 }
1427 goto cleanup;
1428 } else {
1429 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1430 commands);
1431 }
1432 }
1433
1434 /*
1435 * Other parameters for the endpoint SHOULD be copied from the
1436 * existing parameters of the association (e.g. number of
1437 * outbound streams) into the INIT ACK and cookie.
1438 * FIXME: We are copying parameters from the endpoint not the
1439 * association.
1440 */
1441 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1442 if (!new_asoc)
1443 goto nomem;
1444
1445 /* In the outbound INIT ACK the endpoint MUST copy its current
1446 * Verification Tag and Peers Verification tag into a reserved
1447 * place (local tie-tag and per tie-tag) within the state cookie.
1448 */
1449 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1450 sctp_source(chunk),
1451 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001452 GFP_ATOMIC))
1453 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* Make sure no new addresses are being added during the
1456 * restart. Do not do this check for COOKIE-WAIT state,
1457 * since there are no peer addresses to check against.
1458 * Upon return an ABORT will have been sent if needed.
1459 */
1460 if (!sctp_state(asoc, COOKIE_WAIT)) {
1461 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1462 commands)) {
1463 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001464 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 }
1467
1468 sctp_tietags_populate(new_asoc, asoc);
1469
1470 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1471
1472 /* If there are errors need to be reported for unknown parameters,
1473 * make sure to reserve enough room in the INIT ACK for them.
1474 */
1475 len = 0;
1476 if (err_chunk) {
1477 len = ntohs(err_chunk->chunk_hdr->length) -
1478 sizeof(sctp_chunkhdr_t);
1479 }
1480
1481 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1482 goto nomem;
1483
1484 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1485 if (!repl)
1486 goto nomem;
1487
1488 /* If there are errors need to be reported for unknown parameters,
1489 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1490 * parameter.
1491 */
1492 if (err_chunk) {
1493 /* Get the "Unrecognized parameter" parameter(s) out of the
1494 * ERROR chunk generated by sctp_verify_init(). Since the
1495 * error cause code for "unknown parameter" and the
1496 * "Unrecognized parameter" type is the same, we can
1497 * construct the parameters in INIT ACK by copying the
1498 * ERROR causes over.
1499 */
1500 unk_param = (sctp_unrecognized_param_t *)
1501 ((__u8 *)(err_chunk->chunk_hdr) +
1502 sizeof(sctp_chunkhdr_t));
1503 /* Replace the cause code with the "Unrecognized parameter"
1504 * parameter type.
1505 */
1506 sctp_addto_chunk(repl, len, unk_param);
1507 }
1508
1509 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1510 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1511
1512 /*
1513 * Note: After sending out INIT ACK with the State Cookie parameter,
1514 * "Z" MUST NOT allocate any resources for this new association.
1515 * Otherwise, "Z" will be vulnerable to resource attacks.
1516 */
1517 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1518 retval = SCTP_DISPOSITION_CONSUME;
1519
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001520 return retval;
1521
1522nomem:
1523 retval = SCTP_DISPOSITION_NOMEM;
1524nomem_retval:
1525 if (new_asoc)
1526 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527cleanup:
1528 if (err_chunk)
1529 sctp_chunk_free(err_chunk);
1530 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
1533/*
1534 * Handle simultanous INIT.
1535 * This means we started an INIT and then we got an INIT request from
1536 * our peer.
1537 *
1538 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1539 * This usually indicates an initialization collision, i.e., each
1540 * endpoint is attempting, at about the same time, to establish an
1541 * association with the other endpoint.
1542 *
1543 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1544 * endpoint MUST respond with an INIT ACK using the same parameters it
1545 * sent in its original INIT chunk (including its Verification Tag,
1546 * unchanged). These original parameters are combined with those from the
1547 * newly received INIT chunk. The endpoint shall also generate a State
1548 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1549 * INIT to calculate the State Cookie.
1550 *
1551 * After that, the endpoint MUST NOT change its state, the T1-init
1552 * timer shall be left running and the corresponding TCB MUST NOT be
1553 * destroyed. The normal procedures for handling State Cookies when
1554 * a TCB exists will resolve the duplicate INITs to a single association.
1555 *
1556 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1557 * its Tie-Tags with the Tag information of itself and its peer (see
1558 * section 5.2.2 for a description of the Tie-Tags).
1559 *
1560 * Verification Tag: Not explicit, but an INIT can not have a valid
1561 * verification tag, so we skip the check.
1562 *
1563 * Inputs
1564 * (endpoint, asoc, chunk)
1565 *
1566 * Outputs
1567 * (asoc, reply_msg, msg_up, timers, counters)
1568 *
1569 * The return value is the disposition of the chunk.
1570 */
1571sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1572 const struct sctp_association *asoc,
1573 const sctp_subtype_t type,
1574 void *arg,
1575 sctp_cmd_seq_t *commands)
1576{
1577 /* Call helper to do the real work for both simulataneous and
1578 * duplicate INIT chunk handling.
1579 */
1580 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1581}
1582
1583/*
1584 * Handle duplicated INIT messages. These are usually delayed
1585 * restransmissions.
1586 *
1587 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1588 * COOKIE-ECHOED and COOKIE-WAIT
1589 *
1590 * Unless otherwise stated, upon reception of an unexpected INIT for
1591 * this association, the endpoint shall generate an INIT ACK with a
1592 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1593 * current Verification Tag and peer's Verification Tag into a reserved
1594 * place within the state cookie. We shall refer to these locations as
1595 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1596 * containing this INIT ACK MUST carry a Verification Tag value equal to
1597 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1598 * MUST contain a new Initiation Tag (randomly generated see Section
1599 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1600 * existing parameters of the association (e.g. number of outbound
1601 * streams) into the INIT ACK and cookie.
1602 *
1603 * After sending out the INIT ACK, the endpoint shall take no further
1604 * actions, i.e., the existing association, including its current state,
1605 * and the corresponding TCB MUST NOT be changed.
1606 *
1607 * Note: Only when a TCB exists and the association is not in a COOKIE-
1608 * WAIT state are the Tie-Tags populated. For a normal association INIT
1609 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1610 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1611 * State Cookie are populated as specified in section 5.2.1.
1612 *
1613 * Verification Tag: Not specified, but an INIT has no way of knowing
1614 * what the verification tag could be, so we ignore it.
1615 *
1616 * Inputs
1617 * (endpoint, asoc, chunk)
1618 *
1619 * Outputs
1620 * (asoc, reply_msg, msg_up, timers, counters)
1621 *
1622 * The return value is the disposition of the chunk.
1623 */
1624sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1625 const struct sctp_association *asoc,
1626 const sctp_subtype_t type,
1627 void *arg,
1628 sctp_cmd_seq_t *commands)
1629{
1630 /* Call helper to do the real work for both simulataneous and
1631 * duplicate INIT chunk handling.
1632 */
1633 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1634}
1635
1636
Vlad Yasevich610ab732007-01-15 19:18:30 -08001637/*
1638 * Unexpected INIT-ACK handler.
1639 *
1640 * Section 5.2.3
1641 * If an INIT ACK received by an endpoint in any state other than the
1642 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1643 * An unexpected INIT ACK usually indicates the processing of an old or
1644 * duplicated INIT chunk.
1645*/
1646sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1647 const struct sctp_association *asoc,
1648 const sctp_subtype_t type,
1649 void *arg, sctp_cmd_seq_t *commands)
1650{
1651 /* Per the above section, we'll discard the chunk if we have an
1652 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1653 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001654 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001655 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1656 else
1657 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1658}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1661 *
1662 * Section 5.2.4
1663 * A) In this case, the peer may have restarted.
1664 */
1665static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1666 const struct sctp_association *asoc,
1667 struct sctp_chunk *chunk,
1668 sctp_cmd_seq_t *commands,
1669 struct sctp_association *new_asoc)
1670{
1671 sctp_init_chunk_t *peer_init;
1672 struct sctp_ulpevent *ev;
1673 struct sctp_chunk *repl;
1674 struct sctp_chunk *err;
1675 sctp_disposition_t disposition;
1676
1677 /* new_asoc is a brand-new association, so these are not yet
1678 * side effects--it is safe to run them here.
1679 */
1680 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1681
1682 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1683 sctp_source(chunk), peer_init,
1684 GFP_ATOMIC))
1685 goto nomem;
1686
1687 /* Make sure no new addresses are being added during the
1688 * restart. Though this is a pretty complicated attack
1689 * since you'd have to get inside the cookie.
1690 */
1691 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1692 return SCTP_DISPOSITION_CONSUME;
1693 }
1694
1695 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1696 * the peer has restarted (Action A), it MUST NOT setup a new
1697 * association but instead resend the SHUTDOWN ACK and send an ERROR
1698 * chunk with a "Cookie Received while Shutting Down" error cause to
1699 * its peer.
1700 */
1701 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1702 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1703 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1704 chunk, commands);
1705 if (SCTP_DISPOSITION_NOMEM == disposition)
1706 goto nomem;
1707
1708 err = sctp_make_op_error(asoc, chunk,
1709 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1710 NULL, 0);
1711 if (err)
1712 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1713 SCTP_CHUNK(err));
1714
1715 return SCTP_DISPOSITION_CONSUME;
1716 }
1717
1718 /* For now, fail any unsent/unacked data. Consider the optional
1719 * choice of resending of this data.
1720 */
1721 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 repl = sctp_make_cookie_ack(new_asoc, chunk);
1724 if (!repl)
1725 goto nomem;
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /* Report association restart to upper layer. */
1728 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1729 new_asoc->c.sinit_num_ostreams,
1730 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001731 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (!ev)
1733 goto nomem_ev;
1734
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001735 /* Update the content of current association. */
1736 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1737 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1739 return SCTP_DISPOSITION_CONSUME;
1740
1741nomem_ev:
1742 sctp_chunk_free(repl);
1743nomem:
1744 return SCTP_DISPOSITION_NOMEM;
1745}
1746
1747/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1748 *
1749 * Section 5.2.4
1750 * B) In this case, both sides may be attempting to start an association
1751 * at about the same time but the peer endpoint started its INIT
1752 * after responding to the local endpoint's INIT
1753 */
1754/* This case represents an initialization collision. */
1755static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1756 const struct sctp_association *asoc,
1757 struct sctp_chunk *chunk,
1758 sctp_cmd_seq_t *commands,
1759 struct sctp_association *new_asoc)
1760{
1761 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct sctp_chunk *repl;
1763
1764 /* new_asoc is a brand-new association, so these are not yet
1765 * side effects--it is safe to run them here.
1766 */
1767 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1768 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1769 sctp_source(chunk), peer_init,
1770 GFP_ATOMIC))
1771 goto nomem;
1772
1773 /* Update the content of current association. */
1774 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1775 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1776 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1777 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1778 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1779
1780 repl = sctp_make_cookie_ack(new_asoc, chunk);
1781 if (!repl)
1782 goto nomem;
1783
1784 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* RFC 2960 5.1 Normal Establishment of an Association
1787 *
1788 * D) IMPLEMENTATION NOTE: An implementation may choose to
1789 * send the Communication Up notification to the SCTP user
1790 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001791 *
1792 * Sadly, this needs to be implemented as a side-effect, because
1793 * we are not guaranteed to have set the association id of the real
1794 * association and so these notifications need to be delayed until
1795 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Vlad Yasevich07d93962007-05-04 13:55:27 -07001798 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
1800 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001801 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001803 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001804 *
1805 * This also needs to be done as a side effect for the same reason as
1806 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001808 if (asoc->peer.adaptation_ind)
1809 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 return SCTP_DISPOSITION_CONSUME;
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813nomem:
1814 return SCTP_DISPOSITION_NOMEM;
1815}
1816
1817/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1818 *
1819 * Section 5.2.4
1820 * C) In this case, the local endpoint's cookie has arrived late.
1821 * Before it arrived, the local endpoint sent an INIT and received an
1822 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1823 * but a new tag of its own.
1824 */
1825/* This case represents an initialization collision. */
1826static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1827 const struct sctp_association *asoc,
1828 struct sctp_chunk *chunk,
1829 sctp_cmd_seq_t *commands,
1830 struct sctp_association *new_asoc)
1831{
1832 /* The cookie should be silently discarded.
1833 * The endpoint SHOULD NOT change states and should leave
1834 * any timers running.
1835 */
1836 return SCTP_DISPOSITION_DISCARD;
1837}
1838
1839/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1840 *
1841 * Section 5.2.4
1842 *
1843 * D) When both local and remote tags match the endpoint should always
1844 * enter the ESTABLISHED state, if it has not already done so.
1845 */
1846/* This case represents an initialization collision. */
1847static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1848 const struct sctp_association *asoc,
1849 struct sctp_chunk *chunk,
1850 sctp_cmd_seq_t *commands,
1851 struct sctp_association *new_asoc)
1852{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001853 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct sctp_chunk *repl;
1855
1856 /* Clarification from Implementor's Guide:
1857 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001858 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1859 * It should stop any cookie timer that may be running and send
1860 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 */
1862
1863 /* Don't accidentally move back into established state. */
1864 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1865 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1866 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1867 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1868 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1869 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1870 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1871 SCTP_NULL());
1872
1873 /* RFC 2960 5.1 Normal Establishment of an Association
1874 *
1875 * D) IMPLEMENTATION NOTE: An implementation may choose
1876 * to send the Communication Up notification to the
1877 * SCTP user upon reception of a valid COOKIE
1878 * ECHO chunk.
1879 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001880 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001882 asoc->c.sinit_num_ostreams,
1883 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001884 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (!ev)
1886 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001889 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001891 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001893 if (asoc->peer.adaptation_ind) {
1894 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001896 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto nomem;
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 repl = sctp_make_cookie_ack(new_asoc, chunk);
1903 if (!repl)
1904 goto nomem;
1905
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001906 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1907
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001908 if (ev)
1909 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1910 SCTP_ULPEVENT(ev));
1911 if (ai_ev)
1912 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1913 SCTP_ULPEVENT(ai_ev));
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 return SCTP_DISPOSITION_CONSUME;
1916
1917nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001918 if (ai_ev)
1919 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 if (ev)
1921 sctp_ulpevent_free(ev);
1922 return SCTP_DISPOSITION_NOMEM;
1923}
1924
1925/*
1926 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1927 * chunk was retransmitted and then delayed in the network.
1928 *
1929 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1930 *
1931 * Verification Tag: None. Do cookie validation.
1932 *
1933 * Inputs
1934 * (endpoint, asoc, chunk)
1935 *
1936 * Outputs
1937 * (asoc, reply_msg, msg_up, timers, counters)
1938 *
1939 * The return value is the disposition of the chunk.
1940 */
1941sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1942 const struct sctp_association *asoc,
1943 const sctp_subtype_t type,
1944 void *arg,
1945 sctp_cmd_seq_t *commands)
1946{
1947 sctp_disposition_t retval;
1948 struct sctp_chunk *chunk = arg;
1949 struct sctp_association *new_asoc;
1950 int error = 0;
1951 char action;
1952 struct sctp_chunk *err_chk_p;
1953
1954 /* Make sure that the chunk has a valid length from the protocol
1955 * perspective. In this case check to make sure we have at least
1956 * enough for the chunk header. Cookie length verification is
1957 * done later.
1958 */
1959 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1960 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1961 commands);
1962
1963 /* "Decode" the chunk. We have no optional parameters so we
1964 * are in good shape.
1965 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001966 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001967 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1968 sizeof(sctp_chunkhdr_t)))
1969 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1972 * of a duplicate COOKIE ECHO match the Verification Tags of the
1973 * current association, consider the State Cookie valid even if
1974 * the lifespan is exceeded.
1975 */
1976 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1977 &err_chk_p);
1978
1979 /* FIXME:
1980 * If the re-build failed, what is the proper error path
1981 * from here?
1982 *
1983 * [We should abort the association. --piggy]
1984 */
1985 if (!new_asoc) {
1986 /* FIXME: Several errors are possible. A bad cookie should
1987 * be silently discarded, but think about logging it too.
1988 */
1989 switch (error) {
1990 case -SCTP_IERROR_NOMEM:
1991 goto nomem;
1992
1993 case -SCTP_IERROR_STALE_COOKIE:
1994 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
1995 err_chk_p);
1996 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1997 case -SCTP_IERROR_BAD_SIG:
1998 default:
1999 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 }
2002
2003 /* Compare the tie_tag in cookie with the verification tag of
2004 * current association.
2005 */
2006 action = sctp_tietags_compare(new_asoc, asoc);
2007
2008 switch (action) {
2009 case 'A': /* Association restart. */
2010 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2011 new_asoc);
2012 break;
2013
2014 case 'B': /* Collision case B. */
2015 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2016 new_asoc);
2017 break;
2018
2019 case 'C': /* Collision case C. */
2020 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2021 new_asoc);
2022 break;
2023
2024 case 'D': /* Collision case D. */
2025 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2026 new_asoc);
2027 break;
2028
2029 default: /* Discard packet for all others. */
2030 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2031 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 /* Delete the tempory new association. */
2035 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2036 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2037
2038 return retval;
2039
2040nomem:
2041 return SCTP_DISPOSITION_NOMEM;
2042}
2043
2044/*
2045 * Process an ABORT. (SHUTDOWN-PENDING state)
2046 *
2047 * See sctp_sf_do_9_1_abort().
2048 */
2049sctp_disposition_t sctp_sf_shutdown_pending_abort(
2050 const struct sctp_endpoint *ep,
2051 const struct sctp_association *asoc,
2052 const sctp_subtype_t type,
2053 void *arg,
2054 sctp_cmd_seq_t *commands)
2055{
2056 struct sctp_chunk *chunk = arg;
2057
2058 if (!sctp_vtag_verify_either(chunk, asoc))
2059 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2060
2061 /* Make sure that the ABORT chunk has a valid length.
2062 * Since this is an ABORT chunk, we have to discard it
2063 * because of the following text:
2064 * RFC 2960, Section 3.3.7
2065 * If an endpoint receives an ABORT with a format error or for an
2066 * association that doesn't exist, it MUST silently discard it.
2067 * Becasue the length is "invalid", we can't really discard just
2068 * as we do not know its true length. So, to be safe, discard the
2069 * packet.
2070 */
2071 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2072 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2073
Vlad Yasevich75205f42007-12-20 14:12:59 -08002074 /* ADD-IP: Special case for ABORT chunks
2075 * F4) One special consideration is that ABORT Chunks arriving
2076 * destined to the IP address being deleted MUST be
2077 * ignored (see Section 5.3.1 for further details).
2078 */
2079 if (SCTP_ADDR_DEL ==
2080 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2081 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2082
Vlad Yasevich75205f42007-12-20 14:12:59 -08002083 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
2086/*
2087 * Process an ABORT. (SHUTDOWN-SENT state)
2088 *
2089 * See sctp_sf_do_9_1_abort().
2090 */
2091sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2092 const struct sctp_association *asoc,
2093 const sctp_subtype_t type,
2094 void *arg,
2095 sctp_cmd_seq_t *commands)
2096{
2097 struct sctp_chunk *chunk = arg;
2098
2099 if (!sctp_vtag_verify_either(chunk, asoc))
2100 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2101
2102 /* Make sure that the ABORT chunk has a valid length.
2103 * Since this is an ABORT chunk, we have to discard it
2104 * because of the following text:
2105 * RFC 2960, Section 3.3.7
2106 * If an endpoint receives an ABORT with a format error or for an
2107 * association that doesn't exist, it MUST silently discard it.
2108 * Becasue the length is "invalid", we can't really discard just
2109 * as we do not know its true length. So, to be safe, discard the
2110 * packet.
2111 */
2112 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2113 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2114
Vlad Yasevich75205f42007-12-20 14:12:59 -08002115 /* ADD-IP: Special case for ABORT chunks
2116 * F4) One special consideration is that ABORT Chunks arriving
2117 * destined to the IP address being deleted MUST be
2118 * ignored (see Section 5.3.1 for further details).
2119 */
2120 if (SCTP_ADDR_DEL ==
2121 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2122 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /* Stop the T2-shutdown timer. */
2125 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2126 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2127
2128 /* Stop the T5-shutdown guard timer. */
2129 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2130 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2131
Vlad Yasevich75205f42007-12-20 14:12:59 -08002132 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135/*
2136 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2137 *
2138 * See sctp_sf_do_9_1_abort().
2139 */
2140sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2141 const struct sctp_endpoint *ep,
2142 const struct sctp_association *asoc,
2143 const sctp_subtype_t type,
2144 void *arg,
2145 sctp_cmd_seq_t *commands)
2146{
2147 /* The same T2 timer, so we should be able to use
2148 * common function with the SHUTDOWN-SENT state.
2149 */
2150 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2151}
2152
2153/*
2154 * Handle an Error received in COOKIE_ECHOED state.
2155 *
2156 * Only handle the error type of stale COOKIE Error, the other errors will
2157 * be ignored.
2158 *
2159 * Inputs
2160 * (endpoint, asoc, chunk)
2161 *
2162 * Outputs
2163 * (asoc, reply_msg, msg_up, timers, counters)
2164 *
2165 * The return value is the disposition of the chunk.
2166 */
2167sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2168 const struct sctp_association *asoc,
2169 const sctp_subtype_t type,
2170 void *arg,
2171 sctp_cmd_seq_t *commands)
2172{
2173 struct sctp_chunk *chunk = arg;
2174 sctp_errhdr_t *err;
2175
2176 if (!sctp_vtag_verify(chunk, asoc))
2177 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2178
2179 /* Make sure that the ERROR chunk has a valid length.
2180 * The parameter walking depends on this as well.
2181 */
2182 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2183 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2184 commands);
2185
2186 /* Process the error here */
2187 /* FUTURE FIXME: When PR-SCTP related and other optional
2188 * parms are emitted, this will have to change to handle multiple
2189 * errors.
2190 */
2191 sctp_walk_errors(err, chunk->chunk_hdr) {
2192 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002193 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 arg, commands);
2195 }
2196
2197 /* It is possible to have malformed error causes, and that
2198 * will cause us to end the walk early. However, since
2199 * we are discarding the packet, there should be no adverse
2200 * affects.
2201 */
2202 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2203}
2204
2205/*
2206 * Handle a Stale COOKIE Error
2207 *
2208 * Section: 5.2.6 Handle Stale COOKIE Error
2209 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2210 * one of the following three alternatives.
2211 * ...
2212 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2213 * Preservative parameter requesting an extension to the lifetime of
2214 * the State Cookie. When calculating the time extension, an
2215 * implementation SHOULD use the RTT information measured based on the
2216 * previous COOKIE ECHO / ERROR exchange, and should add no more
2217 * than 1 second beyond the measured RTT, due to long State Cookie
2218 * lifetimes making the endpoint more subject to a replay attack.
2219 *
2220 * Verification Tag: Not explicit, but safe to ignore.
2221 *
2222 * Inputs
2223 * (endpoint, asoc, chunk)
2224 *
2225 * Outputs
2226 * (asoc, reply_msg, msg_up, timers, counters)
2227 *
2228 * The return value is the disposition of the chunk.
2229 */
2230static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2231 const struct sctp_association *asoc,
2232 const sctp_subtype_t type,
2233 void *arg,
2234 sctp_cmd_seq_t *commands)
2235{
2236 struct sctp_chunk *chunk = arg;
2237 time_t stale;
2238 sctp_cookie_preserve_param_t bht;
2239 sctp_errhdr_t *err;
2240 struct sctp_chunk *reply;
2241 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002242 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Vlad Yasevich81845c22006-01-30 15:59:54 -08002244 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002245 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2246 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002248 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return SCTP_DISPOSITION_DELETE_TCB;
2250 }
2251
2252 err = (sctp_errhdr_t *)(chunk->skb->data);
2253
2254 /* When calculating the time extension, an implementation
2255 * SHOULD use the RTT information measured based on the
2256 * previous COOKIE ECHO / ERROR exchange, and should add no
2257 * more than 1 second beyond the measured RTT, due to long
2258 * State Cookie lifetimes making the endpoint more subject to
2259 * a replay attack.
2260 * Measure of Staleness's unit is usec. (1/1000000 sec)
2261 * Suggested Cookie Life-span Increment's unit is msec.
2262 * (1/1000 sec)
2263 * In general, if you use the suggested cookie life, the value
2264 * found in the field of measure of staleness should be doubled
2265 * to give ample time to retransmit the new cookie and thus
2266 * yield a higher probability of success on the reattempt.
2267 */
Al Viro34bcca22006-11-20 17:27:15 -08002268 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 stale = (stale * 2) / 1000;
2270
2271 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2272 bht.param_hdr.length = htons(sizeof(bht));
2273 bht.lifespan_increment = htonl(stale);
2274
2275 /* Build that new INIT chunk. */
2276 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2277 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2278 if (!reply)
2279 goto nomem;
2280
2281 sctp_addto_chunk(reply, sizeof(bht), &bht);
2282
2283 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2284 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2285
2286 /* Stop pending T3-rtx and heartbeat timers */
2287 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2288 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2289
2290 /* Delete non-primary peer ip addresses since we are transitioning
2291 * back to the COOKIE-WAIT state
2292 */
2293 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2294
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002295 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2296 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002298 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 SCTP_TRANSPORT(asoc->peer.primary_path));
2300
2301 /* Cast away the const modifier, as we want to just
2302 * rerun it through as a sideffect.
2303 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002304 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2307 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2308 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2309 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2310 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2311 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2312
2313 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2314
2315 return SCTP_DISPOSITION_CONSUME;
2316
2317nomem:
2318 return SCTP_DISPOSITION_NOMEM;
2319}
2320
2321/*
2322 * Process an ABORT.
2323 *
2324 * Section: 9.1
2325 * After checking the Verification Tag, the receiving endpoint shall
2326 * remove the association from its record, and shall report the
2327 * termination to its upper layer.
2328 *
2329 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2330 * B) Rules for packet carrying ABORT:
2331 *
2332 * - The endpoint shall always fill in the Verification Tag field of the
2333 * outbound packet with the destination endpoint's tag value if it
2334 * is known.
2335 *
2336 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2337 * MUST follow the procedure described in Section 8.4.
2338 *
2339 * - The receiver MUST accept the packet if the Verification Tag
2340 * matches either its own tag, OR the tag of its peer. Otherwise, the
2341 * receiver MUST silently discard the packet and take no further
2342 * action.
2343 *
2344 * Inputs
2345 * (endpoint, asoc, chunk)
2346 *
2347 * Outputs
2348 * (asoc, reply_msg, msg_up, timers, counters)
2349 *
2350 * The return value is the disposition of the chunk.
2351 */
2352sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2353 const struct sctp_association *asoc,
2354 const sctp_subtype_t type,
2355 void *arg,
2356 sctp_cmd_seq_t *commands)
2357{
2358 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 if (!sctp_vtag_verify_either(chunk, asoc))
2361 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2362
2363 /* Make sure that the ABORT chunk has a valid length.
2364 * Since this is an ABORT chunk, we have to discard it
2365 * because of the following text:
2366 * RFC 2960, Section 3.3.7
2367 * If an endpoint receives an ABORT with a format error or for an
2368 * association that doesn't exist, it MUST silently discard it.
2369 * Becasue the length is "invalid", we can't really discard just
2370 * as we do not know its true length. So, to be safe, discard the
2371 * packet.
2372 */
2373 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2374 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2375
Vlad Yasevich75205f42007-12-20 14:12:59 -08002376 /* ADD-IP: Special case for ABORT chunks
2377 * F4) One special consideration is that ABORT Chunks arriving
2378 * destined to the IP address being deleted MUST be
2379 * ignored (see Section 5.3.1 for further details).
2380 */
2381 if (SCTP_ADDR_DEL ==
2382 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2383 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2384
2385 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2386}
2387
2388static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2389 const struct sctp_association *asoc,
2390 const sctp_subtype_t type,
2391 void *arg,
2392 sctp_cmd_seq_t *commands)
2393{
2394 struct sctp_chunk *chunk = arg;
2395 unsigned len;
2396 __be16 error = SCTP_ERROR_NO_ERROR;
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /* See if we have an error cause code in the chunk. */
2399 len = ntohs(chunk->chunk_hdr->length);
2400 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2401 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2402
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002403 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002404 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002405 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2407 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2408
2409 return SCTP_DISPOSITION_ABORT;
2410}
2411
2412/*
2413 * Process an ABORT. (COOKIE-WAIT state)
2414 *
2415 * See sctp_sf_do_9_1_abort() above.
2416 */
2417sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2418 const struct sctp_association *asoc,
2419 const sctp_subtype_t type,
2420 void *arg,
2421 sctp_cmd_seq_t *commands)
2422{
2423 struct sctp_chunk *chunk = arg;
2424 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002425 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 if (!sctp_vtag_verify_either(chunk, asoc))
2428 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2429
2430 /* Make sure that the ABORT chunk has a valid length.
2431 * Since this is an ABORT chunk, we have to discard it
2432 * because of the following text:
2433 * RFC 2960, Section 3.3.7
2434 * If an endpoint receives an ABORT with a format error or for an
2435 * association that doesn't exist, it MUST silently discard it.
2436 * Becasue the length is "invalid", we can't really discard just
2437 * as we do not know its true length. So, to be safe, discard the
2438 * packet.
2439 */
2440 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2441 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2442
2443 /* See if we have an error cause code in the chunk. */
2444 len = ntohs(chunk->chunk_hdr->length);
2445 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2446 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2447
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002448 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2449 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
2451
2452/*
2453 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2454 */
2455sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2456 const struct sctp_association *asoc,
2457 const sctp_subtype_t type,
2458 void *arg,
2459 sctp_cmd_seq_t *commands)
2460{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002461 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2462 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002463 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
2465
2466/*
2467 * Process an ABORT. (COOKIE-ECHOED state)
2468 */
2469sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2470 const struct sctp_association *asoc,
2471 const sctp_subtype_t type,
2472 void *arg,
2473 sctp_cmd_seq_t *commands)
2474{
2475 /* There is a single T1 timer, so we should be able to use
2476 * common function with the COOKIE-WAIT state.
2477 */
2478 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2479}
2480
2481/*
2482 * Stop T1 timer and abort association with "INIT failed".
2483 *
2484 * This is common code called by several sctp_sf_*_abort() functions above.
2485 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002486static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002487 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002488 const struct sctp_association *asoc,
2489 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002491 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2493 SCTP_STATE(SCTP_STATE_CLOSED));
2494 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2495 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2496 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002497 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /* CMD_INIT_FAILED will DELETE_TCB. */
2499 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002500 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002501 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504/*
2505 * sctp_sf_do_9_2_shut
2506 *
2507 * Section: 9.2
2508 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2509 * - enter the SHUTDOWN-RECEIVED state,
2510 *
2511 * - stop accepting new data from its SCTP user
2512 *
2513 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2514 * that all its outstanding DATA chunks have been received by the
2515 * SHUTDOWN sender.
2516 *
2517 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2518 * send a SHUTDOWN in response to a ULP request. And should discard
2519 * subsequent SHUTDOWN chunks.
2520 *
2521 * If there are still outstanding DATA chunks left, the SHUTDOWN
2522 * receiver shall continue to follow normal data transmission
2523 * procedures defined in Section 6 until all outstanding DATA chunks
2524 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2525 * new data from its SCTP user.
2526 *
2527 * Verification Tag: 8.5 Verification Tag [Normal verification]
2528 *
2529 * Inputs
2530 * (endpoint, asoc, chunk)
2531 *
2532 * Outputs
2533 * (asoc, reply_msg, msg_up, timers, counters)
2534 *
2535 * The return value is the disposition of the chunk.
2536 */
2537sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2538 const struct sctp_association *asoc,
2539 const sctp_subtype_t type,
2540 void *arg,
2541 sctp_cmd_seq_t *commands)
2542{
2543 struct sctp_chunk *chunk = arg;
2544 sctp_shutdownhdr_t *sdh;
2545 sctp_disposition_t disposition;
2546 struct sctp_ulpevent *ev;
2547
2548 if (!sctp_vtag_verify(chunk, asoc))
2549 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2550
2551 /* Make sure that the SHUTDOWN chunk has a valid length. */
2552 if (!sctp_chunk_length_valid(chunk,
2553 sizeof(struct sctp_shutdown_chunk_t)))
2554 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2555 commands);
2556
2557 /* Convert the elaborate header. */
2558 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2559 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2560 chunk->subh.shutdown_hdr = sdh;
2561
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002562 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2563 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2564 * inform the application that it should cease sending data.
2565 */
2566 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2567 if (!ev) {
2568 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002569 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002570 }
2571 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2574 * - enter the SHUTDOWN-RECEIVED state,
2575 * - stop accepting new data from its SCTP user
2576 *
2577 * [This is implicit in the new state.]
2578 */
2579 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2580 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2581 disposition = SCTP_DISPOSITION_CONSUME;
2582
2583 if (sctp_outq_is_empty(&asoc->outqueue)) {
2584 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2585 arg, commands);
2586 }
2587
2588 if (SCTP_DISPOSITION_NOMEM == disposition)
2589 goto out;
2590
2591 /* - verify, by checking the Cumulative TSN Ack field of the
2592 * chunk, that all its outstanding DATA chunks have been
2593 * received by the SHUTDOWN sender.
2594 */
2595 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002596 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598out:
2599 return disposition;
2600}
2601
2602/* RFC 2960 9.2
2603 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2604 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2605 * transport addresses (either in the IP addresses or in the INIT chunk)
2606 * that belong to this association, it should discard the INIT chunk and
2607 * retransmit the SHUTDOWN ACK chunk.
2608 */
2609sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2610 const struct sctp_association *asoc,
2611 const sctp_subtype_t type,
2612 void *arg,
2613 sctp_cmd_seq_t *commands)
2614{
2615 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2616 struct sctp_chunk *reply;
2617
Vlad Yasevichece25df2007-09-07 16:30:54 -04002618 /* Make sure that the chunk has a valid length */
2619 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2620 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2621 commands);
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 /* Since we are not going to really process this INIT, there
2624 * is no point in verifying chunk boundries. Just generate
2625 * the SHUTDOWN ACK.
2626 */
2627 reply = sctp_make_shutdown_ack(asoc, chunk);
2628 if (NULL == reply)
2629 goto nomem;
2630
2631 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2632 * the T2-SHUTDOWN timer.
2633 */
2634 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2635
2636 /* and restart the T2-shutdown timer. */
2637 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2638 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2639
2640 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2641
2642 return SCTP_DISPOSITION_CONSUME;
2643nomem:
2644 return SCTP_DISPOSITION_NOMEM;
2645}
2646
2647/*
2648 * sctp_sf_do_ecn_cwr
2649 *
2650 * Section: Appendix A: Explicit Congestion Notification
2651 *
2652 * CWR:
2653 *
2654 * RFC 2481 details a specific bit for a sender to send in the header of
2655 * its next outbound TCP segment to indicate to its peer that it has
2656 * reduced its congestion window. This is termed the CWR bit. For
2657 * SCTP the same indication is made by including the CWR chunk.
2658 * This chunk contains one data element, i.e. the TSN number that
2659 * was sent in the ECNE chunk. This element represents the lowest
2660 * TSN number in the datagram that was originally marked with the
2661 * CE bit.
2662 *
2663 * Verification Tag: 8.5 Verification Tag [Normal verification]
2664 * Inputs
2665 * (endpoint, asoc, chunk)
2666 *
2667 * Outputs
2668 * (asoc, reply_msg, msg_up, timers, counters)
2669 *
2670 * The return value is the disposition of the chunk.
2671 */
2672sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2673 const struct sctp_association *asoc,
2674 const sctp_subtype_t type,
2675 void *arg,
2676 sctp_cmd_seq_t *commands)
2677{
2678 sctp_cwrhdr_t *cwr;
2679 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002680 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 if (!sctp_vtag_verify(chunk, asoc))
2683 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2684
2685 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2686 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2687 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2690 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2691
Al Viro34bcca22006-11-20 17:27:15 -08002692 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002695 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 /* Stop sending ECNE. */
2697 sctp_add_cmd_sf(commands,
2698 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002699 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701 return SCTP_DISPOSITION_CONSUME;
2702}
2703
2704/*
2705 * sctp_sf_do_ecne
2706 *
2707 * Section: Appendix A: Explicit Congestion Notification
2708 *
2709 * ECN-Echo
2710 *
2711 * RFC 2481 details a specific bit for a receiver to send back in its
2712 * TCP acknowledgements to notify the sender of the Congestion
2713 * Experienced (CE) bit having arrived from the network. For SCTP this
2714 * same indication is made by including the ECNE chunk. This chunk
2715 * contains one data element, i.e. the lowest TSN associated with the IP
2716 * datagram marked with the CE bit.....
2717 *
2718 * Verification Tag: 8.5 Verification Tag [Normal verification]
2719 * Inputs
2720 * (endpoint, asoc, chunk)
2721 *
2722 * Outputs
2723 * (asoc, reply_msg, msg_up, timers, counters)
2724 *
2725 * The return value is the disposition of the chunk.
2726 */
2727sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2728 const struct sctp_association *asoc,
2729 const sctp_subtype_t type,
2730 void *arg,
2731 sctp_cmd_seq_t *commands)
2732{
2733 sctp_ecnehdr_t *ecne;
2734 struct sctp_chunk *chunk = arg;
2735
2736 if (!sctp_vtag_verify(chunk, asoc))
2737 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2738
2739 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2740 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2741 commands);
2742
2743 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2744 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2745
2746 /* If this is a newer ECNE than the last CWR packet we sent out */
2747 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2748 SCTP_U32(ntohl(ecne->lowest_tsn)));
2749
2750 return SCTP_DISPOSITION_CONSUME;
2751}
2752
2753/*
2754 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2755 *
2756 * The SCTP endpoint MUST always acknowledge the reception of each valid
2757 * DATA chunk.
2758 *
2759 * The guidelines on delayed acknowledgement algorithm specified in
2760 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2761 * acknowledgement SHOULD be generated for at least every second packet
2762 * (not every second DATA chunk) received, and SHOULD be generated within
2763 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2764 * situations it may be beneficial for an SCTP transmitter to be more
2765 * conservative than the algorithms detailed in this document allow.
2766 * However, an SCTP transmitter MUST NOT be more aggressive than the
2767 * following algorithms allow.
2768 *
2769 * A SCTP receiver MUST NOT generate more than one SACK for every
2770 * incoming packet, other than to update the offered window as the
2771 * receiving application consumes new data.
2772 *
2773 * Verification Tag: 8.5 Verification Tag [Normal verification]
2774 *
2775 * Inputs
2776 * (endpoint, asoc, chunk)
2777 *
2778 * Outputs
2779 * (asoc, reply_msg, msg_up, timers, counters)
2780 *
2781 * The return value is the disposition of the chunk.
2782 */
2783sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2784 const struct sctp_association *asoc,
2785 const sctp_subtype_t type,
2786 void *arg,
2787 sctp_cmd_seq_t *commands)
2788{
2789 struct sctp_chunk *chunk = arg;
2790 int error;
2791
2792 if (!sctp_vtag_verify(chunk, asoc)) {
2793 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2794 SCTP_NULL());
2795 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2799 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2800 commands);
2801
2802 error = sctp_eat_data(asoc, chunk, commands );
2803 switch (error) {
2804 case SCTP_IERROR_NO_ERROR:
2805 break;
2806 case SCTP_IERROR_HIGH_TSN:
2807 case SCTP_IERROR_BAD_STREAM:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002808 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 goto discard_noforce;
2810 case SCTP_IERROR_DUP_TSN:
2811 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002812 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 goto discard_force;
2814 case SCTP_IERROR_NO_DATA:
2815 goto consume;
2816 default:
2817 BUG();
2818 }
2819
2820 if (asoc->autoclose) {
2821 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2822 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2823 }
2824
2825 /* If this is the last chunk in a packet, we need to count it
2826 * toward sack generation. Note that we need to SACK every
2827 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2828 * THEM. We elect to NOT generate SACK's if the chunk fails
2829 * the verification tag test.
2830 *
2831 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2832 *
2833 * The SCTP endpoint MUST always acknowledge the reception of
2834 * each valid DATA chunk.
2835 *
2836 * The guidelines on delayed acknowledgement algorithm
2837 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2838 * Specifically, an acknowledgement SHOULD be generated for at
2839 * least every second packet (not every second DATA chunk)
2840 * received, and SHOULD be generated within 200 ms of the
2841 * arrival of any unacknowledged DATA chunk. In some
2842 * situations it may be beneficial for an SCTP transmitter to
2843 * be more conservative than the algorithms detailed in this
2844 * document allow. However, an SCTP transmitter MUST NOT be
2845 * more aggressive than the following algorithms allow.
2846 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002847 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return SCTP_DISPOSITION_CONSUME;
2851
2852discard_force:
2853 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2854 *
2855 * When a packet arrives with duplicate DATA chunk(s) and with
2856 * no new DATA chunk(s), the endpoint MUST immediately send a
2857 * SACK with no delay. If a packet arrives with duplicate
2858 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2859 * MAY immediately send a SACK. Normally receipt of duplicate
2860 * DATA chunks will occur when the original SACK chunk was lost
2861 * and the peer's RTO has expired. The duplicate TSN number(s)
2862 * SHOULD be reported in the SACK as duplicate.
2863 */
2864 /* In our case, we split the MAY SACK advice up whether or not
2865 * the last chunk is a duplicate.'
2866 */
2867 if (chunk->end_of_packet)
2868 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2869 return SCTP_DISPOSITION_DISCARD;
2870
2871discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002872 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return SCTP_DISPOSITION_DISCARD;
2876consume:
2877 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881/*
2882 * sctp_sf_eat_data_fast_4_4
2883 *
2884 * Section: 4 (4)
2885 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2886 * DATA chunks without delay.
2887 *
2888 * Verification Tag: 8.5 Verification Tag [Normal verification]
2889 * Inputs
2890 * (endpoint, asoc, chunk)
2891 *
2892 * Outputs
2893 * (asoc, reply_msg, msg_up, timers, counters)
2894 *
2895 * The return value is the disposition of the chunk.
2896 */
2897sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2898 const struct sctp_association *asoc,
2899 const sctp_subtype_t type,
2900 void *arg,
2901 sctp_cmd_seq_t *commands)
2902{
2903 struct sctp_chunk *chunk = arg;
2904 int error;
2905
2906 if (!sctp_vtag_verify(chunk, asoc)) {
2907 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2908 SCTP_NULL());
2909 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2910 }
2911
2912 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2913 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2914 commands);
2915
2916 error = sctp_eat_data(asoc, chunk, commands );
2917 switch (error) {
2918 case SCTP_IERROR_NO_ERROR:
2919 case SCTP_IERROR_HIGH_TSN:
2920 case SCTP_IERROR_DUP_TSN:
2921 case SCTP_IERROR_IGNORE_TSN:
2922 case SCTP_IERROR_BAD_STREAM:
2923 break;
2924 case SCTP_IERROR_NO_DATA:
2925 goto consume;
2926 default:
2927 BUG();
2928 }
2929
2930 /* Go a head and force a SACK, since we are shutting down. */
2931
2932 /* Implementor's Guide.
2933 *
2934 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
2935 * respond to each received packet containing one or more DATA chunk(s)
2936 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
2937 */
2938 if (chunk->end_of_packet) {
2939 /* We must delay the chunk creation since the cumulative
2940 * TSN has not been updated yet.
2941 */
2942 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
2943 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2944 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2945 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2946 }
2947
2948consume:
2949 return SCTP_DISPOSITION_CONSUME;
2950}
2951
2952/*
2953 * Section: 6.2 Processing a Received SACK
2954 * D) Any time a SACK arrives, the endpoint performs the following:
2955 *
2956 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
2957 * then drop the SACK. Since Cumulative TSN Ack is monotonically
2958 * increasing, a SACK whose Cumulative TSN Ack is less than the
2959 * Cumulative TSN Ack Point indicates an out-of-order SACK.
2960 *
2961 * ii) Set rwnd equal to the newly received a_rwnd minus the number
2962 * of bytes still outstanding after processing the Cumulative TSN Ack
2963 * and the Gap Ack Blocks.
2964 *
2965 * iii) If the SACK is missing a TSN that was previously
2966 * acknowledged via a Gap Ack Block (e.g., the data receiver
2967 * reneged on the data), then mark the corresponding DATA chunk
2968 * as available for retransmit: Mark it as missing for fast
2969 * retransmit as described in Section 7.2.4 and if no retransmit
2970 * timer is running for the destination address to which the DATA
2971 * chunk was originally transmitted, then T3-rtx is started for
2972 * that destination address.
2973 *
2974 * Verification Tag: 8.5 Verification Tag [Normal verification]
2975 *
2976 * Inputs
2977 * (endpoint, asoc, chunk)
2978 *
2979 * Outputs
2980 * (asoc, reply_msg, msg_up, timers, counters)
2981 *
2982 * The return value is the disposition of the chunk.
2983 */
2984sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
2985 const struct sctp_association *asoc,
2986 const sctp_subtype_t type,
2987 void *arg,
2988 sctp_cmd_seq_t *commands)
2989{
2990 struct sctp_chunk *chunk = arg;
2991 sctp_sackhdr_t *sackh;
2992 __u32 ctsn;
2993
2994 if (!sctp_vtag_verify(chunk, asoc))
2995 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2996
2997 /* Make sure that the SACK chunk has a valid length. */
2998 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
2999 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3000 commands);
3001
3002 /* Pull the SACK chunk from the data buffer */
3003 sackh = sctp_sm_pull_sack(chunk);
3004 /* Was this a bogus SACK? */
3005 if (!sackh)
3006 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3007 chunk->subh.sack_hdr = sackh;
3008 ctsn = ntohl(sackh->cum_tsn_ack);
3009
3010 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3011 * Ack Point, then drop the SACK. Since Cumulative TSN
3012 * Ack is monotonically increasing, a SACK whose
3013 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3014 * Point indicates an out-of-order SACK.
3015 */
3016 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3017 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3018 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3019 return SCTP_DISPOSITION_DISCARD;
3020 }
3021
Wei Yongjunaecedea2007-08-02 16:57:44 +08003022 /* If Cumulative TSN Ack beyond the max tsn currently
3023 * send, terminating the association and respond to the
3024 * sender with an ABORT.
3025 */
3026 if (!TSN_lt(ctsn, asoc->next_tsn))
3027 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* Return this SACK for further processing. */
3030 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3031
3032 /* Note: We do the rest of the work on the PROCESS_SACK
3033 * sideeffect.
3034 */
3035 return SCTP_DISPOSITION_CONSUME;
3036}
3037
3038/*
3039 * Generate an ABORT in response to a packet.
3040 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003041 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003043 * 8) The receiver should respond to the sender of the OOTB packet with
3044 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3045 * MUST fill in the Verification Tag field of the outbound packet
3046 * with the value found in the Verification Tag field of the OOTB
3047 * packet and set the T-bit in the Chunk Flags to indicate that the
3048 * Verification Tag is reflected. After sending this ABORT, the
3049 * receiver of the OOTB packet shall discard the OOTB packet and take
3050 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 *
3052 * Verification Tag:
3053 *
3054 * The return value is the disposition of the chunk.
3055*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003056static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 const struct sctp_association *asoc,
3058 const sctp_subtype_t type,
3059 void *arg,
3060 sctp_cmd_seq_t *commands)
3061{
3062 struct sctp_packet *packet = NULL;
3063 struct sctp_chunk *chunk = arg;
3064 struct sctp_chunk *abort;
3065
3066 packet = sctp_ootb_pkt_new(asoc, chunk);
3067
3068 if (packet) {
3069 /* Make an ABORT. The T bit will be set if the asoc
3070 * is NULL.
3071 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003072 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (!abort) {
3074 sctp_ootb_pkt_free(packet);
3075 return SCTP_DISPOSITION_NOMEM;
3076 }
3077
Jerome Forissier047a2422005-04-28 11:58:43 -07003078 /* Reflect vtag if T-Bit is set */
3079 if (sctp_test_T_bit(abort))
3080 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3081
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 /* Set the skb to the belonging sock for accounting. */
3083 abort->skb->sk = ep->base.sk;
3084
3085 sctp_packet_append_chunk(packet, abort);
3086
3087 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3088 SCTP_PACKET(packet));
3089
3090 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3091
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003092 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return SCTP_DISPOSITION_CONSUME;
3094 }
3095
3096 return SCTP_DISPOSITION_NOMEM;
3097}
3098
3099/*
3100 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3101 * event as ULP notification for each cause included in the chunk.
3102 *
3103 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3104 *
3105 * The return value is the disposition of the chunk.
3106*/
3107sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3108 const struct sctp_association *asoc,
3109 const sctp_subtype_t type,
3110 void *arg,
3111 sctp_cmd_seq_t *commands)
3112{
3113 struct sctp_chunk *chunk = arg;
3114 struct sctp_ulpevent *ev;
3115
3116 if (!sctp_vtag_verify(chunk, asoc))
3117 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3118
3119 /* Make sure that the ERROR chunk has a valid length. */
3120 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3121 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3122 commands);
3123
3124 while (chunk->chunk_end > chunk->skb->data) {
3125 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
3126 GFP_ATOMIC);
3127 if (!ev)
3128 goto nomem;
3129
Ilpo Järvinenbc09dff2008-03-27 17:54:29 -07003130 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3131 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003133 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
3135 return SCTP_DISPOSITION_CONSUME;
3136
3137nomem:
3138 return SCTP_DISPOSITION_NOMEM;
3139}
3140
3141/*
3142 * Process an inbound SHUTDOWN ACK.
3143 *
3144 * From Section 9.2:
3145 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3146 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3147 * peer, and remove all record of the association.
3148 *
3149 * The return value is the disposition.
3150 */
3151sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3152 const struct sctp_association *asoc,
3153 const sctp_subtype_t type,
3154 void *arg,
3155 sctp_cmd_seq_t *commands)
3156{
3157 struct sctp_chunk *chunk = arg;
3158 struct sctp_chunk *reply;
3159 struct sctp_ulpevent *ev;
3160
3161 if (!sctp_vtag_verify(chunk, asoc))
3162 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3163
3164 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3165 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3166 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3167 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /* 10.2 H) SHUTDOWN COMPLETE notification
3169 *
3170 * When SCTP completes the shutdown procedures (section 9.2) this
3171 * notification is passed to the upper layer.
3172 */
3173 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003174 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 if (!ev)
3176 goto nomem;
3177
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003178 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3179 reply = sctp_make_shutdown_complete(asoc, chunk);
3180 if (!reply)
3181 goto nomem_chunk;
3182
3183 /* Do all the commands now (after allocation), so that we
3184 * have consistent state if memory allocation failes
3185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3187
3188 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3189 * stop the T2-shutdown timer,
3190 */
3191 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3192 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3193
3194 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3195 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3198 SCTP_STATE(SCTP_STATE_CLOSED));
3199 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3200 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3201 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3202
3203 /* ...and remove all record of the association. */
3204 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3205 return SCTP_DISPOSITION_DELETE_TCB;
3206
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003207nomem_chunk:
3208 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209nomem:
3210 return SCTP_DISPOSITION_NOMEM;
3211}
3212
3213/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003214 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3217 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3218 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3219 * packet must fill in the Verification Tag field of the outbound
3220 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003221 * set the T-bit in the Chunk Flags to indicate that the Verification
3222 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 *
3224 * 8) The receiver should respond to the sender of the OOTB packet with
3225 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3226 * MUST fill in the Verification Tag field of the outbound packet
3227 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003228 * packet and set the T-bit in the Chunk Flags to indicate that the
3229 * Verification Tag is reflected. After sending this ABORT, the
3230 * receiver of the OOTB packet shall discard the OOTB packet and take
3231 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 */
3233sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3234 const struct sctp_association *asoc,
3235 const sctp_subtype_t type,
3236 void *arg,
3237 sctp_cmd_seq_t *commands)
3238{
3239 struct sctp_chunk *chunk = arg;
3240 struct sk_buff *skb = chunk->skb;
3241 sctp_chunkhdr_t *ch;
3242 __u8 *ch_end;
3243 int ootb_shut_ack = 0;
3244
3245 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3246
3247 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3248 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003249 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003251 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3252 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
Vlad Yasevichece25df2007-09-07 16:30:54 -04003254 /* Now that we know we at least have a chunk header,
3255 * do things that are type appropriate.
3256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3258 ootb_shut_ack = 1;
3259
3260 /* RFC 2960, Section 3.3.7
3261 * Moreover, under any circumstances, an endpoint that
3262 * receives an ABORT MUST NOT respond to that ABORT by
3263 * sending an ABORT of its own.
3264 */
3265 if (SCTP_CID_ABORT == ch->type)
3266 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003267
Vlad Yasevichece25df2007-09-07 16:30:54 -04003268 /* Report violation if chunk len overflows */
3269 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3270 if (ch_end > skb_tail_pointer(skb))
3271 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3272 commands);
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003275 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003278 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003280 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
3283/*
3284 * Handle an "Out of the blue" SHUTDOWN ACK.
3285 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003286 * Section: 8.4 5, sctpimpguide 2.41.
3287 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003289 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3290 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3291 * packet must fill in the Verification Tag field of the outbound
3292 * packet with the Verification Tag received in the SHUTDOWN ACK and
3293 * set the T-bit in the Chunk Flags to indicate that the Verification
3294 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 *
3296 * Inputs
3297 * (endpoint, asoc, type, arg, commands)
3298 *
3299 * Outputs
3300 * (sctp_disposition_t)
3301 *
3302 * The return value is the disposition of the chunk.
3303 */
3304static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3305 const struct sctp_association *asoc,
3306 const sctp_subtype_t type,
3307 void *arg,
3308 sctp_cmd_seq_t *commands)
3309{
3310 struct sctp_packet *packet = NULL;
3311 struct sctp_chunk *chunk = arg;
3312 struct sctp_chunk *shut;
3313
3314 packet = sctp_ootb_pkt_new(asoc, chunk);
3315
3316 if (packet) {
3317 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003318 * The T bit will be set if the asoc is NULL.
3319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 shut = sctp_make_shutdown_complete(asoc, chunk);
3321 if (!shut) {
3322 sctp_ootb_pkt_free(packet);
3323 return SCTP_DISPOSITION_NOMEM;
3324 }
3325
Jerome Forissier047a2422005-04-28 11:58:43 -07003326 /* Reflect vtag if T-Bit is set */
3327 if (sctp_test_T_bit(shut))
3328 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 /* Set the skb to the belonging sock for accounting. */
3331 shut->skb->sk = ep->base.sk;
3332
3333 sctp_packet_append_chunk(packet, shut);
3334
3335 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3336 SCTP_PACKET(packet));
3337
3338 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3339
3340 /* If the chunk length is invalid, we don't want to process
3341 * the reset of the packet.
3342 */
3343 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3344 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3345
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003346 /* We need to discard the rest of the packet to prevent
3347 * potential bomming attacks from additional bundled chunks.
3348 * This is documented in SCTP Threats ID.
3349 */
3350 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 }
3352
3353 return SCTP_DISPOSITION_NOMEM;
3354}
3355
3356/*
3357 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3358 *
3359 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3360 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3361 * procedures in section 8.4 SHOULD be followed, in other words it
3362 * should be treated as an Out Of The Blue packet.
3363 * [This means that we do NOT check the Verification Tag on these
3364 * chunks. --piggy ]
3365 *
3366 */
3367sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3368 const struct sctp_association *asoc,
3369 const sctp_subtype_t type,
3370 void *arg,
3371 sctp_cmd_seq_t *commands)
3372{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003373 struct sctp_chunk *chunk = arg;
3374
3375 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3376 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3377 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3378 commands);
3379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 /* Although we do have an association in this case, it corresponds
3381 * to a restarted association. So the packet is treated as an OOTB
3382 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3383 * called with a NULL association.
3384 */
Wei Yongjun8190f892008-09-08 12:13:55 +08003385 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3386
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3388}
3389
3390/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3391sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3392 const struct sctp_association *asoc,
3393 const sctp_subtype_t type, void *arg,
3394 sctp_cmd_seq_t *commands)
3395{
3396 struct sctp_chunk *chunk = arg;
3397 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003398 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003400 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003402 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 if (!sctp_vtag_verify(chunk, asoc)) {
3405 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3406 SCTP_NULL());
3407 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3408 }
3409
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003410 /* ADD-IP: Section 4.1.1
3411 * This chunk MUST be sent in an authenticated way by using
3412 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3413 * is received unauthenticated it MUST be silently discarded as
3414 * described in [I-D.ietf-tsvwg-sctp-auth].
3415 */
3416 if (!sctp_addip_noauth && !chunk->auth)
3417 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3420 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3421 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3422 commands);
3423
3424 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3425 serial = ntohl(hdr->serial);
3426
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003427 addr_param = (union sctp_addr_param *)hdr->params;
3428 length = ntohs(addr_param->p.length);
3429 if (length < sizeof(sctp_paramhdr_t))
Wei Yongjunba016672008-09-30 05:32:24 -07003430 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003431 (void *)addr_param, commands);
3432
3433 /* Verify the ASCONF chunk before processing it. */
3434 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003435 (sctp_paramhdr_t *)((void *)addr_param + length),
3436 (void *)chunk->chunk_end,
3437 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003438 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3439 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003440
Vlad Yasevicha08de642007-12-20 14:11:47 -08003441 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003443 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 */
3445 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003446 /* If this is the first instance of ASCONF in the packet,
3447 * we can clean our old ASCONF-ACKs.
3448 */
3449 if (!chunk->has_asconf)
3450 sctp_assoc_clean_asconf_ack_cache(asoc);
3451
3452 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3453 * expected, process the ASCONF as described below and after
3454 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3455 * the response packet and cache a copy of it (in the event it
3456 * later needs to be retransmitted).
3457 *
3458 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 */
3460 asconf_ack = sctp_process_asconf((struct sctp_association *)
3461 asoc, chunk);
3462 if (!asconf_ack)
3463 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003464 } else if (serial < asoc->peer.addip_serial + 1) {
3465 /* ADDIP 5.2 E2)
3466 * If the value found in the Sequence Number is less than the
3467 * ('Peer- Sequence-Number' + 1), simply skip to the next
3468 * ASCONF, and include in the outbound response packet
3469 * any previously cached ASCONF-ACK response that was
3470 * sent and saved that matches the Sequence Number of the
3471 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3472 * Chunk exists. This will occur when an older ASCONF
3473 * arrives out of order. In such a case, the receiver
3474 * should skip the ASCONF Chunk and not include ASCONF-ACK
3475 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003477 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3478 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return SCTP_DISPOSITION_DISCARD;
3480 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003481 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return SCTP_DISPOSITION_DISCARD;
3485 }
3486
Vlad Yasevicha08de642007-12-20 14:11:47 -08003487 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3488 * containing the ASCONF-ACK Chunks MUST be the source address of
3489 * the SCTP packet that held the ASCONF Chunks.
3490 *
3491 * To do this properly, we'll set the destination address of the chunk
3492 * and at the transmit time, will try look up the transport to use.
3493 * Since ASCONFs may be bundled, the correct transport may not be
3494 * created untill we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003496 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return SCTP_DISPOSITION_CONSUME;
3500}
3501
3502/*
3503 * ADDIP Section 4.3 General rules for address manipulation
3504 * When building TLV parameters for the ASCONF Chunk that will add or
3505 * delete IP addresses the D0 to D13 rules should be applied:
3506 */
3507sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3508 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003509 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 sctp_cmd_seq_t *commands)
3511{
3512 struct sctp_chunk *asconf_ack = arg;
3513 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3514 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003515 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 sctp_addiphdr_t *addip_hdr;
3517 __u32 sent_serial, rcvd_serial;
3518
3519 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3520 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3521 SCTP_NULL());
3522 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3523 }
3524
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003525 /* ADD-IP, Section 4.1.2:
3526 * This chunk MUST be sent in an authenticated way by using
3527 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3528 * is received unauthenticated it MUST be silently discarded as
3529 * described in [I-D.ietf-tsvwg-sctp-auth].
3530 */
3531 if (!sctp_addip_noauth && !asconf_ack->auth)
3532 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 /* Make sure that the ADDIP chunk has a valid length. */
3535 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3536 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3537 commands);
3538
3539 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3540 rcvd_serial = ntohl(addip_hdr->serial);
3541
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003542 /* Verify the ASCONF-ACK chunk before processing it. */
3543 if (!sctp_verify_asconf(asoc,
3544 (sctp_paramhdr_t *)addip_hdr->params,
3545 (void *)asconf_ack->chunk_end,
3546 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003547 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3548 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 if (last_asconf) {
3551 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3552 sent_serial = ntohl(addip_hdr->serial);
3553 } else {
3554 sent_serial = asoc->addip_serial - 1;
3555 }
3556
3557 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3558 * equal to the next serial number to be used but no ASCONF chunk is
3559 * outstanding the endpoint MUST ABORT the association. Note that a
3560 * sequence number is greater than if it is no more than 2^^31-1
3561 * larger than the current sequence number (using serial arithmetic).
3562 */
3563 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3564 !(asoc->addip_last_asconf)) {
3565 abort = sctp_make_abort(asoc, asconf_ack,
3566 sizeof(sctp_errhdr_t));
3567 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003568 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3570 SCTP_CHUNK(abort));
3571 }
3572 /* We are going to ABORT, so we might as well stop
3573 * processing the rest of the chunks in the packet.
3574 */
3575 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3576 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3577 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003578 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003579 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003581 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3583 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3584 return SCTP_DISPOSITION_ABORT;
3585 }
3586
3587 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3588 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3589 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3590
3591 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3592 asconf_ack))
3593 return SCTP_DISPOSITION_CONSUME;
3594
3595 abort = sctp_make_abort(asoc, asconf_ack,
3596 sizeof(sctp_errhdr_t));
3597 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003598 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3600 SCTP_CHUNK(abort));
3601 }
3602 /* We are going to ABORT, so we might as well stop
3603 * processing the rest of the chunks in the packet.
3604 */
3605 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003606 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003607 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003609 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3611 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3612 return SCTP_DISPOSITION_ABORT;
3613 }
3614
3615 return SCTP_DISPOSITION_DISCARD;
3616}
3617
3618/*
3619 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3620 *
3621 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3622 * its cumulative TSN point to the value carried in the FORWARD TSN
3623 * chunk, and then MUST further advance its cumulative TSN point locally
3624 * if possible.
3625 * After the above processing, the data receiver MUST stop reporting any
3626 * missing TSNs earlier than or equal to the new cumulative TSN point.
3627 *
3628 * Verification Tag: 8.5 Verification Tag [Normal verification]
3629 *
3630 * The return value is the disposition of the chunk.
3631 */
3632sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3633 const struct sctp_association *asoc,
3634 const sctp_subtype_t type,
3635 void *arg,
3636 sctp_cmd_seq_t *commands)
3637{
3638 struct sctp_chunk *chunk = arg;
3639 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3640 __u16 len;
3641 __u32 tsn;
3642
3643 if (!sctp_vtag_verify(chunk, asoc)) {
3644 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3645 SCTP_NULL());
3646 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3647 }
3648
3649 /* Make sure that the FORWARD_TSN chunk has valid length. */
3650 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3651 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3652 commands);
3653
3654 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3655 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3656 len = ntohs(chunk->chunk_hdr->length);
3657 len -= sizeof(struct sctp_chunkhdr);
3658 skb_pull(chunk->skb, len);
3659
3660 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003661 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663 /* The TSN is too high--silently discard the chunk and count on it
3664 * getting retransmitted later.
3665 */
3666 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3667 goto discard_noforce;
3668
3669 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3670 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003671 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003673
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 /* Count this as receiving DATA. */
3675 if (asoc->autoclose) {
3676 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3677 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3678 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003681 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 */
3683 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
3685 return SCTP_DISPOSITION_CONSUME;
3686
3687discard_noforce:
3688 return SCTP_DISPOSITION_DISCARD;
3689}
3690
3691sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3692 const struct sctp_endpoint *ep,
3693 const struct sctp_association *asoc,
3694 const sctp_subtype_t type,
3695 void *arg,
3696 sctp_cmd_seq_t *commands)
3697{
3698 struct sctp_chunk *chunk = arg;
3699 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3700 __u16 len;
3701 __u32 tsn;
3702
3703 if (!sctp_vtag_verify(chunk, asoc)) {
3704 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3705 SCTP_NULL());
3706 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3707 }
3708
3709 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3710 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3711 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3712 commands);
3713
3714 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3715 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3716 len = ntohs(chunk->chunk_hdr->length);
3717 len -= sizeof(struct sctp_chunkhdr);
3718 skb_pull(chunk->skb, len);
3719
3720 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003721 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723 /* The TSN is too high--silently discard the chunk and count on it
3724 * getting retransmitted later.
3725 */
3726 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3727 goto gen_shutdown;
3728
3729 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3730 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003731 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 /* Go a head and force a SACK, since we are shutting down. */
3735gen_shutdown:
3736 /* Implementor's Guide.
3737 *
3738 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3739 * respond to each received packet containing one or more DATA chunk(s)
3740 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3741 */
3742 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3743 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3744 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3745 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3746
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003747 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
3750/*
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003751 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3752 *
3753 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3754 * Identifier field. If this algorithm was not specified by the
3755 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3756 * during association setup, the AUTH chunk and all chunks after it MUST
3757 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3758 * defined in Section 4.1.
3759 *
3760 * If an endpoint with no shared key receives a Shared Key Identifier
3761 * other than 0, it MUST silently discard all authenticated chunks. If
3762 * the endpoint has at least one endpoint pair shared key for the peer,
3763 * it MUST use the key specified by the Shared Key Identifier if a
3764 * key has been configured for that Shared Key Identifier. If no
3765 * endpoint pair shared key has been configured for that Shared Key
3766 * Identifier, all authenticated chunks MUST be silently discarded.
3767 *
3768 * Verification Tag: 8.5 Verification Tag [Normal verification]
3769 *
3770 * The return value is the disposition of the chunk.
3771 */
3772static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3773 const struct sctp_association *asoc,
3774 const sctp_subtype_t type,
3775 struct sctp_chunk *chunk)
3776{
3777 struct sctp_authhdr *auth_hdr;
3778 struct sctp_hmac *hmac;
3779 unsigned int sig_len;
3780 __u16 key_id;
3781 __u8 *save_digest;
3782 __u8 *digest;
3783
3784 /* Pull in the auth header, so we can do some more verification */
3785 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3786 chunk->subh.auth_hdr = auth_hdr;
3787 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3788
3789 /* Make sure that we suport the HMAC algorithm from the auth
3790 * chunk.
3791 */
3792 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3793 return SCTP_IERROR_AUTH_BAD_HMAC;
3794
3795 /* Make sure that the provided shared key identifier has been
3796 * configured
3797 */
3798 key_id = ntohs(auth_hdr->shkey_id);
3799 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3800 return SCTP_IERROR_AUTH_BAD_KEYID;
3801
3802
3803 /* Make sure that the length of the signature matches what
3804 * we expect.
3805 */
3806 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3807 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3808 if (sig_len != hmac->hmac_len)
3809 return SCTP_IERROR_PROTO_VIOLATION;
3810
3811 /* Now that we've done validation checks, we can compute and
3812 * verify the hmac. The steps involved are:
3813 * 1. Save the digest from the chunk.
3814 * 2. Zero out the digest in the chunk.
3815 * 3. Compute the new digest
3816 * 4. Compare saved and new digests.
3817 */
3818 digest = auth_hdr->hmac;
3819 skb_pull(chunk->skb, sig_len);
3820
3821 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3822 if (!save_digest)
3823 goto nomem;
3824
3825 memset(digest, 0, sig_len);
3826
3827 sctp_auth_calculate_hmac(asoc, chunk->skb,
3828 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3829 GFP_ATOMIC);
3830
3831 /* Discard the packet if the digests do not match */
3832 if (memcmp(save_digest, digest, sig_len)) {
3833 kfree(save_digest);
3834 return SCTP_IERROR_BAD_SIG;
3835 }
3836
3837 kfree(save_digest);
3838 chunk->auth = 1;
3839
3840 return SCTP_IERROR_NO_ERROR;
3841nomem:
3842 return SCTP_IERROR_NOMEM;
3843}
3844
3845sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3846 const struct sctp_association *asoc,
3847 const sctp_subtype_t type,
3848 void *arg,
3849 sctp_cmd_seq_t *commands)
3850{
3851 struct sctp_authhdr *auth_hdr;
3852 struct sctp_chunk *chunk = arg;
3853 struct sctp_chunk *err_chunk;
3854 sctp_ierror_t error;
3855
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003856 /* Make sure that the peer has AUTH capable */
3857 if (!asoc->peer.auth_capable)
3858 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3859
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003860 if (!sctp_vtag_verify(chunk, asoc)) {
3861 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3862 SCTP_NULL());
3863 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3864 }
3865
3866 /* Make sure that the AUTH chunk has valid length. */
3867 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3868 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3869 commands);
3870
3871 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3872 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3873 switch (error) {
3874 case SCTP_IERROR_AUTH_BAD_HMAC:
3875 /* Generate the ERROR chunk and discard the rest
3876 * of the packet
3877 */
3878 err_chunk = sctp_make_op_error(asoc, chunk,
3879 SCTP_ERROR_UNSUP_HMAC,
3880 &auth_hdr->hmac_id,
3881 sizeof(__u16));
3882 if (err_chunk) {
3883 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3884 SCTP_CHUNK(err_chunk));
3885 }
3886 /* Fall Through */
3887 case SCTP_IERROR_AUTH_BAD_KEYID:
3888 case SCTP_IERROR_BAD_SIG:
3889 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3890 break;
3891 case SCTP_IERROR_PROTO_VIOLATION:
3892 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3893 commands);
3894 break;
3895 case SCTP_IERROR_NOMEM:
3896 return SCTP_DISPOSITION_NOMEM;
3897 default:
3898 break;
3899 }
3900
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003901 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
3902 struct sctp_ulpevent *ev;
3903
3904 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
3905 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
3906
3907 if (!ev)
3908 return -ENOMEM;
3909
3910 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3911 SCTP_ULPEVENT(ev));
3912 }
3913
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003914 return SCTP_DISPOSITION_CONSUME;
3915}
3916
3917/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 * Process an unknown chunk.
3919 *
3920 * Section: 3.2. Also, 2.1 in the implementor's guide.
3921 *
3922 * Chunk Types are encoded such that the highest-order two bits specify
3923 * the action that must be taken if the processing endpoint does not
3924 * recognize the Chunk Type.
3925 *
3926 * 00 - Stop processing this SCTP packet and discard it, do not process
3927 * any further chunks within it.
3928 *
3929 * 01 - Stop processing this SCTP packet and discard it, do not process
3930 * any further chunks within it, and report the unrecognized
3931 * chunk in an 'Unrecognized Chunk Type'.
3932 *
3933 * 10 - Skip this chunk and continue processing.
3934 *
3935 * 11 - Skip this chunk and continue processing, but report in an ERROR
3936 * Chunk using the 'Unrecognized Chunk Type' cause of error.
3937 *
3938 * The return value is the disposition of the chunk.
3939 */
3940sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
3941 const struct sctp_association *asoc,
3942 const sctp_subtype_t type,
3943 void *arg,
3944 sctp_cmd_seq_t *commands)
3945{
3946 struct sctp_chunk *unk_chunk = arg;
3947 struct sctp_chunk *err_chunk;
3948 sctp_chunkhdr_t *hdr;
3949
3950 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
3951
3952 if (!sctp_vtag_verify(unk_chunk, asoc))
3953 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3954
3955 /* Make sure that the chunk has a valid length.
3956 * Since we don't know the chunk type, we use a general
3957 * chunkhdr structure to make a comparison.
3958 */
3959 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
3960 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3961 commands);
3962
3963 switch (type.chunk & SCTP_CID_ACTION_MASK) {
3964 case SCTP_CID_ACTION_DISCARD:
3965 /* Discard the packet. */
3966 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3967 break;
3968 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 /* Generate an ERROR chunk as response. */
3970 hdr = unk_chunk->chunk_hdr;
3971 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3972 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3973 WORD_ROUND(ntohs(hdr->length)));
3974 if (err_chunk) {
3975 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3976 SCTP_CHUNK(err_chunk));
3977 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07003978
3979 /* Discard the packet. */
3980 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 return SCTP_DISPOSITION_CONSUME;
3982 break;
3983 case SCTP_CID_ACTION_SKIP:
3984 /* Skip the chunk. */
3985 return SCTP_DISPOSITION_DISCARD;
3986 break;
3987 case SCTP_CID_ACTION_SKIP_ERR:
3988 /* Generate an ERROR chunk as response. */
3989 hdr = unk_chunk->chunk_hdr;
3990 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3991 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3992 WORD_ROUND(ntohs(hdr->length)));
3993 if (err_chunk) {
3994 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3995 SCTP_CHUNK(err_chunk));
3996 }
3997 /* Skip the chunk. */
3998 return SCTP_DISPOSITION_CONSUME;
3999 break;
4000 default:
4001 break;
4002 }
4003
4004 return SCTP_DISPOSITION_DISCARD;
4005}
4006
4007/*
4008 * Discard the chunk.
4009 *
4010 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4011 * [Too numerous to mention...]
4012 * Verification Tag: No verification needed.
4013 * Inputs
4014 * (endpoint, asoc, chunk)
4015 *
4016 * Outputs
4017 * (asoc, reply_msg, msg_up, timers, counters)
4018 *
4019 * The return value is the disposition of the chunk.
4020 */
4021sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4022 const struct sctp_association *asoc,
4023 const sctp_subtype_t type,
4024 void *arg,
4025 sctp_cmd_seq_t *commands)
4026{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004027 struct sctp_chunk *chunk = arg;
4028
4029 /* Make sure that the chunk has a valid length.
4030 * Since we don't know the chunk type, we use a general
4031 * chunkhdr structure to make a comparison.
4032 */
4033 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4034 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4035 commands);
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4038 return SCTP_DISPOSITION_DISCARD;
4039}
4040
4041/*
4042 * Discard the whole packet.
4043 *
4044 * Section: 8.4 2)
4045 *
4046 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4047 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 *
4049 * Verification Tag: No verification necessary
4050 *
4051 * Inputs
4052 * (endpoint, asoc, chunk)
4053 *
4054 * Outputs
4055 * (asoc, reply_msg, msg_up, timers, counters)
4056 *
4057 * The return value is the disposition of the chunk.
4058 */
4059sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4060 const struct sctp_association *asoc,
4061 const sctp_subtype_t type,
4062 void *arg,
4063 sctp_cmd_seq_t *commands)
4064{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004065 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4067
4068 return SCTP_DISPOSITION_CONSUME;
4069}
4070
4071
4072/*
4073 * The other end is violating protocol.
4074 *
4075 * Section: Not specified
4076 * Verification Tag: Not specified
4077 * Inputs
4078 * (endpoint, asoc, chunk)
4079 *
4080 * Outputs
4081 * (asoc, reply_msg, msg_up, timers, counters)
4082 *
4083 * We simply tag the chunk as a violation. The state machine will log
4084 * the violation and continue.
4085 */
4086sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4087 const struct sctp_association *asoc,
4088 const sctp_subtype_t type,
4089 void *arg,
4090 sctp_cmd_seq_t *commands)
4091{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004092 struct sctp_chunk *chunk = arg;
4093
4094 /* Make sure that the chunk has a valid length. */
4095 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4096 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4097 commands);
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return SCTP_DISPOSITION_VIOLATION;
4100}
4101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004103 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004105static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004106 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004109 sctp_cmd_seq_t *commands,
4110 const __u8 *payload,
4111 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004113 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 struct sctp_chunk *chunk = arg;
4115 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004117 /* SCTP-AUTH, Section 6.3:
4118 * It should be noted that if the receiver wants to tear
4119 * down an association in an authenticated way only, the
4120 * handling of malformed packets should not result in
4121 * tearing down the association.
4122 *
4123 * This means that if we only want to abort associations
4124 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +02004125 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004126 * was malformed.
4127 */
4128 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4129 goto discard;
4130
Jesper Juhl9abed242007-11-11 23:57:49 +01004131 /* Make the abort chunk. */
4132 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4133 if (!abort)
4134 goto nomem;
4135
Vlad Yasevichece25df2007-09-07 16:30:54 -04004136 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004137 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4138 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4139 !asoc->peer.i.init_tag) {
4140 sctp_initack_chunk_t *initack;
4141
4142 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4143 if (!sctp_chunk_length_valid(chunk,
4144 sizeof(sctp_initack_chunk_t)))
4145 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4146 else {
4147 unsigned int inittag;
4148
4149 inittag = ntohl(initack->init_hdr.init_tag);
4150 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4151 SCTP_U32(inittag));
4152 }
4153 }
4154
Vlad Yasevichece25df2007-09-07 16:30:54 -04004155 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4156 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157
Vlad Yasevichece25df2007-09-07 16:30:54 -04004158 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4159 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4160 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4161 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4162 SCTP_ERROR(ECONNREFUSED));
4163 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4164 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4165 } else {
4166 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4167 SCTP_ERROR(ECONNABORTED));
4168 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4169 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4170 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004173 packet = sctp_ootb_pkt_new(asoc, chunk);
4174
4175 if (!packet)
4176 goto nomem_pkt;
4177
4178 if (sctp_test_T_bit(abort))
4179 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4180
4181 abort->skb->sk = ep->base.sk;
4182
4183 sctp_packet_append_chunk(packet, abort);
4184
4185 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4186 SCTP_PACKET(packet));
4187
4188 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 }
4190
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004191discard:
Vlad Yasevichece25df2007-09-07 16:30:54 -04004192 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 return SCTP_DISPOSITION_ABORT;
4197
Vlad Yasevichece25df2007-09-07 16:30:54 -04004198nomem_pkt:
4199 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200nomem:
4201 return SCTP_DISPOSITION_NOMEM;
4202}
4203
Wei Yongjunaecedea2007-08-02 16:57:44 +08004204/*
4205 * Handle a protocol violation when the chunk length is invalid.
4206 * "Invalid" length is identified as smaller then the minimal length a
4207 * given chunk can be. For example, a SACK chunk has invalid length
4208 * if it's length is set to be smaller then the size of sctp_sack_chunk_t.
4209 *
4210 * We inform the other end by sending an ABORT with a Protocol Violation
4211 * error code.
4212 *
4213 * Section: Not specified
4214 * Verification Tag: Nothing to do
4215 * Inputs
4216 * (endpoint, asoc, chunk)
4217 *
4218 * Outputs
4219 * (reply_msg, msg_up, counters)
4220 *
4221 * Generate an ABORT chunk and terminate the association.
4222 */
4223static sctp_disposition_t sctp_sf_violation_chunklen(
4224 const struct sctp_endpoint *ep,
4225 const struct sctp_association *asoc,
4226 const sctp_subtype_t type,
4227 void *arg,
4228 sctp_cmd_seq_t *commands)
4229{
Florian Westphal24448442008-03-23 22:46:34 -07004230 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004231
Vlad Yasevichece25df2007-09-07 16:30:54 -04004232 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004233 sizeof(err_str));
4234}
4235
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004236/*
4237 * Handle a protocol violation when the parameter length is invalid.
4238 * "Invalid" length is identified as smaller then the minimal length a
4239 * given parameter can be.
4240 */
4241static sctp_disposition_t sctp_sf_violation_paramlen(
4242 const struct sctp_endpoint *ep,
4243 const struct sctp_association *asoc,
4244 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004245 void *arg, void *ext,
4246 sctp_cmd_seq_t *commands)
4247{
4248 struct sctp_chunk *chunk = arg;
4249 struct sctp_paramhdr *param = ext;
4250 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004251
Wei Yongjunba016672008-09-30 05:32:24 -07004252 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4253 goto discard;
4254
4255 /* Make the abort chunk. */
4256 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4257 if (!abort)
4258 goto nomem;
4259
4260 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4261 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4262
4263 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4264 SCTP_ERROR(ECONNABORTED));
4265 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4266 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4267 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4268
4269discard:
4270 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4271
4272 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4273
4274 return SCTP_DISPOSITION_ABORT;
4275nomem:
4276 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004277}
4278
Wei Yongjunaecedea2007-08-02 16:57:44 +08004279/* Handle a protocol violation when the peer trying to advance the
4280 * cumulative tsn ack to a point beyond the max tsn currently sent.
4281 *
4282 * We inform the other end by sending an ABORT with a Protocol Violation
4283 * error code.
4284 */
4285static sctp_disposition_t sctp_sf_violation_ctsn(
4286 const struct sctp_endpoint *ep,
4287 const struct sctp_association *asoc,
4288 const sctp_subtype_t type,
4289 void *arg,
4290 sctp_cmd_seq_t *commands)
4291{
Florian Westphal24448442008-03-23 22:46:34 -07004292 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004293
Vlad Yasevichece25df2007-09-07 16:30:54 -04004294 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004295 sizeof(err_str));
4296}
4297
Vlad Yasevichece25df2007-09-07 16:30:54 -04004298/* Handle protocol violation of an invalid chunk bundling. For example,
4299 * when we have an association and we recieve bundled INIT-ACK, or
4300 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4301 * statement from the specs. Additinally, there might be an attacker
4302 * on the path and we may not want to continue this communication.
4303 */
4304static sctp_disposition_t sctp_sf_violation_chunk(
4305 const struct sctp_endpoint *ep,
4306 const struct sctp_association *asoc,
4307 const sctp_subtype_t type,
4308 void *arg,
4309 sctp_cmd_seq_t *commands)
4310{
Florian Westphal24448442008-03-23 22:46:34 -07004311 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004312
4313 if (!asoc)
4314 return sctp_sf_violation(ep, asoc, type, arg, commands);
4315
4316 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4317 sizeof(err_str));
4318}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319/***************************************************************************
4320 * These are the state functions for handling primitive (Section 10) events.
4321 ***************************************************************************/
4322/*
4323 * sctp_sf_do_prm_asoc
4324 *
4325 * Section: 10.1 ULP-to-SCTP
4326 * B) Associate
4327 *
4328 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4329 * outbound stream count)
4330 * -> association id [,destination transport addr list] [,outbound stream
4331 * count]
4332 *
4333 * This primitive allows the upper layer to initiate an association to a
4334 * specific peer endpoint.
4335 *
4336 * The peer endpoint shall be specified by one of the transport addresses
4337 * which defines the endpoint (see Section 1.4). If the local SCTP
4338 * instance has not been initialized, the ASSOCIATE is considered an
4339 * error.
4340 * [This is not relevant for the kernel implementation since we do all
4341 * initialization at boot time. It we hadn't initialized we wouldn't
4342 * get anywhere near this code.]
4343 *
4344 * An association id, which is a local handle to the SCTP association,
4345 * will be returned on successful establishment of the association. If
4346 * SCTP is not able to open an SCTP association with the peer endpoint,
4347 * an error is returned.
4348 * [In the kernel implementation, the struct sctp_association needs to
4349 * be created BEFORE causing this primitive to run.]
4350 *
4351 * Other association parameters may be returned, including the
4352 * complete destination transport addresses of the peer as well as the
4353 * outbound stream count of the local endpoint. One of the transport
4354 * address from the returned destination addresses will be selected by
4355 * the local endpoint as default primary path for sending SCTP packets
4356 * to this peer. The returned "destination transport addr list" can
4357 * be used by the ULP to change the default primary path or to force
4358 * sending a packet to a specific transport address. [All of this
4359 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4360 * function.]
4361 *
4362 * Mandatory attributes:
4363 *
4364 * o local SCTP instance name - obtained from the INITIALIZE operation.
4365 * [This is the argument asoc.]
4366 * o destination transport addr - specified as one of the transport
4367 * addresses of the peer endpoint with which the association is to be
4368 * established.
4369 * [This is asoc->peer.active_path.]
4370 * o outbound stream count - the number of outbound streams the ULP
4371 * would like to open towards this peer endpoint.
4372 * [BUG: This is not currently implemented.]
4373 * Optional attributes:
4374 *
4375 * None.
4376 *
4377 * The return value is a disposition.
4378 */
4379sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4380 const struct sctp_association *asoc,
4381 const sctp_subtype_t type,
4382 void *arg,
4383 sctp_cmd_seq_t *commands)
4384{
4385 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004386 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 /* The comment below says that we enter COOKIE-WAIT AFTER
4389 * sending the INIT, but that doesn't actually work in our
4390 * implementation...
4391 */
4392 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4393 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4394
4395 /* RFC 2960 5.1 Normal Establishment of an Association
4396 *
4397 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4398 * must provide its Verification Tag (Tag_A) in the Initiate
4399 * Tag field. Tag_A SHOULD be a random number in the range of
4400 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4401 */
4402
4403 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4404 if (!repl)
4405 goto nomem;
4406
4407 /* Cast away the const modifier, as we want to just
4408 * rerun it through as a sideffect.
4409 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004410 my_asoc = (struct sctp_association *)asoc;
4411 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412
Frank Filz3f7a87d2005-06-20 13:14:57 -07004413 /* Choose transport for INIT. */
4414 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4415 SCTP_CHUNK(repl));
4416
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 /* After sending the INIT, "A" starts the T1-init timer and
4418 * enters the COOKIE-WAIT state.
4419 */
4420 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4421 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4422 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4423 return SCTP_DISPOSITION_CONSUME;
4424
4425nomem:
4426 return SCTP_DISPOSITION_NOMEM;
4427}
4428
4429/*
4430 * Process the SEND primitive.
4431 *
4432 * Section: 10.1 ULP-to-SCTP
4433 * E) Send
4434 *
4435 * Format: SEND(association id, buffer address, byte count [,context]
4436 * [,stream id] [,life time] [,destination transport address]
4437 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4438 * -> result
4439 *
4440 * This is the main method to send user data via SCTP.
4441 *
4442 * Mandatory attributes:
4443 *
4444 * o association id - local handle to the SCTP association
4445 *
4446 * o buffer address - the location where the user message to be
4447 * transmitted is stored;
4448 *
4449 * o byte count - The size of the user data in number of bytes;
4450 *
4451 * Optional attributes:
4452 *
4453 * o context - an optional 32 bit integer that will be carried in the
4454 * sending failure notification to the ULP if the transportation of
4455 * this User Message fails.
4456 *
4457 * o stream id - to indicate which stream to send the data on. If not
4458 * specified, stream 0 will be used.
4459 *
4460 * o life time - specifies the life time of the user data. The user data
4461 * will not be sent by SCTP after the life time expires. This
4462 * parameter can be used to avoid efforts to transmit stale
4463 * user messages. SCTP notifies the ULP if the data cannot be
4464 * initiated to transport (i.e. sent to the destination via SCTP's
4465 * send primitive) within the life time variable. However, the
4466 * user data will be transmitted if SCTP has attempted to transmit a
4467 * chunk before the life time expired.
4468 *
4469 * o destination transport address - specified as one of the destination
4470 * transport addresses of the peer endpoint to which this packet
4471 * should be sent. Whenever possible, SCTP should use this destination
4472 * transport address for sending the packets, instead of the current
4473 * primary path.
4474 *
4475 * o unorder flag - this flag, if present, indicates that the user
4476 * would like the data delivered in an unordered fashion to the peer
4477 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4478 * message).
4479 *
4480 * o no-bundle flag - instructs SCTP not to bundle this user data with
4481 * other outbound DATA chunks. SCTP MAY still bundle even when
4482 * this flag is present, when faced with network congestion.
4483 *
4484 * o payload protocol-id - A 32 bit unsigned integer that is to be
4485 * passed to the peer indicating the type of payload protocol data
4486 * being transmitted. This value is passed as opaque data by SCTP.
4487 *
4488 * The return value is the disposition.
4489 */
4490sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4491 const struct sctp_association *asoc,
4492 const sctp_subtype_t type,
4493 void *arg,
4494 sctp_cmd_seq_t *commands)
4495{
4496 struct sctp_chunk *chunk = arg;
4497
4498 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
4499 return SCTP_DISPOSITION_CONSUME;
4500}
4501
4502/*
4503 * Process the SHUTDOWN primitive.
4504 *
4505 * Section: 10.1:
4506 * C) Shutdown
4507 *
4508 * Format: SHUTDOWN(association id)
4509 * -> result
4510 *
4511 * Gracefully closes an association. Any locally queued user data
4512 * will be delivered to the peer. The association will be terminated only
4513 * after the peer acknowledges all the SCTP packets sent. A success code
4514 * will be returned on successful termination of the association. If
4515 * attempting to terminate the association results in a failure, an error
4516 * code shall be returned.
4517 *
4518 * Mandatory attributes:
4519 *
4520 * o association id - local handle to the SCTP association
4521 *
4522 * Optional attributes:
4523 *
4524 * None.
4525 *
4526 * The return value is the disposition.
4527 */
4528sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4529 const struct sctp_endpoint *ep,
4530 const struct sctp_association *asoc,
4531 const sctp_subtype_t type,
4532 void *arg,
4533 sctp_cmd_seq_t *commands)
4534{
4535 int disposition;
4536
4537 /* From 9.2 Shutdown of an Association
4538 * Upon receipt of the SHUTDOWN primitive from its upper
4539 * layer, the endpoint enters SHUTDOWN-PENDING state and
4540 * remains there until all outstanding data has been
4541 * acknowledged by its peer. The endpoint accepts no new data
4542 * from its upper layer, but retransmits data to the far end
4543 * if necessary to fill gaps.
4544 */
4545 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4546 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 disposition = SCTP_DISPOSITION_CONSUME;
4549 if (sctp_outq_is_empty(&asoc->outqueue)) {
4550 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4551 arg, commands);
4552 }
4553 return disposition;
4554}
4555
4556/*
4557 * Process the ABORT primitive.
4558 *
4559 * Section: 10.1:
4560 * C) Abort
4561 *
4562 * Format: Abort(association id [, cause code])
4563 * -> result
4564 *
4565 * Ungracefully closes an association. Any locally queued user data
4566 * will be discarded and an ABORT chunk is sent to the peer. A success code
4567 * will be returned on successful abortion of the association. If
4568 * attempting to abort the association results in a failure, an error
4569 * code shall be returned.
4570 *
4571 * Mandatory attributes:
4572 *
4573 * o association id - local handle to the SCTP association
4574 *
4575 * Optional attributes:
4576 *
4577 * o cause code - reason of the abort to be passed to the peer
4578 *
4579 * None.
4580 *
4581 * The return value is the disposition.
4582 */
4583sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4584 const struct sctp_endpoint *ep,
4585 const struct sctp_association *asoc,
4586 const sctp_subtype_t type,
4587 void *arg,
4588 sctp_cmd_seq_t *commands)
4589{
4590 /* From 9.1 Abort of an Association
4591 * Upon receipt of the ABORT primitive from its upper
4592 * layer, the endpoint enters CLOSED state and
4593 * discard all outstanding data has been
4594 * acknowledged by its peer. The endpoint accepts no new data
4595 * from its upper layer, but retransmits data to the far end
4596 * if necessary to fill gaps.
4597 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004598 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 sctp_disposition_t retval;
4600
4601 retval = SCTP_DISPOSITION_CONSUME;
4602
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004603 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 /* Even if we can't send the ABORT due to low memory delete the
4606 * TCB. This is a departure from our typical NOMEM handling.
4607 */
4608
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004609 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4610 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 /* Delete the established association. */
4612 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004613 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
4615 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4616 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4617
4618 return retval;
4619}
4620
4621/* We tried an illegal operation on an association which is closed. */
4622sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4623 const struct sctp_association *asoc,
4624 const sctp_subtype_t type,
4625 void *arg,
4626 sctp_cmd_seq_t *commands)
4627{
4628 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4629 return SCTP_DISPOSITION_CONSUME;
4630}
4631
4632/* We tried an illegal operation on an association which is shutting
4633 * down.
4634 */
4635sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4636 const struct sctp_association *asoc,
4637 const sctp_subtype_t type,
4638 void *arg,
4639 sctp_cmd_seq_t *commands)
4640{
4641 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4642 SCTP_ERROR(-ESHUTDOWN));
4643 return SCTP_DISPOSITION_CONSUME;
4644}
4645
4646/*
4647 * sctp_cookie_wait_prm_shutdown
4648 *
4649 * Section: 4 Note: 2
4650 * Verification Tag:
4651 * Inputs
4652 * (endpoint, asoc)
4653 *
4654 * The RFC does not explicitly address this issue, but is the route through the
4655 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4656 *
4657 * Outputs
4658 * (timers)
4659 */
4660sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4661 const struct sctp_endpoint *ep,
4662 const struct sctp_association *asoc,
4663 const sctp_subtype_t type,
4664 void *arg,
4665 sctp_cmd_seq_t *commands)
4666{
4667 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4668 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4669
4670 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4671 SCTP_STATE(SCTP_STATE_CLOSED));
4672
4673 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4674
4675 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4676
4677 return SCTP_DISPOSITION_DELETE_TCB;
4678}
4679
4680/*
4681 * sctp_cookie_echoed_prm_shutdown
4682 *
4683 * Section: 4 Note: 2
4684 * Verification Tag:
4685 * Inputs
4686 * (endpoint, asoc)
4687 *
4688 * The RFC does not explcitly address this issue, but is the route through the
4689 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4690 *
4691 * Outputs
4692 * (timers)
4693 */
4694sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4695 const struct sctp_endpoint *ep,
4696 const struct sctp_association *asoc,
4697 const sctp_subtype_t type,
4698 void *arg, sctp_cmd_seq_t *commands)
4699{
4700 /* There is a single T1 timer, so we should be able to use
4701 * common function with the COOKIE-WAIT state.
4702 */
4703 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4704}
4705
4706/*
4707 * sctp_sf_cookie_wait_prm_abort
4708 *
4709 * Section: 4 Note: 2
4710 * Verification Tag:
4711 * Inputs
4712 * (endpoint, asoc)
4713 *
4714 * The RFC does not explicitly address this issue, but is the route through the
4715 * state table when someone issues an abort while in COOKIE_WAIT state.
4716 *
4717 * Outputs
4718 * (timers)
4719 */
4720sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4721 const struct sctp_endpoint *ep,
4722 const struct sctp_association *asoc,
4723 const sctp_subtype_t type,
4724 void *arg,
4725 sctp_cmd_seq_t *commands)
4726{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004727 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 sctp_disposition_t retval;
4729
4730 /* Stop T1-init timer */
4731 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4732 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4733 retval = SCTP_DISPOSITION_CONSUME;
4734
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004735 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
4737 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4738 SCTP_STATE(SCTP_STATE_CLOSED));
4739
4740 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4741
4742 /* Even if we can't send the ABORT due to low memory delete the
4743 * TCB. This is a departure from our typical NOMEM handling.
4744 */
4745
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004746 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4747 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 /* Delete the established association. */
4749 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004750 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
4752 return retval;
4753}
4754
4755/*
4756 * sctp_sf_cookie_echoed_prm_abort
4757 *
4758 * Section: 4 Note: 3
4759 * Verification Tag:
4760 * Inputs
4761 * (endpoint, asoc)
4762 *
4763 * The RFC does not explcitly address this issue, but is the route through the
4764 * state table when someone issues an abort while in COOKIE_ECHOED state.
4765 *
4766 * Outputs
4767 * (timers)
4768 */
4769sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4770 const struct sctp_endpoint *ep,
4771 const struct sctp_association *asoc,
4772 const sctp_subtype_t type,
4773 void *arg,
4774 sctp_cmd_seq_t *commands)
4775{
4776 /* There is a single T1 timer, so we should be able to use
4777 * common function with the COOKIE-WAIT state.
4778 */
4779 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4780}
4781
4782/*
4783 * sctp_sf_shutdown_pending_prm_abort
4784 *
4785 * Inputs
4786 * (endpoint, asoc)
4787 *
4788 * The RFC does not explicitly address this issue, but is the route through the
4789 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4790 *
4791 * Outputs
4792 * (timers)
4793 */
4794sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4795 const struct sctp_endpoint *ep,
4796 const struct sctp_association *asoc,
4797 const sctp_subtype_t type,
4798 void *arg,
4799 sctp_cmd_seq_t *commands)
4800{
4801 /* Stop the T5-shutdown guard timer. */
4802 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4803 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4804
4805 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4806}
4807
4808/*
4809 * sctp_sf_shutdown_sent_prm_abort
4810 *
4811 * Inputs
4812 * (endpoint, asoc)
4813 *
4814 * The RFC does not explicitly address this issue, but is the route through the
4815 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4816 *
4817 * Outputs
4818 * (timers)
4819 */
4820sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4821 const struct sctp_endpoint *ep,
4822 const struct sctp_association *asoc,
4823 const sctp_subtype_t type,
4824 void *arg,
4825 sctp_cmd_seq_t *commands)
4826{
4827 /* Stop the T2-shutdown timer. */
4828 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4829 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4830
4831 /* Stop the T5-shutdown guard timer. */
4832 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4833 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4834
4835 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4836}
4837
4838/*
4839 * sctp_sf_cookie_echoed_prm_abort
4840 *
4841 * Inputs
4842 * (endpoint, asoc)
4843 *
4844 * The RFC does not explcitly address this issue, but is the route through the
4845 * state table when someone issues an abort while in COOKIE_ECHOED state.
4846 *
4847 * Outputs
4848 * (timers)
4849 */
4850sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4851 const struct sctp_endpoint *ep,
4852 const struct sctp_association *asoc,
4853 const sctp_subtype_t type,
4854 void *arg,
4855 sctp_cmd_seq_t *commands)
4856{
4857 /* The same T2 timer, so we should be able to use
4858 * common function with the SHUTDOWN-SENT state.
4859 */
4860 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4861}
4862
4863/*
4864 * Process the REQUESTHEARTBEAT primitive
4865 *
4866 * 10.1 ULP-to-SCTP
4867 * J) Request Heartbeat
4868 *
4869 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4870 *
4871 * -> result
4872 *
4873 * Instructs the local endpoint to perform a HeartBeat on the specified
4874 * destination transport address of the given association. The returned
4875 * result should indicate whether the transmission of the HEARTBEAT
4876 * chunk to the destination address is successful.
4877 *
4878 * Mandatory attributes:
4879 *
4880 * o association id - local handle to the SCTP association
4881 *
4882 * o destination transport address - the transport address of the
4883 * association on which a heartbeat should be issued.
4884 */
4885sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4886 const struct sctp_endpoint *ep,
4887 const struct sctp_association *asoc,
4888 const sctp_subtype_t type,
4889 void *arg,
4890 sctp_cmd_seq_t *commands)
4891{
Vlad Yasevichfb785252007-03-19 17:02:03 -07004892 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4893 (struct sctp_transport *)arg, commands))
4894 return SCTP_DISPOSITION_NOMEM;
4895
4896 /*
4897 * RFC 2960 (bis), section 8.3
4898 *
4899 * D) Request an on-demand HEARTBEAT on a specific destination
4900 * transport address of a given association.
4901 *
4902 * The endpoint should increment the respective error counter of
4903 * the destination transport address each time a HEARTBEAT is sent
4904 * to that address and not acknowledged within one RTO.
4905 *
4906 */
4907 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
4908 SCTP_TRANSPORT(arg));
4909 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910}
4911
4912/*
4913 * ADDIP Section 4.1 ASCONF Chunk Procedures
4914 * When an endpoint has an ASCONF signaled change to be sent to the
4915 * remote endpoint it should do A1 to A9
4916 */
4917sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4918 const struct sctp_association *asoc,
4919 const sctp_subtype_t type,
4920 void *arg,
4921 sctp_cmd_seq_t *commands)
4922{
4923 struct sctp_chunk *chunk = arg;
4924
4925 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
4926 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4927 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4928 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
4929 return SCTP_DISPOSITION_CONSUME;
4930}
4931
4932/*
4933 * Ignore the primitive event
4934 *
4935 * The return value is the disposition of the primitive.
4936 */
4937sctp_disposition_t sctp_sf_ignore_primitive(
4938 const struct sctp_endpoint *ep,
4939 const struct sctp_association *asoc,
4940 const sctp_subtype_t type,
4941 void *arg,
4942 sctp_cmd_seq_t *commands)
4943{
4944 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
4945 return SCTP_DISPOSITION_DISCARD;
4946}
4947
4948/***************************************************************************
4949 * These are the state functions for the OTHER events.
4950 ***************************************************************************/
4951
4952/*
4953 * Start the shutdown negotiation.
4954 *
4955 * From Section 9.2:
4956 * Once all its outstanding data has been acknowledged, the endpoint
4957 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
4958 * TSN Ack field the last sequential TSN it has received from the peer.
4959 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
4960 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
4961 * with the updated last sequential TSN received from its peer.
4962 *
4963 * The return value is the disposition.
4964 */
4965sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
4966 const struct sctp_endpoint *ep,
4967 const struct sctp_association *asoc,
4968 const sctp_subtype_t type,
4969 void *arg,
4970 sctp_cmd_seq_t *commands)
4971{
4972 struct sctp_chunk *reply;
4973
4974 /* Once all its outstanding data has been acknowledged, the
4975 * endpoint shall send a SHUTDOWN chunk to its peer including
4976 * in the Cumulative TSN Ack field the last sequential TSN it
4977 * has received from the peer.
4978 */
4979 reply = sctp_make_shutdown(asoc, NULL);
4980 if (!reply)
4981 goto nomem;
4982
4983 /* Set the transport for the SHUTDOWN chunk and the timeout for the
4984 * T2-shutdown timer.
4985 */
4986 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4987
4988 /* It shall then start the T2-shutdown timer */
4989 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4990 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4991
Wei Yongjun536428a2008-09-05 08:55:26 +08004992 /* RFC 4960 Section 9.2
4993 * The sender of the SHUTDOWN MAY also start an overall guard timer
4994 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4995 */
4996 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4997 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4998
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 if (asoc->autoclose)
5000 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5001 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5002
5003 /* and enter the SHUTDOWN-SENT state. */
5004 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5005 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5006
5007 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5008 *
5009 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005010 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 */
5012 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5013
5014 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5015
5016 return SCTP_DISPOSITION_CONSUME;
5017
5018nomem:
5019 return SCTP_DISPOSITION_NOMEM;
5020}
5021
5022/*
5023 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5024 *
5025 * From Section 9.2:
5026 *
5027 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5028 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5029 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5030 * endpoint must re-send the SHUTDOWN ACK.
5031 *
5032 * The return value is the disposition.
5033 */
5034sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5035 const struct sctp_endpoint *ep,
5036 const struct sctp_association *asoc,
5037 const sctp_subtype_t type,
5038 void *arg,
5039 sctp_cmd_seq_t *commands)
5040{
5041 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5042 struct sctp_chunk *reply;
5043
5044 /* There are 2 ways of getting here:
5045 * 1) called in response to a SHUTDOWN chunk
5046 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5047 *
5048 * For the case (2), the arg parameter is set to NULL. We need
5049 * to check that we have a chunk before accessing it's fields.
5050 */
5051 if (chunk) {
5052 if (!sctp_vtag_verify(chunk, asoc))
5053 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5054
5055 /* Make sure that the SHUTDOWN chunk has a valid length. */
5056 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5057 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5058 commands);
5059 }
5060
5061 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5062 * shall send a SHUTDOWN ACK ...
5063 */
5064 reply = sctp_make_shutdown_ack(asoc, chunk);
5065 if (!reply)
5066 goto nomem;
5067
5068 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5069 * the T2-shutdown timer.
5070 */
5071 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5072
5073 /* and start/restart a T2-shutdown timer of its own, */
5074 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5075 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5076
5077 if (asoc->autoclose)
5078 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5079 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5080
5081 /* Enter the SHUTDOWN-ACK-SENT state. */
5082 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5083 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5084
5085 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5086 *
5087 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005088 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 */
5090 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5091
5092 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5093
5094 return SCTP_DISPOSITION_CONSUME;
5095
5096nomem:
5097 return SCTP_DISPOSITION_NOMEM;
5098}
5099
5100/*
5101 * Ignore the event defined as other
5102 *
5103 * The return value is the disposition of the event.
5104 */
5105sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5106 const struct sctp_association *asoc,
5107 const sctp_subtype_t type,
5108 void *arg,
5109 sctp_cmd_seq_t *commands)
5110{
5111 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5112 return SCTP_DISPOSITION_DISCARD;
5113}
5114
5115/************************************************************
5116 * These are the state functions for handling timeout events.
5117 ************************************************************/
5118
5119/*
5120 * RTX Timeout
5121 *
5122 * Section: 6.3.3 Handle T3-rtx Expiration
5123 *
5124 * Whenever the retransmission timer T3-rtx expires for a destination
5125 * address, do the following:
5126 * [See below]
5127 *
5128 * The return value is the disposition of the chunk.
5129 */
5130sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5131 const struct sctp_association *asoc,
5132 const sctp_subtype_t type,
5133 void *arg,
5134 sctp_cmd_seq_t *commands)
5135{
5136 struct sctp_transport *transport = arg;
5137
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005138 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005141 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5142 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5144 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005145 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5147 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5148 return SCTP_DISPOSITION_DELETE_TCB;
5149 }
5150
5151 /* E1) For the destination address for which the timer
5152 * expires, adjust its ssthresh with rules defined in Section
5153 * 7.2.3 and set the cwnd <- MTU.
5154 */
5155
5156 /* E2) For the destination address for which the timer
5157 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5158 * maximum value discussed in rule C7 above (RTO.max) may be
5159 * used to provide an upper bound to this doubling operation.
5160 */
5161
5162 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5163 * outstanding DATA chunks for the address for which the
5164 * T3-rtx has expired will fit into a single packet, subject
5165 * to the MTU constraint for the path corresponding to the
5166 * destination transport address to which the retransmission
5167 * is being sent (this may be different from the address for
5168 * which the timer expires [see Section 6.4]). Call this
5169 * value K. Bundle and retransmit those K DATA chunks in a
5170 * single packet to the destination endpoint.
5171 *
5172 * Note: Any DATA chunks that were sent to the address for
5173 * which the T3-rtx timer expired but did not fit in one MTU
5174 * (rule E3 above), should be marked for retransmission and
5175 * sent as soon as cwnd allows (normally when a SACK arrives).
5176 */
5177
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 /* Do some failure management (Section 8.2). */
5179 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5180
Vlad Yasevich1845a572007-02-21 02:06:19 -08005181 /* NB: Rules E4 and F1 are implicit in R1. */
5182 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5183
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 return SCTP_DISPOSITION_CONSUME;
5185}
5186
5187/*
5188 * Generate delayed SACK on timeout
5189 *
5190 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5191 *
5192 * The guidelines on delayed acknowledgement algorithm specified in
5193 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5194 * acknowledgement SHOULD be generated for at least every second packet
5195 * (not every second DATA chunk) received, and SHOULD be generated
5196 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5197 * some situations it may be beneficial for an SCTP transmitter to be
5198 * more conservative than the algorithms detailed in this document
5199 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5200 * the following algorithms allow.
5201 */
5202sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5203 const struct sctp_association *asoc,
5204 const sctp_subtype_t type,
5205 void *arg,
5206 sctp_cmd_seq_t *commands)
5207{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005208 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5210 return SCTP_DISPOSITION_CONSUME;
5211}
5212
5213/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005214 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 *
5216 * Section: 4 Note: 2
5217 * Verification Tag:
5218 * Inputs
5219 * (endpoint, asoc)
5220 *
5221 * RFC 2960 Section 4 Notes
5222 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5223 * and re-start the T1-init timer without changing state. This MUST
5224 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5225 * endpoint MUST abort the initialization process and report the
5226 * error to SCTP user.
5227 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005228 * Outputs
5229 * (timers, events)
5230 *
5231 */
5232sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5233 const struct sctp_association *asoc,
5234 const sctp_subtype_t type,
5235 void *arg,
5236 sctp_cmd_seq_t *commands)
5237{
5238 struct sctp_chunk *repl = NULL;
5239 struct sctp_bind_addr *bp;
5240 int attempts = asoc->init_err_counter + 1;
5241
5242 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005243 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005244
Vlad Yasevich81845c22006-01-30 15:59:54 -08005245 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005246 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5247 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5248 if (!repl)
5249 return SCTP_DISPOSITION_NOMEM;
5250
5251 /* Choose transport for INIT. */
5252 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5253 SCTP_CHUNK(repl));
5254
5255 /* Issue a sideeffect to do the needed accounting. */
5256 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5257 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5258
5259 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5260 } else {
5261 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5262 " max_init_attempts: %d\n",
5263 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005264 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5265 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005266 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005267 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005268 return SCTP_DISPOSITION_DELETE_TCB;
5269 }
5270
5271 return SCTP_DISPOSITION_CONSUME;
5272}
5273
5274/*
5275 * sctp_sf_t1_cookie_timer_expire
5276 *
5277 * Section: 4 Note: 2
5278 * Verification Tag:
5279 * Inputs
5280 * (endpoint, asoc)
5281 *
5282 * RFC 2960 Section 4 Notes
5283 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 * COOKIE ECHO and re-start the T1-cookie timer without changing
5285 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5286 * After that, the endpoint MUST abort the initialization process and
5287 * report the error to SCTP user.
5288 *
5289 * Outputs
5290 * (timers, events)
5291 *
5292 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005293sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 const struct sctp_association *asoc,
5295 const sctp_subtype_t type,
5296 void *arg,
5297 sctp_cmd_seq_t *commands)
5298{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005299 struct sctp_chunk *repl = NULL;
5300 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301
Frank Filz3f7a87d2005-06-20 13:14:57 -07005302 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005303 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
Vlad Yasevich81845c22006-01-30 15:59:54 -08005305 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005306 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005308 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005310 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5311 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005313 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5314 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5315
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5317 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005318 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5319 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005321 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 return SCTP_DISPOSITION_DELETE_TCB;
5323 }
5324
5325 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326}
5327
5328/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5329 * with the updated last sequential TSN received from its peer.
5330 *
5331 * An endpoint should limit the number of retransmissions of the
5332 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5333 * If this threshold is exceeded the endpoint should destroy the TCB and
5334 * MUST report the peer endpoint unreachable to the upper layer (and
5335 * thus the association enters the CLOSED state). The reception of any
5336 * packet from its peer (i.e. as the peer sends all of its queued DATA
5337 * chunks) should clear the endpoint's retransmission count and restart
5338 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5339 * all of its queued DATA chunks that have not yet been sent.
5340 */
5341sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5342 const struct sctp_association *asoc,
5343 const sctp_subtype_t type,
5344 void *arg,
5345 sctp_cmd_seq_t *commands)
5346{
5347 struct sctp_chunk *reply = NULL;
5348
5349 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005350 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5351
Neil Horman58fbbed2008-02-29 11:40:56 -08005352 ((struct sctp_association *)asoc)->shutdown_retries++;
5353
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005355 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5356 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5358 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005359 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5361 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5362 return SCTP_DISPOSITION_DELETE_TCB;
5363 }
5364
5365 switch (asoc->state) {
5366 case SCTP_STATE_SHUTDOWN_SENT:
5367 reply = sctp_make_shutdown(asoc, NULL);
5368 break;
5369
5370 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5371 reply = sctp_make_shutdown_ack(asoc, NULL);
5372 break;
5373
5374 default:
5375 BUG();
5376 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
5379 if (!reply)
5380 goto nomem;
5381
5382 /* Do some failure management (Section 8.2). */
5383 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5384 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5385
5386 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5387 * the T2-shutdown timer.
5388 */
5389 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5390
5391 /* Restart the T2-shutdown timer. */
5392 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5393 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5394 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5395 return SCTP_DISPOSITION_CONSUME;
5396
5397nomem:
5398 return SCTP_DISPOSITION_NOMEM;
5399}
5400
5401/*
5402 * ADDIP Section 4.1 ASCONF CHunk Procedures
5403 * If the T4 RTO timer expires the endpoint should do B1 to B5
5404 */
5405sctp_disposition_t sctp_sf_t4_timer_expire(
5406 const struct sctp_endpoint *ep,
5407 const struct sctp_association *asoc,
5408 const sctp_subtype_t type,
5409 void *arg,
5410 sctp_cmd_seq_t *commands)
5411{
5412 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5413 struct sctp_transport *transport = chunk->transport;
5414
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005415 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5418 * detection on the appropriate destination address as defined in
5419 * RFC2960 [5] section 8.1 and 8.2.
5420 */
5421 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5422
5423 /* Reconfig T4 timer and transport. */
5424 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5425
5426 /* ADDIP 4.1 B2) Increment the association error counters and perform
5427 * endpoint failure detection on the association as defined in
5428 * RFC2960 [5] section 8.1 and 8.2.
5429 * association error counter is incremented in SCTP_CMD_STRIKE.
5430 */
5431 if (asoc->overall_error_count >= asoc->max_retrans) {
5432 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5433 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005434 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5435 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005437 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5439 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
5440 return SCTP_DISPOSITION_ABORT;
5441 }
5442
5443 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5444 * the ASCONF chunk was sent by doubling the RTO timer value.
5445 * This is done in SCTP_CMD_STRIKE.
5446 */
5447
5448 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5449 * choose an alternate destination address (please refer to RFC2960
5450 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005451 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 * ASCONF sent.
5453 */
5454 sctp_chunk_hold(asoc->addip_last_asconf);
5455 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5456 SCTP_CHUNK(asoc->addip_last_asconf));
5457
5458 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5459 * destination is selected, then the RTO used will be that of the new
5460 * destination address.
5461 */
5462 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5463 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5464
5465 return SCTP_DISPOSITION_CONSUME;
5466}
5467
5468/* sctpimpguide-05 Section 2.12.2
5469 * The sender of the SHUTDOWN MAY also start an overall guard timer
5470 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5471 * At the expiration of this timer the sender SHOULD abort the association
5472 * by sending an ABORT chunk.
5473 */
5474sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5475 const struct sctp_association *asoc,
5476 const sctp_subtype_t type,
5477 void *arg,
5478 sctp_cmd_seq_t *commands)
5479{
5480 struct sctp_chunk *reply = NULL;
5481
5482 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005483 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484
5485 reply = sctp_make_abort(asoc, NULL, 0);
5486 if (!reply)
5487 goto nomem;
5488
5489 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005490 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5491 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005493 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
5495 return SCTP_DISPOSITION_DELETE_TCB;
5496nomem:
5497 return SCTP_DISPOSITION_NOMEM;
5498}
5499
5500/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5501 * the association is automatically closed by starting the shutdown process.
5502 * The work that needs to be done is same as when SHUTDOWN is initiated by
5503 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5504 */
5505sctp_disposition_t sctp_sf_autoclose_timer_expire(
5506 const struct sctp_endpoint *ep,
5507 const struct sctp_association *asoc,
5508 const sctp_subtype_t type,
5509 void *arg,
5510 sctp_cmd_seq_t *commands)
5511{
5512 int disposition;
5513
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005514 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5515
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /* From 9.2 Shutdown of an Association
5517 * Upon receipt of the SHUTDOWN primitive from its upper
5518 * layer, the endpoint enters SHUTDOWN-PENDING state and
5519 * remains there until all outstanding data has been
5520 * acknowledged by its peer. The endpoint accepts no new data
5521 * from its upper layer, but retransmits data to the far end
5522 * if necessary to fill gaps.
5523 */
5524 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5525 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5526
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 disposition = SCTP_DISPOSITION_CONSUME;
5528 if (sctp_outq_is_empty(&asoc->outqueue)) {
5529 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5530 arg, commands);
5531 }
5532 return disposition;
5533}
5534
5535/*****************************************************************************
5536 * These are sa state functions which could apply to all types of events.
5537 ****************************************************************************/
5538
5539/*
5540 * This table entry is not implemented.
5541 *
5542 * Inputs
5543 * (endpoint, asoc, chunk)
5544 *
5545 * The return value is the disposition of the chunk.
5546 */
5547sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5548 const struct sctp_association *asoc,
5549 const sctp_subtype_t type,
5550 void *arg,
5551 sctp_cmd_seq_t *commands)
5552{
5553 return SCTP_DISPOSITION_NOT_IMPL;
5554}
5555
5556/*
5557 * This table entry represents a bug.
5558 *
5559 * Inputs
5560 * (endpoint, asoc, chunk)
5561 *
5562 * The return value is the disposition of the chunk.
5563 */
5564sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5565 const struct sctp_association *asoc,
5566 const sctp_subtype_t type,
5567 void *arg,
5568 sctp_cmd_seq_t *commands)
5569{
5570 return SCTP_DISPOSITION_BUG;
5571}
5572
5573/*
5574 * This table entry represents the firing of a timer in the wrong state.
5575 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5576 * when the association is in the wrong state. This event should
5577 * be ignored, so as to prevent any rearming of the timer.
5578 *
5579 * Inputs
5580 * (endpoint, asoc, chunk)
5581 *
5582 * The return value is the disposition of the chunk.
5583 */
5584sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5585 const struct sctp_association *asoc,
5586 const sctp_subtype_t type,
5587 void *arg,
5588 sctp_cmd_seq_t *commands)
5589{
5590 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5591 return SCTP_DISPOSITION_CONSUME;
5592}
5593
5594/********************************************************************
5595 * 2nd Level Abstractions
5596 ********************************************************************/
5597
5598/* Pull the SACK chunk based on the SACK header. */
5599static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5600{
5601 struct sctp_sackhdr *sack;
5602 unsigned int len;
5603 __u16 num_blocks;
5604 __u16 num_dup_tsns;
5605
5606 /* Protect ourselves from reading too far into
5607 * the skb from a bogus sender.
5608 */
5609 sack = (struct sctp_sackhdr *) chunk->skb->data;
5610
5611 num_blocks = ntohs(sack->num_gap_ack_blocks);
5612 num_dup_tsns = ntohs(sack->num_dup_tsns);
5613 len = sizeof(struct sctp_sackhdr);
5614 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5615 if (len > chunk->skb->len)
5616 return NULL;
5617
5618 skb_pull(chunk->skb, len);
5619
5620 return sack;
5621}
5622
5623/* Create an ABORT packet to be sent as a response, with the specified
5624 * error causes.
5625 */
5626static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5627 const struct sctp_association *asoc,
5628 struct sctp_chunk *chunk,
5629 const void *payload,
5630 size_t paylen)
5631{
5632 struct sctp_packet *packet;
5633 struct sctp_chunk *abort;
5634
5635 packet = sctp_ootb_pkt_new(asoc, chunk);
5636
5637 if (packet) {
5638 /* Make an ABORT.
5639 * The T bit will be set if the asoc is NULL.
5640 */
5641 abort = sctp_make_abort(asoc, chunk, paylen);
5642 if (!abort) {
5643 sctp_ootb_pkt_free(packet);
5644 return NULL;
5645 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005646
5647 /* Reflect vtag if T-Bit is set */
5648 if (sctp_test_T_bit(abort))
5649 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 /* Add specified error causes, i.e., payload, to the
5652 * end of the chunk.
5653 */
5654 sctp_addto_chunk(abort, paylen, payload);
5655
5656 /* Set the skb to the belonging sock for accounting. */
5657 abort->skb->sk = ep->base.sk;
5658
5659 sctp_packet_append_chunk(packet, abort);
5660
5661 }
5662
5663 return packet;
5664}
5665
5666/* Allocate a packet for responding in the OOTB conditions. */
5667static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5668 const struct sctp_chunk *chunk)
5669{
5670 struct sctp_packet *packet;
5671 struct sctp_transport *transport;
5672 __u16 sport;
5673 __u16 dport;
5674 __u32 vtag;
5675
5676 /* Get the source and destination port from the inbound packet. */
5677 sport = ntohs(chunk->sctp_hdr->dest);
5678 dport = ntohs(chunk->sctp_hdr->source);
5679
5680 /* The V-tag is going to be the same as the inbound packet if no
5681 * association exists, otherwise, use the peer's vtag.
5682 */
5683 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005684 /* Special case the INIT-ACK as there is no peer's vtag
5685 * yet.
5686 */
5687 switch(chunk->chunk_hdr->type) {
5688 case SCTP_CID_INIT_ACK:
5689 {
5690 sctp_initack_chunk_t *initack;
5691
5692 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5693 vtag = ntohl(initack->init_hdr.init_tag);
5694 break;
5695 }
5696 default:
5697 vtag = asoc->peer.i.init_tag;
5698 break;
5699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 } else {
5701 /* Special case the INIT and stale COOKIE_ECHO as there is no
5702 * vtag yet.
5703 */
5704 switch(chunk->chunk_hdr->type) {
5705 case SCTP_CID_INIT:
5706 {
5707 sctp_init_chunk_t *init;
5708
5709 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5710 vtag = ntohl(init->init_hdr.init_tag);
5711 break;
5712 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005713 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 vtag = ntohl(chunk->sctp_hdr->vtag);
5715 break;
5716 }
5717 }
5718
5719 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005720 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 if (!transport)
5722 goto nomem;
5723
5724 /* Cache a route for the transport with the chunk's destination as
5725 * the source address.
5726 */
Al Viro16b0a032006-11-20 17:13:38 -08005727 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 sctp_sk(sctp_get_ctl_sock()));
5729
5730 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5731 packet = sctp_packet_config(packet, vtag, 0);
5732
5733 return packet;
5734
5735nomem:
5736 return NULL;
5737}
5738
5739/* Free the packet allocated earlier for responding in the OOTB condition. */
5740void sctp_ootb_pkt_free(struct sctp_packet *packet)
5741{
5742 sctp_transport_free(packet->transport);
5743}
5744
5745/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5746static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5747 const struct sctp_association *asoc,
5748 const struct sctp_chunk *chunk,
5749 sctp_cmd_seq_t *commands,
5750 struct sctp_chunk *err_chunk)
5751{
5752 struct sctp_packet *packet;
5753
5754 if (err_chunk) {
5755 packet = sctp_ootb_pkt_new(asoc, chunk);
5756 if (packet) {
5757 struct sctp_signed_cookie *cookie;
5758
5759 /* Override the OOTB vtag from the cookie. */
5760 cookie = chunk->subh.cookie_hdr;
5761 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005762
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 /* Set the skb to the belonging sock for accounting. */
5764 err_chunk->skb->sk = ep->base.sk;
5765 sctp_packet_append_chunk(packet, err_chunk);
5766 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5767 SCTP_PACKET(packet));
5768 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5769 } else
5770 sctp_chunk_free (err_chunk);
5771 }
5772}
5773
5774
5775/* Process a data chunk */
5776static int sctp_eat_data(const struct sctp_association *asoc,
5777 struct sctp_chunk *chunk,
5778 sctp_cmd_seq_t *commands)
5779{
5780 sctp_datahdr_t *data_hdr;
5781 struct sctp_chunk *err;
5782 size_t datalen;
5783 sctp_verb_t deliver;
5784 int tmp;
5785 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005786 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005787 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788
5789 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5790 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5791
5792 tsn = ntohl(data_hdr->tsn);
5793 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5794
5795 /* ASSERT: Now skb->data is really the user data. */
5796
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 /* Process ECN based congestion.
5798 *
5799 * Since the chunk structure is reused for all chunks within
5800 * a packet, we use ecn_ce_done to track if we've already
5801 * done CE processing for this packet.
5802 *
5803 * We need to do ECN processing even if we plan to discard the
5804 * chunk later.
5805 */
5806
5807 if (!chunk->ecn_ce_done) {
5808 struct sctp_af *af;
5809 chunk->ecn_ce_done = 1;
5810
5811 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005812 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
5814 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5815 /* Do real work as sideffect. */
5816 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5817 SCTP_U32(tsn));
5818 }
5819 }
5820
5821 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5822 if (tmp < 0) {
5823 /* The TSN is too high--silently discard the chunk and
5824 * count on it getting retransmitted later.
5825 */
5826 return SCTP_IERROR_HIGH_TSN;
5827 } else if (tmp > 0) {
5828 /* This is a duplicate. Record it. */
5829 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5830 return SCTP_IERROR_DUP_TSN;
5831 }
5832
5833 /* This is a new TSN. */
5834
5835 /* Discard if there is no room in the receive window.
5836 * Actually, allow a little bit of overflow (up to a MTU).
5837 */
5838 datalen = ntohs(chunk->chunk_hdr->length);
5839 datalen -= sizeof(sctp_data_chunk_t);
5840
5841 deliver = SCTP_CMD_CHUNK_ULP;
5842
5843 /* Think about partial delivery. */
5844 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5845
5846 /* Even if we don't accept this chunk there is
5847 * memory pressure.
5848 */
5849 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5850 }
5851
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005852 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 * seems a bit troublesome in that frag_point varies based on
5854 * PMTU. In cases, such as loopback, this might be a rather
5855 * large spill over.
5856 */
Neil Horman4d93df02007-08-15 16:07:44 -07005857 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5858 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859
5860 /* If this is the next TSN, consider reneging to make
5861 * room. Note: Playing nice with a confused sender. A
5862 * malicious sender can still eat up all our buffer
5863 * space and in the future we may want to detect and
5864 * do more drastic reneging.
5865 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005866 if (sctp_tsnmap_has_gap(map) &&
5867 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5869 deliver = SCTP_CMD_RENEGE;
5870 } else {
5871 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5872 "rwnd: %d\n", tsn, datalen,
5873 asoc->rwnd);
5874 return SCTP_IERROR_IGNORE_TSN;
5875 }
5876 }
5877
5878 /*
Neil Horman4d93df02007-08-15 16:07:44 -07005879 * Also try to renege to limit our memory usage in the event that
5880 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08005881 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07005882 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5883 * memory usage too much
5884 */
5885 if (*sk->sk_prot_creator->memory_pressure) {
5886 if (sctp_tsnmap_has_gap(map) &&
5887 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5888 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5889 deliver = SCTP_CMD_RENEGE;
5890 }
5891 }
5892
5893 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 * Section 3.3.10.9 No User Data (9)
5895 *
5896 * Cause of error
5897 * ---------------
5898 * No User Data: This error cause is returned to the originator of a
5899 * DATA chunk if a received DATA chunk has no user data.
5900 */
5901 if (unlikely(0 == datalen)) {
5902 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5903 if (err) {
5904 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5905 SCTP_CHUNK(err));
5906 }
5907 /* We are going to ABORT, so we might as well stop
5908 * processing the rest of the chunks in the packet.
5909 */
5910 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005911 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5912 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005914 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5916 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5917 return SCTP_IERROR_NO_DATA;
5918 }
5919
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07005920 chunk->data_accepted = 1;
5921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 /* Note: Some chunks may get overcounted (if we drop) or overcounted
5923 * if we renege and the chunk arrives again.
5924 */
5925 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
5926 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
5927 else
5928 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
5929
5930 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
5931 *
5932 * If an endpoint receive a DATA chunk with an invalid stream
5933 * identifier, it shall acknowledge the reception of the DATA chunk
5934 * following the normal procedure, immediately send an ERROR chunk
5935 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
5936 * and discard the DATA chunk.
5937 */
5938 if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07005939 /* Mark tsn as received even though we drop it */
5940 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
5941
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
5943 &data_hdr->stream,
5944 sizeof(data_hdr->stream));
5945 if (err)
5946 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5947 SCTP_CHUNK(err));
5948 return SCTP_IERROR_BAD_STREAM;
5949 }
5950
5951 /* Send the data up to the user. Note: Schedule the
5952 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
5953 * chunk needs the updated rwnd.
5954 */
5955 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
5956
5957 return SCTP_IERROR_NO_ERROR;
5958}