blob: 45b8bcafd8272ca1322d7569e4e6ba9fadfd5faa [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
7 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05008 * This is part of the SCTP Linux Kernel Implementation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * These are the state functions for the state machine.
11 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050012 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
17 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050018 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
28 *
29 * Please send any bug reports or fixes you make to the
30 * email address(es):
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 *
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
35 *
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
48 *
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
51 */
52
53#include <linux/types.h>
54#include <linux/kernel.h>
55#include <linux/ip.h>
56#include <linux/ipv6.h>
57#include <linux/net.h>
58#include <linux/inet.h>
59#include <net/sock.h>
60#include <net/inet_ecn.h>
61#include <linux/skbuff.h>
62#include <net/sctp/sctp.h>
63#include <net/sctp/sm.h>
64#include <net/sctp/structs.h>
65
66static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
67 const struct sctp_association *asoc,
68 struct sctp_chunk *chunk,
69 const void *payload,
70 size_t paylen);
71static int sctp_eat_data(const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
73 sctp_cmd_seq_t *commands);
74static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
75 const struct sctp_chunk *chunk);
76static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
77 const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk,
79 sctp_cmd_seq_t *commands,
80 struct sctp_chunk *err_chunk);
81static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
82 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
84 void *arg,
85 sctp_cmd_seq_t *commands);
86static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
87 const struct sctp_association *asoc,
88 const sctp_subtype_t type,
89 void *arg,
90 sctp_cmd_seq_t *commands);
Vlad Yasevichece25df2007-09-07 16:30:54 -040091static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
92 const struct sctp_association *asoc,
93 const sctp_subtype_t type,
94 void *arg,
95 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
97
Adrian Bunk52c1da32005-06-23 22:05:33 -070098static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -080099 __be16 error, int sk_err,
Adrian Bunk52c1da32005-06-23 22:05:33 -0700100 const struct sctp_association *asoc,
101 struct sctp_transport *transport);
102
Wei Yongjunaecedea2007-08-02 16:57:44 +0800103static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -0400104 const struct sctp_endpoint *ep,
Wei Yongjunaecedea2007-08-02 16:57:44 +0800105 const struct sctp_association *asoc,
106 void *arg,
107 sctp_cmd_seq_t *commands,
108 const __u8 *payload,
109 const size_t paylen);
110
Adrian Bunk52c1da32005-06-23 22:05:33 -0700111static sctp_disposition_t sctp_sf_violation_chunklen(
112 const struct sctp_endpoint *ep,
113 const struct sctp_association *asoc,
114 const sctp_subtype_t type,
115 void *arg,
116 sctp_cmd_seq_t *commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800118static sctp_disposition_t sctp_sf_violation_paramlen(
119 const struct sctp_endpoint *ep,
120 const struct sctp_association *asoc,
121 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -0700122 void *arg, void *ext,
Wei Yongjun6f4c6182007-09-19 17:19:52 +0800123 sctp_cmd_seq_t *commands);
124
Wei Yongjunaecedea2007-08-02 16:57:44 +0800125static sctp_disposition_t sctp_sf_violation_ctsn(
126 const struct sctp_endpoint *ep,
127 const struct sctp_association *asoc,
128 const sctp_subtype_t type,
129 void *arg,
130 sctp_cmd_seq_t *commands);
131
Vlad Yasevichece25df2007-09-07 16:30:54 -0400132static sctp_disposition_t sctp_sf_violation_chunk(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
136 void *arg,
137 sctp_cmd_seq_t *commands);
138
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700139static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
140 const struct sctp_association *asoc,
141 const sctp_subtype_t type,
142 struct sctp_chunk *chunk);
143
Vlad Yasevich75205f42007-12-20 14:12:59 -0800144static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
145 const struct sctp_association *asoc,
146 const sctp_subtype_t type,
147 void *arg,
148 sctp_cmd_seq_t *commands);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/* Small helper function that checks if the chunk length
151 * is of the appropriate length. The 'required_length' argument
152 * is set to be the size of a specific chunk we are testing.
153 * Return Values: 1 = Valid length
154 * 0 = Invalid length
155 *
156 */
157static inline int
158sctp_chunk_length_valid(struct sctp_chunk *chunk,
159 __u16 required_length)
160{
161 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
162
163 if (unlikely(chunk_length < required_length))
164 return 0;
165
166 return 1;
167}
168
169/**********************************************************
170 * These are the state functions for handling chunk events.
171 **********************************************************/
172
173/*
174 * Process the final SHUTDOWN COMPLETE.
175 *
176 * Section: 4 (C) (diagram), 9.2
177 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
178 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
179 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
180 * should stop the T2-shutdown timer and remove all knowledge of the
181 * association (and thus the association enters the CLOSED state).
182 *
Jerome Forissier047a2422005-04-28 11:58:43 -0700183 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * C) Rules for packet carrying SHUTDOWN COMPLETE:
185 * ...
Jerome Forissier047a2422005-04-28 11:58:43 -0700186 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
187 * if the Verification Tag field of the packet matches its own tag and
188 * the T bit is not set
189 * OR
190 * it is set to its peer's tag and the T bit is set in the Chunk
191 * Flags.
192 * Otherwise, the receiver MUST silently discard the packet
193 * and take no further action. An endpoint MUST ignore the
194 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Inputs
197 * (endpoint, asoc, chunk)
198 *
199 * Outputs
200 * (asoc, reply_msg, msg_up, timers, counters)
201 *
202 * The return value is the disposition of the chunk.
203 */
204sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
205 const struct sctp_association *asoc,
206 const sctp_subtype_t type,
207 void *arg,
208 sctp_cmd_seq_t *commands)
209{
210 struct sctp_chunk *chunk = arg;
211 struct sctp_ulpevent *ev;
212
Vlad Yasevichece25df2007-09-07 16:30:54 -0400213 if (!sctp_vtag_verify_either(chunk, asoc))
214 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 /* RFC 2960 6.10 Bundling
217 *
218 * An endpoint MUST NOT bundle INIT, INIT ACK or
219 * SHUTDOWN COMPLETE with any other chunks.
220 */
221 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400222 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Vlad Yasevichece25df2007-09-07 16:30:54 -0400224 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
225 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
226 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
227 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 /* RFC 2960 10.2 SCTP-to-ULP
230 *
231 * H) SHUTDOWN COMPLETE notification
232 *
233 * When SCTP completes the shutdown procedures (section 9.2) this
234 * notification is passed to the upper layer.
235 */
236 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700237 0, 0, 0, NULL, GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700238 if (ev)
239 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900240 SCTP_ULPEVENT(ev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
243 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
244 * not the chunk should be discarded. If the endpoint is in
245 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
246 * T2-shutdown timer and remove all knowledge of the
247 * association (and thus the association enters the CLOSED
248 * state).
249 */
250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
251 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
252
253 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
254 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
255
256 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
257 SCTP_STATE(SCTP_STATE_CLOSED));
258
259 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
260 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
261
262 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
263
264 return SCTP_DISPOSITION_DELETE_TCB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/*
268 * Respond to a normal INIT chunk.
269 * We are the side that is being asked for an association.
270 *
271 * Section: 5.1 Normal Establishment of an Association, B
272 * B) "Z" shall respond immediately with an INIT ACK chunk. The
273 * destination IP address of the INIT ACK MUST be set to the source
274 * IP address of the INIT to which this INIT ACK is responding. In
275 * the response, besides filling in other parameters, "Z" must set the
276 * Verification Tag field to Tag_A, and also provide its own
277 * Verification Tag (Tag_Z) in the Initiate Tag field.
278 *
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900279 * Verification Tag: Must be 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 *
281 * Inputs
282 * (endpoint, asoc, chunk)
283 *
284 * Outputs
285 * (asoc, reply_msg, msg_up, timers, counters)
286 *
287 * The return value is the disposition of the chunk.
288 */
289sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
290 const struct sctp_association *asoc,
291 const sctp_subtype_t type,
292 void *arg,
293 sctp_cmd_seq_t *commands)
294{
295 struct sctp_chunk *chunk = arg;
296 struct sctp_chunk *repl;
297 struct sctp_association *new_asoc;
298 struct sctp_chunk *err_chunk;
299 struct sctp_packet *packet;
300 sctp_unrecognized_param_t *unk_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 int len;
302
303 /* 6.10 Bundling
304 * An endpoint MUST NOT bundle INIT, INIT ACK or
305 * SHUTDOWN COMPLETE with any other chunks.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900306 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * IG Section 2.11.2
308 * Furthermore, we require that the receiver of an INIT chunk MUST
309 * enforce these rules by silently discarding an arriving packet
310 * with an INIT chunk that is bundled with other chunks.
311 */
312 if (!chunk->singleton)
313 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
314
315 /* If the packet is an OOTB packet which is temporarily on the
316 * control endpoint, respond with an ABORT.
317 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800318 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
319 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900324 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326 if (chunk->sctp_hdr->vtag != 0)
327 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
328
329 /* Make sure that the INIT chunk has a valid length.
330 * Normally, this would cause an ABORT with a Protocol Violation
331 * error, but since we don't have an association, we'll
332 * just discard the packet.
333 */
334 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
335 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
336
Vlad Yasevichbec96402009-07-30 18:08:28 -0400337 /* If the INIT is coming toward a closing socket, we'll send back
338 * and ABORT. Essentially, this catches the race of INIT being
339 * backloged to the socket at the same time as the user isses close().
340 * Since the socket and all its associations are going away, we
341 * can treat this OOTB
342 */
343 if (sctp_sstate(ep->base.sk, CLOSING))
344 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Verify the INIT chunk before processing it. */
347 err_chunk = NULL;
348 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
349 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
350 &err_chunk)) {
351 /* This chunk contains fatal error. It is to be discarded.
352 * Send an ABORT, with causes if there is any.
353 */
354 if (err_chunk) {
355 packet = sctp_abort_pkt_new(ep, asoc, arg,
356 (__u8 *)(err_chunk->chunk_hdr) +
357 sizeof(sctp_chunkhdr_t),
358 ntohs(err_chunk->chunk_hdr->length) -
359 sizeof(sctp_chunkhdr_t));
360
361 sctp_chunk_free(err_chunk);
362
363 if (packet) {
364 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
365 SCTP_PACKET(packet));
366 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
367 return SCTP_DISPOSITION_CONSUME;
368 } else {
369 return SCTP_DISPOSITION_NOMEM;
370 }
371 } else {
372 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
373 commands);
374 }
375 }
376
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900377 /* Grab the INIT header. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
379
380 /* Tag the variable length parameters. */
381 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
382
383 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
384 if (!new_asoc)
385 goto nomem;
386
387 /* The call, sctp_process_init(), can fail on memory allocation. */
388 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
389 sctp_source(chunk),
390 (sctp_init_chunk_t *)chunk->chunk_hdr,
391 GFP_ATOMIC))
392 goto nomem_init;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
395
396 /* If there are errors need to be reported for unknown parameters,
397 * make sure to reserve enough room in the INIT ACK for them.
398 */
399 len = 0;
400 if (err_chunk)
401 len = ntohs(err_chunk->chunk_hdr->length) -
402 sizeof(sctp_chunkhdr_t);
403
404 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700405 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
408 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700409 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* If there are errors need to be reported for unknown parameters,
412 * include them in the outgoing INIT ACK as "Unrecognized parameter"
413 * parameter.
414 */
415 if (err_chunk) {
416 /* Get the "Unrecognized parameter" parameter(s) out of the
417 * ERROR chunk generated by sctp_verify_init(). Since the
418 * error cause code for "unknown parameter" and the
419 * "Unrecognized parameter" type is the same, we can
420 * construct the parameters in INIT ACK by copying the
421 * ERROR causes over.
422 */
423 unk_param = (sctp_unrecognized_param_t *)
424 ((__u8 *)(err_chunk->chunk_hdr) +
425 sizeof(sctp_chunkhdr_t));
426 /* Replace the cause code with the "Unrecognized parameter"
427 * parameter type.
428 */
429 sctp_addto_chunk(repl, len, unk_param);
430 sctp_chunk_free(err_chunk);
431 }
432
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700433 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
436
437 /*
438 * Note: After sending out INIT ACK with the State Cookie parameter,
439 * "Z" MUST NOT allocate any resources, nor keep any states for the
440 * new association. Otherwise, "Z" will be vulnerable to resource
441 * attacks.
442 */
443 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
444
445 return SCTP_DISPOSITION_DELETE_TCB;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447nomem_init:
448 sctp_association_free(new_asoc);
449nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700450 if (err_chunk)
451 sctp_chunk_free(err_chunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return SCTP_DISPOSITION_NOMEM;
453}
454
455/*
456 * Respond to a normal INIT ACK chunk.
457 * We are the side that is initiating the association.
458 *
459 * Section: 5.1 Normal Establishment of an Association, C
460 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
461 * timer and leave COOKIE-WAIT state. "A" shall then send the State
462 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
463 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
464 *
465 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
466 * DATA chunks, but it MUST be the first chunk in the packet and
467 * until the COOKIE ACK is returned the sender MUST NOT send any
468 * other packets to the peer.
469 *
470 * Verification Tag: 3.3.3
471 * If the value of the Initiate Tag in a received INIT ACK chunk is
472 * found to be 0, the receiver MUST treat it as an error and close the
473 * association by transmitting an ABORT.
474 *
475 * Inputs
476 * (endpoint, asoc, chunk)
477 *
478 * Outputs
479 * (asoc, reply_msg, msg_up, timers, counters)
480 *
481 * The return value is the disposition of the chunk.
482 */
483sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
484 const struct sctp_association *asoc,
485 const sctp_subtype_t type,
486 void *arg,
487 sctp_cmd_seq_t *commands)
488{
489 struct sctp_chunk *chunk = arg;
490 sctp_init_chunk_t *initchunk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct sctp_chunk *err_chunk;
492 struct sctp_packet *packet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (!sctp_vtag_verify(chunk, asoc))
495 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /* 6.10 Bundling
498 * An endpoint MUST NOT bundle INIT, INIT ACK or
499 * SHUTDOWN COMPLETE with any other chunks.
500 */
501 if (!chunk->singleton)
Vlad Yasevichece25df2007-09-07 16:30:54 -0400502 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Vlad Yasevichece25df2007-09-07 16:30:54 -0400504 /* Make sure that the INIT-ACK chunk has a valid length */
505 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
506 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
507 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* Grab the INIT header. */
509 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Verify the INIT chunk before processing it. */
512 err_chunk = NULL;
513 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
514 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
515 &err_chunk)) {
516
Vlad Yasevich853f4b52008-01-20 06:10:46 -0800517 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /* This chunk contains fatal error. It is to be discarded.
Vlad Yasevichd6701192007-12-20 14:13:31 -0800520 * Send an ABORT, with causes. If there are no causes,
521 * then there wasn't enough memory. Just terminate
522 * the association.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 */
524 if (err_chunk) {
525 packet = sctp_abort_pkt_new(ep, asoc, arg,
526 (__u8 *)(err_chunk->chunk_hdr) +
527 sizeof(sctp_chunkhdr_t),
528 ntohs(err_chunk->chunk_hdr->length) -
529 sizeof(sctp_chunkhdr_t));
530
531 sctp_chunk_free(err_chunk);
532
533 if (packet) {
534 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
535 SCTP_PACKET(packet));
536 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700537 error = SCTP_ERROR_INV_PARAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700540
541 /* SCTP-AUTH, Section 6.3:
542 * It should be noted that if the receiver wants to tear
543 * down an association in an authenticated way only, the
544 * handling of malformed packets should not result in
545 * tearing down the association.
546 *
547 * This means that if we only want to abort associations
548 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +0200549 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700550 * was malformed.
551 */
552 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
553 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
554
555 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700556 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
557 asoc, chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
560 /* Tag the variable length parameters. Note that we never
561 * convert the parameters in an INIT chunk.
562 */
563 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
564
565 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
566
567 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
568 SCTP_PEER_INIT(initchunk));
569
Frank Filz3f7a87d2005-06-20 13:14:57 -0700570 /* Reset init error count upon receipt of INIT-ACK. */
571 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* 5.1 C) "A" shall stop the T1-init timer and leave
574 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
575 * timer, and enter the COOKIE-ECHOED state.
576 */
577 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
578 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
579 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
580 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
581 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
582 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
583
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700584 /* SCTP-AUTH: genereate the assocition shared keys so that
585 * we can potentially signe the COOKIE-ECHO.
586 */
587 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* 5.1 C) "A" shall then send the State Cookie received in the
590 * INIT ACK chunk in a COOKIE ECHO chunk, ...
591 */
592 /* If there is any errors to report, send the ERROR chunk generated
593 * for unknown parameters as well.
594 */
595 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
596 SCTP_CHUNK(err_chunk));
597
598 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/*
602 * Respond to a normal COOKIE ECHO chunk.
603 * We are the side that is being asked for an association.
604 *
605 * Section: 5.1 Normal Establishment of an Association, D
606 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
607 * with a COOKIE ACK chunk after building a TCB and moving to
608 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
609 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
610 * chunk MUST be the first chunk in the packet.
611 *
612 * IMPLEMENTATION NOTE: An implementation may choose to send the
613 * Communication Up notification to the SCTP user upon reception
614 * of a valid COOKIE ECHO chunk.
615 *
616 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
617 * D) Rules for packet carrying a COOKIE ECHO
618 *
619 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
620 * Initial Tag received in the INIT ACK.
621 *
622 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
623 *
624 * Inputs
625 * (endpoint, asoc, chunk)
626 *
627 * Outputs
628 * (asoc, reply_msg, msg_up, timers, counters)
629 *
630 * The return value is the disposition of the chunk.
631 */
632sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
633 const struct sctp_association *asoc,
634 const sctp_subtype_t type, void *arg,
635 sctp_cmd_seq_t *commands)
636{
637 struct sctp_chunk *chunk = arg;
638 struct sctp_association *new_asoc;
639 sctp_init_chunk_t *peer_init;
640 struct sctp_chunk *repl;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700641 struct sctp_ulpevent *ev, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 int error = 0;
643 struct sctp_chunk *err_chk_p;
Vlad Yasevich609ee462007-08-31 03:10:59 +0900644 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* If the packet is an OOTB packet which is temporarily on the
647 * control endpoint, respond with an ABORT.
648 */
Wei Yongjun8190f892008-09-08 12:13:55 +0800649 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
650 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
Vlad Yasevichece25df2007-09-07 16:30:54 -0400651 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Wei Yongjun8190f892008-09-08 12:13:55 +0800652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* Make sure that the COOKIE_ECHO chunk has a valid length.
655 * In this case, we check that we have enough for at least a
656 * chunk header. More detailed verification is done
657 * in sctp_unpack_cookie().
658 */
659 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
660 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
661
Vlad Yasevich609ee462007-08-31 03:10:59 +0900662 /* If the endpoint is not listening or if the number of associations
663 * on the TCP-style socket exceed the max backlog, respond with an
664 * ABORT.
665 */
666 sk = ep->base.sk;
667 if (!sctp_sstate(sk, LISTENING) ||
668 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
669 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 /* "Decode" the chunk. We have no optional parameters so we
672 * are in good shape.
673 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900674 chunk->subh.cookie_hdr =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -0700676 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
677 sizeof(sctp_chunkhdr_t)))
678 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
681 * "Z" will reply with a COOKIE ACK chunk after building a TCB
682 * and moving to the ESTABLISHED state.
683 */
684 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
685 &err_chk_p);
686
687 /* FIXME:
688 * If the re-build failed, what is the proper error path
689 * from here?
690 *
691 * [We should abort the association. --piggy]
692 */
693 if (!new_asoc) {
694 /* FIXME: Several errors are possible. A bad cookie should
695 * be silently discarded, but think about logging it too.
696 */
697 switch (error) {
698 case -SCTP_IERROR_NOMEM:
699 goto nomem;
700
701 case -SCTP_IERROR_STALE_COOKIE:
702 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
703 err_chk_p);
704 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
705
706 case -SCTP_IERROR_BAD_SIG:
707 default:
708 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700713 /* Delay state machine commands until later.
714 *
715 * Re-build the bind address for the association is done in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * the sctp_unpack_cookie() already.
717 */
718 /* This is a brand-new association, so these are not yet side
719 * effects--it is safe to run them here.
720 */
721 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
722
723 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
Al Viro6a1e5f32006-11-20 17:12:25 -0800724 &chunk->subh.cookie_hdr->c.peer_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 peer_init, GFP_ATOMIC))
726 goto nomem_init;
727
Vlad Yasevich730fc3d2007-09-16 19:32:11 -0700728 /* SCTP-AUTH: Now that we've populate required fields in
729 * sctp_process_init, set up the assocaition shared keys as
730 * necessary so that we can potentially authenticate the ACK
731 */
732 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
733 if (error)
734 goto nomem_init;
735
Vlad Yasevichbbd0d592007-10-03 17:51:34 -0700736 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
737 * is supposed to be authenticated and we have to do delayed
738 * authentication. We've just recreated the association using
739 * the information in the cookie and now it's much easier to
740 * do the authentication.
741 */
742 if (chunk->auth_chunk) {
743 struct sctp_chunk auth;
744 sctp_ierror_t ret;
745
746 /* set-up our fake chunk so that we can process it */
747 auth.skb = chunk->auth_chunk;
748 auth.asoc = chunk->asoc;
749 auth.sctp_hdr = chunk->sctp_hdr;
750 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
751 sizeof(sctp_chunkhdr_t));
752 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
753 auth.transport = chunk->transport;
754
755 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
756
757 /* We can now safely free the auth_chunk clone */
758 kfree_skb(chunk->auth_chunk);
759
760 if (ret != SCTP_IERROR_NO_ERROR) {
761 sctp_association_free(new_asoc);
762 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
763 }
764 }
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 repl = sctp_make_cookie_ack(new_asoc, chunk);
767 if (!repl)
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700768 goto nomem_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* RFC 2960 5.1 Normal Establishment of an Association
771 *
772 * D) IMPLEMENTATION NOTE: An implementation may choose to
773 * send the Communication Up notification to the SCTP user
774 * upon reception of a valid COOKIE ECHO chunk.
775 */
776 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
777 new_asoc->c.sinit_num_ostreams,
778 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700779 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!ev)
781 goto nomem_ev;
782
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900783 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800784 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800786 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800788 if (new_asoc->peer.adaptation_ind) {
789 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700791 if (!ai_ev)
792 goto nomem_aiev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
794
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700795 /* Add all the state machine commands now since we've created
796 * everything. This way we don't introduce memory corruptions
797 * during side-effect processing and correclty count established
798 * associations.
799 */
800 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
801 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
802 SCTP_STATE(SCTP_STATE_ESTABLISHED));
803 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
804 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
805 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
806
807 if (new_asoc->autoclose)
808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
809 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
810
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700811 /* This will send the COOKIE ACK */
812 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
813
814 /* Queue the ASSOC_CHANGE event */
815 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
816
817 /* Send up the Adaptation Layer Indication event */
818 if (ai_ev)
819 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
820 SCTP_ULPEVENT(ai_ev));
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return SCTP_DISPOSITION_CONSUME;
823
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -0700824nomem_aiev:
825 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826nomem_ev:
827 sctp_chunk_free(repl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828nomem_init:
829 sctp_association_free(new_asoc);
830nomem:
831 return SCTP_DISPOSITION_NOMEM;
832}
833
834/*
835 * Respond to a normal COOKIE ACK chunk.
836 * We are the side that is being asked for an association.
837 *
838 * RFC 2960 5.1 Normal Establishment of an Association
839 *
840 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
841 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
842 * timer. It may also notify its ULP about the successful
843 * establishment of the association with a Communication Up
844 * notification (see Section 10).
845 *
846 * Verification Tag:
847 * Inputs
848 * (endpoint, asoc, chunk)
849 *
850 * Outputs
851 * (asoc, reply_msg, msg_up, timers, counters)
852 *
853 * The return value is the disposition of the chunk.
854 */
855sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
856 const struct sctp_association *asoc,
857 const sctp_subtype_t type, void *arg,
858 sctp_cmd_seq_t *commands)
859{
860 struct sctp_chunk *chunk = arg;
861 struct sctp_ulpevent *ev;
862
863 if (!sctp_vtag_verify(chunk, asoc))
864 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
865
866 /* Verify that the chunk length for the COOKIE-ACK is OK.
867 * If we don't do this, any bundled chunks may be junked.
868 */
869 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
870 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
871 commands);
872
873 /* Reset init error count upon receipt of COOKIE-ACK,
874 * to avoid problems with the managemement of this
875 * counter in stale cookie situations when a transition back
876 * from the COOKIE-ECHOED state to the COOKIE-WAIT
877 * state is performed.
878 */
Frank Filz3f7a87d2005-06-20 13:14:57 -0700879 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* RFC 2960 5.1 Normal Establishment of an Association
882 *
883 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
884 * from the COOKIE-ECHOED state to the ESTABLISHED state,
885 * stopping the T1-cookie timer.
886 */
887 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
888 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
889 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
890 SCTP_STATE(SCTP_STATE_ESTABLISHED));
891 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
892 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
893 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
894 if (asoc->autoclose)
895 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
896 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* It may also notify its ULP about the successful
899 * establishment of the association with a Communication Up
900 * notification (see Section 10).
901 */
902 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
903 0, asoc->c.sinit_num_ostreams,
904 asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -0700905 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (!ev)
908 goto nomem;
909
910 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
911
912 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800913 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800915 * peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -0800917 if (asoc->peer.adaptation_ind) {
918 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (!ev)
920 goto nomem;
921
922 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
923 SCTP_ULPEVENT(ev));
924 }
925
926 return SCTP_DISPOSITION_CONSUME;
927nomem:
928 return SCTP_DISPOSITION_NOMEM;
929}
930
931/* Generate and sendout a heartbeat packet. */
932static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
933 const struct sctp_association *asoc,
934 const sctp_subtype_t type,
935 void *arg,
936 sctp_cmd_seq_t *commands)
937{
938 struct sctp_transport *transport = (struct sctp_transport *) arg;
939 struct sctp_chunk *reply;
940 sctp_sender_hb_info_t hbinfo;
941 size_t paylen = 0;
942
943 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
944 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
Al Viro63de08f2006-11-20 17:07:25 -0800945 hbinfo.daddr = transport->ipaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 hbinfo.sent_at = jiffies;
Sridhar Samudralaad8fec12006-07-21 14:48:50 -0700947 hbinfo.hb_nonce = transport->hb_nonce;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* Send a heartbeat to our peer. */
950 paylen = sizeof(sctp_sender_hb_info_t);
951 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
952 if (!reply)
953 return SCTP_DISPOSITION_NOMEM;
954
955 /* Set rto_pending indicating that an RTT measurement
956 * is started with this heartbeat chunk.
957 */
958 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
959 SCTP_TRANSPORT(transport));
960
961 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
962 return SCTP_DISPOSITION_CONSUME;
963}
964
965/* Generate a HEARTBEAT packet on the given transport. */
966sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
967 const struct sctp_association *asoc,
968 const sctp_subtype_t type,
969 void *arg,
970 sctp_cmd_seq_t *commands)
971{
972 struct sctp_transport *transport = (struct sctp_transport *) arg;
973
Vlad Yasevichb9f84782009-08-26 09:36:25 -0400974 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -0700975 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
976 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
978 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -0800979 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
981 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
982 return SCTP_DISPOSITION_DELETE_TCB;
983 }
984
985 /* Section 3.3.5.
986 * The Sender-specific Heartbeat Info field should normally include
987 * information about the sender's current time when this HEARTBEAT
988 * chunk is sent and the destination transport address to which this
989 * HEARTBEAT is sent (see Section 8.3).
990 */
991
Frank Filz52ccb8e2005-12-22 11:36:46 -0800992 if (transport->param_flags & SPP_HB_ENABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (SCTP_DISPOSITION_NOMEM ==
994 sctp_sf_heartbeat(ep, asoc, type, arg,
995 commands))
996 return SCTP_DISPOSITION_NOMEM;
997 /* Set transport error counter and association error counter
998 * when sending heartbeat.
999 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00001000 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1001 SCTP_TRANSPORT(transport));
1002 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 SCTP_TRANSPORT(transport));
1004 }
1005 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1006 SCTP_TRANSPORT(transport));
1007
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001008 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011/*
1012 * Process an heartbeat request.
1013 *
1014 * Section: 8.3 Path Heartbeat
1015 * The receiver of the HEARTBEAT should immediately respond with a
1016 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1017 * from the received HEARTBEAT chunk.
1018 *
1019 * Verification Tag: 8.5 Verification Tag [Normal verification]
1020 * When receiving an SCTP packet, the endpoint MUST ensure that the
1021 * value in the Verification Tag field of the received SCTP packet
1022 * matches its own Tag. If the received Verification Tag value does not
1023 * match the receiver's own tag value, the receiver shall silently
1024 * discard the packet and shall not process it any further except for
1025 * those cases listed in Section 8.5.1 below.
1026 *
1027 * Inputs
1028 * (endpoint, asoc, chunk)
1029 *
1030 * Outputs
1031 * (asoc, reply_msg, msg_up, timers, counters)
1032 *
1033 * The return value is the disposition of the chunk.
1034 */
1035sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1036 const struct sctp_association *asoc,
1037 const sctp_subtype_t type,
1038 void *arg,
1039 sctp_cmd_seq_t *commands)
1040{
1041 struct sctp_chunk *chunk = arg;
1042 struct sctp_chunk *reply;
1043 size_t paylen = 0;
1044
1045 if (!sctp_vtag_verify(chunk, asoc))
1046 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1047
1048 /* Make sure that the HEARTBEAT chunk has a valid length. */
1049 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1050 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1051 commands);
1052
1053 /* 8.3 The receiver of the HEARTBEAT should immediately
1054 * respond with a HEARTBEAT ACK that contains the Heartbeat
1055 * Information field copied from the received HEARTBEAT chunk.
1056 */
1057 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1058 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001059 if (!pskb_pull(chunk->skb, paylen))
1060 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 reply = sctp_make_heartbeat_ack(asoc, chunk,
1063 chunk->subh.hb_hdr, paylen);
1064 if (!reply)
1065 goto nomem;
1066
1067 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1068 return SCTP_DISPOSITION_CONSUME;
1069
1070nomem:
1071 return SCTP_DISPOSITION_NOMEM;
1072}
1073
1074/*
1075 * Process the returning HEARTBEAT ACK.
1076 *
1077 * Section: 8.3 Path Heartbeat
1078 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1079 * should clear the error counter of the destination transport
1080 * address to which the HEARTBEAT was sent, and mark the destination
1081 * transport address as active if it is not so marked. The endpoint may
1082 * optionally report to the upper layer when an inactive destination
1083 * address is marked as active due to the reception of the latest
1084 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1085 * clear the association overall error count as well (as defined
1086 * in section 8.1).
1087 *
1088 * The receiver of the HEARTBEAT ACK should also perform an RTT
1089 * measurement for that destination transport address using the time
1090 * value carried in the HEARTBEAT ACK chunk.
1091 *
1092 * Verification Tag: 8.5 Verification Tag [Normal verification]
1093 *
1094 * Inputs
1095 * (endpoint, asoc, chunk)
1096 *
1097 * Outputs
1098 * (asoc, reply_msg, msg_up, timers, counters)
1099 *
1100 * The return value is the disposition of the chunk.
1101 */
1102sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1103 const struct sctp_association *asoc,
1104 const sctp_subtype_t type,
1105 void *arg,
1106 sctp_cmd_seq_t *commands)
1107{
1108 struct sctp_chunk *chunk = arg;
1109 union sctp_addr from_addr;
1110 struct sctp_transport *link;
1111 sctp_sender_hb_info_t *hbinfo;
1112 unsigned long max_interval;
1113
1114 if (!sctp_vtag_verify(chunk, asoc))
1115 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1116
1117 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
Wei Yongjundadb50c2009-08-22 11:27:37 +08001118 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1119 sizeof(sctp_sender_hb_info_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1121 commands);
1122
1123 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001124 /* Make sure that the length of the parameter is what we expect */
1125 if (ntohs(hbinfo->param_hdr.length) !=
1126 sizeof(sctp_sender_hb_info_t)) {
1127 return SCTP_DISPOSITION_DISCARD;
1128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001131 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001134 if (unlikely(!link)) {
1135 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001136 if (net_ratelimit())
1137 printk(KERN_WARNING
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001138 "%s association %p could not find address %pI6\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001139 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001140 asoc,
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001141 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001142 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001143 if (net_ratelimit())
1144 printk(KERN_WARNING
Harvey Harrison21454aa2008-10-31 00:54:56 -07001145 "%s association %p could not find address %pI4\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001146 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001147 asoc,
Harvey Harrison21454aa2008-10-31 00:54:56 -07001148 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return SCTP_DISPOSITION_DISCARD;
1151 }
1152
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001153 /* Validate the 64-bit random nonce. */
1154 if (hbinfo->hb_nonce != link->hb_nonce)
1155 return SCTP_DISPOSITION_DISCARD;
1156
Frank Filz52ccb8e2005-12-22 11:36:46 -08001157 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* Check if the timestamp looks valid. */
1160 if (time_after(hbinfo->sent_at, jiffies) ||
1161 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001162 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001164 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return SCTP_DISPOSITION_DISCARD;
1166 }
1167
1168 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1169 * the HEARTBEAT should clear the error counter of the
1170 * destination transport address to which the HEARTBEAT was
1171 * sent and mark the destination transport address as active if
1172 * it is not so marked.
1173 */
1174 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1175
1176 return SCTP_DISPOSITION_CONSUME;
1177}
1178
1179/* Helper function to send out an abort for the restart
1180 * condition.
1181 */
1182static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1183 struct sctp_chunk *init,
1184 sctp_cmd_seq_t *commands)
1185{
1186 int len;
1187 struct sctp_packet *pkt;
1188 union sctp_addr_param *addrparm;
1189 struct sctp_errhdr *errhdr;
1190 struct sctp_endpoint *ep;
1191 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1192 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1193
1194 /* Build the error on the stack. We are way to malloc crazy
1195 * throughout the code today.
1196 */
1197 errhdr = (struct sctp_errhdr *)buffer;
1198 addrparm = (union sctp_addr_param *)errhdr->variable;
1199
1200 /* Copy into a parm format. */
1201 len = af->to_addr_param(ssa, addrparm);
1202 len += sizeof(sctp_errhdr_t);
1203
1204 errhdr->cause = SCTP_ERROR_RESTART;
1205 errhdr->length = htons(len);
1206
1207 /* Assign to the control socket. */
1208 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1209
1210 /* Association is NULL since this may be a restart attack and we
1211 * want to send back the attacker's vtag.
1212 */
1213 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1214
1215 if (!pkt)
1216 goto out;
1217 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1218
1219 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1220
1221 /* Discard the rest of the inbound packet. */
1222 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1223
1224out:
1225 /* Even if there is no memory, treat as a failure so
1226 * the packet will get dropped.
1227 */
1228 return 0;
1229}
1230
1231/* A restart is occurring, check to make sure no new addresses
1232 * are being added as we may be under a takeover attack.
1233 */
1234static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1235 const struct sctp_association *asoc,
1236 struct sctp_chunk *init,
1237 sctp_cmd_seq_t *commands)
1238{
1239 struct sctp_transport *new_addr, *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 int found;
1241
1242 /* Implementor's Guide - Sectin 5.2.2
1243 * ...
1244 * Before responding the endpoint MUST check to see if the
1245 * unexpected INIT adds new addresses to the association. If new
1246 * addresses are added to the association, the endpoint MUST respond
1247 * with an ABORT..
1248 */
1249
1250 /* Search through all current addresses and make sure
1251 * we aren't adding any new ones.
1252 */
1253 new_addr = NULL;
1254 found = 0;
1255
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001256 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1257 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 found = 0;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001259 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1260 transports) {
Al Viro5f242a12006-11-20 17:05:23 -08001261 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1262 &addr->ipaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 found = 1;
1264 break;
1265 }
1266 }
1267 if (!found)
1268 break;
1269 }
1270
1271 /* If a new address was added, ABORT the sender. */
1272 if (!found && new_addr) {
Al Viro7dd8a582006-11-20 17:23:25 -08001273 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275
1276 /* Return success if all addresses were found. */
1277 return found;
1278}
1279
1280/* Populate the verification/tie tags based on overlapping INIT
1281 * scenario.
1282 *
1283 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1284 */
1285static void sctp_tietags_populate(struct sctp_association *new_asoc,
1286 const struct sctp_association *asoc)
1287{
1288 switch (asoc->state) {
1289
1290 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1291
1292 case SCTP_STATE_COOKIE_WAIT:
1293 new_asoc->c.my_vtag = asoc->c.my_vtag;
1294 new_asoc->c.my_ttag = asoc->c.my_vtag;
1295 new_asoc->c.peer_ttag = 0;
1296 break;
1297
1298 case SCTP_STATE_COOKIE_ECHOED:
1299 new_asoc->c.my_vtag = asoc->c.my_vtag;
1300 new_asoc->c.my_ttag = asoc->c.my_vtag;
1301 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1302 break;
1303
1304 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1305 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1306 */
1307 default:
1308 new_asoc->c.my_ttag = asoc->c.my_vtag;
1309 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1310 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 /* Other parameters for the endpoint SHOULD be copied from the
1314 * existing parameters of the association (e.g. number of
1315 * outbound streams) into the INIT ACK and cookie.
1316 */
1317 new_asoc->rwnd = asoc->rwnd;
1318 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1319 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1320 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1321}
1322
1323/*
1324 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1325 * handling action.
1326 *
1327 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1328 *
1329 * Returns value representing action to be taken. These action values
1330 * correspond to Action/Description values in RFC 2960, Table 2.
1331 */
1332static char sctp_tietags_compare(struct sctp_association *new_asoc,
1333 const struct sctp_association *asoc)
1334{
1335 /* In this case, the peer may have restarted. */
1336 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1337 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1338 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1339 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1340 return 'A';
1341
1342 /* Collision case B. */
1343 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1344 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1345 (0 == asoc->c.peer_vtag))) {
1346 return 'B';
1347 }
1348
1349 /* Collision case D. */
1350 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1351 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1352 return 'D';
1353
1354 /* Collision case C. */
1355 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1356 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1357 (0 == new_asoc->c.my_ttag) &&
1358 (0 == new_asoc->c.peer_ttag))
1359 return 'C';
1360
1361 /* No match to any of the special cases; discard this packet. */
1362 return 'E';
1363}
1364
1365/* Common helper routine for both duplicate and simulataneous INIT
1366 * chunk handling.
1367 */
1368static sctp_disposition_t sctp_sf_do_unexpected_init(
1369 const struct sctp_endpoint *ep,
1370 const struct sctp_association *asoc,
1371 const sctp_subtype_t type,
1372 void *arg, sctp_cmd_seq_t *commands)
1373{
1374 sctp_disposition_t retval;
1375 struct sctp_chunk *chunk = arg;
1376 struct sctp_chunk *repl;
1377 struct sctp_association *new_asoc;
1378 struct sctp_chunk *err_chunk;
1379 struct sctp_packet *packet;
1380 sctp_unrecognized_param_t *unk_param;
1381 int len;
1382
1383 /* 6.10 Bundling
1384 * An endpoint MUST NOT bundle INIT, INIT ACK or
1385 * SHUTDOWN COMPLETE with any other chunks.
1386 *
1387 * IG Section 2.11.2
1388 * Furthermore, we require that the receiver of an INIT chunk MUST
1389 * enforce these rules by silently discarding an arriving packet
1390 * with an INIT chunk that is bundled with other chunks.
1391 */
1392 if (!chunk->singleton)
1393 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1394
1395 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001396 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
1398 if (chunk->sctp_hdr->vtag != 0)
1399 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1400
1401 /* Make sure that the INIT chunk has a valid length.
1402 * In this case, we generate a protocol violation since we have
1403 * an association established.
1404 */
1405 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1406 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1407 commands);
1408 /* Grab the INIT header. */
1409 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1410
1411 /* Tag the variable length parameters. */
1412 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1413
1414 /* Verify the INIT chunk before processing it. */
1415 err_chunk = NULL;
1416 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1417 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1418 &err_chunk)) {
1419 /* This chunk contains fatal error. It is to be discarded.
1420 * Send an ABORT, with causes if there is any.
1421 */
1422 if (err_chunk) {
1423 packet = sctp_abort_pkt_new(ep, asoc, arg,
1424 (__u8 *)(err_chunk->chunk_hdr) +
1425 sizeof(sctp_chunkhdr_t),
1426 ntohs(err_chunk->chunk_hdr->length) -
1427 sizeof(sctp_chunkhdr_t));
1428
1429 if (packet) {
1430 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1431 SCTP_PACKET(packet));
1432 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1433 retval = SCTP_DISPOSITION_CONSUME;
1434 } else {
1435 retval = SCTP_DISPOSITION_NOMEM;
1436 }
1437 goto cleanup;
1438 } else {
1439 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1440 commands);
1441 }
1442 }
1443
1444 /*
1445 * Other parameters for the endpoint SHOULD be copied from the
1446 * existing parameters of the association (e.g. number of
1447 * outbound streams) into the INIT ACK and cookie.
1448 * FIXME: We are copying parameters from the endpoint not the
1449 * association.
1450 */
1451 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1452 if (!new_asoc)
1453 goto nomem;
1454
1455 /* In the outbound INIT ACK the endpoint MUST copy its current
1456 * Verification Tag and Peers Verification tag into a reserved
1457 * place (local tie-tag and per tie-tag) within the state cookie.
1458 */
1459 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1460 sctp_source(chunk),
1461 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001462 GFP_ATOMIC))
1463 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 /* Make sure no new addresses are being added during the
1466 * restart. Do not do this check for COOKIE-WAIT state,
1467 * since there are no peer addresses to check against.
1468 * Upon return an ABORT will have been sent if needed.
1469 */
1470 if (!sctp_state(asoc, COOKIE_WAIT)) {
1471 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1472 commands)) {
1473 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001474 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
1476 }
1477
1478 sctp_tietags_populate(new_asoc, asoc);
1479
1480 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1481
1482 /* If there are errors need to be reported for unknown parameters,
1483 * make sure to reserve enough room in the INIT ACK for them.
1484 */
1485 len = 0;
1486 if (err_chunk) {
1487 len = ntohs(err_chunk->chunk_hdr->length) -
1488 sizeof(sctp_chunkhdr_t);
1489 }
1490
1491 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1492 goto nomem;
1493
1494 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1495 if (!repl)
1496 goto nomem;
1497
1498 /* If there are errors need to be reported for unknown parameters,
1499 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1500 * parameter.
1501 */
1502 if (err_chunk) {
1503 /* Get the "Unrecognized parameter" parameter(s) out of the
1504 * ERROR chunk generated by sctp_verify_init(). Since the
1505 * error cause code for "unknown parameter" and the
1506 * "Unrecognized parameter" type is the same, we can
1507 * construct the parameters in INIT ACK by copying the
1508 * ERROR causes over.
1509 */
1510 unk_param = (sctp_unrecognized_param_t *)
1511 ((__u8 *)(err_chunk->chunk_hdr) +
1512 sizeof(sctp_chunkhdr_t));
1513 /* Replace the cause code with the "Unrecognized parameter"
1514 * parameter type.
1515 */
1516 sctp_addto_chunk(repl, len, unk_param);
1517 }
1518
1519 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1520 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1521
1522 /*
1523 * Note: After sending out INIT ACK with the State Cookie parameter,
1524 * "Z" MUST NOT allocate any resources for this new association.
1525 * Otherwise, "Z" will be vulnerable to resource attacks.
1526 */
1527 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1528 retval = SCTP_DISPOSITION_CONSUME;
1529
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001530 return retval;
1531
1532nomem:
1533 retval = SCTP_DISPOSITION_NOMEM;
1534nomem_retval:
1535 if (new_asoc)
1536 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537cleanup:
1538 if (err_chunk)
1539 sctp_chunk_free(err_chunk);
1540 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541}
1542
1543/*
1544 * Handle simultanous INIT.
1545 * This means we started an INIT and then we got an INIT request from
1546 * our peer.
1547 *
1548 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1549 * This usually indicates an initialization collision, i.e., each
1550 * endpoint is attempting, at about the same time, to establish an
1551 * association with the other endpoint.
1552 *
1553 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1554 * endpoint MUST respond with an INIT ACK using the same parameters it
1555 * sent in its original INIT chunk (including its Verification Tag,
1556 * unchanged). These original parameters are combined with those from the
1557 * newly received INIT chunk. The endpoint shall also generate a State
1558 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1559 * INIT to calculate the State Cookie.
1560 *
1561 * After that, the endpoint MUST NOT change its state, the T1-init
1562 * timer shall be left running and the corresponding TCB MUST NOT be
1563 * destroyed. The normal procedures for handling State Cookies when
1564 * a TCB exists will resolve the duplicate INITs to a single association.
1565 *
1566 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1567 * its Tie-Tags with the Tag information of itself and its peer (see
1568 * section 5.2.2 for a description of the Tie-Tags).
1569 *
1570 * Verification Tag: Not explicit, but an INIT can not have a valid
1571 * verification tag, so we skip the check.
1572 *
1573 * Inputs
1574 * (endpoint, asoc, chunk)
1575 *
1576 * Outputs
1577 * (asoc, reply_msg, msg_up, timers, counters)
1578 *
1579 * The return value is the disposition of the chunk.
1580 */
1581sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1582 const struct sctp_association *asoc,
1583 const sctp_subtype_t type,
1584 void *arg,
1585 sctp_cmd_seq_t *commands)
1586{
1587 /* Call helper to do the real work for both simulataneous and
1588 * duplicate INIT chunk handling.
1589 */
1590 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1591}
1592
1593/*
1594 * Handle duplicated INIT messages. These are usually delayed
1595 * restransmissions.
1596 *
1597 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1598 * COOKIE-ECHOED and COOKIE-WAIT
1599 *
1600 * Unless otherwise stated, upon reception of an unexpected INIT for
1601 * this association, the endpoint shall generate an INIT ACK with a
1602 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1603 * current Verification Tag and peer's Verification Tag into a reserved
1604 * place within the state cookie. We shall refer to these locations as
1605 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1606 * containing this INIT ACK MUST carry a Verification Tag value equal to
1607 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1608 * MUST contain a new Initiation Tag (randomly generated see Section
1609 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1610 * existing parameters of the association (e.g. number of outbound
1611 * streams) into the INIT ACK and cookie.
1612 *
1613 * After sending out the INIT ACK, the endpoint shall take no further
1614 * actions, i.e., the existing association, including its current state,
1615 * and the corresponding TCB MUST NOT be changed.
1616 *
1617 * Note: Only when a TCB exists and the association is not in a COOKIE-
1618 * WAIT state are the Tie-Tags populated. For a normal association INIT
1619 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1620 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1621 * State Cookie are populated as specified in section 5.2.1.
1622 *
1623 * Verification Tag: Not specified, but an INIT has no way of knowing
1624 * what the verification tag could be, so we ignore it.
1625 *
1626 * Inputs
1627 * (endpoint, asoc, chunk)
1628 *
1629 * Outputs
1630 * (asoc, reply_msg, msg_up, timers, counters)
1631 *
1632 * The return value is the disposition of the chunk.
1633 */
1634sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1635 const struct sctp_association *asoc,
1636 const sctp_subtype_t type,
1637 void *arg,
1638 sctp_cmd_seq_t *commands)
1639{
1640 /* Call helper to do the real work for both simulataneous and
1641 * duplicate INIT chunk handling.
1642 */
1643 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1644}
1645
1646
Vlad Yasevich610ab732007-01-15 19:18:30 -08001647/*
1648 * Unexpected INIT-ACK handler.
1649 *
1650 * Section 5.2.3
1651 * If an INIT ACK received by an endpoint in any state other than the
1652 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1653 * An unexpected INIT ACK usually indicates the processing of an old or
1654 * duplicated INIT chunk.
1655*/
1656sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1657 const struct sctp_association *asoc,
1658 const sctp_subtype_t type,
1659 void *arg, sctp_cmd_seq_t *commands)
1660{
1661 /* Per the above section, we'll discard the chunk if we have an
1662 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1663 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001664 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001665 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1666 else
1667 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1671 *
1672 * Section 5.2.4
1673 * A) In this case, the peer may have restarted.
1674 */
1675static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1676 const struct sctp_association *asoc,
1677 struct sctp_chunk *chunk,
1678 sctp_cmd_seq_t *commands,
1679 struct sctp_association *new_asoc)
1680{
1681 sctp_init_chunk_t *peer_init;
1682 struct sctp_ulpevent *ev;
1683 struct sctp_chunk *repl;
1684 struct sctp_chunk *err;
1685 sctp_disposition_t disposition;
1686
1687 /* new_asoc is a brand-new association, so these are not yet
1688 * side effects--it is safe to run them here.
1689 */
1690 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1691
1692 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1693 sctp_source(chunk), peer_init,
1694 GFP_ATOMIC))
1695 goto nomem;
1696
1697 /* Make sure no new addresses are being added during the
1698 * restart. Though this is a pretty complicated attack
1699 * since you'd have to get inside the cookie.
1700 */
1701 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1702 return SCTP_DISPOSITION_CONSUME;
1703 }
1704
1705 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1706 * the peer has restarted (Action A), it MUST NOT setup a new
1707 * association but instead resend the SHUTDOWN ACK and send an ERROR
1708 * chunk with a "Cookie Received while Shutting Down" error cause to
1709 * its peer.
1710 */
1711 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1712 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1713 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1714 chunk, commands);
1715 if (SCTP_DISPOSITION_NOMEM == disposition)
1716 goto nomem;
1717
1718 err = sctp_make_op_error(asoc, chunk,
1719 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1720 NULL, 0);
1721 if (err)
1722 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1723 SCTP_CHUNK(err));
1724
1725 return SCTP_DISPOSITION_CONSUME;
1726 }
1727
1728 /* For now, fail any unsent/unacked data. Consider the optional
1729 * choice of resending of this data.
1730 */
1731 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 repl = sctp_make_cookie_ack(new_asoc, chunk);
1734 if (!repl)
1735 goto nomem;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* Report association restart to upper layer. */
1738 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1739 new_asoc->c.sinit_num_ostreams,
1740 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001741 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (!ev)
1743 goto nomem_ev;
1744
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001745 /* Update the content of current association. */
1746 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1749 return SCTP_DISPOSITION_CONSUME;
1750
1751nomem_ev:
1752 sctp_chunk_free(repl);
1753nomem:
1754 return SCTP_DISPOSITION_NOMEM;
1755}
1756
1757/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1758 *
1759 * Section 5.2.4
1760 * B) In this case, both sides may be attempting to start an association
1761 * at about the same time but the peer endpoint started its INIT
1762 * after responding to the local endpoint's INIT
1763 */
1764/* This case represents an initialization collision. */
1765static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1766 const struct sctp_association *asoc,
1767 struct sctp_chunk *chunk,
1768 sctp_cmd_seq_t *commands,
1769 struct sctp_association *new_asoc)
1770{
1771 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct sctp_chunk *repl;
1773
1774 /* new_asoc is a brand-new association, so these are not yet
1775 * side effects--it is safe to run them here.
1776 */
1777 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1778 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1779 sctp_source(chunk), peer_init,
1780 GFP_ATOMIC))
1781 goto nomem;
1782
1783 /* Update the content of current association. */
1784 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1785 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1786 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1787 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1788 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1789
1790 repl = sctp_make_cookie_ack(new_asoc, chunk);
1791 if (!repl)
1792 goto nomem;
1793
1794 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /* RFC 2960 5.1 Normal Establishment of an Association
1797 *
1798 * D) IMPLEMENTATION NOTE: An implementation may choose to
1799 * send the Communication Up notification to the SCTP user
1800 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001801 *
1802 * Sadly, this needs to be implemented as a side-effect, because
1803 * we are not guaranteed to have set the association id of the real
1804 * association and so these notifications need to be delayed until
1805 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Vlad Yasevich07d93962007-05-04 13:55:27 -07001808 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001811 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001813 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001814 *
1815 * This also needs to be done as a side effect for the same reason as
1816 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001818 if (asoc->peer.adaptation_ind)
1819 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 return SCTP_DISPOSITION_CONSUME;
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823nomem:
1824 return SCTP_DISPOSITION_NOMEM;
1825}
1826
1827/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1828 *
1829 * Section 5.2.4
1830 * C) In this case, the local endpoint's cookie has arrived late.
1831 * Before it arrived, the local endpoint sent an INIT and received an
1832 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1833 * but a new tag of its own.
1834 */
1835/* This case represents an initialization collision. */
1836static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1837 const struct sctp_association *asoc,
1838 struct sctp_chunk *chunk,
1839 sctp_cmd_seq_t *commands,
1840 struct sctp_association *new_asoc)
1841{
1842 /* The cookie should be silently discarded.
1843 * The endpoint SHOULD NOT change states and should leave
1844 * any timers running.
1845 */
1846 return SCTP_DISPOSITION_DISCARD;
1847}
1848
1849/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1850 *
1851 * Section 5.2.4
1852 *
1853 * D) When both local and remote tags match the endpoint should always
1854 * enter the ESTABLISHED state, if it has not already done so.
1855 */
1856/* This case represents an initialization collision. */
1857static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1858 const struct sctp_association *asoc,
1859 struct sctp_chunk *chunk,
1860 sctp_cmd_seq_t *commands,
1861 struct sctp_association *new_asoc)
1862{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001863 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct sctp_chunk *repl;
1865
1866 /* Clarification from Implementor's Guide:
1867 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001868 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1869 * It should stop any cookie timer that may be running and send
1870 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
1872
1873 /* Don't accidentally move back into established state. */
1874 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1875 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1876 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1877 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1878 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1879 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1880 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1881 SCTP_NULL());
1882
1883 /* RFC 2960 5.1 Normal Establishment of an Association
1884 *
1885 * D) IMPLEMENTATION NOTE: An implementation may choose
1886 * to send the Communication Up notification to the
1887 * SCTP user upon reception of a valid COOKIE
1888 * ECHO chunk.
1889 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001890 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001892 asoc->c.sinit_num_ostreams,
1893 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001894 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (!ev)
1896 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001899 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001901 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001903 if (asoc->peer.adaptation_ind) {
1904 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001906 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto nomem;
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
1910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 repl = sctp_make_cookie_ack(new_asoc, chunk);
1913 if (!repl)
1914 goto nomem;
1915
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001916 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1917
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001918 if (ev)
1919 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1920 SCTP_ULPEVENT(ev));
1921 if (ai_ev)
1922 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1923 SCTP_ULPEVENT(ai_ev));
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return SCTP_DISPOSITION_CONSUME;
1926
1927nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001928 if (ai_ev)
1929 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (ev)
1931 sctp_ulpevent_free(ev);
1932 return SCTP_DISPOSITION_NOMEM;
1933}
1934
1935/*
1936 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1937 * chunk was retransmitted and then delayed in the network.
1938 *
1939 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1940 *
1941 * Verification Tag: None. Do cookie validation.
1942 *
1943 * Inputs
1944 * (endpoint, asoc, chunk)
1945 *
1946 * Outputs
1947 * (asoc, reply_msg, msg_up, timers, counters)
1948 *
1949 * The return value is the disposition of the chunk.
1950 */
1951sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1952 const struct sctp_association *asoc,
1953 const sctp_subtype_t type,
1954 void *arg,
1955 sctp_cmd_seq_t *commands)
1956{
1957 sctp_disposition_t retval;
1958 struct sctp_chunk *chunk = arg;
1959 struct sctp_association *new_asoc;
1960 int error = 0;
1961 char action;
1962 struct sctp_chunk *err_chk_p;
1963
1964 /* Make sure that the chunk has a valid length from the protocol
1965 * perspective. In this case check to make sure we have at least
1966 * enough for the chunk header. Cookie length verification is
1967 * done later.
1968 */
1969 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1970 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1971 commands);
1972
1973 /* "Decode" the chunk. We have no optional parameters so we
1974 * are in good shape.
1975 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001976 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001977 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1978 sizeof(sctp_chunkhdr_t)))
1979 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1982 * of a duplicate COOKIE ECHO match the Verification Tags of the
1983 * current association, consider the State Cookie valid even if
1984 * the lifespan is exceeded.
1985 */
1986 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1987 &err_chk_p);
1988
1989 /* FIXME:
1990 * If the re-build failed, what is the proper error path
1991 * from here?
1992 *
1993 * [We should abort the association. --piggy]
1994 */
1995 if (!new_asoc) {
1996 /* FIXME: Several errors are possible. A bad cookie should
1997 * be silently discarded, but think about logging it too.
1998 */
1999 switch (error) {
2000 case -SCTP_IERROR_NOMEM:
2001 goto nomem;
2002
2003 case -SCTP_IERROR_STALE_COOKIE:
2004 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2005 err_chk_p);
2006 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2007 case -SCTP_IERROR_BAD_SIG:
2008 default:
2009 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
2013 /* Compare the tie_tag in cookie with the verification tag of
2014 * current association.
2015 */
2016 action = sctp_tietags_compare(new_asoc, asoc);
2017
2018 switch (action) {
2019 case 'A': /* Association restart. */
2020 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2021 new_asoc);
2022 break;
2023
2024 case 'B': /* Collision case B. */
2025 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2026 new_asoc);
2027 break;
2028
2029 case 'C': /* Collision case C. */
2030 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2031 new_asoc);
2032 break;
2033
2034 case 'D': /* Collision case D. */
2035 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2036 new_asoc);
2037 break;
2038
2039 default: /* Discard packet for all others. */
2040 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2041 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 /* Delete the tempory new association. */
2045 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2046 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2047
2048 return retval;
2049
2050nomem:
2051 return SCTP_DISPOSITION_NOMEM;
2052}
2053
2054/*
2055 * Process an ABORT. (SHUTDOWN-PENDING state)
2056 *
2057 * See sctp_sf_do_9_1_abort().
2058 */
2059sctp_disposition_t sctp_sf_shutdown_pending_abort(
2060 const struct sctp_endpoint *ep,
2061 const struct sctp_association *asoc,
2062 const sctp_subtype_t type,
2063 void *arg,
2064 sctp_cmd_seq_t *commands)
2065{
2066 struct sctp_chunk *chunk = arg;
2067
2068 if (!sctp_vtag_verify_either(chunk, asoc))
2069 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2070
2071 /* Make sure that the ABORT chunk has a valid length.
2072 * Since this is an ABORT chunk, we have to discard it
2073 * because of the following text:
2074 * RFC 2960, Section 3.3.7
2075 * If an endpoint receives an ABORT with a format error or for an
2076 * association that doesn't exist, it MUST silently discard it.
2077 * Becasue the length is "invalid", we can't really discard just
2078 * as we do not know its true length. So, to be safe, discard the
2079 * packet.
2080 */
2081 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2082 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2083
Vlad Yasevich75205f42007-12-20 14:12:59 -08002084 /* ADD-IP: Special case for ABORT chunks
2085 * F4) One special consideration is that ABORT Chunks arriving
2086 * destined to the IP address being deleted MUST be
2087 * ignored (see Section 5.3.1 for further details).
2088 */
2089 if (SCTP_ADDR_DEL ==
2090 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2091 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2092
Vlad Yasevich75205f42007-12-20 14:12:59 -08002093 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
2096/*
2097 * Process an ABORT. (SHUTDOWN-SENT state)
2098 *
2099 * See sctp_sf_do_9_1_abort().
2100 */
2101sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2102 const struct sctp_association *asoc,
2103 const sctp_subtype_t type,
2104 void *arg,
2105 sctp_cmd_seq_t *commands)
2106{
2107 struct sctp_chunk *chunk = arg;
2108
2109 if (!sctp_vtag_verify_either(chunk, asoc))
2110 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2111
2112 /* Make sure that the ABORT chunk has a valid length.
2113 * Since this is an ABORT chunk, we have to discard it
2114 * because of the following text:
2115 * RFC 2960, Section 3.3.7
2116 * If an endpoint receives an ABORT with a format error or for an
2117 * association that doesn't exist, it MUST silently discard it.
2118 * Becasue the length is "invalid", we can't really discard just
2119 * as we do not know its true length. So, to be safe, discard the
2120 * packet.
2121 */
2122 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2123 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2124
Vlad Yasevich75205f42007-12-20 14:12:59 -08002125 /* ADD-IP: Special case for ABORT chunks
2126 * F4) One special consideration is that ABORT Chunks arriving
2127 * destined to the IP address being deleted MUST be
2128 * ignored (see Section 5.3.1 for further details).
2129 */
2130 if (SCTP_ADDR_DEL ==
2131 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2132 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /* Stop the T2-shutdown timer. */
2135 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2136 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2137
2138 /* Stop the T5-shutdown guard timer. */
2139 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2140 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2141
Vlad Yasevich75205f42007-12-20 14:12:59 -08002142 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145/*
2146 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2147 *
2148 * See sctp_sf_do_9_1_abort().
2149 */
2150sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2151 const struct sctp_endpoint *ep,
2152 const struct sctp_association *asoc,
2153 const sctp_subtype_t type,
2154 void *arg,
2155 sctp_cmd_seq_t *commands)
2156{
2157 /* The same T2 timer, so we should be able to use
2158 * common function with the SHUTDOWN-SENT state.
2159 */
2160 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2161}
2162
2163/*
2164 * Handle an Error received in COOKIE_ECHOED state.
2165 *
2166 * Only handle the error type of stale COOKIE Error, the other errors will
2167 * be ignored.
2168 *
2169 * Inputs
2170 * (endpoint, asoc, chunk)
2171 *
2172 * Outputs
2173 * (asoc, reply_msg, msg_up, timers, counters)
2174 *
2175 * The return value is the disposition of the chunk.
2176 */
2177sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2178 const struct sctp_association *asoc,
2179 const sctp_subtype_t type,
2180 void *arg,
2181 sctp_cmd_seq_t *commands)
2182{
2183 struct sctp_chunk *chunk = arg;
2184 sctp_errhdr_t *err;
2185
2186 if (!sctp_vtag_verify(chunk, asoc))
2187 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2188
2189 /* Make sure that the ERROR chunk has a valid length.
2190 * The parameter walking depends on this as well.
2191 */
2192 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2193 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2194 commands);
2195
2196 /* Process the error here */
2197 /* FUTURE FIXME: When PR-SCTP related and other optional
2198 * parms are emitted, this will have to change to handle multiple
2199 * errors.
2200 */
2201 sctp_walk_errors(err, chunk->chunk_hdr) {
2202 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002203 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 arg, commands);
2205 }
2206
2207 /* It is possible to have malformed error causes, and that
2208 * will cause us to end the walk early. However, since
2209 * we are discarding the packet, there should be no adverse
2210 * affects.
2211 */
2212 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2213}
2214
2215/*
2216 * Handle a Stale COOKIE Error
2217 *
2218 * Section: 5.2.6 Handle Stale COOKIE Error
2219 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2220 * one of the following three alternatives.
2221 * ...
2222 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2223 * Preservative parameter requesting an extension to the lifetime of
2224 * the State Cookie. When calculating the time extension, an
2225 * implementation SHOULD use the RTT information measured based on the
2226 * previous COOKIE ECHO / ERROR exchange, and should add no more
2227 * than 1 second beyond the measured RTT, due to long State Cookie
2228 * lifetimes making the endpoint more subject to a replay attack.
2229 *
2230 * Verification Tag: Not explicit, but safe to ignore.
2231 *
2232 * Inputs
2233 * (endpoint, asoc, chunk)
2234 *
2235 * Outputs
2236 * (asoc, reply_msg, msg_up, timers, counters)
2237 *
2238 * The return value is the disposition of the chunk.
2239 */
2240static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2241 const struct sctp_association *asoc,
2242 const sctp_subtype_t type,
2243 void *arg,
2244 sctp_cmd_seq_t *commands)
2245{
2246 struct sctp_chunk *chunk = arg;
2247 time_t stale;
2248 sctp_cookie_preserve_param_t bht;
2249 sctp_errhdr_t *err;
2250 struct sctp_chunk *reply;
2251 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002252 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Vlad Yasevich81845c22006-01-30 15:59:54 -08002254 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002255 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2256 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002258 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return SCTP_DISPOSITION_DELETE_TCB;
2260 }
2261
2262 err = (sctp_errhdr_t *)(chunk->skb->data);
2263
2264 /* When calculating the time extension, an implementation
2265 * SHOULD use the RTT information measured based on the
2266 * previous COOKIE ECHO / ERROR exchange, and should add no
2267 * more than 1 second beyond the measured RTT, due to long
2268 * State Cookie lifetimes making the endpoint more subject to
2269 * a replay attack.
2270 * Measure of Staleness's unit is usec. (1/1000000 sec)
2271 * Suggested Cookie Life-span Increment's unit is msec.
2272 * (1/1000 sec)
2273 * In general, if you use the suggested cookie life, the value
2274 * found in the field of measure of staleness should be doubled
2275 * to give ample time to retransmit the new cookie and thus
2276 * yield a higher probability of success on the reattempt.
2277 */
Al Viro34bcca22006-11-20 17:27:15 -08002278 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 stale = (stale * 2) / 1000;
2280
2281 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2282 bht.param_hdr.length = htons(sizeof(bht));
2283 bht.lifespan_increment = htonl(stale);
2284
2285 /* Build that new INIT chunk. */
2286 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2287 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2288 if (!reply)
2289 goto nomem;
2290
2291 sctp_addto_chunk(reply, sizeof(bht), &bht);
2292
2293 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2294 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2295
2296 /* Stop pending T3-rtx and heartbeat timers */
2297 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2298 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2299
2300 /* Delete non-primary peer ip addresses since we are transitioning
2301 * back to the COOKIE-WAIT state
2302 */
2303 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2304
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002305 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2306 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002308 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 SCTP_TRANSPORT(asoc->peer.primary_path));
2310
2311 /* Cast away the const modifier, as we want to just
2312 * rerun it through as a sideffect.
2313 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002314 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2317 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2318 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2319 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2320 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2321 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2322
2323 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2324
2325 return SCTP_DISPOSITION_CONSUME;
2326
2327nomem:
2328 return SCTP_DISPOSITION_NOMEM;
2329}
2330
2331/*
2332 * Process an ABORT.
2333 *
2334 * Section: 9.1
2335 * After checking the Verification Tag, the receiving endpoint shall
2336 * remove the association from its record, and shall report the
2337 * termination to its upper layer.
2338 *
2339 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2340 * B) Rules for packet carrying ABORT:
2341 *
2342 * - The endpoint shall always fill in the Verification Tag field of the
2343 * outbound packet with the destination endpoint's tag value if it
2344 * is known.
2345 *
2346 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2347 * MUST follow the procedure described in Section 8.4.
2348 *
2349 * - The receiver MUST accept the packet if the Verification Tag
2350 * matches either its own tag, OR the tag of its peer. Otherwise, the
2351 * receiver MUST silently discard the packet and take no further
2352 * action.
2353 *
2354 * Inputs
2355 * (endpoint, asoc, chunk)
2356 *
2357 * Outputs
2358 * (asoc, reply_msg, msg_up, timers, counters)
2359 *
2360 * The return value is the disposition of the chunk.
2361 */
2362sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2363 const struct sctp_association *asoc,
2364 const sctp_subtype_t type,
2365 void *arg,
2366 sctp_cmd_seq_t *commands)
2367{
2368 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 if (!sctp_vtag_verify_either(chunk, asoc))
2371 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2372
2373 /* Make sure that the ABORT chunk has a valid length.
2374 * Since this is an ABORT chunk, we have to discard it
2375 * because of the following text:
2376 * RFC 2960, Section 3.3.7
2377 * If an endpoint receives an ABORT with a format error or for an
2378 * association that doesn't exist, it MUST silently discard it.
2379 * Becasue the length is "invalid", we can't really discard just
2380 * as we do not know its true length. So, to be safe, discard the
2381 * packet.
2382 */
2383 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2384 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2385
Vlad Yasevich75205f42007-12-20 14:12:59 -08002386 /* ADD-IP: Special case for ABORT chunks
2387 * F4) One special consideration is that ABORT Chunks arriving
2388 * destined to the IP address being deleted MUST be
2389 * ignored (see Section 5.3.1 for further details).
2390 */
2391 if (SCTP_ADDR_DEL ==
2392 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2393 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2394
2395 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2396}
2397
2398static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2399 const struct sctp_association *asoc,
2400 const sctp_subtype_t type,
2401 void *arg,
2402 sctp_cmd_seq_t *commands)
2403{
2404 struct sctp_chunk *chunk = arg;
2405 unsigned len;
2406 __be16 error = SCTP_ERROR_NO_ERROR;
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* See if we have an error cause code in the chunk. */
2409 len = ntohs(chunk->chunk_hdr->length);
2410 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2411 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2412
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002413 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002414 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002415 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2417 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2418
2419 return SCTP_DISPOSITION_ABORT;
2420}
2421
2422/*
2423 * Process an ABORT. (COOKIE-WAIT state)
2424 *
2425 * See sctp_sf_do_9_1_abort() above.
2426 */
2427sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2428 const struct sctp_association *asoc,
2429 const sctp_subtype_t type,
2430 void *arg,
2431 sctp_cmd_seq_t *commands)
2432{
2433 struct sctp_chunk *chunk = arg;
2434 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002435 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 if (!sctp_vtag_verify_either(chunk, asoc))
2438 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2439
2440 /* Make sure that the ABORT chunk has a valid length.
2441 * Since this is an ABORT chunk, we have to discard it
2442 * because of the following text:
2443 * RFC 2960, Section 3.3.7
2444 * If an endpoint receives an ABORT with a format error or for an
2445 * association that doesn't exist, it MUST silently discard it.
2446 * Becasue the length is "invalid", we can't really discard just
2447 * as we do not know its true length. So, to be safe, discard the
2448 * packet.
2449 */
2450 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2451 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2452
2453 /* See if we have an error cause code in the chunk. */
2454 len = ntohs(chunk->chunk_hdr->length);
2455 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2456 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2457
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002458 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2459 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
2462/*
2463 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2464 */
2465sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2466 const struct sctp_association *asoc,
2467 const sctp_subtype_t type,
2468 void *arg,
2469 sctp_cmd_seq_t *commands)
2470{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002471 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2472 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002473 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
2476/*
2477 * Process an ABORT. (COOKIE-ECHOED state)
2478 */
2479sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2480 const struct sctp_association *asoc,
2481 const sctp_subtype_t type,
2482 void *arg,
2483 sctp_cmd_seq_t *commands)
2484{
2485 /* There is a single T1 timer, so we should be able to use
2486 * common function with the COOKIE-WAIT state.
2487 */
2488 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2489}
2490
2491/*
2492 * Stop T1 timer and abort association with "INIT failed".
2493 *
2494 * This is common code called by several sctp_sf_*_abort() functions above.
2495 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002496static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002497 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002498 const struct sctp_association *asoc,
2499 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002501 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2503 SCTP_STATE(SCTP_STATE_CLOSED));
2504 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2505 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2506 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002507 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 /* CMD_INIT_FAILED will DELETE_TCB. */
2509 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002510 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002511 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514/*
2515 * sctp_sf_do_9_2_shut
2516 *
2517 * Section: 9.2
2518 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2519 * - enter the SHUTDOWN-RECEIVED state,
2520 *
2521 * - stop accepting new data from its SCTP user
2522 *
2523 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2524 * that all its outstanding DATA chunks have been received by the
2525 * SHUTDOWN sender.
2526 *
2527 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2528 * send a SHUTDOWN in response to a ULP request. And should discard
2529 * subsequent SHUTDOWN chunks.
2530 *
2531 * If there are still outstanding DATA chunks left, the SHUTDOWN
2532 * receiver shall continue to follow normal data transmission
2533 * procedures defined in Section 6 until all outstanding DATA chunks
2534 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2535 * new data from its SCTP user.
2536 *
2537 * Verification Tag: 8.5 Verification Tag [Normal verification]
2538 *
2539 * Inputs
2540 * (endpoint, asoc, chunk)
2541 *
2542 * Outputs
2543 * (asoc, reply_msg, msg_up, timers, counters)
2544 *
2545 * The return value is the disposition of the chunk.
2546 */
2547sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2548 const struct sctp_association *asoc,
2549 const sctp_subtype_t type,
2550 void *arg,
2551 sctp_cmd_seq_t *commands)
2552{
2553 struct sctp_chunk *chunk = arg;
2554 sctp_shutdownhdr_t *sdh;
2555 sctp_disposition_t disposition;
2556 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002557 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
2559 if (!sctp_vtag_verify(chunk, asoc))
2560 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2561
2562 /* Make sure that the SHUTDOWN chunk has a valid length. */
2563 if (!sctp_chunk_length_valid(chunk,
2564 sizeof(struct sctp_shutdown_chunk_t)))
2565 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2566 commands);
2567
2568 /* Convert the elaborate header. */
2569 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2570 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2571 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002572 ctsn = ntohl(sdh->cum_tsn_ack);
2573
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002574 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2575 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2576 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2577 return SCTP_DISPOSITION_DISCARD;
2578 }
2579
Wei Yongjundf10eec2008-10-23 01:00:21 -07002580 /* If Cumulative TSN Ack beyond the max tsn currently
2581 * send, terminating the association and respond to the
2582 * sender with an ABORT.
2583 */
2584 if (!TSN_lt(ctsn, asoc->next_tsn))
2585 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002587 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2588 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2589 * inform the application that it should cease sending data.
2590 */
2591 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2592 if (!ev) {
2593 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002594 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002595 }
2596 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2599 * - enter the SHUTDOWN-RECEIVED state,
2600 * - stop accepting new data from its SCTP user
2601 *
2602 * [This is implicit in the new state.]
2603 */
2604 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2605 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2606 disposition = SCTP_DISPOSITION_CONSUME;
2607
2608 if (sctp_outq_is_empty(&asoc->outqueue)) {
2609 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2610 arg, commands);
2611 }
2612
2613 if (SCTP_DISPOSITION_NOMEM == disposition)
2614 goto out;
2615
2616 /* - verify, by checking the Cumulative TSN Ack field of the
2617 * chunk, that all its outstanding DATA chunks have been
2618 * received by the SHUTDOWN sender.
2619 */
2620 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002621 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623out:
2624 return disposition;
2625}
2626
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002627/*
2628 * sctp_sf_do_9_2_shut_ctsn
2629 *
2630 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2631 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2632 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2633 * MUST be processed.
2634 */
2635sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2636 const struct sctp_association *asoc,
2637 const sctp_subtype_t type,
2638 void *arg,
2639 sctp_cmd_seq_t *commands)
2640{
2641 struct sctp_chunk *chunk = arg;
2642 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002643 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002644
2645 if (!sctp_vtag_verify(chunk, asoc))
2646 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2647
2648 /* Make sure that the SHUTDOWN chunk has a valid length. */
2649 if (!sctp_chunk_length_valid(chunk,
2650 sizeof(struct sctp_shutdown_chunk_t)))
2651 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2652 commands);
2653
2654 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002655 ctsn = ntohl(sdh->cum_tsn_ack);
2656
2657 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2658 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2659 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2660 return SCTP_DISPOSITION_DISCARD;
2661 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002662
2663 /* If Cumulative TSN Ack beyond the max tsn currently
2664 * send, terminating the association and respond to the
2665 * sender with an ABORT.
2666 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002667 if (!TSN_lt(ctsn, asoc->next_tsn))
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002668 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2669
2670 /* verify, by checking the Cumulative TSN Ack field of the
2671 * chunk, that all its outstanding DATA chunks have been
2672 * received by the SHUTDOWN sender.
2673 */
2674 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2675 SCTP_BE32(sdh->cum_tsn_ack));
2676
2677 return SCTP_DISPOSITION_CONSUME;
2678}
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/* RFC 2960 9.2
2681 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2682 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2683 * transport addresses (either in the IP addresses or in the INIT chunk)
2684 * that belong to this association, it should discard the INIT chunk and
2685 * retransmit the SHUTDOWN ACK chunk.
2686 */
2687sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2688 const struct sctp_association *asoc,
2689 const sctp_subtype_t type,
2690 void *arg,
2691 sctp_cmd_seq_t *commands)
2692{
2693 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2694 struct sctp_chunk *reply;
2695
Vlad Yasevichece25df2007-09-07 16:30:54 -04002696 /* Make sure that the chunk has a valid length */
2697 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2698 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2699 commands);
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /* Since we are not going to really process this INIT, there
2702 * is no point in verifying chunk boundries. Just generate
2703 * the SHUTDOWN ACK.
2704 */
2705 reply = sctp_make_shutdown_ack(asoc, chunk);
2706 if (NULL == reply)
2707 goto nomem;
2708
2709 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2710 * the T2-SHUTDOWN timer.
2711 */
2712 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2713
2714 /* and restart the T2-shutdown timer. */
2715 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2716 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2717
2718 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2719
2720 return SCTP_DISPOSITION_CONSUME;
2721nomem:
2722 return SCTP_DISPOSITION_NOMEM;
2723}
2724
2725/*
2726 * sctp_sf_do_ecn_cwr
2727 *
2728 * Section: Appendix A: Explicit Congestion Notification
2729 *
2730 * CWR:
2731 *
2732 * RFC 2481 details a specific bit for a sender to send in the header of
2733 * its next outbound TCP segment to indicate to its peer that it has
2734 * reduced its congestion window. This is termed the CWR bit. For
2735 * SCTP the same indication is made by including the CWR chunk.
2736 * This chunk contains one data element, i.e. the TSN number that
2737 * was sent in the ECNE chunk. This element represents the lowest
2738 * TSN number in the datagram that was originally marked with the
2739 * CE bit.
2740 *
2741 * Verification Tag: 8.5 Verification Tag [Normal verification]
2742 * Inputs
2743 * (endpoint, asoc, chunk)
2744 *
2745 * Outputs
2746 * (asoc, reply_msg, msg_up, timers, counters)
2747 *
2748 * The return value is the disposition of the chunk.
2749 */
2750sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2751 const struct sctp_association *asoc,
2752 const sctp_subtype_t type,
2753 void *arg,
2754 sctp_cmd_seq_t *commands)
2755{
2756 sctp_cwrhdr_t *cwr;
2757 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002758 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 if (!sctp_vtag_verify(chunk, asoc))
2761 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2762
2763 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2764 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2765 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2768 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2769
Al Viro34bcca22006-11-20 17:27:15 -08002770 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002773 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 /* Stop sending ECNE. */
2775 sctp_add_cmd_sf(commands,
2776 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002777 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
2779 return SCTP_DISPOSITION_CONSUME;
2780}
2781
2782/*
2783 * sctp_sf_do_ecne
2784 *
2785 * Section: Appendix A: Explicit Congestion Notification
2786 *
2787 * ECN-Echo
2788 *
2789 * RFC 2481 details a specific bit for a receiver to send back in its
2790 * TCP acknowledgements to notify the sender of the Congestion
2791 * Experienced (CE) bit having arrived from the network. For SCTP this
2792 * same indication is made by including the ECNE chunk. This chunk
2793 * contains one data element, i.e. the lowest TSN associated with the IP
2794 * datagram marked with the CE bit.....
2795 *
2796 * Verification Tag: 8.5 Verification Tag [Normal verification]
2797 * Inputs
2798 * (endpoint, asoc, chunk)
2799 *
2800 * Outputs
2801 * (asoc, reply_msg, msg_up, timers, counters)
2802 *
2803 * The return value is the disposition of the chunk.
2804 */
2805sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2806 const struct sctp_association *asoc,
2807 const sctp_subtype_t type,
2808 void *arg,
2809 sctp_cmd_seq_t *commands)
2810{
2811 sctp_ecnehdr_t *ecne;
2812 struct sctp_chunk *chunk = arg;
2813
2814 if (!sctp_vtag_verify(chunk, asoc))
2815 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2816
2817 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2818 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2819 commands);
2820
2821 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2822 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2823
2824 /* If this is a newer ECNE than the last CWR packet we sent out */
2825 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2826 SCTP_U32(ntohl(ecne->lowest_tsn)));
2827
2828 return SCTP_DISPOSITION_CONSUME;
2829}
2830
2831/*
2832 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2833 *
2834 * The SCTP endpoint MUST always acknowledge the reception of each valid
2835 * DATA chunk.
2836 *
2837 * The guidelines on delayed acknowledgement algorithm specified in
2838 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2839 * acknowledgement SHOULD be generated for at least every second packet
2840 * (not every second DATA chunk) received, and SHOULD be generated within
2841 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2842 * situations it may be beneficial for an SCTP transmitter to be more
2843 * conservative than the algorithms detailed in this document allow.
2844 * However, an SCTP transmitter MUST NOT be more aggressive than the
2845 * following algorithms allow.
2846 *
2847 * A SCTP receiver MUST NOT generate more than one SACK for every
2848 * incoming packet, other than to update the offered window as the
2849 * receiving application consumes new data.
2850 *
2851 * Verification Tag: 8.5 Verification Tag [Normal verification]
2852 *
2853 * Inputs
2854 * (endpoint, asoc, chunk)
2855 *
2856 * Outputs
2857 * (asoc, reply_msg, msg_up, timers, counters)
2858 *
2859 * The return value is the disposition of the chunk.
2860 */
2861sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2862 const struct sctp_association *asoc,
2863 const sctp_subtype_t type,
2864 void *arg,
2865 sctp_cmd_seq_t *commands)
2866{
2867 struct sctp_chunk *chunk = arg;
2868 int error;
2869
2870 if (!sctp_vtag_verify(chunk, asoc)) {
2871 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2872 SCTP_NULL());
2873 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2877 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2878 commands);
2879
2880 error = sctp_eat_data(asoc, chunk, commands );
2881 switch (error) {
2882 case SCTP_IERROR_NO_ERROR:
2883 break;
2884 case SCTP_IERROR_HIGH_TSN:
2885 case SCTP_IERROR_BAD_STREAM:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002886 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 goto discard_noforce;
2888 case SCTP_IERROR_DUP_TSN:
2889 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002890 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 goto discard_force;
2892 case SCTP_IERROR_NO_DATA:
2893 goto consume;
2894 default:
2895 BUG();
2896 }
2897
2898 if (asoc->autoclose) {
2899 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2900 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2901 }
2902
2903 /* If this is the last chunk in a packet, we need to count it
2904 * toward sack generation. Note that we need to SACK every
2905 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2906 * THEM. We elect to NOT generate SACK's if the chunk fails
2907 * the verification tag test.
2908 *
2909 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2910 *
2911 * The SCTP endpoint MUST always acknowledge the reception of
2912 * each valid DATA chunk.
2913 *
2914 * The guidelines on delayed acknowledgement algorithm
2915 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2916 * Specifically, an acknowledgement SHOULD be generated for at
2917 * least every second packet (not every second DATA chunk)
2918 * received, and SHOULD be generated within 200 ms of the
2919 * arrival of any unacknowledged DATA chunk. In some
2920 * situations it may be beneficial for an SCTP transmitter to
2921 * be more conservative than the algorithms detailed in this
2922 * document allow. However, an SCTP transmitter MUST NOT be
2923 * more aggressive than the following algorithms allow.
2924 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002925 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return SCTP_DISPOSITION_CONSUME;
2929
2930discard_force:
2931 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2932 *
2933 * When a packet arrives with duplicate DATA chunk(s) and with
2934 * no new DATA chunk(s), the endpoint MUST immediately send a
2935 * SACK with no delay. If a packet arrives with duplicate
2936 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2937 * MAY immediately send a SACK. Normally receipt of duplicate
2938 * DATA chunks will occur when the original SACK chunk was lost
2939 * and the peer's RTO has expired. The duplicate TSN number(s)
2940 * SHOULD be reported in the SACK as duplicate.
2941 */
2942 /* In our case, we split the MAY SACK advice up whether or not
2943 * the last chunk is a duplicate.'
2944 */
2945 if (chunk->end_of_packet)
2946 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2947 return SCTP_DISPOSITION_DISCARD;
2948
2949discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002950 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return SCTP_DISPOSITION_DISCARD;
2954consume:
2955 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002956
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
2959/*
2960 * sctp_sf_eat_data_fast_4_4
2961 *
2962 * Section: 4 (4)
2963 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2964 * DATA chunks without delay.
2965 *
2966 * Verification Tag: 8.5 Verification Tag [Normal verification]
2967 * Inputs
2968 * (endpoint, asoc, chunk)
2969 *
2970 * Outputs
2971 * (asoc, reply_msg, msg_up, timers, counters)
2972 *
2973 * The return value is the disposition of the chunk.
2974 */
2975sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2976 const struct sctp_association *asoc,
2977 const sctp_subtype_t type,
2978 void *arg,
2979 sctp_cmd_seq_t *commands)
2980{
2981 struct sctp_chunk *chunk = arg;
2982 int error;
2983
2984 if (!sctp_vtag_verify(chunk, asoc)) {
2985 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2986 SCTP_NULL());
2987 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2988 }
2989
2990 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2991 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2992 commands);
2993
2994 error = sctp_eat_data(asoc, chunk, commands );
2995 switch (error) {
2996 case SCTP_IERROR_NO_ERROR:
2997 case SCTP_IERROR_HIGH_TSN:
2998 case SCTP_IERROR_DUP_TSN:
2999 case SCTP_IERROR_IGNORE_TSN:
3000 case SCTP_IERROR_BAD_STREAM:
3001 break;
3002 case SCTP_IERROR_NO_DATA:
3003 goto consume;
3004 default:
3005 BUG();
3006 }
3007
3008 /* Go a head and force a SACK, since we are shutting down. */
3009
3010 /* Implementor's Guide.
3011 *
3012 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3013 * respond to each received packet containing one or more DATA chunk(s)
3014 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3015 */
3016 if (chunk->end_of_packet) {
3017 /* We must delay the chunk creation since the cumulative
3018 * TSN has not been updated yet.
3019 */
3020 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3021 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3022 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3023 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3024 }
3025
3026consume:
3027 return SCTP_DISPOSITION_CONSUME;
3028}
3029
3030/*
3031 * Section: 6.2 Processing a Received SACK
3032 * D) Any time a SACK arrives, the endpoint performs the following:
3033 *
3034 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3035 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3036 * increasing, a SACK whose Cumulative TSN Ack is less than the
3037 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3038 *
3039 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3040 * of bytes still outstanding after processing the Cumulative TSN Ack
3041 * and the Gap Ack Blocks.
3042 *
3043 * iii) If the SACK is missing a TSN that was previously
3044 * acknowledged via a Gap Ack Block (e.g., the data receiver
3045 * reneged on the data), then mark the corresponding DATA chunk
3046 * as available for retransmit: Mark it as missing for fast
3047 * retransmit as described in Section 7.2.4 and if no retransmit
3048 * timer is running for the destination address to which the DATA
3049 * chunk was originally transmitted, then T3-rtx is started for
3050 * that destination address.
3051 *
3052 * Verification Tag: 8.5 Verification Tag [Normal verification]
3053 *
3054 * Inputs
3055 * (endpoint, asoc, chunk)
3056 *
3057 * Outputs
3058 * (asoc, reply_msg, msg_up, timers, counters)
3059 *
3060 * The return value is the disposition of the chunk.
3061 */
3062sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3063 const struct sctp_association *asoc,
3064 const sctp_subtype_t type,
3065 void *arg,
3066 sctp_cmd_seq_t *commands)
3067{
3068 struct sctp_chunk *chunk = arg;
3069 sctp_sackhdr_t *sackh;
3070 __u32 ctsn;
3071
3072 if (!sctp_vtag_verify(chunk, asoc))
3073 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3074
3075 /* Make sure that the SACK chunk has a valid length. */
3076 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3077 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3078 commands);
3079
3080 /* Pull the SACK chunk from the data buffer */
3081 sackh = sctp_sm_pull_sack(chunk);
3082 /* Was this a bogus SACK? */
3083 if (!sackh)
3084 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3085 chunk->subh.sack_hdr = sackh;
3086 ctsn = ntohl(sackh->cum_tsn_ack);
3087
3088 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3089 * Ack Point, then drop the SACK. Since Cumulative TSN
3090 * Ack is monotonically increasing, a SACK whose
3091 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3092 * Point indicates an out-of-order SACK.
3093 */
3094 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3095 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3096 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3097 return SCTP_DISPOSITION_DISCARD;
3098 }
3099
Wei Yongjunaecedea2007-08-02 16:57:44 +08003100 /* If Cumulative TSN Ack beyond the max tsn currently
3101 * send, terminating the association and respond to the
3102 * sender with an ABORT.
3103 */
3104 if (!TSN_lt(ctsn, asoc->next_tsn))
3105 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /* Return this SACK for further processing. */
3108 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3109
3110 /* Note: We do the rest of the work on the PROCESS_SACK
3111 * sideeffect.
3112 */
3113 return SCTP_DISPOSITION_CONSUME;
3114}
3115
3116/*
3117 * Generate an ABORT in response to a packet.
3118 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003119 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003121 * 8) The receiver should respond to the sender of the OOTB packet with
3122 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3123 * MUST fill in the Verification Tag field of the outbound packet
3124 * with the value found in the Verification Tag field of the OOTB
3125 * packet and set the T-bit in the Chunk Flags to indicate that the
3126 * Verification Tag is reflected. After sending this ABORT, the
3127 * receiver of the OOTB packet shall discard the OOTB packet and take
3128 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 *
3130 * Verification Tag:
3131 *
3132 * The return value is the disposition of the chunk.
3133*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003134static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 const struct sctp_association *asoc,
3136 const sctp_subtype_t type,
3137 void *arg,
3138 sctp_cmd_seq_t *commands)
3139{
3140 struct sctp_packet *packet = NULL;
3141 struct sctp_chunk *chunk = arg;
3142 struct sctp_chunk *abort;
3143
3144 packet = sctp_ootb_pkt_new(asoc, chunk);
3145
3146 if (packet) {
3147 /* Make an ABORT. The T bit will be set if the asoc
3148 * is NULL.
3149 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003150 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (!abort) {
3152 sctp_ootb_pkt_free(packet);
3153 return SCTP_DISPOSITION_NOMEM;
3154 }
3155
Jerome Forissier047a2422005-04-28 11:58:43 -07003156 /* Reflect vtag if T-Bit is set */
3157 if (sctp_test_T_bit(abort))
3158 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /* Set the skb to the belonging sock for accounting. */
3161 abort->skb->sk = ep->base.sk;
3162
3163 sctp_packet_append_chunk(packet, abort);
3164
3165 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3166 SCTP_PACKET(packet));
3167
3168 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3169
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003170 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 return SCTP_DISPOSITION_CONSUME;
3172 }
3173
3174 return SCTP_DISPOSITION_NOMEM;
3175}
3176
3177/*
3178 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3179 * event as ULP notification for each cause included in the chunk.
3180 *
3181 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3182 *
3183 * The return value is the disposition of the chunk.
3184*/
3185sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3186 const struct sctp_association *asoc,
3187 const sctp_subtype_t type,
3188 void *arg,
3189 sctp_cmd_seq_t *commands)
3190{
3191 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 if (!sctp_vtag_verify(chunk, asoc))
3194 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3195
3196 /* Make sure that the ERROR chunk has a valid length. */
3197 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3198 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3199 commands);
3200
Wei Yongjun3df26782009-03-02 06:46:51 +00003201 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3202 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207/*
3208 * Process an inbound SHUTDOWN ACK.
3209 *
3210 * From Section 9.2:
3211 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3212 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3213 * peer, and remove all record of the association.
3214 *
3215 * The return value is the disposition.
3216 */
3217sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3218 const struct sctp_association *asoc,
3219 const sctp_subtype_t type,
3220 void *arg,
3221 sctp_cmd_seq_t *commands)
3222{
3223 struct sctp_chunk *chunk = arg;
3224 struct sctp_chunk *reply;
3225 struct sctp_ulpevent *ev;
3226
3227 if (!sctp_vtag_verify(chunk, asoc))
3228 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3229
3230 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3231 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3232 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3233 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 /* 10.2 H) SHUTDOWN COMPLETE notification
3235 *
3236 * When SCTP completes the shutdown procedures (section 9.2) this
3237 * notification is passed to the upper layer.
3238 */
3239 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003240 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 if (!ev)
3242 goto nomem;
3243
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003244 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3245 reply = sctp_make_shutdown_complete(asoc, chunk);
3246 if (!reply)
3247 goto nomem_chunk;
3248
3249 /* Do all the commands now (after allocation), so that we
3250 * have consistent state if memory allocation failes
3251 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3253
3254 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3255 * stop the T2-shutdown timer,
3256 */
3257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3258 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3259
3260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3261 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3262
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3264 SCTP_STATE(SCTP_STATE_CLOSED));
3265 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3266 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3267 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3268
3269 /* ...and remove all record of the association. */
3270 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3271 return SCTP_DISPOSITION_DELETE_TCB;
3272
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003273nomem_chunk:
3274 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275nomem:
3276 return SCTP_DISPOSITION_NOMEM;
3277}
3278
3279/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003280 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3283 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3284 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3285 * packet must fill in the Verification Tag field of the outbound
3286 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003287 * set the T-bit in the Chunk Flags to indicate that the Verification
3288 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 *
3290 * 8) The receiver should respond to the sender of the OOTB packet with
3291 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3292 * MUST fill in the Verification Tag field of the outbound packet
3293 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003294 * packet and set the T-bit in the Chunk Flags to indicate that the
3295 * Verification Tag is reflected. After sending this ABORT, the
3296 * receiver of the OOTB packet shall discard the OOTB packet and take
3297 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 */
3299sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3300 const struct sctp_association *asoc,
3301 const sctp_subtype_t type,
3302 void *arg,
3303 sctp_cmd_seq_t *commands)
3304{
3305 struct sctp_chunk *chunk = arg;
3306 struct sk_buff *skb = chunk->skb;
3307 sctp_chunkhdr_t *ch;
3308 __u8 *ch_end;
3309 int ootb_shut_ack = 0;
3310
3311 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3312
3313 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3314 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003315 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003317 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3318 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Vlad Yasevichece25df2007-09-07 16:30:54 -04003320 /* Now that we know we at least have a chunk header,
3321 * do things that are type appropriate.
3322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3324 ootb_shut_ack = 1;
3325
3326 /* RFC 2960, Section 3.3.7
3327 * Moreover, under any circumstances, an endpoint that
3328 * receives an ABORT MUST NOT respond to that ABORT by
3329 * sending an ABORT of its own.
3330 */
3331 if (SCTP_CID_ABORT == ch->type)
3332 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003333
Vlad Yasevichece25df2007-09-07 16:30:54 -04003334 /* Report violation if chunk len overflows */
3335 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3336 if (ch_end > skb_tail_pointer(skb))
3337 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3338 commands);
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003341 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003344 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003346 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
3349/*
3350 * Handle an "Out of the blue" SHUTDOWN ACK.
3351 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003352 * Section: 8.4 5, sctpimpguide 2.41.
3353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003355 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3356 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3357 * packet must fill in the Verification Tag field of the outbound
3358 * packet with the Verification Tag received in the SHUTDOWN ACK and
3359 * set the T-bit in the Chunk Flags to indicate that the Verification
3360 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 *
3362 * Inputs
3363 * (endpoint, asoc, type, arg, commands)
3364 *
3365 * Outputs
3366 * (sctp_disposition_t)
3367 *
3368 * The return value is the disposition of the chunk.
3369 */
3370static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3371 const struct sctp_association *asoc,
3372 const sctp_subtype_t type,
3373 void *arg,
3374 sctp_cmd_seq_t *commands)
3375{
3376 struct sctp_packet *packet = NULL;
3377 struct sctp_chunk *chunk = arg;
3378 struct sctp_chunk *shut;
3379
3380 packet = sctp_ootb_pkt_new(asoc, chunk);
3381
3382 if (packet) {
3383 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003384 * The T bit will be set if the asoc is NULL.
3385 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 shut = sctp_make_shutdown_complete(asoc, chunk);
3387 if (!shut) {
3388 sctp_ootb_pkt_free(packet);
3389 return SCTP_DISPOSITION_NOMEM;
3390 }
3391
Jerome Forissier047a2422005-04-28 11:58:43 -07003392 /* Reflect vtag if T-Bit is set */
3393 if (sctp_test_T_bit(shut))
3394 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 /* Set the skb to the belonging sock for accounting. */
3397 shut->skb->sk = ep->base.sk;
3398
3399 sctp_packet_append_chunk(packet, shut);
3400
3401 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3402 SCTP_PACKET(packet));
3403
3404 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3405
3406 /* If the chunk length is invalid, we don't want to process
3407 * the reset of the packet.
3408 */
3409 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3410 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3411
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003412 /* We need to discard the rest of the packet to prevent
3413 * potential bomming attacks from additional bundled chunks.
3414 * This is documented in SCTP Threats ID.
3415 */
3416 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
3418
3419 return SCTP_DISPOSITION_NOMEM;
3420}
3421
3422/*
3423 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3424 *
3425 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3426 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3427 * procedures in section 8.4 SHOULD be followed, in other words it
3428 * should be treated as an Out Of The Blue packet.
3429 * [This means that we do NOT check the Verification Tag on these
3430 * chunks. --piggy ]
3431 *
3432 */
3433sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3434 const struct sctp_association *asoc,
3435 const sctp_subtype_t type,
3436 void *arg,
3437 sctp_cmd_seq_t *commands)
3438{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003439 struct sctp_chunk *chunk = arg;
3440
3441 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3442 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3443 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3444 commands);
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /* Although we do have an association in this case, it corresponds
3447 * to a restarted association. So the packet is treated as an OOTB
3448 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3449 * called with a NULL association.
3450 */
Wei Yongjun8190f892008-09-08 12:13:55 +08003451 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3454}
3455
3456/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3457sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3458 const struct sctp_association *asoc,
3459 const sctp_subtype_t type, void *arg,
3460 sctp_cmd_seq_t *commands)
3461{
3462 struct sctp_chunk *chunk = arg;
3463 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003464 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003466 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003468 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
3470 if (!sctp_vtag_verify(chunk, asoc)) {
3471 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3472 SCTP_NULL());
3473 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3474 }
3475
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003476 /* ADD-IP: Section 4.1.1
3477 * This chunk MUST be sent in an authenticated way by using
3478 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3479 * is received unauthenticated it MUST be silently discarded as
3480 * described in [I-D.ietf-tsvwg-sctp-auth].
3481 */
3482 if (!sctp_addip_noauth && !chunk->auth)
3483 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3486 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3487 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3488 commands);
3489
3490 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3491 serial = ntohl(hdr->serial);
3492
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003493 addr_param = (union sctp_addr_param *)hdr->params;
3494 length = ntohs(addr_param->p.length);
3495 if (length < sizeof(sctp_paramhdr_t))
Wei Yongjunba016672008-09-30 05:32:24 -07003496 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003497 (void *)addr_param, commands);
3498
3499 /* Verify the ASCONF chunk before processing it. */
3500 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003501 (sctp_paramhdr_t *)((void *)addr_param + length),
3502 (void *)chunk->chunk_end,
3503 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003504 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3505 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003506
Vlad Yasevicha08de642007-12-20 14:11:47 -08003507 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003509 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 */
3511 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003512 /* If this is the first instance of ASCONF in the packet,
3513 * we can clean our old ASCONF-ACKs.
3514 */
3515 if (!chunk->has_asconf)
3516 sctp_assoc_clean_asconf_ack_cache(asoc);
3517
3518 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3519 * expected, process the ASCONF as described below and after
3520 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3521 * the response packet and cache a copy of it (in the event it
3522 * later needs to be retransmitted).
3523 *
3524 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 */
3526 asconf_ack = sctp_process_asconf((struct sctp_association *)
3527 asoc, chunk);
3528 if (!asconf_ack)
3529 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003530 } else if (serial < asoc->peer.addip_serial + 1) {
3531 /* ADDIP 5.2 E2)
3532 * If the value found in the Sequence Number is less than the
3533 * ('Peer- Sequence-Number' + 1), simply skip to the next
3534 * ASCONF, and include in the outbound response packet
3535 * any previously cached ASCONF-ACK response that was
3536 * sent and saved that matches the Sequence Number of the
3537 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3538 * Chunk exists. This will occur when an older ASCONF
3539 * arrives out of order. In such a case, the receiver
3540 * should skip the ASCONF Chunk and not include ASCONF-ACK
3541 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003543 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3544 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return SCTP_DISPOSITION_DISCARD;
3546 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003547 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003549 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 return SCTP_DISPOSITION_DISCARD;
3551 }
3552
Vlad Yasevicha08de642007-12-20 14:11:47 -08003553 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3554 * containing the ASCONF-ACK Chunks MUST be the source address of
3555 * the SCTP packet that held the ASCONF Chunks.
3556 *
3557 * To do this properly, we'll set the destination address of the chunk
3558 * and at the transmit time, will try look up the transport to use.
3559 * Since ASCONFs may be bundled, the correct transport may not be
3560 * created untill we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003562 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return SCTP_DISPOSITION_CONSUME;
3566}
3567
3568/*
3569 * ADDIP Section 4.3 General rules for address manipulation
3570 * When building TLV parameters for the ASCONF Chunk that will add or
3571 * delete IP addresses the D0 to D13 rules should be applied:
3572 */
3573sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3574 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003575 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 sctp_cmd_seq_t *commands)
3577{
3578 struct sctp_chunk *asconf_ack = arg;
3579 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3580 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003581 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 sctp_addiphdr_t *addip_hdr;
3583 __u32 sent_serial, rcvd_serial;
3584
3585 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3586 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3587 SCTP_NULL());
3588 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3589 }
3590
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003591 /* ADD-IP, Section 4.1.2:
3592 * This chunk MUST be sent in an authenticated way by using
3593 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3594 * is received unauthenticated it MUST be silently discarded as
3595 * described in [I-D.ietf-tsvwg-sctp-auth].
3596 */
3597 if (!sctp_addip_noauth && !asconf_ack->auth)
3598 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 /* Make sure that the ADDIP chunk has a valid length. */
3601 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3602 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3603 commands);
3604
3605 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3606 rcvd_serial = ntohl(addip_hdr->serial);
3607
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003608 /* Verify the ASCONF-ACK chunk before processing it. */
3609 if (!sctp_verify_asconf(asoc,
3610 (sctp_paramhdr_t *)addip_hdr->params,
3611 (void *)asconf_ack->chunk_end,
3612 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003613 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3614 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 if (last_asconf) {
3617 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3618 sent_serial = ntohl(addip_hdr->serial);
3619 } else {
3620 sent_serial = asoc->addip_serial - 1;
3621 }
3622
3623 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3624 * equal to the next serial number to be used but no ASCONF chunk is
3625 * outstanding the endpoint MUST ABORT the association. Note that a
3626 * sequence number is greater than if it is no more than 2^^31-1
3627 * larger than the current sequence number (using serial arithmetic).
3628 */
3629 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3630 !(asoc->addip_last_asconf)) {
3631 abort = sctp_make_abort(asoc, asconf_ack,
3632 sizeof(sctp_errhdr_t));
3633 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003634 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3636 SCTP_CHUNK(abort));
3637 }
3638 /* We are going to ABORT, so we might as well stop
3639 * processing the rest of the chunks in the packet.
3640 */
3641 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3642 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3643 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003644 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003645 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003647 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3649 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3650 return SCTP_DISPOSITION_ABORT;
3651 }
3652
3653 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3654 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3655 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3656
3657 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3658 asconf_ack))
3659 return SCTP_DISPOSITION_CONSUME;
3660
3661 abort = sctp_make_abort(asoc, asconf_ack,
3662 sizeof(sctp_errhdr_t));
3663 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003664 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3666 SCTP_CHUNK(abort));
3667 }
3668 /* We are going to ABORT, so we might as well stop
3669 * processing the rest of the chunks in the packet.
3670 */
3671 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003672 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003673 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003675 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3677 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3678 return SCTP_DISPOSITION_ABORT;
3679 }
3680
3681 return SCTP_DISPOSITION_DISCARD;
3682}
3683
3684/*
3685 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3686 *
3687 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3688 * its cumulative TSN point to the value carried in the FORWARD TSN
3689 * chunk, and then MUST further advance its cumulative TSN point locally
3690 * if possible.
3691 * After the above processing, the data receiver MUST stop reporting any
3692 * missing TSNs earlier than or equal to the new cumulative TSN point.
3693 *
3694 * Verification Tag: 8.5 Verification Tag [Normal verification]
3695 *
3696 * The return value is the disposition of the chunk.
3697 */
3698sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3699 const struct sctp_association *asoc,
3700 const sctp_subtype_t type,
3701 void *arg,
3702 sctp_cmd_seq_t *commands)
3703{
3704 struct sctp_chunk *chunk = arg;
3705 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003706 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 __u16 len;
3708 __u32 tsn;
3709
3710 if (!sctp_vtag_verify(chunk, asoc)) {
3711 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3712 SCTP_NULL());
3713 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3714 }
3715
3716 /* Make sure that the FORWARD_TSN chunk has valid length. */
3717 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3718 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3719 commands);
3720
3721 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3722 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3723 len = ntohs(chunk->chunk_hdr->length);
3724 len -= sizeof(struct sctp_chunkhdr);
3725 skb_pull(chunk->skb, len);
3726
3727 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003728 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730 /* The TSN is too high--silently discard the chunk and count on it
3731 * getting retransmitted later.
3732 */
3733 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3734 goto discard_noforce;
3735
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003736 /* Silently discard the chunk if stream-id is not valid */
3737 sctp_walk_fwdtsn(skip, chunk) {
3738 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3739 goto discard_noforce;
3740 }
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3743 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003744 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003746
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 /* Count this as receiving DATA. */
3748 if (asoc->autoclose) {
3749 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3750 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3751 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003752
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003754 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 */
3756 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
3758 return SCTP_DISPOSITION_CONSUME;
3759
3760discard_noforce:
3761 return SCTP_DISPOSITION_DISCARD;
3762}
3763
3764sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3765 const struct sctp_endpoint *ep,
3766 const struct sctp_association *asoc,
3767 const sctp_subtype_t type,
3768 void *arg,
3769 sctp_cmd_seq_t *commands)
3770{
3771 struct sctp_chunk *chunk = arg;
3772 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003773 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 __u16 len;
3775 __u32 tsn;
3776
3777 if (!sctp_vtag_verify(chunk, asoc)) {
3778 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3779 SCTP_NULL());
3780 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3781 }
3782
3783 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3784 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3785 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3786 commands);
3787
3788 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3789 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3790 len = ntohs(chunk->chunk_hdr->length);
3791 len -= sizeof(struct sctp_chunkhdr);
3792 skb_pull(chunk->skb, len);
3793
3794 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003795 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796
3797 /* The TSN is too high--silently discard the chunk and count on it
3798 * getting retransmitted later.
3799 */
3800 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3801 goto gen_shutdown;
3802
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003803 /* Silently discard the chunk if stream-id is not valid */
3804 sctp_walk_fwdtsn(skip, chunk) {
3805 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3806 goto gen_shutdown;
3807 }
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3810 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003811 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 /* Go a head and force a SACK, since we are shutting down. */
3815gen_shutdown:
3816 /* Implementor's Guide.
3817 *
3818 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3819 * respond to each received packet containing one or more DATA chunk(s)
3820 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3821 */
3822 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3823 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3824 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3825 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3826
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003827 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828}
3829
3830/*
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003831 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3832 *
3833 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3834 * Identifier field. If this algorithm was not specified by the
3835 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3836 * during association setup, the AUTH chunk and all chunks after it MUST
3837 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3838 * defined in Section 4.1.
3839 *
3840 * If an endpoint with no shared key receives a Shared Key Identifier
3841 * other than 0, it MUST silently discard all authenticated chunks. If
3842 * the endpoint has at least one endpoint pair shared key for the peer,
3843 * it MUST use the key specified by the Shared Key Identifier if a
3844 * key has been configured for that Shared Key Identifier. If no
3845 * endpoint pair shared key has been configured for that Shared Key
3846 * Identifier, all authenticated chunks MUST be silently discarded.
3847 *
3848 * Verification Tag: 8.5 Verification Tag [Normal verification]
3849 *
3850 * The return value is the disposition of the chunk.
3851 */
3852static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3853 const struct sctp_association *asoc,
3854 const sctp_subtype_t type,
3855 struct sctp_chunk *chunk)
3856{
3857 struct sctp_authhdr *auth_hdr;
3858 struct sctp_hmac *hmac;
3859 unsigned int sig_len;
3860 __u16 key_id;
3861 __u8 *save_digest;
3862 __u8 *digest;
3863
3864 /* Pull in the auth header, so we can do some more verification */
3865 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3866 chunk->subh.auth_hdr = auth_hdr;
3867 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3868
3869 /* Make sure that we suport the HMAC algorithm from the auth
3870 * chunk.
3871 */
3872 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3873 return SCTP_IERROR_AUTH_BAD_HMAC;
3874
3875 /* Make sure that the provided shared key identifier has been
3876 * configured
3877 */
3878 key_id = ntohs(auth_hdr->shkey_id);
3879 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3880 return SCTP_IERROR_AUTH_BAD_KEYID;
3881
3882
3883 /* Make sure that the length of the signature matches what
3884 * we expect.
3885 */
3886 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3887 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3888 if (sig_len != hmac->hmac_len)
3889 return SCTP_IERROR_PROTO_VIOLATION;
3890
3891 /* Now that we've done validation checks, we can compute and
3892 * verify the hmac. The steps involved are:
3893 * 1. Save the digest from the chunk.
3894 * 2. Zero out the digest in the chunk.
3895 * 3. Compute the new digest
3896 * 4. Compare saved and new digests.
3897 */
3898 digest = auth_hdr->hmac;
3899 skb_pull(chunk->skb, sig_len);
3900
3901 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3902 if (!save_digest)
3903 goto nomem;
3904
3905 memset(digest, 0, sig_len);
3906
3907 sctp_auth_calculate_hmac(asoc, chunk->skb,
3908 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3909 GFP_ATOMIC);
3910
3911 /* Discard the packet if the digests do not match */
3912 if (memcmp(save_digest, digest, sig_len)) {
3913 kfree(save_digest);
3914 return SCTP_IERROR_BAD_SIG;
3915 }
3916
3917 kfree(save_digest);
3918 chunk->auth = 1;
3919
3920 return SCTP_IERROR_NO_ERROR;
3921nomem:
3922 return SCTP_IERROR_NOMEM;
3923}
3924
3925sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3926 const struct sctp_association *asoc,
3927 const sctp_subtype_t type,
3928 void *arg,
3929 sctp_cmd_seq_t *commands)
3930{
3931 struct sctp_authhdr *auth_hdr;
3932 struct sctp_chunk *chunk = arg;
3933 struct sctp_chunk *err_chunk;
3934 sctp_ierror_t error;
3935
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003936 /* Make sure that the peer has AUTH capable */
3937 if (!asoc->peer.auth_capable)
3938 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3939
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003940 if (!sctp_vtag_verify(chunk, asoc)) {
3941 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3942 SCTP_NULL());
3943 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3944 }
3945
3946 /* Make sure that the AUTH chunk has valid length. */
3947 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3948 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3949 commands);
3950
3951 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3952 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3953 switch (error) {
3954 case SCTP_IERROR_AUTH_BAD_HMAC:
3955 /* Generate the ERROR chunk and discard the rest
3956 * of the packet
3957 */
3958 err_chunk = sctp_make_op_error(asoc, chunk,
3959 SCTP_ERROR_UNSUP_HMAC,
3960 &auth_hdr->hmac_id,
3961 sizeof(__u16));
3962 if (err_chunk) {
3963 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3964 SCTP_CHUNK(err_chunk));
3965 }
3966 /* Fall Through */
3967 case SCTP_IERROR_AUTH_BAD_KEYID:
3968 case SCTP_IERROR_BAD_SIG:
3969 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3970 break;
3971 case SCTP_IERROR_PROTO_VIOLATION:
3972 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3973 commands);
3974 break;
3975 case SCTP_IERROR_NOMEM:
3976 return SCTP_DISPOSITION_NOMEM;
3977 default:
3978 break;
3979 }
3980
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003981 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
3982 struct sctp_ulpevent *ev;
3983
3984 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
3985 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
3986
3987 if (!ev)
3988 return -ENOMEM;
3989
3990 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3991 SCTP_ULPEVENT(ev));
3992 }
3993
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003994 return SCTP_DISPOSITION_CONSUME;
3995}
3996
3997/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 * Process an unknown chunk.
3999 *
4000 * Section: 3.2. Also, 2.1 in the implementor's guide.
4001 *
4002 * Chunk Types are encoded such that the highest-order two bits specify
4003 * the action that must be taken if the processing endpoint does not
4004 * recognize the Chunk Type.
4005 *
4006 * 00 - Stop processing this SCTP packet and discard it, do not process
4007 * any further chunks within it.
4008 *
4009 * 01 - Stop processing this SCTP packet and discard it, do not process
4010 * any further chunks within it, and report the unrecognized
4011 * chunk in an 'Unrecognized Chunk Type'.
4012 *
4013 * 10 - Skip this chunk and continue processing.
4014 *
4015 * 11 - Skip this chunk and continue processing, but report in an ERROR
4016 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4017 *
4018 * The return value is the disposition of the chunk.
4019 */
4020sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4021 const struct sctp_association *asoc,
4022 const sctp_subtype_t type,
4023 void *arg,
4024 sctp_cmd_seq_t *commands)
4025{
4026 struct sctp_chunk *unk_chunk = arg;
4027 struct sctp_chunk *err_chunk;
4028 sctp_chunkhdr_t *hdr;
4029
4030 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4031
4032 if (!sctp_vtag_verify(unk_chunk, asoc))
4033 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4034
4035 /* Make sure that the chunk has a valid length.
4036 * Since we don't know the chunk type, we use a general
4037 * chunkhdr structure to make a comparison.
4038 */
4039 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4040 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4041 commands);
4042
4043 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4044 case SCTP_CID_ACTION_DISCARD:
4045 /* Discard the packet. */
4046 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4047 break;
4048 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 /* Generate an ERROR chunk as response. */
4050 hdr = unk_chunk->chunk_hdr;
4051 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4052 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4053 WORD_ROUND(ntohs(hdr->length)));
4054 if (err_chunk) {
4055 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4056 SCTP_CHUNK(err_chunk));
4057 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004058
4059 /* Discard the packet. */
4060 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 return SCTP_DISPOSITION_CONSUME;
4062 break;
4063 case SCTP_CID_ACTION_SKIP:
4064 /* Skip the chunk. */
4065 return SCTP_DISPOSITION_DISCARD;
4066 break;
4067 case SCTP_CID_ACTION_SKIP_ERR:
4068 /* Generate an ERROR chunk as response. */
4069 hdr = unk_chunk->chunk_hdr;
4070 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4071 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4072 WORD_ROUND(ntohs(hdr->length)));
4073 if (err_chunk) {
4074 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4075 SCTP_CHUNK(err_chunk));
4076 }
4077 /* Skip the chunk. */
4078 return SCTP_DISPOSITION_CONSUME;
4079 break;
4080 default:
4081 break;
4082 }
4083
4084 return SCTP_DISPOSITION_DISCARD;
4085}
4086
4087/*
4088 * Discard the chunk.
4089 *
4090 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4091 * [Too numerous to mention...]
4092 * Verification Tag: No verification needed.
4093 * Inputs
4094 * (endpoint, asoc, chunk)
4095 *
4096 * Outputs
4097 * (asoc, reply_msg, msg_up, timers, counters)
4098 *
4099 * The return value is the disposition of the chunk.
4100 */
4101sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4102 const struct sctp_association *asoc,
4103 const sctp_subtype_t type,
4104 void *arg,
4105 sctp_cmd_seq_t *commands)
4106{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004107 struct sctp_chunk *chunk = arg;
4108
4109 /* Make sure that the chunk has a valid length.
4110 * Since we don't know the chunk type, we use a general
4111 * chunkhdr structure to make a comparison.
4112 */
4113 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4114 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4115 commands);
4116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4118 return SCTP_DISPOSITION_DISCARD;
4119}
4120
4121/*
4122 * Discard the whole packet.
4123 *
4124 * Section: 8.4 2)
4125 *
4126 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4127 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 *
4129 * Verification Tag: No verification necessary
4130 *
4131 * Inputs
4132 * (endpoint, asoc, chunk)
4133 *
4134 * Outputs
4135 * (asoc, reply_msg, msg_up, timers, counters)
4136 *
4137 * The return value is the disposition of the chunk.
4138 */
4139sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4140 const struct sctp_association *asoc,
4141 const sctp_subtype_t type,
4142 void *arg,
4143 sctp_cmd_seq_t *commands)
4144{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004145 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4147
4148 return SCTP_DISPOSITION_CONSUME;
4149}
4150
4151
4152/*
4153 * The other end is violating protocol.
4154 *
4155 * Section: Not specified
4156 * Verification Tag: Not specified
4157 * Inputs
4158 * (endpoint, asoc, chunk)
4159 *
4160 * Outputs
4161 * (asoc, reply_msg, msg_up, timers, counters)
4162 *
4163 * We simply tag the chunk as a violation. The state machine will log
4164 * the violation and continue.
4165 */
4166sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4167 const struct sctp_association *asoc,
4168 const sctp_subtype_t type,
4169 void *arg,
4170 sctp_cmd_seq_t *commands)
4171{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004172 struct sctp_chunk *chunk = arg;
4173
4174 /* Make sure that the chunk has a valid length. */
4175 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4176 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4177 commands);
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 return SCTP_DISPOSITION_VIOLATION;
4180}
4181
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004183 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004185static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004186 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004189 sctp_cmd_seq_t *commands,
4190 const __u8 *payload,
4191 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004193 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 struct sctp_chunk *chunk = arg;
4195 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004197 /* SCTP-AUTH, Section 6.3:
4198 * It should be noted that if the receiver wants to tear
4199 * down an association in an authenticated way only, the
4200 * handling of malformed packets should not result in
4201 * tearing down the association.
4202 *
4203 * This means that if we only want to abort associations
4204 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +02004205 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004206 * was malformed.
4207 */
4208 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4209 goto discard;
4210
Jesper Juhl9abed242007-11-11 23:57:49 +01004211 /* Make the abort chunk. */
4212 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4213 if (!abort)
4214 goto nomem;
4215
Vlad Yasevichece25df2007-09-07 16:30:54 -04004216 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004217 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4218 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4219 !asoc->peer.i.init_tag) {
4220 sctp_initack_chunk_t *initack;
4221
4222 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4223 if (!sctp_chunk_length_valid(chunk,
4224 sizeof(sctp_initack_chunk_t)))
4225 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4226 else {
4227 unsigned int inittag;
4228
4229 inittag = ntohl(initack->init_hdr.init_tag);
4230 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4231 SCTP_U32(inittag));
4232 }
4233 }
4234
Vlad Yasevichece25df2007-09-07 16:30:54 -04004235 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4236 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Vlad Yasevichece25df2007-09-07 16:30:54 -04004238 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4239 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4240 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4241 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4242 SCTP_ERROR(ECONNREFUSED));
4243 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4244 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4245 } else {
4246 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4247 SCTP_ERROR(ECONNABORTED));
4248 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4249 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4250 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004253 packet = sctp_ootb_pkt_new(asoc, chunk);
4254
4255 if (!packet)
4256 goto nomem_pkt;
4257
4258 if (sctp_test_T_bit(abort))
4259 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4260
4261 abort->skb->sk = ep->base.sk;
4262
4263 sctp_packet_append_chunk(packet, abort);
4264
4265 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4266 SCTP_PACKET(packet));
4267
4268 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 }
4270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004272
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004273discard:
4274 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 return SCTP_DISPOSITION_ABORT;
4276
Vlad Yasevichece25df2007-09-07 16:30:54 -04004277nomem_pkt:
4278 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279nomem:
4280 return SCTP_DISPOSITION_NOMEM;
4281}
4282
Wei Yongjunaecedea2007-08-02 16:57:44 +08004283/*
4284 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004285 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004286 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004287 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004288 *
4289 * We inform the other end by sending an ABORT with a Protocol Violation
4290 * error code.
4291 *
4292 * Section: Not specified
4293 * Verification Tag: Nothing to do
4294 * Inputs
4295 * (endpoint, asoc, chunk)
4296 *
4297 * Outputs
4298 * (reply_msg, msg_up, counters)
4299 *
4300 * Generate an ABORT chunk and terminate the association.
4301 */
4302static sctp_disposition_t sctp_sf_violation_chunklen(
4303 const struct sctp_endpoint *ep,
4304 const struct sctp_association *asoc,
4305 const sctp_subtype_t type,
4306 void *arg,
4307 sctp_cmd_seq_t *commands)
4308{
Florian Westphal24448442008-03-23 22:46:34 -07004309 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004310
Vlad Yasevichece25df2007-09-07 16:30:54 -04004311 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004312 sizeof(err_str));
4313}
4314
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004315/*
4316 * Handle a protocol violation when the parameter length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004317 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004318 * given parameter can be.
4319 */
4320static sctp_disposition_t sctp_sf_violation_paramlen(
4321 const struct sctp_endpoint *ep,
4322 const struct sctp_association *asoc,
4323 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004324 void *arg, void *ext,
4325 sctp_cmd_seq_t *commands)
4326{
4327 struct sctp_chunk *chunk = arg;
4328 struct sctp_paramhdr *param = ext;
4329 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004330
Wei Yongjunba016672008-09-30 05:32:24 -07004331 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4332 goto discard;
4333
4334 /* Make the abort chunk. */
4335 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4336 if (!abort)
4337 goto nomem;
4338
4339 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4340 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4341
4342 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4343 SCTP_ERROR(ECONNABORTED));
4344 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4345 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4346 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004347 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004348
4349discard:
4350 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004351 return SCTP_DISPOSITION_ABORT;
4352nomem:
4353 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004354}
4355
Wei Yongjunaecedea2007-08-02 16:57:44 +08004356/* Handle a protocol violation when the peer trying to advance the
4357 * cumulative tsn ack to a point beyond the max tsn currently sent.
4358 *
4359 * We inform the other end by sending an ABORT with a Protocol Violation
4360 * error code.
4361 */
4362static sctp_disposition_t sctp_sf_violation_ctsn(
4363 const struct sctp_endpoint *ep,
4364 const struct sctp_association *asoc,
4365 const sctp_subtype_t type,
4366 void *arg,
4367 sctp_cmd_seq_t *commands)
4368{
Florian Westphal24448442008-03-23 22:46:34 -07004369 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004370
Vlad Yasevichece25df2007-09-07 16:30:54 -04004371 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004372 sizeof(err_str));
4373}
4374
Vlad Yasevichece25df2007-09-07 16:30:54 -04004375/* Handle protocol violation of an invalid chunk bundling. For example,
4376 * when we have an association and we recieve bundled INIT-ACK, or
4377 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4378 * statement from the specs. Additinally, there might be an attacker
4379 * on the path and we may not want to continue this communication.
4380 */
4381static sctp_disposition_t sctp_sf_violation_chunk(
4382 const struct sctp_endpoint *ep,
4383 const struct sctp_association *asoc,
4384 const sctp_subtype_t type,
4385 void *arg,
4386 sctp_cmd_seq_t *commands)
4387{
Florian Westphal24448442008-03-23 22:46:34 -07004388 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004389
4390 if (!asoc)
4391 return sctp_sf_violation(ep, asoc, type, arg, commands);
4392
4393 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4394 sizeof(err_str));
4395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396/***************************************************************************
4397 * These are the state functions for handling primitive (Section 10) events.
4398 ***************************************************************************/
4399/*
4400 * sctp_sf_do_prm_asoc
4401 *
4402 * Section: 10.1 ULP-to-SCTP
4403 * B) Associate
4404 *
4405 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4406 * outbound stream count)
4407 * -> association id [,destination transport addr list] [,outbound stream
4408 * count]
4409 *
4410 * This primitive allows the upper layer to initiate an association to a
4411 * specific peer endpoint.
4412 *
4413 * The peer endpoint shall be specified by one of the transport addresses
4414 * which defines the endpoint (see Section 1.4). If the local SCTP
4415 * instance has not been initialized, the ASSOCIATE is considered an
4416 * error.
4417 * [This is not relevant for the kernel implementation since we do all
4418 * initialization at boot time. It we hadn't initialized we wouldn't
4419 * get anywhere near this code.]
4420 *
4421 * An association id, which is a local handle to the SCTP association,
4422 * will be returned on successful establishment of the association. If
4423 * SCTP is not able to open an SCTP association with the peer endpoint,
4424 * an error is returned.
4425 * [In the kernel implementation, the struct sctp_association needs to
4426 * be created BEFORE causing this primitive to run.]
4427 *
4428 * Other association parameters may be returned, including the
4429 * complete destination transport addresses of the peer as well as the
4430 * outbound stream count of the local endpoint. One of the transport
4431 * address from the returned destination addresses will be selected by
4432 * the local endpoint as default primary path for sending SCTP packets
4433 * to this peer. The returned "destination transport addr list" can
4434 * be used by the ULP to change the default primary path or to force
4435 * sending a packet to a specific transport address. [All of this
4436 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4437 * function.]
4438 *
4439 * Mandatory attributes:
4440 *
4441 * o local SCTP instance name - obtained from the INITIALIZE operation.
4442 * [This is the argument asoc.]
4443 * o destination transport addr - specified as one of the transport
4444 * addresses of the peer endpoint with which the association is to be
4445 * established.
4446 * [This is asoc->peer.active_path.]
4447 * o outbound stream count - the number of outbound streams the ULP
4448 * would like to open towards this peer endpoint.
4449 * [BUG: This is not currently implemented.]
4450 * Optional attributes:
4451 *
4452 * None.
4453 *
4454 * The return value is a disposition.
4455 */
4456sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4457 const struct sctp_association *asoc,
4458 const sctp_subtype_t type,
4459 void *arg,
4460 sctp_cmd_seq_t *commands)
4461{
4462 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004463 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 /* The comment below says that we enter COOKIE-WAIT AFTER
4466 * sending the INIT, but that doesn't actually work in our
4467 * implementation...
4468 */
4469 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4470 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4471
4472 /* RFC 2960 5.1 Normal Establishment of an Association
4473 *
4474 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4475 * must provide its Verification Tag (Tag_A) in the Initiate
4476 * Tag field. Tag_A SHOULD be a random number in the range of
4477 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4478 */
4479
4480 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4481 if (!repl)
4482 goto nomem;
4483
4484 /* Cast away the const modifier, as we want to just
4485 * rerun it through as a sideffect.
4486 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004487 my_asoc = (struct sctp_association *)asoc;
4488 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Frank Filz3f7a87d2005-06-20 13:14:57 -07004490 /* Choose transport for INIT. */
4491 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4492 SCTP_CHUNK(repl));
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 /* After sending the INIT, "A" starts the T1-init timer and
4495 * enters the COOKIE-WAIT state.
4496 */
4497 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4498 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4499 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4500 return SCTP_DISPOSITION_CONSUME;
4501
4502nomem:
4503 return SCTP_DISPOSITION_NOMEM;
4504}
4505
4506/*
4507 * Process the SEND primitive.
4508 *
4509 * Section: 10.1 ULP-to-SCTP
4510 * E) Send
4511 *
4512 * Format: SEND(association id, buffer address, byte count [,context]
4513 * [,stream id] [,life time] [,destination transport address]
4514 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4515 * -> result
4516 *
4517 * This is the main method to send user data via SCTP.
4518 *
4519 * Mandatory attributes:
4520 *
4521 * o association id - local handle to the SCTP association
4522 *
4523 * o buffer address - the location where the user message to be
4524 * transmitted is stored;
4525 *
4526 * o byte count - The size of the user data in number of bytes;
4527 *
4528 * Optional attributes:
4529 *
4530 * o context - an optional 32 bit integer that will be carried in the
4531 * sending failure notification to the ULP if the transportation of
4532 * this User Message fails.
4533 *
4534 * o stream id - to indicate which stream to send the data on. If not
4535 * specified, stream 0 will be used.
4536 *
4537 * o life time - specifies the life time of the user data. The user data
4538 * will not be sent by SCTP after the life time expires. This
4539 * parameter can be used to avoid efforts to transmit stale
4540 * user messages. SCTP notifies the ULP if the data cannot be
4541 * initiated to transport (i.e. sent to the destination via SCTP's
4542 * send primitive) within the life time variable. However, the
4543 * user data will be transmitted if SCTP has attempted to transmit a
4544 * chunk before the life time expired.
4545 *
4546 * o destination transport address - specified as one of the destination
4547 * transport addresses of the peer endpoint to which this packet
4548 * should be sent. Whenever possible, SCTP should use this destination
4549 * transport address for sending the packets, instead of the current
4550 * primary path.
4551 *
4552 * o unorder flag - this flag, if present, indicates that the user
4553 * would like the data delivered in an unordered fashion to the peer
4554 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4555 * message).
4556 *
4557 * o no-bundle flag - instructs SCTP not to bundle this user data with
4558 * other outbound DATA chunks. SCTP MAY still bundle even when
4559 * this flag is present, when faced with network congestion.
4560 *
4561 * o payload protocol-id - A 32 bit unsigned integer that is to be
4562 * passed to the peer indicating the type of payload protocol data
4563 * being transmitted. This value is passed as opaque data by SCTP.
4564 *
4565 * The return value is the disposition.
4566 */
4567sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4568 const struct sctp_association *asoc,
4569 const sctp_subtype_t type,
4570 void *arg,
4571 sctp_cmd_seq_t *commands)
4572{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004573 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004575 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 return SCTP_DISPOSITION_CONSUME;
4577}
4578
4579/*
4580 * Process the SHUTDOWN primitive.
4581 *
4582 * Section: 10.1:
4583 * C) Shutdown
4584 *
4585 * Format: SHUTDOWN(association id)
4586 * -> result
4587 *
4588 * Gracefully closes an association. Any locally queued user data
4589 * will be delivered to the peer. The association will be terminated only
4590 * after the peer acknowledges all the SCTP packets sent. A success code
4591 * will be returned on successful termination of the association. If
4592 * attempting to terminate the association results in a failure, an error
4593 * code shall be returned.
4594 *
4595 * Mandatory attributes:
4596 *
4597 * o association id - local handle to the SCTP association
4598 *
4599 * Optional attributes:
4600 *
4601 * None.
4602 *
4603 * The return value is the disposition.
4604 */
4605sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4606 const struct sctp_endpoint *ep,
4607 const struct sctp_association *asoc,
4608 const sctp_subtype_t type,
4609 void *arg,
4610 sctp_cmd_seq_t *commands)
4611{
4612 int disposition;
4613
4614 /* From 9.2 Shutdown of an Association
4615 * Upon receipt of the SHUTDOWN primitive from its upper
4616 * layer, the endpoint enters SHUTDOWN-PENDING state and
4617 * remains there until all outstanding data has been
4618 * acknowledged by its peer. The endpoint accepts no new data
4619 * from its upper layer, but retransmits data to the far end
4620 * if necessary to fill gaps.
4621 */
4622 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4623 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 disposition = SCTP_DISPOSITION_CONSUME;
4626 if (sctp_outq_is_empty(&asoc->outqueue)) {
4627 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4628 arg, commands);
4629 }
4630 return disposition;
4631}
4632
4633/*
4634 * Process the ABORT primitive.
4635 *
4636 * Section: 10.1:
4637 * C) Abort
4638 *
4639 * Format: Abort(association id [, cause code])
4640 * -> result
4641 *
4642 * Ungracefully closes an association. Any locally queued user data
4643 * will be discarded and an ABORT chunk is sent to the peer. A success code
4644 * will be returned on successful abortion of the association. If
4645 * attempting to abort the association results in a failure, an error
4646 * code shall be returned.
4647 *
4648 * Mandatory attributes:
4649 *
4650 * o association id - local handle to the SCTP association
4651 *
4652 * Optional attributes:
4653 *
4654 * o cause code - reason of the abort to be passed to the peer
4655 *
4656 * None.
4657 *
4658 * The return value is the disposition.
4659 */
4660sctp_disposition_t sctp_sf_do_9_1_prm_abort(
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 /* From 9.1 Abort of an Association
4668 * Upon receipt of the ABORT primitive from its upper
4669 * layer, the endpoint enters CLOSED state and
4670 * discard all outstanding data has been
4671 * acknowledged by its peer. The endpoint accepts no new data
4672 * from its upper layer, but retransmits data to the far end
4673 * if necessary to fill gaps.
4674 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004675 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 sctp_disposition_t retval;
4677
4678 retval = SCTP_DISPOSITION_CONSUME;
4679
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004680 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681
4682 /* Even if we can't send the ABORT due to low memory delete the
4683 * TCB. This is a departure from our typical NOMEM handling.
4684 */
4685
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004686 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4687 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 /* Delete the established association. */
4689 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004690 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
4692 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4693 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4694
4695 return retval;
4696}
4697
4698/* We tried an illegal operation on an association which is closed. */
4699sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4700 const struct sctp_association *asoc,
4701 const sctp_subtype_t type,
4702 void *arg,
4703 sctp_cmd_seq_t *commands)
4704{
4705 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4706 return SCTP_DISPOSITION_CONSUME;
4707}
4708
4709/* We tried an illegal operation on an association which is shutting
4710 * down.
4711 */
4712sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4713 const struct sctp_association *asoc,
4714 const sctp_subtype_t type,
4715 void *arg,
4716 sctp_cmd_seq_t *commands)
4717{
4718 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4719 SCTP_ERROR(-ESHUTDOWN));
4720 return SCTP_DISPOSITION_CONSUME;
4721}
4722
4723/*
4724 * sctp_cookie_wait_prm_shutdown
4725 *
4726 * Section: 4 Note: 2
4727 * Verification Tag:
4728 * Inputs
4729 * (endpoint, asoc)
4730 *
4731 * The RFC does not explicitly address this issue, but is the route through the
4732 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4733 *
4734 * Outputs
4735 * (timers)
4736 */
4737sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4738 const struct sctp_endpoint *ep,
4739 const struct sctp_association *asoc,
4740 const sctp_subtype_t type,
4741 void *arg,
4742 sctp_cmd_seq_t *commands)
4743{
4744 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4745 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4746
4747 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4748 SCTP_STATE(SCTP_STATE_CLOSED));
4749
4750 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4751
4752 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4753
4754 return SCTP_DISPOSITION_DELETE_TCB;
4755}
4756
4757/*
4758 * sctp_cookie_echoed_prm_shutdown
4759 *
4760 * Section: 4 Note: 2
4761 * Verification Tag:
4762 * Inputs
4763 * (endpoint, asoc)
4764 *
4765 * The RFC does not explcitly address this issue, but is the route through the
4766 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4767 *
4768 * Outputs
4769 * (timers)
4770 */
4771sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4772 const struct sctp_endpoint *ep,
4773 const struct sctp_association *asoc,
4774 const sctp_subtype_t type,
4775 void *arg, sctp_cmd_seq_t *commands)
4776{
4777 /* There is a single T1 timer, so we should be able to use
4778 * common function with the COOKIE-WAIT state.
4779 */
4780 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4781}
4782
4783/*
4784 * sctp_sf_cookie_wait_prm_abort
4785 *
4786 * Section: 4 Note: 2
4787 * Verification Tag:
4788 * Inputs
4789 * (endpoint, asoc)
4790 *
4791 * The RFC does not explicitly address this issue, but is the route through the
4792 * state table when someone issues an abort while in COOKIE_WAIT state.
4793 *
4794 * Outputs
4795 * (timers)
4796 */
4797sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4798 const struct sctp_endpoint *ep,
4799 const struct sctp_association *asoc,
4800 const sctp_subtype_t type,
4801 void *arg,
4802 sctp_cmd_seq_t *commands)
4803{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004804 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 sctp_disposition_t retval;
4806
4807 /* Stop T1-init timer */
4808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4809 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4810 retval = SCTP_DISPOSITION_CONSUME;
4811
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004812 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
4814 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4815 SCTP_STATE(SCTP_STATE_CLOSED));
4816
4817 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4818
4819 /* Even if we can't send the ABORT due to low memory delete the
4820 * TCB. This is a departure from our typical NOMEM handling.
4821 */
4822
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004823 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4824 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 /* Delete the established association. */
4826 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004827 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
4829 return retval;
4830}
4831
4832/*
4833 * sctp_sf_cookie_echoed_prm_abort
4834 *
4835 * Section: 4 Note: 3
4836 * Verification Tag:
4837 * Inputs
4838 * (endpoint, asoc)
4839 *
4840 * The RFC does not explcitly address this issue, but is the route through the
4841 * state table when someone issues an abort while in COOKIE_ECHOED state.
4842 *
4843 * Outputs
4844 * (timers)
4845 */
4846sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4847 const struct sctp_endpoint *ep,
4848 const struct sctp_association *asoc,
4849 const sctp_subtype_t type,
4850 void *arg,
4851 sctp_cmd_seq_t *commands)
4852{
4853 /* There is a single T1 timer, so we should be able to use
4854 * common function with the COOKIE-WAIT state.
4855 */
4856 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4857}
4858
4859/*
4860 * sctp_sf_shutdown_pending_prm_abort
4861 *
4862 * Inputs
4863 * (endpoint, asoc)
4864 *
4865 * The RFC does not explicitly address this issue, but is the route through the
4866 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4867 *
4868 * Outputs
4869 * (timers)
4870 */
4871sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4872 const struct sctp_endpoint *ep,
4873 const struct sctp_association *asoc,
4874 const sctp_subtype_t type,
4875 void *arg,
4876 sctp_cmd_seq_t *commands)
4877{
4878 /* Stop the T5-shutdown guard timer. */
4879 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4880 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4881
4882 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4883}
4884
4885/*
4886 * sctp_sf_shutdown_sent_prm_abort
4887 *
4888 * Inputs
4889 * (endpoint, asoc)
4890 *
4891 * The RFC does not explicitly address this issue, but is the route through the
4892 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4893 *
4894 * Outputs
4895 * (timers)
4896 */
4897sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4898 const struct sctp_endpoint *ep,
4899 const struct sctp_association *asoc,
4900 const sctp_subtype_t type,
4901 void *arg,
4902 sctp_cmd_seq_t *commands)
4903{
4904 /* Stop the T2-shutdown timer. */
4905 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4906 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4907
4908 /* Stop the T5-shutdown guard timer. */
4909 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4910 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4911
4912 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4913}
4914
4915/*
4916 * sctp_sf_cookie_echoed_prm_abort
4917 *
4918 * Inputs
4919 * (endpoint, asoc)
4920 *
4921 * The RFC does not explcitly address this issue, but is the route through the
4922 * state table when someone issues an abort while in COOKIE_ECHOED state.
4923 *
4924 * Outputs
4925 * (timers)
4926 */
4927sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4928 const struct sctp_endpoint *ep,
4929 const struct sctp_association *asoc,
4930 const sctp_subtype_t type,
4931 void *arg,
4932 sctp_cmd_seq_t *commands)
4933{
4934 /* The same T2 timer, so we should be able to use
4935 * common function with the SHUTDOWN-SENT state.
4936 */
4937 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4938}
4939
4940/*
4941 * Process the REQUESTHEARTBEAT primitive
4942 *
4943 * 10.1 ULP-to-SCTP
4944 * J) Request Heartbeat
4945 *
4946 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4947 *
4948 * -> result
4949 *
4950 * Instructs the local endpoint to perform a HeartBeat on the specified
4951 * destination transport address of the given association. The returned
4952 * result should indicate whether the transmission of the HEARTBEAT
4953 * chunk to the destination address is successful.
4954 *
4955 * Mandatory attributes:
4956 *
4957 * o association id - local handle to the SCTP association
4958 *
4959 * o destination transport address - the transport address of the
4960 * association on which a heartbeat should be issued.
4961 */
4962sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4963 const struct sctp_endpoint *ep,
4964 const struct sctp_association *asoc,
4965 const sctp_subtype_t type,
4966 void *arg,
4967 sctp_cmd_seq_t *commands)
4968{
Vlad Yasevichfb785252007-03-19 17:02:03 -07004969 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4970 (struct sctp_transport *)arg, commands))
4971 return SCTP_DISPOSITION_NOMEM;
4972
4973 /*
4974 * RFC 2960 (bis), section 8.3
4975 *
4976 * D) Request an on-demand HEARTBEAT on a specific destination
4977 * transport address of a given association.
4978 *
4979 * The endpoint should increment the respective error counter of
4980 * the destination transport address each time a HEARTBEAT is sent
4981 * to that address and not acknowledged within one RTO.
4982 *
4983 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00004984 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07004985 SCTP_TRANSPORT(arg));
4986 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987}
4988
4989/*
4990 * ADDIP Section 4.1 ASCONF Chunk Procedures
4991 * When an endpoint has an ASCONF signaled change to be sent to the
4992 * remote endpoint it should do A1 to A9
4993 */
4994sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4995 const struct sctp_association *asoc,
4996 const sctp_subtype_t type,
4997 void *arg,
4998 sctp_cmd_seq_t *commands)
4999{
5000 struct sctp_chunk *chunk = arg;
5001
5002 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5003 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5004 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5005 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5006 return SCTP_DISPOSITION_CONSUME;
5007}
5008
5009/*
5010 * Ignore the primitive event
5011 *
5012 * The return value is the disposition of the primitive.
5013 */
5014sctp_disposition_t sctp_sf_ignore_primitive(
5015 const struct sctp_endpoint *ep,
5016 const struct sctp_association *asoc,
5017 const sctp_subtype_t type,
5018 void *arg,
5019 sctp_cmd_seq_t *commands)
5020{
5021 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5022 return SCTP_DISPOSITION_DISCARD;
5023}
5024
5025/***************************************************************************
5026 * These are the state functions for the OTHER events.
5027 ***************************************************************************/
5028
5029/*
5030 * Start the shutdown negotiation.
5031 *
5032 * From Section 9.2:
5033 * Once all its outstanding data has been acknowledged, the endpoint
5034 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5035 * TSN Ack field the last sequential TSN it has received from the peer.
5036 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5037 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5038 * with the updated last sequential TSN received from its peer.
5039 *
5040 * The return value is the disposition.
5041 */
5042sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5043 const struct sctp_endpoint *ep,
5044 const struct sctp_association *asoc,
5045 const sctp_subtype_t type,
5046 void *arg,
5047 sctp_cmd_seq_t *commands)
5048{
5049 struct sctp_chunk *reply;
5050
5051 /* Once all its outstanding data has been acknowledged, the
5052 * endpoint shall send a SHUTDOWN chunk to its peer including
5053 * in the Cumulative TSN Ack field the last sequential TSN it
5054 * has received from the peer.
5055 */
5056 reply = sctp_make_shutdown(asoc, NULL);
5057 if (!reply)
5058 goto nomem;
5059
5060 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5061 * T2-shutdown timer.
5062 */
5063 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5064
5065 /* It shall then start the T2-shutdown timer */
5066 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5067 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5068
Wei Yongjun536428a2008-09-05 08:55:26 +08005069 /* RFC 4960 Section 9.2
5070 * The sender of the SHUTDOWN MAY also start an overall guard timer
5071 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5072 */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5074 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 if (asoc->autoclose)
5077 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5078 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5079
5080 /* and enter the SHUTDOWN-SENT state. */
5081 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5082 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5083
5084 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5085 *
5086 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005087 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 */
5089 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5090
5091 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5092
5093 return SCTP_DISPOSITION_CONSUME;
5094
5095nomem:
5096 return SCTP_DISPOSITION_NOMEM;
5097}
5098
5099/*
5100 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5101 *
5102 * From Section 9.2:
5103 *
5104 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5105 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5106 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5107 * endpoint must re-send the SHUTDOWN ACK.
5108 *
5109 * The return value is the disposition.
5110 */
5111sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5112 const struct sctp_endpoint *ep,
5113 const struct sctp_association *asoc,
5114 const sctp_subtype_t type,
5115 void *arg,
5116 sctp_cmd_seq_t *commands)
5117{
5118 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5119 struct sctp_chunk *reply;
5120
5121 /* There are 2 ways of getting here:
5122 * 1) called in response to a SHUTDOWN chunk
5123 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5124 *
5125 * For the case (2), the arg parameter is set to NULL. We need
5126 * to check that we have a chunk before accessing it's fields.
5127 */
5128 if (chunk) {
5129 if (!sctp_vtag_verify(chunk, asoc))
5130 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5131
5132 /* Make sure that the SHUTDOWN chunk has a valid length. */
5133 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5134 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5135 commands);
5136 }
5137
5138 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5139 * shall send a SHUTDOWN ACK ...
5140 */
5141 reply = sctp_make_shutdown_ack(asoc, chunk);
5142 if (!reply)
5143 goto nomem;
5144
5145 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5146 * the T2-shutdown timer.
5147 */
5148 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5149
5150 /* and start/restart a T2-shutdown timer of its own, */
5151 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5152 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5153
5154 if (asoc->autoclose)
5155 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5156 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5157
5158 /* Enter the SHUTDOWN-ACK-SENT state. */
5159 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5160 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5161
5162 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5163 *
5164 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005165 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 */
5167 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5168
5169 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5170
5171 return SCTP_DISPOSITION_CONSUME;
5172
5173nomem:
5174 return SCTP_DISPOSITION_NOMEM;
5175}
5176
5177/*
5178 * Ignore the event defined as other
5179 *
5180 * The return value is the disposition of the event.
5181 */
5182sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5183 const struct sctp_association *asoc,
5184 const sctp_subtype_t type,
5185 void *arg,
5186 sctp_cmd_seq_t *commands)
5187{
5188 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5189 return SCTP_DISPOSITION_DISCARD;
5190}
5191
5192/************************************************************
5193 * These are the state functions for handling timeout events.
5194 ************************************************************/
5195
5196/*
5197 * RTX Timeout
5198 *
5199 * Section: 6.3.3 Handle T3-rtx Expiration
5200 *
5201 * Whenever the retransmission timer T3-rtx expires for a destination
5202 * address, do the following:
5203 * [See below]
5204 *
5205 * The return value is the disposition of the chunk.
5206 */
5207sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5208 const struct sctp_association *asoc,
5209 const sctp_subtype_t type,
5210 void *arg,
5211 sctp_cmd_seq_t *commands)
5212{
5213 struct sctp_transport *transport = arg;
5214
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005215 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005218 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5219 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5221 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005222 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5224 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5225 return SCTP_DISPOSITION_DELETE_TCB;
5226 }
5227
5228 /* E1) For the destination address for which the timer
5229 * expires, adjust its ssthresh with rules defined in Section
5230 * 7.2.3 and set the cwnd <- MTU.
5231 */
5232
5233 /* E2) For the destination address for which the timer
5234 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5235 * maximum value discussed in rule C7 above (RTO.max) may be
5236 * used to provide an upper bound to this doubling operation.
5237 */
5238
5239 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5240 * outstanding DATA chunks for the address for which the
5241 * T3-rtx has expired will fit into a single packet, subject
5242 * to the MTU constraint for the path corresponding to the
5243 * destination transport address to which the retransmission
5244 * is being sent (this may be different from the address for
5245 * which the timer expires [see Section 6.4]). Call this
5246 * value K. Bundle and retransmit those K DATA chunks in a
5247 * single packet to the destination endpoint.
5248 *
5249 * Note: Any DATA chunks that were sent to the address for
5250 * which the T3-rtx timer expired but did not fit in one MTU
5251 * (rule E3 above), should be marked for retransmission and
5252 * sent as soon as cwnd allows (normally when a SACK arrives).
5253 */
5254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 /* Do some failure management (Section 8.2). */
5256 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5257
Vlad Yasevich1845a572007-02-21 02:06:19 -08005258 /* NB: Rules E4 and F1 are implicit in R1. */
5259 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5260
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 return SCTP_DISPOSITION_CONSUME;
5262}
5263
5264/*
5265 * Generate delayed SACK on timeout
5266 *
5267 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5268 *
5269 * The guidelines on delayed acknowledgement algorithm specified in
5270 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5271 * acknowledgement SHOULD be generated for at least every second packet
5272 * (not every second DATA chunk) received, and SHOULD be generated
5273 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5274 * some situations it may be beneficial for an SCTP transmitter to be
5275 * more conservative than the algorithms detailed in this document
5276 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5277 * the following algorithms allow.
5278 */
5279sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5280 const struct sctp_association *asoc,
5281 const sctp_subtype_t type,
5282 void *arg,
5283 sctp_cmd_seq_t *commands)
5284{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005285 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5287 return SCTP_DISPOSITION_CONSUME;
5288}
5289
5290/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005291 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 *
5293 * Section: 4 Note: 2
5294 * Verification Tag:
5295 * Inputs
5296 * (endpoint, asoc)
5297 *
5298 * RFC 2960 Section 4 Notes
5299 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5300 * and re-start the T1-init timer without changing state. This MUST
5301 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5302 * endpoint MUST abort the initialization process and report the
5303 * error to SCTP user.
5304 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005305 * Outputs
5306 * (timers, events)
5307 *
5308 */
5309sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5310 const struct sctp_association *asoc,
5311 const sctp_subtype_t type,
5312 void *arg,
5313 sctp_cmd_seq_t *commands)
5314{
5315 struct sctp_chunk *repl = NULL;
5316 struct sctp_bind_addr *bp;
5317 int attempts = asoc->init_err_counter + 1;
5318
5319 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005320 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005321
Vlad Yasevich81845c22006-01-30 15:59:54 -08005322 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005323 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5324 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5325 if (!repl)
5326 return SCTP_DISPOSITION_NOMEM;
5327
5328 /* Choose transport for INIT. */
5329 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5330 SCTP_CHUNK(repl));
5331
5332 /* Issue a sideeffect to do the needed accounting. */
5333 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5334 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5335
5336 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5337 } else {
5338 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5339 " max_init_attempts: %d\n",
5340 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005341 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5342 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005343 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005344 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005345 return SCTP_DISPOSITION_DELETE_TCB;
5346 }
5347
5348 return SCTP_DISPOSITION_CONSUME;
5349}
5350
5351/*
5352 * sctp_sf_t1_cookie_timer_expire
5353 *
5354 * Section: 4 Note: 2
5355 * Verification Tag:
5356 * Inputs
5357 * (endpoint, asoc)
5358 *
5359 * RFC 2960 Section 4 Notes
5360 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 * COOKIE ECHO and re-start the T1-cookie timer without changing
5362 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5363 * After that, the endpoint MUST abort the initialization process and
5364 * report the error to SCTP user.
5365 *
5366 * Outputs
5367 * (timers, events)
5368 *
5369 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005370sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 const struct sctp_association *asoc,
5372 const sctp_subtype_t type,
5373 void *arg,
5374 sctp_cmd_seq_t *commands)
5375{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005376 struct sctp_chunk *repl = NULL;
5377 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Frank Filz3f7a87d2005-06-20 13:14:57 -07005379 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005380 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
Vlad Yasevich81845c22006-01-30 15:59:54 -08005382 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005383 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005385 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005387 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5388 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005390 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5391 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5394 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005395 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5396 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005398 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 return SCTP_DISPOSITION_DELETE_TCB;
5400 }
5401
5402 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403}
5404
5405/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5406 * with the updated last sequential TSN received from its peer.
5407 *
5408 * An endpoint should limit the number of retransmissions of the
5409 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5410 * If this threshold is exceeded the endpoint should destroy the TCB and
5411 * MUST report the peer endpoint unreachable to the upper layer (and
5412 * thus the association enters the CLOSED state). The reception of any
5413 * packet from its peer (i.e. as the peer sends all of its queued DATA
5414 * chunks) should clear the endpoint's retransmission count and restart
5415 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5416 * all of its queued DATA chunks that have not yet been sent.
5417 */
5418sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5419 const struct sctp_association *asoc,
5420 const sctp_subtype_t type,
5421 void *arg,
5422 sctp_cmd_seq_t *commands)
5423{
5424 struct sctp_chunk *reply = NULL;
5425
5426 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005427 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5428
Neil Horman58fbbed2008-02-29 11:40:56 -08005429 ((struct sctp_association *)asoc)->shutdown_retries++;
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005432 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5433 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5435 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005436 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5438 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5439 return SCTP_DISPOSITION_DELETE_TCB;
5440 }
5441
5442 switch (asoc->state) {
5443 case SCTP_STATE_SHUTDOWN_SENT:
5444 reply = sctp_make_shutdown(asoc, NULL);
5445 break;
5446
5447 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5448 reply = sctp_make_shutdown_ack(asoc, NULL);
5449 break;
5450
5451 default:
5452 BUG();
5453 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455
5456 if (!reply)
5457 goto nomem;
5458
Wei Yongjun6345b192009-04-26 23:13:35 +08005459 /* Do some failure management (Section 8.2).
5460 * If we remove the transport an SHUTDOWN was last sent to, don't
5461 * do failure management.
5462 */
5463 if (asoc->shutdown_last_sent_to)
5464 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5465 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
5467 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5468 * the T2-shutdown timer.
5469 */
5470 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5471
5472 /* Restart the T2-shutdown timer. */
5473 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5474 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5475 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5476 return SCTP_DISPOSITION_CONSUME;
5477
5478nomem:
5479 return SCTP_DISPOSITION_NOMEM;
5480}
5481
5482/*
5483 * ADDIP Section 4.1 ASCONF CHunk Procedures
5484 * If the T4 RTO timer expires the endpoint should do B1 to B5
5485 */
5486sctp_disposition_t sctp_sf_t4_timer_expire(
5487 const struct sctp_endpoint *ep,
5488 const struct sctp_association *asoc,
5489 const sctp_subtype_t type,
5490 void *arg,
5491 sctp_cmd_seq_t *commands)
5492{
5493 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5494 struct sctp_transport *transport = chunk->transport;
5495
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005496 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5499 * detection on the appropriate destination address as defined in
5500 * RFC2960 [5] section 8.1 and 8.2.
5501 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005502 if (transport)
5503 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5504 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505
5506 /* Reconfig T4 timer and transport. */
5507 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5508
5509 /* ADDIP 4.1 B2) Increment the association error counters and perform
5510 * endpoint failure detection on the association as defined in
5511 * RFC2960 [5] section 8.1 and 8.2.
5512 * association error counter is incremented in SCTP_CMD_STRIKE.
5513 */
5514 if (asoc->overall_error_count >= asoc->max_retrans) {
5515 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5516 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005517 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5518 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005520 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjun3d5a0192008-10-09 14:32:24 -07005522 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 return SCTP_DISPOSITION_ABORT;
5524 }
5525
5526 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5527 * the ASCONF chunk was sent by doubling the RTO timer value.
5528 * This is done in SCTP_CMD_STRIKE.
5529 */
5530
5531 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5532 * choose an alternate destination address (please refer to RFC2960
5533 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005534 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 * ASCONF sent.
5536 */
5537 sctp_chunk_hold(asoc->addip_last_asconf);
5538 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5539 SCTP_CHUNK(asoc->addip_last_asconf));
5540
5541 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5542 * destination is selected, then the RTO used will be that of the new
5543 * destination address.
5544 */
5545 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5546 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5547
5548 return SCTP_DISPOSITION_CONSUME;
5549}
5550
5551/* sctpimpguide-05 Section 2.12.2
5552 * The sender of the SHUTDOWN MAY also start an overall guard timer
5553 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5554 * At the expiration of this timer the sender SHOULD abort the association
5555 * by sending an ABORT chunk.
5556 */
5557sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5558 const struct sctp_association *asoc,
5559 const sctp_subtype_t type,
5560 void *arg,
5561 sctp_cmd_seq_t *commands)
5562{
5563 struct sctp_chunk *reply = NULL;
5564
5565 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005566 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
5568 reply = sctp_make_abort(asoc, NULL, 0);
5569 if (!reply)
5570 goto nomem;
5571
5572 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005573 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5574 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005576 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005578 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5579 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5580
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 return SCTP_DISPOSITION_DELETE_TCB;
5582nomem:
5583 return SCTP_DISPOSITION_NOMEM;
5584}
5585
5586/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5587 * the association is automatically closed by starting the shutdown process.
5588 * The work that needs to be done is same as when SHUTDOWN is initiated by
5589 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5590 */
5591sctp_disposition_t sctp_sf_autoclose_timer_expire(
5592 const struct sctp_endpoint *ep,
5593 const struct sctp_association *asoc,
5594 const sctp_subtype_t type,
5595 void *arg,
5596 sctp_cmd_seq_t *commands)
5597{
5598 int disposition;
5599
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005600 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5601
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 /* From 9.2 Shutdown of an Association
5603 * Upon receipt of the SHUTDOWN primitive from its upper
5604 * layer, the endpoint enters SHUTDOWN-PENDING state and
5605 * remains there until all outstanding data has been
5606 * acknowledged by its peer. The endpoint accepts no new data
5607 * from its upper layer, but retransmits data to the far end
5608 * if necessary to fill gaps.
5609 */
5610 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5611 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 disposition = SCTP_DISPOSITION_CONSUME;
5614 if (sctp_outq_is_empty(&asoc->outqueue)) {
5615 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5616 arg, commands);
5617 }
5618 return disposition;
5619}
5620
5621/*****************************************************************************
5622 * These are sa state functions which could apply to all types of events.
5623 ****************************************************************************/
5624
5625/*
5626 * This table entry is not implemented.
5627 *
5628 * Inputs
5629 * (endpoint, asoc, chunk)
5630 *
5631 * The return value is the disposition of the chunk.
5632 */
5633sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5634 const struct sctp_association *asoc,
5635 const sctp_subtype_t type,
5636 void *arg,
5637 sctp_cmd_seq_t *commands)
5638{
5639 return SCTP_DISPOSITION_NOT_IMPL;
5640}
5641
5642/*
5643 * This table entry represents a bug.
5644 *
5645 * Inputs
5646 * (endpoint, asoc, chunk)
5647 *
5648 * The return value is the disposition of the chunk.
5649 */
5650sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5651 const struct sctp_association *asoc,
5652 const sctp_subtype_t type,
5653 void *arg,
5654 sctp_cmd_seq_t *commands)
5655{
5656 return SCTP_DISPOSITION_BUG;
5657}
5658
5659/*
5660 * This table entry represents the firing of a timer in the wrong state.
5661 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5662 * when the association is in the wrong state. This event should
5663 * be ignored, so as to prevent any rearming of the timer.
5664 *
5665 * Inputs
5666 * (endpoint, asoc, chunk)
5667 *
5668 * The return value is the disposition of the chunk.
5669 */
5670sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5671 const struct sctp_association *asoc,
5672 const sctp_subtype_t type,
5673 void *arg,
5674 sctp_cmd_seq_t *commands)
5675{
5676 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5677 return SCTP_DISPOSITION_CONSUME;
5678}
5679
5680/********************************************************************
5681 * 2nd Level Abstractions
5682 ********************************************************************/
5683
5684/* Pull the SACK chunk based on the SACK header. */
5685static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5686{
5687 struct sctp_sackhdr *sack;
5688 unsigned int len;
5689 __u16 num_blocks;
5690 __u16 num_dup_tsns;
5691
5692 /* Protect ourselves from reading too far into
5693 * the skb from a bogus sender.
5694 */
5695 sack = (struct sctp_sackhdr *) chunk->skb->data;
5696
5697 num_blocks = ntohs(sack->num_gap_ack_blocks);
5698 num_dup_tsns = ntohs(sack->num_dup_tsns);
5699 len = sizeof(struct sctp_sackhdr);
5700 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5701 if (len > chunk->skb->len)
5702 return NULL;
5703
5704 skb_pull(chunk->skb, len);
5705
5706 return sack;
5707}
5708
5709/* Create an ABORT packet to be sent as a response, with the specified
5710 * error causes.
5711 */
5712static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5713 const struct sctp_association *asoc,
5714 struct sctp_chunk *chunk,
5715 const void *payload,
5716 size_t paylen)
5717{
5718 struct sctp_packet *packet;
5719 struct sctp_chunk *abort;
5720
5721 packet = sctp_ootb_pkt_new(asoc, chunk);
5722
5723 if (packet) {
5724 /* Make an ABORT.
5725 * The T bit will be set if the asoc is NULL.
5726 */
5727 abort = sctp_make_abort(asoc, chunk, paylen);
5728 if (!abort) {
5729 sctp_ootb_pkt_free(packet);
5730 return NULL;
5731 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005732
5733 /* Reflect vtag if T-Bit is set */
5734 if (sctp_test_T_bit(abort))
5735 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5736
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 /* Add specified error causes, i.e., payload, to the
5738 * end of the chunk.
5739 */
5740 sctp_addto_chunk(abort, paylen, payload);
5741
5742 /* Set the skb to the belonging sock for accounting. */
5743 abort->skb->sk = ep->base.sk;
5744
5745 sctp_packet_append_chunk(packet, abort);
5746
5747 }
5748
5749 return packet;
5750}
5751
5752/* Allocate a packet for responding in the OOTB conditions. */
5753static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5754 const struct sctp_chunk *chunk)
5755{
5756 struct sctp_packet *packet;
5757 struct sctp_transport *transport;
5758 __u16 sport;
5759 __u16 dport;
5760 __u32 vtag;
5761
5762 /* Get the source and destination port from the inbound packet. */
5763 sport = ntohs(chunk->sctp_hdr->dest);
5764 dport = ntohs(chunk->sctp_hdr->source);
5765
5766 /* The V-tag is going to be the same as the inbound packet if no
5767 * association exists, otherwise, use the peer's vtag.
5768 */
5769 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005770 /* Special case the INIT-ACK as there is no peer's vtag
5771 * yet.
5772 */
5773 switch(chunk->chunk_hdr->type) {
5774 case SCTP_CID_INIT_ACK:
5775 {
5776 sctp_initack_chunk_t *initack;
5777
5778 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5779 vtag = ntohl(initack->init_hdr.init_tag);
5780 break;
5781 }
5782 default:
5783 vtag = asoc->peer.i.init_tag;
5784 break;
5785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 } else {
5787 /* Special case the INIT and stale COOKIE_ECHO as there is no
5788 * vtag yet.
5789 */
5790 switch(chunk->chunk_hdr->type) {
5791 case SCTP_CID_INIT:
5792 {
5793 sctp_init_chunk_t *init;
5794
5795 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5796 vtag = ntohl(init->init_hdr.init_tag);
5797 break;
5798 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005799 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 vtag = ntohl(chunk->sctp_hdr->vtag);
5801 break;
5802 }
5803 }
5804
5805 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005806 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 if (!transport)
5808 goto nomem;
5809
5810 /* Cache a route for the transport with the chunk's destination as
5811 * the source address.
5812 */
Al Viro16b0a032006-11-20 17:13:38 -08005813 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 sctp_sk(sctp_get_ctl_sock()));
5815
5816 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5817 packet = sctp_packet_config(packet, vtag, 0);
5818
5819 return packet;
5820
5821nomem:
5822 return NULL;
5823}
5824
5825/* Free the packet allocated earlier for responding in the OOTB condition. */
5826void sctp_ootb_pkt_free(struct sctp_packet *packet)
5827{
5828 sctp_transport_free(packet->transport);
5829}
5830
5831/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5832static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5833 const struct sctp_association *asoc,
5834 const struct sctp_chunk *chunk,
5835 sctp_cmd_seq_t *commands,
5836 struct sctp_chunk *err_chunk)
5837{
5838 struct sctp_packet *packet;
5839
5840 if (err_chunk) {
5841 packet = sctp_ootb_pkt_new(asoc, chunk);
5842 if (packet) {
5843 struct sctp_signed_cookie *cookie;
5844
5845 /* Override the OOTB vtag from the cookie. */
5846 cookie = chunk->subh.cookie_hdr;
5847 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 /* Set the skb to the belonging sock for accounting. */
5850 err_chunk->skb->sk = ep->base.sk;
5851 sctp_packet_append_chunk(packet, err_chunk);
5852 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5853 SCTP_PACKET(packet));
5854 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5855 } else
5856 sctp_chunk_free (err_chunk);
5857 }
5858}
5859
5860
5861/* Process a data chunk */
5862static int sctp_eat_data(const struct sctp_association *asoc,
5863 struct sctp_chunk *chunk,
5864 sctp_cmd_seq_t *commands)
5865{
5866 sctp_datahdr_t *data_hdr;
5867 struct sctp_chunk *err;
5868 size_t datalen;
5869 sctp_verb_t deliver;
5870 int tmp;
5871 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005872 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005873 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
5875 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5876 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5877
5878 tsn = ntohl(data_hdr->tsn);
5879 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5880
5881 /* ASSERT: Now skb->data is really the user data. */
5882
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 /* Process ECN based congestion.
5884 *
5885 * Since the chunk structure is reused for all chunks within
5886 * a packet, we use ecn_ce_done to track if we've already
5887 * done CE processing for this packet.
5888 *
5889 * We need to do ECN processing even if we plan to discard the
5890 * chunk later.
5891 */
5892
5893 if (!chunk->ecn_ce_done) {
5894 struct sctp_af *af;
5895 chunk->ecn_ce_done = 1;
5896
5897 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005898 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
5900 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5901 /* Do real work as sideffect. */
5902 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5903 SCTP_U32(tsn));
5904 }
5905 }
5906
5907 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5908 if (tmp < 0) {
5909 /* The TSN is too high--silently discard the chunk and
5910 * count on it getting retransmitted later.
5911 */
5912 return SCTP_IERROR_HIGH_TSN;
5913 } else if (tmp > 0) {
5914 /* This is a duplicate. Record it. */
5915 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5916 return SCTP_IERROR_DUP_TSN;
5917 }
5918
5919 /* This is a new TSN. */
5920
5921 /* Discard if there is no room in the receive window.
5922 * Actually, allow a little bit of overflow (up to a MTU).
5923 */
5924 datalen = ntohs(chunk->chunk_hdr->length);
5925 datalen -= sizeof(sctp_data_chunk_t);
5926
5927 deliver = SCTP_CMD_CHUNK_ULP;
5928
5929 /* Think about partial delivery. */
5930 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5931
5932 /* Even if we don't accept this chunk there is
5933 * memory pressure.
5934 */
5935 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5936 }
5937
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005938 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 * seems a bit troublesome in that frag_point varies based on
5940 * PMTU. In cases, such as loopback, this might be a rather
5941 * large spill over.
5942 */
Neil Horman4d93df02007-08-15 16:07:44 -07005943 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5944 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
5946 /* If this is the next TSN, consider reneging to make
5947 * room. Note: Playing nice with a confused sender. A
5948 * malicious sender can still eat up all our buffer
5949 * space and in the future we may want to detect and
5950 * do more drastic reneging.
5951 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005952 if (sctp_tsnmap_has_gap(map) &&
5953 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5955 deliver = SCTP_CMD_RENEGE;
5956 } else {
5957 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5958 "rwnd: %d\n", tsn, datalen,
5959 asoc->rwnd);
5960 return SCTP_IERROR_IGNORE_TSN;
5961 }
5962 }
5963
5964 /*
Neil Horman4d93df02007-08-15 16:07:44 -07005965 * Also try to renege to limit our memory usage in the event that
5966 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08005967 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07005968 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5969 * memory usage too much
5970 */
5971 if (*sk->sk_prot_creator->memory_pressure) {
5972 if (sctp_tsnmap_has_gap(map) &&
5973 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5974 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5975 deliver = SCTP_CMD_RENEGE;
5976 }
5977 }
5978
5979 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005980 * Section 3.3.10.9 No User Data (9)
5981 *
5982 * Cause of error
5983 * ---------------
5984 * No User Data: This error cause is returned to the originator of a
5985 * DATA chunk if a received DATA chunk has no user data.
5986 */
5987 if (unlikely(0 == datalen)) {
5988 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5989 if (err) {
5990 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5991 SCTP_CHUNK(err));
5992 }
5993 /* We are going to ABORT, so we might as well stop
5994 * processing the rest of the chunks in the packet.
5995 */
5996 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005997 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5998 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08006000 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6002 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6003 return SCTP_IERROR_NO_DATA;
6004 }
6005
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006006 chunk->data_accepted = 1;
6007
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6009 * if we renege and the chunk arrives again.
6010 */
6011 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6012 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6013 else
6014 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6015
6016 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6017 *
6018 * If an endpoint receive a DATA chunk with an invalid stream
6019 * identifier, it shall acknowledge the reception of the DATA chunk
6020 * following the normal procedure, immediately send an ERROR chunk
6021 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6022 * and discard the DATA chunk.
6023 */
6024 if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006025 /* Mark tsn as received even though we drop it */
6026 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6027
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6029 &data_hdr->stream,
6030 sizeof(data_hdr->stream));
6031 if (err)
6032 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6033 SCTP_CHUNK(err));
6034 return SCTP_IERROR_BAD_STREAM;
6035 }
6036
6037 /* Send the data up to the user. Note: Schedule the
6038 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6039 * chunk needs the updated rwnd.
6040 */
6041 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6042
6043 return SCTP_IERROR_NO_ERROR;
6044}