blob: 73bdeb2b6c62302f0fe8ef0621b5ac2b6383723f [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 Yasevichfd102792007-11-15 12:13:32 -0500974 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. */
1118 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1119 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1120 commands);
1121
1122 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
Vladislav Yasevicha6012662006-05-19 14:25:53 -07001123 /* Make sure that the length of the parameter is what we expect */
1124 if (ntohs(hbinfo->param_hdr.length) !=
1125 sizeof(sctp_sender_hb_info_t)) {
1126 return SCTP_DISPOSITION_DISCARD;
1127 }
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 from_addr = hbinfo->daddr;
Al Viro63de08f2006-11-20 17:07:25 -08001130 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 /* This should never happen, but lets log it if so. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07001133 if (unlikely(!link)) {
1134 if (from_addr.sa.sa_family == AF_INET6) {
Wei Yongjund99fa422007-08-27 11:19:24 +08001135 if (net_ratelimit())
1136 printk(KERN_WARNING
Harvey Harrison5b095d9892008-10-29 12:52:50 -07001137 "%s association %p could not find address %pI6\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001138 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001139 asoc,
Harvey Harrisonfdb46ee2008-10-28 16:10:17 -07001140 &from_addr.v6.sin6_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001141 } else {
Wei Yongjund99fa422007-08-27 11:19:24 +08001142 if (net_ratelimit())
1143 printk(KERN_WARNING
Harvey Harrison21454aa2008-10-31 00:54:56 -07001144 "%s association %p could not find address %pI4\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001145 __func__,
Wei Yongjund99fa422007-08-27 11:19:24 +08001146 asoc,
Harvey Harrison21454aa2008-10-31 00:54:56 -07001147 &from_addr.v4.sin_addr.s_addr);
Frank Filz3f7a87d2005-06-20 13:14:57 -07001148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return SCTP_DISPOSITION_DISCARD;
1150 }
1151
Sridhar Samudralaad8fec12006-07-21 14:48:50 -07001152 /* Validate the 64-bit random nonce. */
1153 if (hbinfo->hb_nonce != link->hb_nonce)
1154 return SCTP_DISPOSITION_DISCARD;
1155
Frank Filz52ccb8e2005-12-22 11:36:46 -08001156 max_interval = link->hbinterval + link->rto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* Check if the timestamp looks valid. */
1159 if (time_after(hbinfo->sent_at, jiffies) ||
1160 time_after(jiffies, hbinfo->sent_at + max_interval)) {
Joe Perches9ee46f12007-11-19 23:47:47 -08001161 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 "received for transport: %p\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001163 __func__, link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 return SCTP_DISPOSITION_DISCARD;
1165 }
1166
1167 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1168 * the HEARTBEAT should clear the error counter of the
1169 * destination transport address to which the HEARTBEAT was
1170 * sent and mark the destination transport address as active if
1171 * it is not so marked.
1172 */
1173 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1174
1175 return SCTP_DISPOSITION_CONSUME;
1176}
1177
1178/* Helper function to send out an abort for the restart
1179 * condition.
1180 */
1181static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1182 struct sctp_chunk *init,
1183 sctp_cmd_seq_t *commands)
1184{
1185 int len;
1186 struct sctp_packet *pkt;
1187 union sctp_addr_param *addrparm;
1188 struct sctp_errhdr *errhdr;
1189 struct sctp_endpoint *ep;
1190 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1191 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1192
1193 /* Build the error on the stack. We are way to malloc crazy
1194 * throughout the code today.
1195 */
1196 errhdr = (struct sctp_errhdr *)buffer;
1197 addrparm = (union sctp_addr_param *)errhdr->variable;
1198
1199 /* Copy into a parm format. */
1200 len = af->to_addr_param(ssa, addrparm);
1201 len += sizeof(sctp_errhdr_t);
1202
1203 errhdr->cause = SCTP_ERROR_RESTART;
1204 errhdr->length = htons(len);
1205
1206 /* Assign to the control socket. */
1207 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1208
1209 /* Association is NULL since this may be a restart attack and we
1210 * want to send back the attacker's vtag.
1211 */
1212 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1213
1214 if (!pkt)
1215 goto out;
1216 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1217
1218 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1219
1220 /* Discard the rest of the inbound packet. */
1221 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1222
1223out:
1224 /* Even if there is no memory, treat as a failure so
1225 * the packet will get dropped.
1226 */
1227 return 0;
1228}
1229
1230/* A restart is occurring, check to make sure no new addresses
1231 * are being added as we may be under a takeover attack.
1232 */
1233static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1234 const struct sctp_association *asoc,
1235 struct sctp_chunk *init,
1236 sctp_cmd_seq_t *commands)
1237{
1238 struct sctp_transport *new_addr, *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int found;
1240
1241 /* Implementor's Guide - Sectin 5.2.2
1242 * ...
1243 * Before responding the endpoint MUST check to see if the
1244 * unexpected INIT adds new addresses to the association. If new
1245 * addresses are added to the association, the endpoint MUST respond
1246 * with an ABORT..
1247 */
1248
1249 /* Search through all current addresses and make sure
1250 * we aren't adding any new ones.
1251 */
1252 new_addr = NULL;
1253 found = 0;
1254
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001255 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1256 transports) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 found = 0;
Robert P. J. Day9dbc15f2008-04-12 18:54:24 -07001258 list_for_each_entry(addr, &asoc->peer.transport_addr_list,
1259 transports) {
Al Viro5f242a132006-11-20 17:05:23 -08001260 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1261 &addr->ipaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 found = 1;
1263 break;
1264 }
1265 }
1266 if (!found)
1267 break;
1268 }
1269
1270 /* If a new address was added, ABORT the sender. */
1271 if (!found && new_addr) {
Al Viro7dd8a582006-11-20 17:23:25 -08001272 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
1275 /* Return success if all addresses were found. */
1276 return found;
1277}
1278
1279/* Populate the verification/tie tags based on overlapping INIT
1280 * scenario.
1281 *
1282 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1283 */
1284static void sctp_tietags_populate(struct sctp_association *new_asoc,
1285 const struct sctp_association *asoc)
1286{
1287 switch (asoc->state) {
1288
1289 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1290
1291 case SCTP_STATE_COOKIE_WAIT:
1292 new_asoc->c.my_vtag = asoc->c.my_vtag;
1293 new_asoc->c.my_ttag = asoc->c.my_vtag;
1294 new_asoc->c.peer_ttag = 0;
1295 break;
1296
1297 case SCTP_STATE_COOKIE_ECHOED:
1298 new_asoc->c.my_vtag = asoc->c.my_vtag;
1299 new_asoc->c.my_ttag = asoc->c.my_vtag;
1300 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1301 break;
1302
1303 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1304 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1305 */
1306 default:
1307 new_asoc->c.my_ttag = asoc->c.my_vtag;
1308 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1309 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /* Other parameters for the endpoint SHOULD be copied from the
1313 * existing parameters of the association (e.g. number of
1314 * outbound streams) into the INIT ACK and cookie.
1315 */
1316 new_asoc->rwnd = asoc->rwnd;
1317 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1318 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1319 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1320}
1321
1322/*
1323 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1324 * handling action.
1325 *
1326 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1327 *
1328 * Returns value representing action to be taken. These action values
1329 * correspond to Action/Description values in RFC 2960, Table 2.
1330 */
1331static char sctp_tietags_compare(struct sctp_association *new_asoc,
1332 const struct sctp_association *asoc)
1333{
1334 /* In this case, the peer may have restarted. */
1335 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1336 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1337 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1338 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1339 return 'A';
1340
1341 /* Collision case B. */
1342 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1343 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1344 (0 == asoc->c.peer_vtag))) {
1345 return 'B';
1346 }
1347
1348 /* Collision case D. */
1349 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1350 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1351 return 'D';
1352
1353 /* Collision case C. */
1354 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1355 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1356 (0 == new_asoc->c.my_ttag) &&
1357 (0 == new_asoc->c.peer_ttag))
1358 return 'C';
1359
1360 /* No match to any of the special cases; discard this packet. */
1361 return 'E';
1362}
1363
1364/* Common helper routine for both duplicate and simulataneous INIT
1365 * chunk handling.
1366 */
1367static sctp_disposition_t sctp_sf_do_unexpected_init(
1368 const struct sctp_endpoint *ep,
1369 const struct sctp_association *asoc,
1370 const sctp_subtype_t type,
1371 void *arg, sctp_cmd_seq_t *commands)
1372{
1373 sctp_disposition_t retval;
1374 struct sctp_chunk *chunk = arg;
1375 struct sctp_chunk *repl;
1376 struct sctp_association *new_asoc;
1377 struct sctp_chunk *err_chunk;
1378 struct sctp_packet *packet;
1379 sctp_unrecognized_param_t *unk_param;
1380 int len;
1381
1382 /* 6.10 Bundling
1383 * An endpoint MUST NOT bundle INIT, INIT ACK or
1384 * SHUTDOWN COMPLETE with any other chunks.
1385 *
1386 * IG Section 2.11.2
1387 * Furthermore, we require that the receiver of an INIT chunk MUST
1388 * enforce these rules by silently discarding an arriving packet
1389 * with an INIT chunk that is bundled with other chunks.
1390 */
1391 if (!chunk->singleton)
1392 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1393
1394 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001395 * Tag.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
1397 if (chunk->sctp_hdr->vtag != 0)
1398 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1399
1400 /* Make sure that the INIT chunk has a valid length.
1401 * In this case, we generate a protocol violation since we have
1402 * an association established.
1403 */
1404 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1405 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1406 commands);
1407 /* Grab the INIT header. */
1408 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1409
1410 /* Tag the variable length parameters. */
1411 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1412
1413 /* Verify the INIT chunk before processing it. */
1414 err_chunk = NULL;
1415 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1416 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1417 &err_chunk)) {
1418 /* This chunk contains fatal error. It is to be discarded.
1419 * Send an ABORT, with causes if there is any.
1420 */
1421 if (err_chunk) {
1422 packet = sctp_abort_pkt_new(ep, asoc, arg,
1423 (__u8 *)(err_chunk->chunk_hdr) +
1424 sizeof(sctp_chunkhdr_t),
1425 ntohs(err_chunk->chunk_hdr->length) -
1426 sizeof(sctp_chunkhdr_t));
1427
1428 if (packet) {
1429 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1430 SCTP_PACKET(packet));
1431 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1432 retval = SCTP_DISPOSITION_CONSUME;
1433 } else {
1434 retval = SCTP_DISPOSITION_NOMEM;
1435 }
1436 goto cleanup;
1437 } else {
1438 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1439 commands);
1440 }
1441 }
1442
1443 /*
1444 * Other parameters for the endpoint SHOULD be copied from the
1445 * existing parameters of the association (e.g. number of
1446 * outbound streams) into the INIT ACK and cookie.
1447 * FIXME: We are copying parameters from the endpoint not the
1448 * association.
1449 */
1450 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1451 if (!new_asoc)
1452 goto nomem;
1453
1454 /* In the outbound INIT ACK the endpoint MUST copy its current
1455 * Verification Tag and Peers Verification tag into a reserved
1456 * place (local tie-tag and per tie-tag) within the state cookie.
1457 */
1458 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1459 sctp_source(chunk),
1460 (sctp_init_chunk_t *)chunk->chunk_hdr,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001461 GFP_ATOMIC))
1462 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* Make sure no new addresses are being added during the
1465 * restart. Do not do this check for COOKIE-WAIT state,
1466 * since there are no peer addresses to check against.
1467 * Upon return an ABORT will have been sent if needed.
1468 */
1469 if (!sctp_state(asoc, COOKIE_WAIT)) {
1470 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1471 commands)) {
1472 retval = SCTP_DISPOSITION_CONSUME;
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001473 goto nomem_retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475 }
1476
1477 sctp_tietags_populate(new_asoc, asoc);
1478
1479 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1480
1481 /* If there are errors need to be reported for unknown parameters,
1482 * make sure to reserve enough room in the INIT ACK for them.
1483 */
1484 len = 0;
1485 if (err_chunk) {
1486 len = ntohs(err_chunk->chunk_hdr->length) -
1487 sizeof(sctp_chunkhdr_t);
1488 }
1489
1490 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1491 goto nomem;
1492
1493 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1494 if (!repl)
1495 goto nomem;
1496
1497 /* If there are errors need to be reported for unknown parameters,
1498 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1499 * parameter.
1500 */
1501 if (err_chunk) {
1502 /* Get the "Unrecognized parameter" parameter(s) out of the
1503 * ERROR chunk generated by sctp_verify_init(). Since the
1504 * error cause code for "unknown parameter" and the
1505 * "Unrecognized parameter" type is the same, we can
1506 * construct the parameters in INIT ACK by copying the
1507 * ERROR causes over.
1508 */
1509 unk_param = (sctp_unrecognized_param_t *)
1510 ((__u8 *)(err_chunk->chunk_hdr) +
1511 sizeof(sctp_chunkhdr_t));
1512 /* Replace the cause code with the "Unrecognized parameter"
1513 * parameter type.
1514 */
1515 sctp_addto_chunk(repl, len, unk_param);
1516 }
1517
1518 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1519 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1520
1521 /*
1522 * Note: After sending out INIT ACK with the State Cookie parameter,
1523 * "Z" MUST NOT allocate any resources for this new association.
1524 * Otherwise, "Z" will be vulnerable to resource attacks.
1525 */
1526 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1527 retval = SCTP_DISPOSITION_CONSUME;
1528
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001529 return retval;
1530
1531nomem:
1532 retval = SCTP_DISPOSITION_NOMEM;
1533nomem_retval:
1534 if (new_asoc)
1535 sctp_association_free(new_asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536cleanup:
1537 if (err_chunk)
1538 sctp_chunk_free(err_chunk);
1539 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542/*
1543 * Handle simultanous INIT.
1544 * This means we started an INIT and then we got an INIT request from
1545 * our peer.
1546 *
1547 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1548 * This usually indicates an initialization collision, i.e., each
1549 * endpoint is attempting, at about the same time, to establish an
1550 * association with the other endpoint.
1551 *
1552 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1553 * endpoint MUST respond with an INIT ACK using the same parameters it
1554 * sent in its original INIT chunk (including its Verification Tag,
1555 * unchanged). These original parameters are combined with those from the
1556 * newly received INIT chunk. The endpoint shall also generate a State
1557 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1558 * INIT to calculate the State Cookie.
1559 *
1560 * After that, the endpoint MUST NOT change its state, the T1-init
1561 * timer shall be left running and the corresponding TCB MUST NOT be
1562 * destroyed. The normal procedures for handling State Cookies when
1563 * a TCB exists will resolve the duplicate INITs to a single association.
1564 *
1565 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1566 * its Tie-Tags with the Tag information of itself and its peer (see
1567 * section 5.2.2 for a description of the Tie-Tags).
1568 *
1569 * Verification Tag: Not explicit, but an INIT can not have a valid
1570 * verification tag, so we skip the check.
1571 *
1572 * Inputs
1573 * (endpoint, asoc, chunk)
1574 *
1575 * Outputs
1576 * (asoc, reply_msg, msg_up, timers, counters)
1577 *
1578 * The return value is the disposition of the chunk.
1579 */
1580sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1581 const struct sctp_association *asoc,
1582 const sctp_subtype_t type,
1583 void *arg,
1584 sctp_cmd_seq_t *commands)
1585{
1586 /* Call helper to do the real work for both simulataneous and
1587 * duplicate INIT chunk handling.
1588 */
1589 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1590}
1591
1592/*
1593 * Handle duplicated INIT messages. These are usually delayed
1594 * restransmissions.
1595 *
1596 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1597 * COOKIE-ECHOED and COOKIE-WAIT
1598 *
1599 * Unless otherwise stated, upon reception of an unexpected INIT for
1600 * this association, the endpoint shall generate an INIT ACK with a
1601 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1602 * current Verification Tag and peer's Verification Tag into a reserved
1603 * place within the state cookie. We shall refer to these locations as
1604 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1605 * containing this INIT ACK MUST carry a Verification Tag value equal to
1606 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1607 * MUST contain a new Initiation Tag (randomly generated see Section
1608 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1609 * existing parameters of the association (e.g. number of outbound
1610 * streams) into the INIT ACK and cookie.
1611 *
1612 * After sending out the INIT ACK, the endpoint shall take no further
1613 * actions, i.e., the existing association, including its current state,
1614 * and the corresponding TCB MUST NOT be changed.
1615 *
1616 * Note: Only when a TCB exists and the association is not in a COOKIE-
1617 * WAIT state are the Tie-Tags populated. For a normal association INIT
1618 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1619 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1620 * State Cookie are populated as specified in section 5.2.1.
1621 *
1622 * Verification Tag: Not specified, but an INIT has no way of knowing
1623 * what the verification tag could be, so we ignore it.
1624 *
1625 * Inputs
1626 * (endpoint, asoc, chunk)
1627 *
1628 * Outputs
1629 * (asoc, reply_msg, msg_up, timers, counters)
1630 *
1631 * The return value is the disposition of the chunk.
1632 */
1633sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1634 const struct sctp_association *asoc,
1635 const sctp_subtype_t type,
1636 void *arg,
1637 sctp_cmd_seq_t *commands)
1638{
1639 /* Call helper to do the real work for both simulataneous and
1640 * duplicate INIT chunk handling.
1641 */
1642 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1643}
1644
1645
Vlad Yasevich610ab732007-01-15 19:18:30 -08001646/*
1647 * Unexpected INIT-ACK handler.
1648 *
1649 * Section 5.2.3
1650 * If an INIT ACK received by an endpoint in any state other than the
1651 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1652 * An unexpected INIT ACK usually indicates the processing of an old or
1653 * duplicated INIT chunk.
1654*/
1655sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1656 const struct sctp_association *asoc,
1657 const sctp_subtype_t type,
1658 void *arg, sctp_cmd_seq_t *commands)
1659{
1660 /* Per the above section, we'll discard the chunk if we have an
1661 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1662 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001663 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
Vlad Yasevich610ab732007-01-15 19:18:30 -08001664 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1665 else
1666 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1667}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669/* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1670 *
1671 * Section 5.2.4
1672 * A) In this case, the peer may have restarted.
1673 */
1674static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1675 const struct sctp_association *asoc,
1676 struct sctp_chunk *chunk,
1677 sctp_cmd_seq_t *commands,
1678 struct sctp_association *new_asoc)
1679{
1680 sctp_init_chunk_t *peer_init;
1681 struct sctp_ulpevent *ev;
1682 struct sctp_chunk *repl;
1683 struct sctp_chunk *err;
1684 sctp_disposition_t disposition;
1685
1686 /* new_asoc is a brand-new association, so these are not yet
1687 * side effects--it is safe to run them here.
1688 */
1689 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1690
1691 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1692 sctp_source(chunk), peer_init,
1693 GFP_ATOMIC))
1694 goto nomem;
1695
1696 /* Make sure no new addresses are being added during the
1697 * restart. Though this is a pretty complicated attack
1698 * since you'd have to get inside the cookie.
1699 */
1700 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1701 return SCTP_DISPOSITION_CONSUME;
1702 }
1703
1704 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1705 * the peer has restarted (Action A), it MUST NOT setup a new
1706 * association but instead resend the SHUTDOWN ACK and send an ERROR
1707 * chunk with a "Cookie Received while Shutting Down" error cause to
1708 * its peer.
1709 */
1710 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1711 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1712 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1713 chunk, commands);
1714 if (SCTP_DISPOSITION_NOMEM == disposition)
1715 goto nomem;
1716
1717 err = sctp_make_op_error(asoc, chunk,
1718 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1719 NULL, 0);
1720 if (err)
1721 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1722 SCTP_CHUNK(err));
1723
1724 return SCTP_DISPOSITION_CONSUME;
1725 }
1726
1727 /* For now, fail any unsent/unacked data. Consider the optional
1728 * choice of resending of this data.
1729 */
1730 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 repl = sctp_make_cookie_ack(new_asoc, chunk);
1733 if (!repl)
1734 goto nomem;
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 /* Report association restart to upper layer. */
1737 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1738 new_asoc->c.sinit_num_ostreams,
1739 new_asoc->c.sinit_max_instreams,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07001740 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 if (!ev)
1742 goto nomem_ev;
1743
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001744 /* Update the content of current association. */
1745 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1746 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1748 return SCTP_DISPOSITION_CONSUME;
1749
1750nomem_ev:
1751 sctp_chunk_free(repl);
1752nomem:
1753 return SCTP_DISPOSITION_NOMEM;
1754}
1755
1756/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1757 *
1758 * Section 5.2.4
1759 * B) In this case, both sides may be attempting to start an association
1760 * at about the same time but the peer endpoint started its INIT
1761 * after responding to the local endpoint's INIT
1762 */
1763/* This case represents an initialization collision. */
1764static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1765 const struct sctp_association *asoc,
1766 struct sctp_chunk *chunk,
1767 sctp_cmd_seq_t *commands,
1768 struct sctp_association *new_asoc)
1769{
1770 sctp_init_chunk_t *peer_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct sctp_chunk *repl;
1772
1773 /* new_asoc is a brand-new association, so these are not yet
1774 * side effects--it is safe to run them here.
1775 */
1776 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1777 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1778 sctp_source(chunk), peer_init,
1779 GFP_ATOMIC))
1780 goto nomem;
1781
1782 /* Update the content of current association. */
1783 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1784 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1785 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1786 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1787 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1788
1789 repl = sctp_make_cookie_ack(new_asoc, chunk);
1790 if (!repl)
1791 goto nomem;
1792
1793 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /* RFC 2960 5.1 Normal Establishment of an Association
1796 *
1797 * D) IMPLEMENTATION NOTE: An implementation may choose to
1798 * send the Communication Up notification to the SCTP user
1799 * upon reception of a valid COOKIE ECHO chunk.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001800 *
1801 * Sadly, this needs to be implemented as a side-effect, because
1802 * we are not guaranteed to have set the association id of the real
1803 * association and so these notifications need to be delayed until
1804 * the association id is allocated.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Vlad Yasevich07d93962007-05-04 13:55:27 -07001807 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001810 * When a peer sends a Adaptation Layer Indication parameter , SCTP
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 * delivers this notification to inform the application that of the
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001812 * peers requested adaptation layer.
Vlad Yasevich07d93962007-05-04 13:55:27 -07001813 *
1814 * This also needs to be done as a side effect for the same reason as
1815 * above.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 */
Vlad Yasevich07d93962007-05-04 13:55:27 -07001817 if (asoc->peer.adaptation_ind)
1818 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 return SCTP_DISPOSITION_CONSUME;
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822nomem:
1823 return SCTP_DISPOSITION_NOMEM;
1824}
1825
1826/* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1827 *
1828 * Section 5.2.4
1829 * C) In this case, the local endpoint's cookie has arrived late.
1830 * Before it arrived, the local endpoint sent an INIT and received an
1831 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1832 * but a new tag of its own.
1833 */
1834/* This case represents an initialization collision. */
1835static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1836 const struct sctp_association *asoc,
1837 struct sctp_chunk *chunk,
1838 sctp_cmd_seq_t *commands,
1839 struct sctp_association *new_asoc)
1840{
1841 /* The cookie should be silently discarded.
1842 * The endpoint SHOULD NOT change states and should leave
1843 * any timers running.
1844 */
1845 return SCTP_DISPOSITION_DISCARD;
1846}
1847
1848/* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1849 *
1850 * Section 5.2.4
1851 *
1852 * D) When both local and remote tags match the endpoint should always
1853 * enter the ESTABLISHED state, if it has not already done so.
1854 */
1855/* This case represents an initialization collision. */
1856static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1857 const struct sctp_association *asoc,
1858 struct sctp_chunk *chunk,
1859 sctp_cmd_seq_t *commands,
1860 struct sctp_association *new_asoc)
1861{
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001862 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 struct sctp_chunk *repl;
1864
1865 /* Clarification from Implementor's Guide:
1866 * D) When both local and remote tags match the endpoint should
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001867 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1868 * It should stop any cookie timer that may be running and send
1869 * a COOKIE ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
1871
1872 /* Don't accidentally move back into established state. */
1873 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1874 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1875 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1876 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1877 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1878 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1879 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1880 SCTP_NULL());
1881
1882 /* RFC 2960 5.1 Normal Establishment of an Association
1883 *
1884 * D) IMPLEMENTATION NOTE: An implementation may choose
1885 * to send the Communication Up notification to the
1886 * SCTP user upon reception of a valid COOKIE
1887 * ECHO chunk.
1888 */
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001889 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 SCTP_COMM_UP, 0,
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001891 asoc->c.sinit_num_ostreams,
1892 asoc->c.sinit_max_instreams,
YOSHIFUJI Hideaki9cbcbf42007-07-19 10:44:50 +09001893 NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (!ev)
1895 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897 /* Sockets API Draft Section 5.3.1.6
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001898 * When a peer sends a Adaptation Layer Indication parameter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * SCTP delivers this notification to inform the application
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001900 * that of the peers requested adaptation layer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
Ivan Skytte Jorgensen0f3fffd2006-12-20 16:07:04 -08001902 if (asoc->peer.adaptation_ind) {
1903 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 GFP_ATOMIC);
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001905 if (!ai_ev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 goto nomem;
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 repl = sctp_make_cookie_ack(new_asoc, chunk);
1912 if (!repl)
1913 goto nomem;
1914
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07001915 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1916
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001917 if (ev)
1918 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1919 SCTP_ULPEVENT(ev));
1920 if (ai_ev)
1921 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1922 SCTP_ULPEVENT(ai_ev));
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return SCTP_DISPOSITION_CONSUME;
1925
1926nomem:
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07001927 if (ai_ev)
1928 sctp_ulpevent_free(ai_ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (ev)
1930 sctp_ulpevent_free(ev);
1931 return SCTP_DISPOSITION_NOMEM;
1932}
1933
1934/*
1935 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1936 * chunk was retransmitted and then delayed in the network.
1937 *
1938 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1939 *
1940 * Verification Tag: None. Do cookie validation.
1941 *
1942 * Inputs
1943 * (endpoint, asoc, chunk)
1944 *
1945 * Outputs
1946 * (asoc, reply_msg, msg_up, timers, counters)
1947 *
1948 * The return value is the disposition of the chunk.
1949 */
1950sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1951 const struct sctp_association *asoc,
1952 const sctp_subtype_t type,
1953 void *arg,
1954 sctp_cmd_seq_t *commands)
1955{
1956 sctp_disposition_t retval;
1957 struct sctp_chunk *chunk = arg;
1958 struct sctp_association *new_asoc;
1959 int error = 0;
1960 char action;
1961 struct sctp_chunk *err_chk_p;
1962
1963 /* Make sure that the chunk has a valid length from the protocol
1964 * perspective. In this case check to make sure we have at least
1965 * enough for the chunk header. Cookie length verification is
1966 * done later.
1967 */
1968 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1969 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1970 commands);
1971
1972 /* "Decode" the chunk. We have no optional parameters so we
1973 * are in good shape.
1974 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09001975 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
Sridhar Samudrala62b08082006-05-05 17:04:43 -07001976 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1977 sizeof(sctp_chunkhdr_t)))
1978 goto nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1981 * of a duplicate COOKIE ECHO match the Verification Tags of the
1982 * current association, consider the State Cookie valid even if
1983 * the lifespan is exceeded.
1984 */
1985 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1986 &err_chk_p);
1987
1988 /* FIXME:
1989 * If the re-build failed, what is the proper error path
1990 * from here?
1991 *
1992 * [We should abort the association. --piggy]
1993 */
1994 if (!new_asoc) {
1995 /* FIXME: Several errors are possible. A bad cookie should
1996 * be silently discarded, but think about logging it too.
1997 */
1998 switch (error) {
1999 case -SCTP_IERROR_NOMEM:
2000 goto nomem;
2001
2002 case -SCTP_IERROR_STALE_COOKIE:
2003 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2004 err_chk_p);
2005 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2006 case -SCTP_IERROR_BAD_SIG:
2007 default:
2008 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011
2012 /* Compare the tie_tag in cookie with the verification tag of
2013 * current association.
2014 */
2015 action = sctp_tietags_compare(new_asoc, asoc);
2016
2017 switch (action) {
2018 case 'A': /* Association restart. */
2019 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2020 new_asoc);
2021 break;
2022
2023 case 'B': /* Collision case B. */
2024 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2025 new_asoc);
2026 break;
2027
2028 case 'C': /* Collision case C. */
2029 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2030 new_asoc);
2031 break;
2032
2033 case 'D': /* Collision case D. */
2034 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2035 new_asoc);
2036 break;
2037
2038 default: /* Discard packet for all others. */
2039 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2040 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07002041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /* Delete the tempory new association. */
2044 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2045 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2046
2047 return retval;
2048
2049nomem:
2050 return SCTP_DISPOSITION_NOMEM;
2051}
2052
2053/*
2054 * Process an ABORT. (SHUTDOWN-PENDING state)
2055 *
2056 * See sctp_sf_do_9_1_abort().
2057 */
2058sctp_disposition_t sctp_sf_shutdown_pending_abort(
2059 const struct sctp_endpoint *ep,
2060 const struct sctp_association *asoc,
2061 const sctp_subtype_t type,
2062 void *arg,
2063 sctp_cmd_seq_t *commands)
2064{
2065 struct sctp_chunk *chunk = arg;
2066
2067 if (!sctp_vtag_verify_either(chunk, asoc))
2068 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2069
2070 /* Make sure that the ABORT chunk has a valid length.
2071 * Since this is an ABORT chunk, we have to discard it
2072 * because of the following text:
2073 * RFC 2960, Section 3.3.7
2074 * If an endpoint receives an ABORT with a format error or for an
2075 * association that doesn't exist, it MUST silently discard it.
2076 * Becasue the length is "invalid", we can't really discard just
2077 * as we do not know its true length. So, to be safe, discard the
2078 * packet.
2079 */
2080 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2081 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2082
Vlad Yasevich75205f42007-12-20 14:12:59 -08002083 /* ADD-IP: Special case for ABORT chunks
2084 * F4) One special consideration is that ABORT Chunks arriving
2085 * destined to the IP address being deleted MUST be
2086 * ignored (see Section 5.3.1 for further details).
2087 */
2088 if (SCTP_ADDR_DEL ==
2089 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2090 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2091
Vlad Yasevich75205f42007-12-20 14:12:59 -08002092 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
2095/*
2096 * Process an ABORT. (SHUTDOWN-SENT state)
2097 *
2098 * See sctp_sf_do_9_1_abort().
2099 */
2100sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2101 const struct sctp_association *asoc,
2102 const sctp_subtype_t type,
2103 void *arg,
2104 sctp_cmd_seq_t *commands)
2105{
2106 struct sctp_chunk *chunk = arg;
2107
2108 if (!sctp_vtag_verify_either(chunk, asoc))
2109 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2110
2111 /* Make sure that the ABORT chunk has a valid length.
2112 * Since this is an ABORT chunk, we have to discard it
2113 * because of the following text:
2114 * RFC 2960, Section 3.3.7
2115 * If an endpoint receives an ABORT with a format error or for an
2116 * association that doesn't exist, it MUST silently discard it.
2117 * Becasue the length is "invalid", we can't really discard just
2118 * as we do not know its true length. So, to be safe, discard the
2119 * packet.
2120 */
2121 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2122 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2123
Vlad Yasevich75205f42007-12-20 14:12:59 -08002124 /* ADD-IP: Special case for ABORT chunks
2125 * F4) One special consideration is that ABORT Chunks arriving
2126 * destined to the IP address being deleted MUST be
2127 * ignored (see Section 5.3.1 for further details).
2128 */
2129 if (SCTP_ADDR_DEL ==
2130 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2131 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /* Stop the T2-shutdown timer. */
2134 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2135 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2136
2137 /* Stop the T5-shutdown guard timer. */
2138 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2139 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2140
Vlad Yasevich75205f42007-12-20 14:12:59 -08002141 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
2144/*
2145 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2146 *
2147 * See sctp_sf_do_9_1_abort().
2148 */
2149sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2150 const struct sctp_endpoint *ep,
2151 const struct sctp_association *asoc,
2152 const sctp_subtype_t type,
2153 void *arg,
2154 sctp_cmd_seq_t *commands)
2155{
2156 /* The same T2 timer, so we should be able to use
2157 * common function with the SHUTDOWN-SENT state.
2158 */
2159 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2160}
2161
2162/*
2163 * Handle an Error received in COOKIE_ECHOED state.
2164 *
2165 * Only handle the error type of stale COOKIE Error, the other errors will
2166 * be ignored.
2167 *
2168 * Inputs
2169 * (endpoint, asoc, chunk)
2170 *
2171 * Outputs
2172 * (asoc, reply_msg, msg_up, timers, counters)
2173 *
2174 * The return value is the disposition of the chunk.
2175 */
2176sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2177 const struct sctp_association *asoc,
2178 const sctp_subtype_t type,
2179 void *arg,
2180 sctp_cmd_seq_t *commands)
2181{
2182 struct sctp_chunk *chunk = arg;
2183 sctp_errhdr_t *err;
2184
2185 if (!sctp_vtag_verify(chunk, asoc))
2186 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2187
2188 /* Make sure that the ERROR chunk has a valid length.
2189 * The parameter walking depends on this as well.
2190 */
2191 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2192 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2193 commands);
2194
2195 /* Process the error here */
2196 /* FUTURE FIXME: When PR-SCTP related and other optional
2197 * parms are emitted, this will have to change to handle multiple
2198 * errors.
2199 */
2200 sctp_walk_errors(err, chunk->chunk_hdr) {
2201 if (SCTP_ERROR_STALE_COOKIE == err->cause)
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002202 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 arg, commands);
2204 }
2205
2206 /* It is possible to have malformed error causes, and that
2207 * will cause us to end the walk early. However, since
2208 * we are discarding the packet, there should be no adverse
2209 * affects.
2210 */
2211 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2212}
2213
2214/*
2215 * Handle a Stale COOKIE Error
2216 *
2217 * Section: 5.2.6 Handle Stale COOKIE Error
2218 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2219 * one of the following three alternatives.
2220 * ...
2221 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2222 * Preservative parameter requesting an extension to the lifetime of
2223 * the State Cookie. When calculating the time extension, an
2224 * implementation SHOULD use the RTT information measured based on the
2225 * previous COOKIE ECHO / ERROR exchange, and should add no more
2226 * than 1 second beyond the measured RTT, due to long State Cookie
2227 * lifetimes making the endpoint more subject to a replay attack.
2228 *
2229 * Verification Tag: Not explicit, but safe to ignore.
2230 *
2231 * Inputs
2232 * (endpoint, asoc, chunk)
2233 *
2234 * Outputs
2235 * (asoc, reply_msg, msg_up, timers, counters)
2236 *
2237 * The return value is the disposition of the chunk.
2238 */
2239static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2240 const struct sctp_association *asoc,
2241 const sctp_subtype_t type,
2242 void *arg,
2243 sctp_cmd_seq_t *commands)
2244{
2245 struct sctp_chunk *chunk = arg;
2246 time_t stale;
2247 sctp_cookie_preserve_param_t bht;
2248 sctp_errhdr_t *err;
2249 struct sctp_chunk *reply;
2250 struct sctp_bind_addr *bp;
Frank Filz3f7a87d2005-06-20 13:14:57 -07002251 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Vlad Yasevich81845c22006-01-30 15:59:54 -08002253 if (attempts > asoc->max_init_attempts) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002254 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2255 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002257 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return SCTP_DISPOSITION_DELETE_TCB;
2259 }
2260
2261 err = (sctp_errhdr_t *)(chunk->skb->data);
2262
2263 /* When calculating the time extension, an implementation
2264 * SHOULD use the RTT information measured based on the
2265 * previous COOKIE ECHO / ERROR exchange, and should add no
2266 * more than 1 second beyond the measured RTT, due to long
2267 * State Cookie lifetimes making the endpoint more subject to
2268 * a replay attack.
2269 * Measure of Staleness's unit is usec. (1/1000000 sec)
2270 * Suggested Cookie Life-span Increment's unit is msec.
2271 * (1/1000 sec)
2272 * In general, if you use the suggested cookie life, the value
2273 * found in the field of measure of staleness should be doubled
2274 * to give ample time to retransmit the new cookie and thus
2275 * yield a higher probability of success on the reattempt.
2276 */
Al Viro34bcca22006-11-20 17:27:15 -08002277 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 stale = (stale * 2) / 1000;
2279
2280 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2281 bht.param_hdr.length = htons(sizeof(bht));
2282 bht.lifespan_increment = htonl(stale);
2283
2284 /* Build that new INIT chunk. */
2285 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2286 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2287 if (!reply)
2288 goto nomem;
2289
2290 sctp_addto_chunk(reply, sizeof(bht), &bht);
2291
2292 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2293 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2294
2295 /* Stop pending T3-rtx and heartbeat timers */
2296 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2297 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2298
2299 /* Delete non-primary peer ip addresses since we are transitioning
2300 * back to the COOKIE-WAIT state
2301 */
2302 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2303
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002304 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2305 * resend
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 */
Vlad Yasevichb6157d82007-10-24 15:59:16 -04002307 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 SCTP_TRANSPORT(asoc->peer.primary_path));
2309
2310 /* Cast away the const modifier, as we want to just
2311 * rerun it through as a sideffect.
2312 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07002313 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2316 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2317 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2318 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2319 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2320 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2321
2322 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2323
2324 return SCTP_DISPOSITION_CONSUME;
2325
2326nomem:
2327 return SCTP_DISPOSITION_NOMEM;
2328}
2329
2330/*
2331 * Process an ABORT.
2332 *
2333 * Section: 9.1
2334 * After checking the Verification Tag, the receiving endpoint shall
2335 * remove the association from its record, and shall report the
2336 * termination to its upper layer.
2337 *
2338 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2339 * B) Rules for packet carrying ABORT:
2340 *
2341 * - The endpoint shall always fill in the Verification Tag field of the
2342 * outbound packet with the destination endpoint's tag value if it
2343 * is known.
2344 *
2345 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2346 * MUST follow the procedure described in Section 8.4.
2347 *
2348 * - The receiver MUST accept the packet if the Verification Tag
2349 * matches either its own tag, OR the tag of its peer. Otherwise, the
2350 * receiver MUST silently discard the packet and take no further
2351 * action.
2352 *
2353 * Inputs
2354 * (endpoint, asoc, chunk)
2355 *
2356 * Outputs
2357 * (asoc, reply_msg, msg_up, timers, counters)
2358 *
2359 * The return value is the disposition of the chunk.
2360 */
2361sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2362 const struct sctp_association *asoc,
2363 const sctp_subtype_t type,
2364 void *arg,
2365 sctp_cmd_seq_t *commands)
2366{
2367 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
2369 if (!sctp_vtag_verify_either(chunk, asoc))
2370 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2371
2372 /* Make sure that the ABORT chunk has a valid length.
2373 * Since this is an ABORT chunk, we have to discard it
2374 * because of the following text:
2375 * RFC 2960, Section 3.3.7
2376 * If an endpoint receives an ABORT with a format error or for an
2377 * association that doesn't exist, it MUST silently discard it.
2378 * Becasue the length is "invalid", we can't really discard just
2379 * as we do not know its true length. So, to be safe, discard the
2380 * packet.
2381 */
2382 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2383 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2384
Vlad Yasevich75205f42007-12-20 14:12:59 -08002385 /* ADD-IP: Special case for ABORT chunks
2386 * F4) One special consideration is that ABORT Chunks arriving
2387 * destined to the IP address being deleted MUST be
2388 * ignored (see Section 5.3.1 for further details).
2389 */
2390 if (SCTP_ADDR_DEL ==
2391 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2392 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2393
2394 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2395}
2396
2397static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2398 const struct sctp_association *asoc,
2399 const sctp_subtype_t type,
2400 void *arg,
2401 sctp_cmd_seq_t *commands)
2402{
2403 struct sctp_chunk *chunk = arg;
2404 unsigned len;
2405 __be16 error = SCTP_ERROR_NO_ERROR;
2406
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 /* See if we have an error cause code in the chunk. */
2408 len = ntohs(chunk->chunk_hdr->length);
2409 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2410 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2411
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002412 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002413 /* ASSOC_FAILED will DELETE_TCB. */
Al Viro5be291f2006-11-20 17:01:06 -08002414 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2416 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2417
2418 return SCTP_DISPOSITION_ABORT;
2419}
2420
2421/*
2422 * Process an ABORT. (COOKIE-WAIT state)
2423 *
2424 * See sctp_sf_do_9_1_abort() above.
2425 */
2426sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2427 const struct sctp_association *asoc,
2428 const sctp_subtype_t type,
2429 void *arg,
2430 sctp_cmd_seq_t *commands)
2431{
2432 struct sctp_chunk *chunk = arg;
2433 unsigned len;
Al Virof94c0192006-11-20 17:00:25 -08002434 __be16 error = SCTP_ERROR_NO_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 if (!sctp_vtag_verify_either(chunk, asoc))
2437 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2438
2439 /* Make sure that the ABORT chunk has a valid length.
2440 * Since this is an ABORT chunk, we have to discard it
2441 * because of the following text:
2442 * RFC 2960, Section 3.3.7
2443 * If an endpoint receives an ABORT with a format error or for an
2444 * association that doesn't exist, it MUST silently discard it.
2445 * Becasue the length is "invalid", we can't really discard just
2446 * as we do not know its true length. So, to be safe, discard the
2447 * packet.
2448 */
2449 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2450 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2451
2452 /* See if we have an error cause code in the chunk. */
2453 len = ntohs(chunk->chunk_hdr->length);
2454 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2455 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2456
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002457 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2458 chunk->transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459}
2460
2461/*
2462 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2463 */
2464sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2465 const struct sctp_association *asoc,
2466 const sctp_subtype_t type,
2467 void *arg,
2468 sctp_cmd_seq_t *commands)
2469{
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002470 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2471 ENOPROTOOPT, asoc,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002472 (struct sctp_transport *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475/*
2476 * Process an ABORT. (COOKIE-ECHOED state)
2477 */
2478sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2479 const struct sctp_association *asoc,
2480 const sctp_subtype_t type,
2481 void *arg,
2482 sctp_cmd_seq_t *commands)
2483{
2484 /* There is a single T1 timer, so we should be able to use
2485 * common function with the COOKIE-WAIT state.
2486 */
2487 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2488}
2489
2490/*
2491 * Stop T1 timer and abort association with "INIT failed".
2492 *
2493 * This is common code called by several sctp_sf_*_abort() functions above.
2494 */
Adrian Bunk52c1da32005-06-23 22:05:33 -07002495static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
Al Virof94c0192006-11-20 17:00:25 -08002496 __be16 error, int sk_err,
Frank Filz3f7a87d2005-06-20 13:14:57 -07002497 const struct sctp_association *asoc,
2498 struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Frank Filz3f7a87d2005-06-20 13:14:57 -07002500 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2502 SCTP_STATE(SCTP_STATE_CLOSED));
2503 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2504 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2505 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07002506 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 /* CMD_INIT_FAILED will DELETE_TCB. */
2508 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08002509 SCTP_PERR(error));
Frank Filz3f7a87d2005-06-20 13:14:57 -07002510 return SCTP_DISPOSITION_ABORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
2513/*
2514 * sctp_sf_do_9_2_shut
2515 *
2516 * Section: 9.2
2517 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2518 * - enter the SHUTDOWN-RECEIVED state,
2519 *
2520 * - stop accepting new data from its SCTP user
2521 *
2522 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2523 * that all its outstanding DATA chunks have been received by the
2524 * SHUTDOWN sender.
2525 *
2526 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2527 * send a SHUTDOWN in response to a ULP request. And should discard
2528 * subsequent SHUTDOWN chunks.
2529 *
2530 * If there are still outstanding DATA chunks left, the SHUTDOWN
2531 * receiver shall continue to follow normal data transmission
2532 * procedures defined in Section 6 until all outstanding DATA chunks
2533 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2534 * new data from its SCTP user.
2535 *
2536 * Verification Tag: 8.5 Verification Tag [Normal verification]
2537 *
2538 * Inputs
2539 * (endpoint, asoc, chunk)
2540 *
2541 * Outputs
2542 * (asoc, reply_msg, msg_up, timers, counters)
2543 *
2544 * The return value is the disposition of the chunk.
2545 */
2546sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2547 const struct sctp_association *asoc,
2548 const sctp_subtype_t type,
2549 void *arg,
2550 sctp_cmd_seq_t *commands)
2551{
2552 struct sctp_chunk *chunk = arg;
2553 sctp_shutdownhdr_t *sdh;
2554 sctp_disposition_t disposition;
2555 struct sctp_ulpevent *ev;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002556 __u32 ctsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
2558 if (!sctp_vtag_verify(chunk, asoc))
2559 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2560
2561 /* Make sure that the SHUTDOWN chunk has a valid length. */
2562 if (!sctp_chunk_length_valid(chunk,
2563 sizeof(struct sctp_shutdown_chunk_t)))
2564 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2565 commands);
2566
2567 /* Convert the elaborate header. */
2568 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2569 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2570 chunk->subh.shutdown_hdr = sdh;
Wei Yongjundf10eec2008-10-23 01:00:21 -07002571 ctsn = ntohl(sdh->cum_tsn_ack);
2572
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002573 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2574 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2575 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2576 return SCTP_DISPOSITION_DISCARD;
2577 }
2578
Wei Yongjundf10eec2008-10-23 01:00:21 -07002579 /* If Cumulative TSN Ack beyond the max tsn currently
2580 * send, terminating the association and respond to the
2581 * sender with an ABORT.
2582 */
2583 if (!TSN_lt(ctsn, asoc->next_tsn))
2584 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002586 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2587 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2588 * inform the application that it should cease sending data.
2589 */
2590 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2591 if (!ev) {
2592 disposition = SCTP_DISPOSITION_NOMEM;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002593 goto out;
Sridhar Samudralaeb0e0072005-09-22 23:48:38 -07002594 }
2595 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2596
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2598 * - enter the SHUTDOWN-RECEIVED state,
2599 * - stop accepting new data from its SCTP user
2600 *
2601 * [This is implicit in the new state.]
2602 */
2603 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2604 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2605 disposition = SCTP_DISPOSITION_CONSUME;
2606
2607 if (sctp_outq_is_empty(&asoc->outqueue)) {
2608 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2609 arg, commands);
2610 }
2611
2612 if (SCTP_DISPOSITION_NOMEM == disposition)
2613 goto out;
2614
2615 /* - verify, by checking the Cumulative TSN Ack field of the
2616 * chunk, that all its outstanding DATA chunks have been
2617 * received by the SHUTDOWN sender.
2618 */
2619 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
Al Viro2178eda2006-11-20 17:26:53 -08002620 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622out:
2623 return disposition;
2624}
2625
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002626/*
2627 * sctp_sf_do_9_2_shut_ctsn
2628 *
2629 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2630 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2631 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2632 * MUST be processed.
2633 */
2634sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2635 const struct sctp_association *asoc,
2636 const sctp_subtype_t type,
2637 void *arg,
2638 sctp_cmd_seq_t *commands)
2639{
2640 struct sctp_chunk *chunk = arg;
2641 sctp_shutdownhdr_t *sdh;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002642 __u32 ctsn;
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002643
2644 if (!sctp_vtag_verify(chunk, asoc))
2645 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2646
2647 /* Make sure that the SHUTDOWN chunk has a valid length. */
2648 if (!sctp_chunk_length_valid(chunk,
2649 sizeof(struct sctp_shutdown_chunk_t)))
2650 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2651 commands);
2652
2653 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002654 ctsn = ntohl(sdh->cum_tsn_ack);
2655
2656 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2657 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2658 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2659 return SCTP_DISPOSITION_DISCARD;
2660 }
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002661
2662 /* If Cumulative TSN Ack beyond the max tsn currently
2663 * send, terminating the association and respond to the
2664 * sender with an ABORT.
2665 */
Wei Yongjuna2f36ee2009-08-22 11:24:00 +08002666 if (!TSN_lt(ctsn, asoc->next_tsn))
Wei Yongjun2e3f92d2008-10-23 01:01:18 -07002667 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2668
2669 /* verify, by checking the Cumulative TSN Ack field of the
2670 * chunk, that all its outstanding DATA chunks have been
2671 * received by the SHUTDOWN sender.
2672 */
2673 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2674 SCTP_BE32(sdh->cum_tsn_ack));
2675
2676 return SCTP_DISPOSITION_CONSUME;
2677}
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679/* RFC 2960 9.2
2680 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2681 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2682 * transport addresses (either in the IP addresses or in the INIT chunk)
2683 * that belong to this association, it should discard the INIT chunk and
2684 * retransmit the SHUTDOWN ACK chunk.
2685 */
2686sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2687 const struct sctp_association *asoc,
2688 const sctp_subtype_t type,
2689 void *arg,
2690 sctp_cmd_seq_t *commands)
2691{
2692 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2693 struct sctp_chunk *reply;
2694
Vlad Yasevichece25df2007-09-07 16:30:54 -04002695 /* Make sure that the chunk has a valid length */
2696 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2697 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2698 commands);
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /* Since we are not going to really process this INIT, there
2701 * is no point in verifying chunk boundries. Just generate
2702 * the SHUTDOWN ACK.
2703 */
2704 reply = sctp_make_shutdown_ack(asoc, chunk);
2705 if (NULL == reply)
2706 goto nomem;
2707
2708 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2709 * the T2-SHUTDOWN timer.
2710 */
2711 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2712
2713 /* and restart the T2-shutdown timer. */
2714 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2715 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2716
2717 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2718
2719 return SCTP_DISPOSITION_CONSUME;
2720nomem:
2721 return SCTP_DISPOSITION_NOMEM;
2722}
2723
2724/*
2725 * sctp_sf_do_ecn_cwr
2726 *
2727 * Section: Appendix A: Explicit Congestion Notification
2728 *
2729 * CWR:
2730 *
2731 * RFC 2481 details a specific bit for a sender to send in the header of
2732 * its next outbound TCP segment to indicate to its peer that it has
2733 * reduced its congestion window. This is termed the CWR bit. For
2734 * SCTP the same indication is made by including the CWR chunk.
2735 * This chunk contains one data element, i.e. the TSN number that
2736 * was sent in the ECNE chunk. This element represents the lowest
2737 * TSN number in the datagram that was originally marked with the
2738 * CE bit.
2739 *
2740 * Verification Tag: 8.5 Verification Tag [Normal verification]
2741 * Inputs
2742 * (endpoint, asoc, chunk)
2743 *
2744 * Outputs
2745 * (asoc, reply_msg, msg_up, timers, counters)
2746 *
2747 * The return value is the disposition of the chunk.
2748 */
2749sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2750 const struct sctp_association *asoc,
2751 const sctp_subtype_t type,
2752 void *arg,
2753 sctp_cmd_seq_t *commands)
2754{
2755 sctp_cwrhdr_t *cwr;
2756 struct sctp_chunk *chunk = arg;
Al Viro34bcca22006-11-20 17:27:15 -08002757 u32 lowest_tsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 if (!sctp_vtag_verify(chunk, asoc))
2760 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2761
2762 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2763 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2764 commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2767 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2768
Al Viro34bcca22006-11-20 17:27:15 -08002769 lowest_tsn = ntohl(cwr->lowest_tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 /* Does this CWR ack the last sent congestion notification? */
Al Viro34bcca22006-11-20 17:27:15 -08002772 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /* Stop sending ECNE. */
2774 sctp_add_cmd_sf(commands,
2775 SCTP_CMD_ECN_CWR,
Al Viro34bcca22006-11-20 17:27:15 -08002776 SCTP_U32(lowest_tsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
2778 return SCTP_DISPOSITION_CONSUME;
2779}
2780
2781/*
2782 * sctp_sf_do_ecne
2783 *
2784 * Section: Appendix A: Explicit Congestion Notification
2785 *
2786 * ECN-Echo
2787 *
2788 * RFC 2481 details a specific bit for a receiver to send back in its
2789 * TCP acknowledgements to notify the sender of the Congestion
2790 * Experienced (CE) bit having arrived from the network. For SCTP this
2791 * same indication is made by including the ECNE chunk. This chunk
2792 * contains one data element, i.e. the lowest TSN associated with the IP
2793 * datagram marked with the CE bit.....
2794 *
2795 * Verification Tag: 8.5 Verification Tag [Normal verification]
2796 * Inputs
2797 * (endpoint, asoc, chunk)
2798 *
2799 * Outputs
2800 * (asoc, reply_msg, msg_up, timers, counters)
2801 *
2802 * The return value is the disposition of the chunk.
2803 */
2804sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2805 const struct sctp_association *asoc,
2806 const sctp_subtype_t type,
2807 void *arg,
2808 sctp_cmd_seq_t *commands)
2809{
2810 sctp_ecnehdr_t *ecne;
2811 struct sctp_chunk *chunk = arg;
2812
2813 if (!sctp_vtag_verify(chunk, asoc))
2814 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2815
2816 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2817 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2818 commands);
2819
2820 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2821 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2822
2823 /* If this is a newer ECNE than the last CWR packet we sent out */
2824 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2825 SCTP_U32(ntohl(ecne->lowest_tsn)));
2826
2827 return SCTP_DISPOSITION_CONSUME;
2828}
2829
2830/*
2831 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2832 *
2833 * The SCTP endpoint MUST always acknowledge the reception of each valid
2834 * DATA chunk.
2835 *
2836 * The guidelines on delayed acknowledgement algorithm specified in
2837 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2838 * acknowledgement SHOULD be generated for at least every second packet
2839 * (not every second DATA chunk) received, and SHOULD be generated within
2840 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2841 * situations it may be beneficial for an SCTP transmitter to be more
2842 * conservative than the algorithms detailed in this document allow.
2843 * However, an SCTP transmitter MUST NOT be more aggressive than the
2844 * following algorithms allow.
2845 *
2846 * A SCTP receiver MUST NOT generate more than one SACK for every
2847 * incoming packet, other than to update the offered window as the
2848 * receiving application consumes new data.
2849 *
2850 * Verification Tag: 8.5 Verification Tag [Normal verification]
2851 *
2852 * Inputs
2853 * (endpoint, asoc, chunk)
2854 *
2855 * Outputs
2856 * (asoc, reply_msg, msg_up, timers, counters)
2857 *
2858 * The return value is the disposition of the chunk.
2859 */
2860sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2861 const struct sctp_association *asoc,
2862 const sctp_subtype_t type,
2863 void *arg,
2864 sctp_cmd_seq_t *commands)
2865{
2866 struct sctp_chunk *chunk = arg;
2867 int error;
2868
2869 if (!sctp_vtag_verify(chunk, asoc)) {
2870 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2871 SCTP_NULL());
2872 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2876 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2877 commands);
2878
2879 error = sctp_eat_data(asoc, chunk, commands );
2880 switch (error) {
2881 case SCTP_IERROR_NO_ERROR:
2882 break;
2883 case SCTP_IERROR_HIGH_TSN:
2884 case SCTP_IERROR_BAD_STREAM:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002885 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 goto discard_noforce;
2887 case SCTP_IERROR_DUP_TSN:
2888 case SCTP_IERROR_IGNORE_TSN:
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07002889 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 goto discard_force;
2891 case SCTP_IERROR_NO_DATA:
2892 goto consume;
2893 default:
2894 BUG();
2895 }
2896
2897 if (asoc->autoclose) {
2898 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2899 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2900 }
2901
2902 /* If this is the last chunk in a packet, we need to count it
2903 * toward sack generation. Note that we need to SACK every
2904 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2905 * THEM. We elect to NOT generate SACK's if the chunk fails
2906 * the verification tag test.
2907 *
2908 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2909 *
2910 * The SCTP endpoint MUST always acknowledge the reception of
2911 * each valid DATA chunk.
2912 *
2913 * The guidelines on delayed acknowledgement algorithm
2914 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2915 * Specifically, an acknowledgement SHOULD be generated for at
2916 * least every second packet (not every second DATA chunk)
2917 * received, and SHOULD be generated within 200 ms of the
2918 * arrival of any unacknowledged DATA chunk. In some
2919 * situations it may be beneficial for an SCTP transmitter to
2920 * be more conservative than the algorithms detailed in this
2921 * document allow. However, an SCTP transmitter MUST NOT be
2922 * more aggressive than the following algorithms allow.
2923 */
Frank Filz52ccb8e2005-12-22 11:36:46 -08002924 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return SCTP_DISPOSITION_CONSUME;
2928
2929discard_force:
2930 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2931 *
2932 * When a packet arrives with duplicate DATA chunk(s) and with
2933 * no new DATA chunk(s), the endpoint MUST immediately send a
2934 * SACK with no delay. If a packet arrives with duplicate
2935 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2936 * MAY immediately send a SACK. Normally receipt of duplicate
2937 * DATA chunks will occur when the original SACK chunk was lost
2938 * and the peer's RTO has expired. The duplicate TSN number(s)
2939 * SHOULD be reported in the SACK as duplicate.
2940 */
2941 /* In our case, we split the MAY SACK advice up whether or not
2942 * the last chunk is a duplicate.'
2943 */
2944 if (chunk->end_of_packet)
2945 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2946 return SCTP_DISPOSITION_DISCARD;
2947
2948discard_noforce:
Frank Filz52ccb8e2005-12-22 11:36:46 -08002949 if (chunk->end_of_packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return SCTP_DISPOSITION_DISCARD;
2953consume:
2954 return SCTP_DISPOSITION_CONSUME;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09002955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
2958/*
2959 * sctp_sf_eat_data_fast_4_4
2960 *
2961 * Section: 4 (4)
2962 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2963 * DATA chunks without delay.
2964 *
2965 * Verification Tag: 8.5 Verification Tag [Normal verification]
2966 * Inputs
2967 * (endpoint, asoc, chunk)
2968 *
2969 * Outputs
2970 * (asoc, reply_msg, msg_up, timers, counters)
2971 *
2972 * The return value is the disposition of the chunk.
2973 */
2974sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2975 const struct sctp_association *asoc,
2976 const sctp_subtype_t type,
2977 void *arg,
2978 sctp_cmd_seq_t *commands)
2979{
2980 struct sctp_chunk *chunk = arg;
2981 int error;
2982
2983 if (!sctp_vtag_verify(chunk, asoc)) {
2984 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2985 SCTP_NULL());
2986 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2987 }
2988
2989 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2990 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2991 commands);
2992
2993 error = sctp_eat_data(asoc, chunk, commands );
2994 switch (error) {
2995 case SCTP_IERROR_NO_ERROR:
2996 case SCTP_IERROR_HIGH_TSN:
2997 case SCTP_IERROR_DUP_TSN:
2998 case SCTP_IERROR_IGNORE_TSN:
2999 case SCTP_IERROR_BAD_STREAM:
3000 break;
3001 case SCTP_IERROR_NO_DATA:
3002 goto consume;
3003 default:
3004 BUG();
3005 }
3006
3007 /* Go a head and force a SACK, since we are shutting down. */
3008
3009 /* Implementor's Guide.
3010 *
3011 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3012 * respond to each received packet containing one or more DATA chunk(s)
3013 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3014 */
3015 if (chunk->end_of_packet) {
3016 /* We must delay the chunk creation since the cumulative
3017 * TSN has not been updated yet.
3018 */
3019 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3020 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3021 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3022 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3023 }
3024
3025consume:
3026 return SCTP_DISPOSITION_CONSUME;
3027}
3028
3029/*
3030 * Section: 6.2 Processing a Received SACK
3031 * D) Any time a SACK arrives, the endpoint performs the following:
3032 *
3033 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3034 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3035 * increasing, a SACK whose Cumulative TSN Ack is less than the
3036 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3037 *
3038 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3039 * of bytes still outstanding after processing the Cumulative TSN Ack
3040 * and the Gap Ack Blocks.
3041 *
3042 * iii) If the SACK is missing a TSN that was previously
3043 * acknowledged via a Gap Ack Block (e.g., the data receiver
3044 * reneged on the data), then mark the corresponding DATA chunk
3045 * as available for retransmit: Mark it as missing for fast
3046 * retransmit as described in Section 7.2.4 and if no retransmit
3047 * timer is running for the destination address to which the DATA
3048 * chunk was originally transmitted, then T3-rtx is started for
3049 * that destination address.
3050 *
3051 * Verification Tag: 8.5 Verification Tag [Normal verification]
3052 *
3053 * Inputs
3054 * (endpoint, asoc, chunk)
3055 *
3056 * Outputs
3057 * (asoc, reply_msg, msg_up, timers, counters)
3058 *
3059 * The return value is the disposition of the chunk.
3060 */
3061sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3062 const struct sctp_association *asoc,
3063 const sctp_subtype_t type,
3064 void *arg,
3065 sctp_cmd_seq_t *commands)
3066{
3067 struct sctp_chunk *chunk = arg;
3068 sctp_sackhdr_t *sackh;
3069 __u32 ctsn;
3070
3071 if (!sctp_vtag_verify(chunk, asoc))
3072 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3073
3074 /* Make sure that the SACK chunk has a valid length. */
3075 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3076 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3077 commands);
3078
3079 /* Pull the SACK chunk from the data buffer */
3080 sackh = sctp_sm_pull_sack(chunk);
3081 /* Was this a bogus SACK? */
3082 if (!sackh)
3083 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3084 chunk->subh.sack_hdr = sackh;
3085 ctsn = ntohl(sackh->cum_tsn_ack);
3086
3087 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3088 * Ack Point, then drop the SACK. Since Cumulative TSN
3089 * Ack is monotonically increasing, a SACK whose
3090 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3091 * Point indicates an out-of-order SACK.
3092 */
3093 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3094 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3095 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3096 return SCTP_DISPOSITION_DISCARD;
3097 }
3098
Wei Yongjunaecedea2007-08-02 16:57:44 +08003099 /* If Cumulative TSN Ack beyond the max tsn currently
3100 * send, terminating the association and respond to the
3101 * sender with an ABORT.
3102 */
3103 if (!TSN_lt(ctsn, asoc->next_tsn))
3104 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 /* Return this SACK for further processing. */
3107 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3108
3109 /* Note: We do the rest of the work on the PROCESS_SACK
3110 * sideeffect.
3111 */
3112 return SCTP_DISPOSITION_CONSUME;
3113}
3114
3115/*
3116 * Generate an ABORT in response to a packet.
3117 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003118 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003120 * 8) The receiver should respond to the sender of the OOTB packet with
3121 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3122 * MUST fill in the Verification Tag field of the outbound packet
3123 * with the value found in the Verification Tag field of the OOTB
3124 * packet and set the T-bit in the Chunk Flags to indicate that the
3125 * Verification Tag is reflected. After sending this ABORT, the
3126 * receiver of the OOTB packet shall discard the OOTB packet and take
3127 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 *
3129 * Verification Tag:
3130 *
3131 * The return value is the disposition of the chunk.
3132*/
Vlad Yasevichece25df2007-09-07 16:30:54 -04003133static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 const struct sctp_association *asoc,
3135 const sctp_subtype_t type,
3136 void *arg,
3137 sctp_cmd_seq_t *commands)
3138{
3139 struct sctp_packet *packet = NULL;
3140 struct sctp_chunk *chunk = arg;
3141 struct sctp_chunk *abort;
3142
3143 packet = sctp_ootb_pkt_new(asoc, chunk);
3144
3145 if (packet) {
3146 /* Make an ABORT. The T bit will be set if the asoc
3147 * is NULL.
3148 */
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003149 abort = sctp_make_abort(asoc, chunk, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (!abort) {
3151 sctp_ootb_pkt_free(packet);
3152 return SCTP_DISPOSITION_NOMEM;
3153 }
3154
Jerome Forissier047a2422005-04-28 11:58:43 -07003155 /* Reflect vtag if T-Bit is set */
3156 if (sctp_test_T_bit(abort))
3157 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /* Set the skb to the belonging sock for accounting. */
3160 abort->skb->sk = ep->base.sk;
3161
3162 sctp_packet_append_chunk(packet, abort);
3163
3164 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3165 SCTP_PACKET(packet));
3166
3167 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3168
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003169 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return SCTP_DISPOSITION_CONSUME;
3171 }
3172
3173 return SCTP_DISPOSITION_NOMEM;
3174}
3175
3176/*
3177 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3178 * event as ULP notification for each cause included in the chunk.
3179 *
3180 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3181 *
3182 * The return value is the disposition of the chunk.
3183*/
3184sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3185 const struct sctp_association *asoc,
3186 const sctp_subtype_t type,
3187 void *arg,
3188 sctp_cmd_seq_t *commands)
3189{
3190 struct sctp_chunk *chunk = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 if (!sctp_vtag_verify(chunk, asoc))
3193 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3194
3195 /* Make sure that the ERROR chunk has a valid length. */
3196 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3197 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3198 commands);
3199
Wei Yongjun3df267872009-03-02 06:46:51 +00003200 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3201 SCTP_CHUNK(chunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204}
3205
3206/*
3207 * Process an inbound SHUTDOWN ACK.
3208 *
3209 * From Section 9.2:
3210 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3211 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3212 * peer, and remove all record of the association.
3213 *
3214 * The return value is the disposition.
3215 */
3216sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3217 const struct sctp_association *asoc,
3218 const sctp_subtype_t type,
3219 void *arg,
3220 sctp_cmd_seq_t *commands)
3221{
3222 struct sctp_chunk *chunk = arg;
3223 struct sctp_chunk *reply;
3224 struct sctp_ulpevent *ev;
3225
3226 if (!sctp_vtag_verify(chunk, asoc))
3227 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3228
3229 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3230 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3231 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3232 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 /* 10.2 H) SHUTDOWN COMPLETE notification
3234 *
3235 * When SCTP completes the shutdown procedures (section 9.2) this
3236 * notification is passed to the upper layer.
3237 */
3238 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
Vlad Yasevicha5a35e72007-03-23 11:34:08 -07003239 0, 0, 0, NULL, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 if (!ev)
3241 goto nomem;
3242
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003243 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3244 reply = sctp_make_shutdown_complete(asoc, chunk);
3245 if (!reply)
3246 goto nomem_chunk;
3247
3248 /* Do all the commands now (after allocation), so that we
3249 * have consistent state if memory allocation failes
3250 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3252
3253 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3254 * stop the T2-shutdown timer,
3255 */
3256 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3257 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3258
3259 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3260 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3263 SCTP_STATE(SCTP_STATE_CLOSED));
3264 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3265 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3266 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3267
3268 /* ...and remove all record of the association. */
3269 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3270 return SCTP_DISPOSITION_DELETE_TCB;
3271
Vladislav Yasevichdf7deeb2006-08-22 00:19:51 -07003272nomem_chunk:
3273 sctp_ulpevent_free(ev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274nomem:
3275 return SCTP_DISPOSITION_NOMEM;
3276}
3277
3278/*
Jerome Forissier047a2422005-04-28 11:58:43 -07003279 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3280 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3282 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3283 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3284 * packet must fill in the Verification Tag field of the outbound
3285 * packet with the Verification Tag received in the SHUTDOWN ACK and
Jerome Forissier047a2422005-04-28 11:58:43 -07003286 * set the T-bit in the Chunk Flags to indicate that the Verification
3287 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 *
3289 * 8) The receiver should respond to the sender of the OOTB packet with
3290 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3291 * MUST fill in the Verification Tag field of the outbound packet
3292 * with the value found in the Verification Tag field of the OOTB
Jerome Forissier047a2422005-04-28 11:58:43 -07003293 * packet and set the T-bit in the Chunk Flags to indicate that the
3294 * Verification Tag is reflected. After sending this ABORT, the
3295 * receiver of the OOTB packet shall discard the OOTB packet and take
3296 * no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 */
3298sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3299 const struct sctp_association *asoc,
3300 const sctp_subtype_t type,
3301 void *arg,
3302 sctp_cmd_seq_t *commands)
3303{
3304 struct sctp_chunk *chunk = arg;
3305 struct sk_buff *skb = chunk->skb;
3306 sctp_chunkhdr_t *ch;
3307 __u8 *ch_end;
3308 int ootb_shut_ack = 0;
3309
3310 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3311
3312 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3313 do {
Vlad Yasevichece25df2007-09-07 16:30:54 -04003314 /* Report violation if the chunk is less then minimal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
Vlad Yasevichece25df2007-09-07 16:30:54 -04003316 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3317 commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Vlad Yasevichece25df2007-09-07 16:30:54 -04003319 /* Now that we know we at least have a chunk header,
3320 * do things that are type appropriate.
3321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3323 ootb_shut_ack = 1;
3324
3325 /* RFC 2960, Section 3.3.7
3326 * Moreover, under any circumstances, an endpoint that
3327 * receives an ABORT MUST NOT respond to that ABORT by
3328 * sending an ABORT of its own.
3329 */
3330 if (SCTP_CID_ABORT == ch->type)
3331 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003332
Vlad Yasevichece25df2007-09-07 16:30:54 -04003333 /* Report violation if chunk len overflows */
3334 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3335 if (ch_end > skb_tail_pointer(skb))
3336 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3337 commands);
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 ch = (sctp_chunkhdr_t *) ch_end;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07003340 } while (ch_end < skb_tail_pointer(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 if (ootb_shut_ack)
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003343 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 else
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003345 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
3348/*
3349 * Handle an "Out of the blue" SHUTDOWN ACK.
3350 *
Jerome Forissier047a2422005-04-28 11:58:43 -07003351 * Section: 8.4 5, sctpimpguide 2.41.
3352 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
Jerome Forissier047a2422005-04-28 11:58:43 -07003354 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3355 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3356 * packet must fill in the Verification Tag field of the outbound
3357 * packet with the Verification Tag received in the SHUTDOWN ACK and
3358 * set the T-bit in the Chunk Flags to indicate that the Verification
3359 * Tag is reflected.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 *
3361 * Inputs
3362 * (endpoint, asoc, type, arg, commands)
3363 *
3364 * Outputs
3365 * (sctp_disposition_t)
3366 *
3367 * The return value is the disposition of the chunk.
3368 */
3369static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3370 const struct sctp_association *asoc,
3371 const sctp_subtype_t type,
3372 void *arg,
3373 sctp_cmd_seq_t *commands)
3374{
3375 struct sctp_packet *packet = NULL;
3376 struct sctp_chunk *chunk = arg;
3377 struct sctp_chunk *shut;
3378
3379 packet = sctp_ootb_pkt_new(asoc, chunk);
3380
3381 if (packet) {
3382 /* Make an SHUTDOWN_COMPLETE.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003383 * The T bit will be set if the asoc is NULL.
3384 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 shut = sctp_make_shutdown_complete(asoc, chunk);
3386 if (!shut) {
3387 sctp_ootb_pkt_free(packet);
3388 return SCTP_DISPOSITION_NOMEM;
3389 }
3390
Jerome Forissier047a2422005-04-28 11:58:43 -07003391 /* Reflect vtag if T-Bit is set */
3392 if (sctp_test_T_bit(shut))
3393 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3394
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* Set the skb to the belonging sock for accounting. */
3396 shut->skb->sk = ep->base.sk;
3397
3398 sctp_packet_append_chunk(packet, shut);
3399
3400 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3401 SCTP_PACKET(packet));
3402
3403 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3404
3405 /* If the chunk length is invalid, we don't want to process
3406 * the reset of the packet.
3407 */
3408 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3409 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3410
Vlad Yasevichd3f25962007-09-07 11:47:45 -04003411 /* We need to discard the rest of the packet to prevent
3412 * potential bomming attacks from additional bundled chunks.
3413 * This is documented in SCTP Threats ID.
3414 */
3415 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
3417
3418 return SCTP_DISPOSITION_NOMEM;
3419}
3420
3421/*
3422 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3423 *
3424 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3425 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3426 * procedures in section 8.4 SHOULD be followed, in other words it
3427 * should be treated as an Out Of The Blue packet.
3428 * [This means that we do NOT check the Verification Tag on these
3429 * chunks. --piggy ]
3430 *
3431 */
3432sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3433 const struct sctp_association *asoc,
3434 const sctp_subtype_t type,
3435 void *arg,
3436 sctp_cmd_seq_t *commands)
3437{
Vlad Yasevichece25df2007-09-07 16:30:54 -04003438 struct sctp_chunk *chunk = arg;
3439
3440 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3441 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3442 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3443 commands);
3444
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 /* Although we do have an association in this case, it corresponds
3446 * to a restarted association. So the packet is treated as an OOTB
3447 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3448 * called with a NULL association.
3449 */
Wei Yongjun8190f892008-09-08 12:13:55 +08003450 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3453}
3454
3455/* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3456sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3457 const struct sctp_association *asoc,
3458 const sctp_subtype_t type, void *arg,
3459 sctp_cmd_seq_t *commands)
3460{
3461 struct sctp_chunk *chunk = arg;
3462 struct sctp_chunk *asconf_ack = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003463 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 sctp_addiphdr_t *hdr;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003465 union sctp_addr_param *addr_param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 __u32 serial;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003467 int length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
3469 if (!sctp_vtag_verify(chunk, asoc)) {
3470 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3471 SCTP_NULL());
3472 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3473 }
3474
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003475 /* ADD-IP: Section 4.1.1
3476 * This chunk MUST be sent in an authenticated way by using
3477 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3478 * is received unauthenticated it MUST be silently discarded as
3479 * described in [I-D.ietf-tsvwg-sctp-auth].
3480 */
3481 if (!sctp_addip_noauth && !chunk->auth)
3482 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3485 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3486 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3487 commands);
3488
3489 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3490 serial = ntohl(hdr->serial);
3491
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003492 addr_param = (union sctp_addr_param *)hdr->params;
3493 length = ntohs(addr_param->p.length);
3494 if (length < sizeof(sctp_paramhdr_t))
Wei Yongjunba016672008-09-30 05:32:24 -07003495 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003496 (void *)addr_param, commands);
3497
3498 /* Verify the ASCONF chunk before processing it. */
3499 if (!sctp_verify_asconf(asoc,
Vlad Yasevicha08de642007-12-20 14:11:47 -08003500 (sctp_paramhdr_t *)((void *)addr_param + length),
3501 (void *)chunk->chunk_end,
3502 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003503 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3504 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003505
Vlad Yasevicha08de642007-12-20 14:11:47 -08003506 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 * the endpoint stored in a new association variable
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003508 * 'Peer-Serial-Number'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 */
3510 if (serial == asoc->peer.addip_serial + 1) {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003511 /* If this is the first instance of ASCONF in the packet,
3512 * we can clean our old ASCONF-ACKs.
3513 */
3514 if (!chunk->has_asconf)
3515 sctp_assoc_clean_asconf_ack_cache(asoc);
3516
3517 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3518 * expected, process the ASCONF as described below and after
3519 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3520 * the response packet and cache a copy of it (in the event it
3521 * later needs to be retransmitted).
3522 *
3523 * Essentially, do V1-V5.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 */
3525 asconf_ack = sctp_process_asconf((struct sctp_association *)
3526 asoc, chunk);
3527 if (!asconf_ack)
3528 return SCTP_DISPOSITION_NOMEM;
Vlad Yasevicha08de642007-12-20 14:11:47 -08003529 } else if (serial < asoc->peer.addip_serial + 1) {
3530 /* ADDIP 5.2 E2)
3531 * If the value found in the Sequence Number is less than the
3532 * ('Peer- Sequence-Number' + 1), simply skip to the next
3533 * ASCONF, and include in the outbound response packet
3534 * any previously cached ASCONF-ACK response that was
3535 * sent and saved that matches the Sequence Number of the
3536 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3537 * Chunk exists. This will occur when an older ASCONF
3538 * arrives out of order. In such a case, the receiver
3539 * should skip the ASCONF Chunk and not include ASCONF-ACK
3540 * Chunk for that chunk.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003542 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3543 if (!asconf_ack)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return SCTP_DISPOSITION_DISCARD;
3545 } else {
Vlad Yasevicha08de642007-12-20 14:11:47 -08003546 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 * it must be either a stale packet or from an attacker.
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 return SCTP_DISPOSITION_DISCARD;
3550 }
3551
Vlad Yasevicha08de642007-12-20 14:11:47 -08003552 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3553 * containing the ASCONF-ACK Chunks MUST be the source address of
3554 * the SCTP packet that held the ASCONF Chunks.
3555 *
3556 * To do this properly, we'll set the destination address of the chunk
3557 * and at the transmit time, will try look up the transport to use.
3558 * Since ASCONFs may be bundled, the correct transport may not be
3559 * created untill we process the entire packet, thus this workaround.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 */
Vlad Yasevicha08de642007-12-20 14:11:47 -08003561 asconf_ack->dest = chunk->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003563
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 return SCTP_DISPOSITION_CONSUME;
3565}
3566
3567/*
3568 * ADDIP Section 4.3 General rules for address manipulation
3569 * When building TLV parameters for the ASCONF Chunk that will add or
3570 * delete IP addresses the D0 to D13 rules should be applied:
3571 */
3572sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3573 const struct sctp_association *asoc,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003574 const sctp_subtype_t type, void *arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 sctp_cmd_seq_t *commands)
3576{
3577 struct sctp_chunk *asconf_ack = arg;
3578 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3579 struct sctp_chunk *abort;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003580 struct sctp_paramhdr *err_param = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 sctp_addiphdr_t *addip_hdr;
3582 __u32 sent_serial, rcvd_serial;
3583
3584 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3585 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3586 SCTP_NULL());
3587 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3588 }
3589
Vlad Yasevich6afd2e82007-12-20 14:08:04 -08003590 /* ADD-IP, Section 4.1.2:
3591 * This chunk MUST be sent in an authenticated way by using
3592 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3593 * is received unauthenticated it MUST be silently discarded as
3594 * described in [I-D.ietf-tsvwg-sctp-auth].
3595 */
3596 if (!sctp_addip_noauth && !asconf_ack->auth)
3597 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 /* Make sure that the ADDIP chunk has a valid length. */
3600 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3601 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3602 commands);
3603
3604 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3605 rcvd_serial = ntohl(addip_hdr->serial);
3606
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003607 /* Verify the ASCONF-ACK chunk before processing it. */
3608 if (!sctp_verify_asconf(asoc,
3609 (sctp_paramhdr_t *)addip_hdr->params,
3610 (void *)asconf_ack->chunk_end,
3611 &err_param))
Wei Yongjunba016672008-09-30 05:32:24 -07003612 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3613 (void *)err_param, commands);
Wei Yongjun6f4c6182007-09-19 17:19:52 +08003614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 if (last_asconf) {
3616 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3617 sent_serial = ntohl(addip_hdr->serial);
3618 } else {
3619 sent_serial = asoc->addip_serial - 1;
3620 }
3621
3622 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3623 * equal to the next serial number to be used but no ASCONF chunk is
3624 * outstanding the endpoint MUST ABORT the association. Note that a
3625 * sequence number is greater than if it is no more than 2^^31-1
3626 * larger than the current sequence number (using serial arithmetic).
3627 */
3628 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3629 !(asoc->addip_last_asconf)) {
3630 abort = sctp_make_abort(asoc, asconf_ack,
3631 sizeof(sctp_errhdr_t));
3632 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003633 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3635 SCTP_CHUNK(abort));
3636 }
3637 /* We are going to ABORT, so we might as well stop
3638 * processing the rest of the chunks in the packet.
3639 */
3640 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3641 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3642 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003643 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003644 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003646 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3648 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3649 return SCTP_DISPOSITION_ABORT;
3650 }
3651
3652 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3653 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3654 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3655
3656 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3657 asconf_ack))
3658 return SCTP_DISPOSITION_CONSUME;
3659
3660 abort = sctp_make_abort(asoc, asconf_ack,
3661 sizeof(sctp_errhdr_t));
3662 if (abort) {
Wei Yongjun00f1c2d2007-08-21 15:50:01 +08003663 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3665 SCTP_CHUNK(abort));
3666 }
3667 /* We are going to ABORT, so we might as well stop
3668 * processing the rest of the chunks in the packet.
3669 */
3670 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07003671 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003672 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08003674 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3676 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3677 return SCTP_DISPOSITION_ABORT;
3678 }
3679
3680 return SCTP_DISPOSITION_DISCARD;
3681}
3682
3683/*
3684 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3685 *
3686 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3687 * its cumulative TSN point to the value carried in the FORWARD TSN
3688 * chunk, and then MUST further advance its cumulative TSN point locally
3689 * if possible.
3690 * After the above processing, the data receiver MUST stop reporting any
3691 * missing TSNs earlier than or equal to the new cumulative TSN point.
3692 *
3693 * Verification Tag: 8.5 Verification Tag [Normal verification]
3694 *
3695 * The return value is the disposition of the chunk.
3696 */
3697sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3698 const struct sctp_association *asoc,
3699 const sctp_subtype_t type,
3700 void *arg,
3701 sctp_cmd_seq_t *commands)
3702{
3703 struct sctp_chunk *chunk = arg;
3704 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003705 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 __u16 len;
3707 __u32 tsn;
3708
3709 if (!sctp_vtag_verify(chunk, asoc)) {
3710 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3711 SCTP_NULL());
3712 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3713 }
3714
3715 /* Make sure that the FORWARD_TSN chunk has valid length. */
3716 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3717 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3718 commands);
3719
3720 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3721 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3722 len = ntohs(chunk->chunk_hdr->length);
3723 len -= sizeof(struct sctp_chunkhdr);
3724 skb_pull(chunk->skb, len);
3725
3726 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003727 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 /* The TSN is too high--silently discard the chunk and count on it
3730 * getting retransmitted later.
3731 */
3732 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3733 goto discard_noforce;
3734
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003735 /* Silently discard the chunk if stream-id is not valid */
3736 sctp_walk_fwdtsn(skip, chunk) {
3737 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3738 goto discard_noforce;
3739 }
3740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3742 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003743 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 /* Count this as receiving DATA. */
3747 if (asoc->autoclose) {
3748 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3749 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3750 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 /* FIXME: For now send a SACK, but DATA processing may
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003753 * send another.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 */
3755 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756
3757 return SCTP_DISPOSITION_CONSUME;
3758
3759discard_noforce:
3760 return SCTP_DISPOSITION_DISCARD;
3761}
3762
3763sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3764 const struct sctp_endpoint *ep,
3765 const struct sctp_association *asoc,
3766 const sctp_subtype_t type,
3767 void *arg,
3768 sctp_cmd_seq_t *commands)
3769{
3770 struct sctp_chunk *chunk = arg;
3771 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003772 struct sctp_fwdtsn_skip *skip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 __u16 len;
3774 __u32 tsn;
3775
3776 if (!sctp_vtag_verify(chunk, asoc)) {
3777 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3778 SCTP_NULL());
3779 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3780 }
3781
3782 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3783 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3784 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3785 commands);
3786
3787 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3788 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3789 len = ntohs(chunk->chunk_hdr->length);
3790 len -= sizeof(struct sctp_chunkhdr);
3791 skb_pull(chunk->skb, len);
3792
3793 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
Harvey Harrison0dc47872008-03-05 20:47:47 -08003794 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796 /* The TSN is too high--silently discard the chunk and count on it
3797 * getting retransmitted later.
3798 */
3799 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3800 goto gen_shutdown;
3801
Wei Yongjun9fcb95a2008-12-25 16:58:11 -08003802 /* Silently discard the chunk if stream-id is not valid */
3803 sctp_walk_fwdtsn(skip, chunk) {
3804 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3805 goto gen_shutdown;
3806 }
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3809 if (len > sizeof(struct sctp_fwdtsn_hdr))
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003810 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 SCTP_CHUNK(chunk));
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 /* Go a head and force a SACK, since we are shutting down. */
3814gen_shutdown:
3815 /* Implementor's Guide.
3816 *
3817 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3818 * respond to each received packet containing one or more DATA chunk(s)
3819 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3820 */
3821 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3822 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3823 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3824 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3825
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09003826 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827}
3828
3829/*
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003830 * SCTP-AUTH Section 6.3 Receving authenticated chukns
3831 *
3832 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3833 * Identifier field. If this algorithm was not specified by the
3834 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3835 * during association setup, the AUTH chunk and all chunks after it MUST
3836 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3837 * defined in Section 4.1.
3838 *
3839 * If an endpoint with no shared key receives a Shared Key Identifier
3840 * other than 0, it MUST silently discard all authenticated chunks. If
3841 * the endpoint has at least one endpoint pair shared key for the peer,
3842 * it MUST use the key specified by the Shared Key Identifier if a
3843 * key has been configured for that Shared Key Identifier. If no
3844 * endpoint pair shared key has been configured for that Shared Key
3845 * Identifier, all authenticated chunks MUST be silently discarded.
3846 *
3847 * Verification Tag: 8.5 Verification Tag [Normal verification]
3848 *
3849 * The return value is the disposition of the chunk.
3850 */
3851static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3852 const struct sctp_association *asoc,
3853 const sctp_subtype_t type,
3854 struct sctp_chunk *chunk)
3855{
3856 struct sctp_authhdr *auth_hdr;
3857 struct sctp_hmac *hmac;
3858 unsigned int sig_len;
3859 __u16 key_id;
3860 __u8 *save_digest;
3861 __u8 *digest;
3862
3863 /* Pull in the auth header, so we can do some more verification */
3864 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3865 chunk->subh.auth_hdr = auth_hdr;
3866 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3867
3868 /* Make sure that we suport the HMAC algorithm from the auth
3869 * chunk.
3870 */
3871 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3872 return SCTP_IERROR_AUTH_BAD_HMAC;
3873
3874 /* Make sure that the provided shared key identifier has been
3875 * configured
3876 */
3877 key_id = ntohs(auth_hdr->shkey_id);
3878 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3879 return SCTP_IERROR_AUTH_BAD_KEYID;
3880
3881
3882 /* Make sure that the length of the signature matches what
3883 * we expect.
3884 */
3885 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3886 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3887 if (sig_len != hmac->hmac_len)
3888 return SCTP_IERROR_PROTO_VIOLATION;
3889
3890 /* Now that we've done validation checks, we can compute and
3891 * verify the hmac. The steps involved are:
3892 * 1. Save the digest from the chunk.
3893 * 2. Zero out the digest in the chunk.
3894 * 3. Compute the new digest
3895 * 4. Compare saved and new digests.
3896 */
3897 digest = auth_hdr->hmac;
3898 skb_pull(chunk->skb, sig_len);
3899
3900 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3901 if (!save_digest)
3902 goto nomem;
3903
3904 memset(digest, 0, sig_len);
3905
3906 sctp_auth_calculate_hmac(asoc, chunk->skb,
3907 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3908 GFP_ATOMIC);
3909
3910 /* Discard the packet if the digests do not match */
3911 if (memcmp(save_digest, digest, sig_len)) {
3912 kfree(save_digest);
3913 return SCTP_IERROR_BAD_SIG;
3914 }
3915
3916 kfree(save_digest);
3917 chunk->auth = 1;
3918
3919 return SCTP_IERROR_NO_ERROR;
3920nomem:
3921 return SCTP_IERROR_NOMEM;
3922}
3923
3924sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3925 const struct sctp_association *asoc,
3926 const sctp_subtype_t type,
3927 void *arg,
3928 sctp_cmd_seq_t *commands)
3929{
3930 struct sctp_authhdr *auth_hdr;
3931 struct sctp_chunk *chunk = arg;
3932 struct sctp_chunk *err_chunk;
3933 sctp_ierror_t error;
3934
Wei Yongjund2f19fa2008-02-05 03:02:26 -08003935 /* Make sure that the peer has AUTH capable */
3936 if (!asoc->peer.auth_capable)
3937 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3938
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003939 if (!sctp_vtag_verify(chunk, asoc)) {
3940 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3941 SCTP_NULL());
3942 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3943 }
3944
3945 /* Make sure that the AUTH chunk has valid length. */
3946 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
3947 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3948 commands);
3949
3950 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3951 error = sctp_sf_authenticate(ep, asoc, type, chunk);
3952 switch (error) {
3953 case SCTP_IERROR_AUTH_BAD_HMAC:
3954 /* Generate the ERROR chunk and discard the rest
3955 * of the packet
3956 */
3957 err_chunk = sctp_make_op_error(asoc, chunk,
3958 SCTP_ERROR_UNSUP_HMAC,
3959 &auth_hdr->hmac_id,
3960 sizeof(__u16));
3961 if (err_chunk) {
3962 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3963 SCTP_CHUNK(err_chunk));
3964 }
3965 /* Fall Through */
3966 case SCTP_IERROR_AUTH_BAD_KEYID:
3967 case SCTP_IERROR_BAD_SIG:
3968 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3969 break;
3970 case SCTP_IERROR_PROTO_VIOLATION:
3971 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3972 commands);
3973 break;
3974 case SCTP_IERROR_NOMEM:
3975 return SCTP_DISPOSITION_NOMEM;
3976 default:
3977 break;
3978 }
3979
Vlad Yasevich65b07e52007-09-16 19:34:00 -07003980 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
3981 struct sctp_ulpevent *ev;
3982
3983 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
3984 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
3985
3986 if (!ev)
3987 return -ENOMEM;
3988
3989 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3990 SCTP_ULPEVENT(ev));
3991 }
3992
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07003993 return SCTP_DISPOSITION_CONSUME;
3994}
3995
3996/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 * Process an unknown chunk.
3998 *
3999 * Section: 3.2. Also, 2.1 in the implementor's guide.
4000 *
4001 * Chunk Types are encoded such that the highest-order two bits specify
4002 * the action that must be taken if the processing endpoint does not
4003 * recognize the Chunk Type.
4004 *
4005 * 00 - Stop processing this SCTP packet and discard it, do not process
4006 * any further chunks within it.
4007 *
4008 * 01 - Stop processing this SCTP packet and discard it, do not process
4009 * any further chunks within it, and report the unrecognized
4010 * chunk in an 'Unrecognized Chunk Type'.
4011 *
4012 * 10 - Skip this chunk and continue processing.
4013 *
4014 * 11 - Skip this chunk and continue processing, but report in an ERROR
4015 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4016 *
4017 * The return value is the disposition of the chunk.
4018 */
4019sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4020 const struct sctp_association *asoc,
4021 const sctp_subtype_t type,
4022 void *arg,
4023 sctp_cmd_seq_t *commands)
4024{
4025 struct sctp_chunk *unk_chunk = arg;
4026 struct sctp_chunk *err_chunk;
4027 sctp_chunkhdr_t *hdr;
4028
4029 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4030
4031 if (!sctp_vtag_verify(unk_chunk, asoc))
4032 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4033
4034 /* Make sure that the chunk has a valid length.
4035 * Since we don't know the chunk type, we use a general
4036 * chunkhdr structure to make a comparison.
4037 */
4038 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4039 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4040 commands);
4041
4042 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4043 case SCTP_CID_ACTION_DISCARD:
4044 /* Discard the packet. */
4045 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4046 break;
4047 case SCTP_CID_ACTION_DISCARD_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 /* Generate an ERROR chunk as response. */
4049 hdr = unk_chunk->chunk_hdr;
4050 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4051 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4052 WORD_ROUND(ntohs(hdr->length)));
4053 if (err_chunk) {
4054 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4055 SCTP_CHUNK(err_chunk));
4056 }
Vlad Yasevich2e3216c2008-06-19 16:08:18 -07004057
4058 /* Discard the packet. */
4059 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 return SCTP_DISPOSITION_CONSUME;
4061 break;
4062 case SCTP_CID_ACTION_SKIP:
4063 /* Skip the chunk. */
4064 return SCTP_DISPOSITION_DISCARD;
4065 break;
4066 case SCTP_CID_ACTION_SKIP_ERR:
4067 /* Generate an ERROR chunk as response. */
4068 hdr = unk_chunk->chunk_hdr;
4069 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4070 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4071 WORD_ROUND(ntohs(hdr->length)));
4072 if (err_chunk) {
4073 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4074 SCTP_CHUNK(err_chunk));
4075 }
4076 /* Skip the chunk. */
4077 return SCTP_DISPOSITION_CONSUME;
4078 break;
4079 default:
4080 break;
4081 }
4082
4083 return SCTP_DISPOSITION_DISCARD;
4084}
4085
4086/*
4087 * Discard the chunk.
4088 *
4089 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4090 * [Too numerous to mention...]
4091 * Verification Tag: No verification needed.
4092 * Inputs
4093 * (endpoint, asoc, chunk)
4094 *
4095 * Outputs
4096 * (asoc, reply_msg, msg_up, timers, counters)
4097 *
4098 * The return value is the disposition of the chunk.
4099 */
4100sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4101 const struct sctp_association *asoc,
4102 const sctp_subtype_t type,
4103 void *arg,
4104 sctp_cmd_seq_t *commands)
4105{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004106 struct sctp_chunk *chunk = arg;
4107
4108 /* Make sure that the chunk has a valid length.
4109 * Since we don't know the chunk type, we use a general
4110 * chunkhdr structure to make a comparison.
4111 */
4112 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4113 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4114 commands);
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4117 return SCTP_DISPOSITION_DISCARD;
4118}
4119
4120/*
4121 * Discard the whole packet.
4122 *
4123 * Section: 8.4 2)
4124 *
4125 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4126 * silently discard the OOTB packet and take no further action.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 *
4128 * Verification Tag: No verification necessary
4129 *
4130 * Inputs
4131 * (endpoint, asoc, chunk)
4132 *
4133 * Outputs
4134 * (asoc, reply_msg, msg_up, timers, counters)
4135 *
4136 * The return value is the disposition of the chunk.
4137 */
4138sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4139 const struct sctp_association *asoc,
4140 const sctp_subtype_t type,
4141 void *arg,
4142 sctp_cmd_seq_t *commands)
4143{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07004144 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4146
4147 return SCTP_DISPOSITION_CONSUME;
4148}
4149
4150
4151/*
4152 * The other end is violating protocol.
4153 *
4154 * Section: Not specified
4155 * Verification Tag: Not specified
4156 * Inputs
4157 * (endpoint, asoc, chunk)
4158 *
4159 * Outputs
4160 * (asoc, reply_msg, msg_up, timers, counters)
4161 *
4162 * We simply tag the chunk as a violation. The state machine will log
4163 * the violation and continue.
4164 */
4165sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4166 const struct sctp_association *asoc,
4167 const sctp_subtype_t type,
4168 void *arg,
4169 sctp_cmd_seq_t *commands)
4170{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004171 struct sctp_chunk *chunk = arg;
4172
4173 /* Make sure that the chunk has a valid length. */
4174 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4175 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4176 commands);
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 return SCTP_DISPOSITION_VIOLATION;
4179}
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181/*
Wei Yongjunaecedea2007-08-02 16:57:44 +08004182 * Common function to handle a protocol violation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 */
Wei Yongjunaecedea2007-08-02 16:57:44 +08004184static sctp_disposition_t sctp_sf_abort_violation(
Vlad Yasevichece25df2007-09-07 16:30:54 -04004185 const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 const struct sctp_association *asoc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 void *arg,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004188 sctp_cmd_seq_t *commands,
4189 const __u8 *payload,
4190 const size_t paylen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
Vlad Yasevichece25df2007-09-07 16:30:54 -04004192 struct sctp_packet *packet = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 struct sctp_chunk *chunk = arg;
4194 struct sctp_chunk *abort = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004196 /* SCTP-AUTH, Section 6.3:
4197 * It should be noted that if the receiver wants to tear
4198 * down an association in an authenticated way only, the
4199 * handling of malformed packets should not result in
4200 * tearing down the association.
4201 *
4202 * This means that if we only want to abort associations
4203 * in an authenticated way (i.e AUTH+ABORT), then we
Oliver Pinterac461a02008-02-03 17:52:41 +02004204 * can't destroy this association just becuase the packet
Vlad Yasevichbbd0d592007-10-03 17:51:34 -07004205 * was malformed.
4206 */
4207 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4208 goto discard;
4209
Jesper Juhl9abed242007-11-11 23:57:49 +01004210 /* Make the abort chunk. */
4211 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4212 if (!abort)
4213 goto nomem;
4214
Vlad Yasevichece25df2007-09-07 16:30:54 -04004215 if (asoc) {
Gui Jianfengf4ad85c2008-04-12 18:39:34 -07004216 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4217 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4218 !asoc->peer.i.init_tag) {
4219 sctp_initack_chunk_t *initack;
4220
4221 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4222 if (!sctp_chunk_length_valid(chunk,
4223 sizeof(sctp_initack_chunk_t)))
4224 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4225 else {
4226 unsigned int inittag;
4227
4228 inittag = ntohl(initack->init_hdr.init_tag);
4229 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4230 SCTP_U32(inittag));
4231 }
4232 }
4233
Vlad Yasevichece25df2007-09-07 16:30:54 -04004234 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4235 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
Vlad Yasevichece25df2007-09-07 16:30:54 -04004237 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4238 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4239 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4240 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4241 SCTP_ERROR(ECONNREFUSED));
4242 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4243 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4244 } else {
4245 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4246 SCTP_ERROR(ECONNABORTED));
4247 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4248 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4249 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 } else {
Vlad Yasevichece25df2007-09-07 16:30:54 -04004252 packet = sctp_ootb_pkt_new(asoc, chunk);
4253
4254 if (!packet)
4255 goto nomem_pkt;
4256
4257 if (sctp_test_T_bit(abort))
4258 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4259
4260 abort->skb->sk = ep->base.sk;
4261
4262 sctp_packet_append_chunk(packet, abort);
4263
4264 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4265 SCTP_PACKET(packet));
4266
4267 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 }
4269
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09004271
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004272discard:
4273 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return SCTP_DISPOSITION_ABORT;
4275
Vlad Yasevichece25df2007-09-07 16:30:54 -04004276nomem_pkt:
4277 sctp_chunk_free(abort);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278nomem:
4279 return SCTP_DISPOSITION_NOMEM;
4280}
4281
Wei Yongjunaecedea2007-08-02 16:57:44 +08004282/*
4283 * Handle a protocol violation when the chunk length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004284 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjunaecedea2007-08-02 16:57:44 +08004285 * given chunk can be. For example, a SACK chunk has invalid length
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004286 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
Wei Yongjunaecedea2007-08-02 16:57:44 +08004287 *
4288 * We inform the other end by sending an ABORT with a Protocol Violation
4289 * error code.
4290 *
4291 * Section: Not specified
4292 * Verification Tag: Nothing to do
4293 * Inputs
4294 * (endpoint, asoc, chunk)
4295 *
4296 * Outputs
4297 * (reply_msg, msg_up, counters)
4298 *
4299 * Generate an ABORT chunk and terminate the association.
4300 */
4301static sctp_disposition_t sctp_sf_violation_chunklen(
4302 const struct sctp_endpoint *ep,
4303 const struct sctp_association *asoc,
4304 const sctp_subtype_t type,
4305 void *arg,
4306 sctp_cmd_seq_t *commands)
4307{
Florian Westphal24448442008-03-23 22:46:34 -07004308 static const char err_str[]="The following chunk had invalid length:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004309
Vlad Yasevichece25df2007-09-07 16:30:54 -04004310 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004311 sizeof(err_str));
4312}
4313
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004314/*
4315 * Handle a protocol violation when the parameter length is invalid.
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02004316 * "Invalid" length is identified as smaller than the minimal length a
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004317 * given parameter can be.
4318 */
4319static sctp_disposition_t sctp_sf_violation_paramlen(
4320 const struct sctp_endpoint *ep,
4321 const struct sctp_association *asoc,
4322 const sctp_subtype_t type,
Wei Yongjunba016672008-09-30 05:32:24 -07004323 void *arg, void *ext,
4324 sctp_cmd_seq_t *commands)
4325{
4326 struct sctp_chunk *chunk = arg;
4327 struct sctp_paramhdr *param = ext;
4328 struct sctp_chunk *abort = NULL;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004329
Wei Yongjunba016672008-09-30 05:32:24 -07004330 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4331 goto discard;
4332
4333 /* Make the abort chunk. */
4334 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4335 if (!abort)
4336 goto nomem;
4337
4338 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4339 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4340
4341 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4342 SCTP_ERROR(ECONNABORTED));
4343 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4344 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4345 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Vlad Yasevich56eb82b2008-10-09 14:33:01 -07004346 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjunba016672008-09-30 05:32:24 -07004347
4348discard:
4349 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
Wei Yongjunba016672008-09-30 05:32:24 -07004350 return SCTP_DISPOSITION_ABORT;
4351nomem:
4352 return SCTP_DISPOSITION_NOMEM;
Wei Yongjun6f4c6182007-09-19 17:19:52 +08004353}
4354
Wei Yongjunaecedea2007-08-02 16:57:44 +08004355/* Handle a protocol violation when the peer trying to advance the
4356 * cumulative tsn ack to a point beyond the max tsn currently sent.
4357 *
4358 * We inform the other end by sending an ABORT with a Protocol Violation
4359 * error code.
4360 */
4361static sctp_disposition_t sctp_sf_violation_ctsn(
4362 const struct sctp_endpoint *ep,
4363 const struct sctp_association *asoc,
4364 const sctp_subtype_t type,
4365 void *arg,
4366 sctp_cmd_seq_t *commands)
4367{
Florian Westphal24448442008-03-23 22:46:34 -07004368 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
Wei Yongjunaecedea2007-08-02 16:57:44 +08004369
Vlad Yasevichece25df2007-09-07 16:30:54 -04004370 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
Wei Yongjunaecedea2007-08-02 16:57:44 +08004371 sizeof(err_str));
4372}
4373
Vlad Yasevichece25df2007-09-07 16:30:54 -04004374/* Handle protocol violation of an invalid chunk bundling. For example,
4375 * when we have an association and we recieve bundled INIT-ACK, or
4376 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4377 * statement from the specs. Additinally, there might be an attacker
4378 * on the path and we may not want to continue this communication.
4379 */
4380static sctp_disposition_t sctp_sf_violation_chunk(
4381 const struct sctp_endpoint *ep,
4382 const struct sctp_association *asoc,
4383 const sctp_subtype_t type,
4384 void *arg,
4385 sctp_cmd_seq_t *commands)
4386{
Florian Westphal24448442008-03-23 22:46:34 -07004387 static const char err_str[]="The following chunk violates protocol:";
Vlad Yasevichece25df2007-09-07 16:30:54 -04004388
4389 if (!asoc)
4390 return sctp_sf_violation(ep, asoc, type, arg, commands);
4391
4392 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4393 sizeof(err_str));
4394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395/***************************************************************************
4396 * These are the state functions for handling primitive (Section 10) events.
4397 ***************************************************************************/
4398/*
4399 * sctp_sf_do_prm_asoc
4400 *
4401 * Section: 10.1 ULP-to-SCTP
4402 * B) Associate
4403 *
4404 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4405 * outbound stream count)
4406 * -> association id [,destination transport addr list] [,outbound stream
4407 * count]
4408 *
4409 * This primitive allows the upper layer to initiate an association to a
4410 * specific peer endpoint.
4411 *
4412 * The peer endpoint shall be specified by one of the transport addresses
4413 * which defines the endpoint (see Section 1.4). If the local SCTP
4414 * instance has not been initialized, the ASSOCIATE is considered an
4415 * error.
4416 * [This is not relevant for the kernel implementation since we do all
4417 * initialization at boot time. It we hadn't initialized we wouldn't
4418 * get anywhere near this code.]
4419 *
4420 * An association id, which is a local handle to the SCTP association,
4421 * will be returned on successful establishment of the association. If
4422 * SCTP is not able to open an SCTP association with the peer endpoint,
4423 * an error is returned.
4424 * [In the kernel implementation, the struct sctp_association needs to
4425 * be created BEFORE causing this primitive to run.]
4426 *
4427 * Other association parameters may be returned, including the
4428 * complete destination transport addresses of the peer as well as the
4429 * outbound stream count of the local endpoint. One of the transport
4430 * address from the returned destination addresses will be selected by
4431 * the local endpoint as default primary path for sending SCTP packets
4432 * to this peer. The returned "destination transport addr list" can
4433 * be used by the ULP to change the default primary path or to force
4434 * sending a packet to a specific transport address. [All of this
4435 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4436 * function.]
4437 *
4438 * Mandatory attributes:
4439 *
4440 * o local SCTP instance name - obtained from the INITIALIZE operation.
4441 * [This is the argument asoc.]
4442 * o destination transport addr - specified as one of the transport
4443 * addresses of the peer endpoint with which the association is to be
4444 * established.
4445 * [This is asoc->peer.active_path.]
4446 * o outbound stream count - the number of outbound streams the ULP
4447 * would like to open towards this peer endpoint.
4448 * [BUG: This is not currently implemented.]
4449 * Optional attributes:
4450 *
4451 * None.
4452 *
4453 * The return value is a disposition.
4454 */
4455sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4456 const struct sctp_association *asoc,
4457 const sctp_subtype_t type,
4458 void *arg,
4459 sctp_cmd_seq_t *commands)
4460{
4461 struct sctp_chunk *repl;
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004462 struct sctp_association* my_asoc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 /* The comment below says that we enter COOKIE-WAIT AFTER
4465 * sending the INIT, but that doesn't actually work in our
4466 * implementation...
4467 */
4468 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4469 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4470
4471 /* RFC 2960 5.1 Normal Establishment of an Association
4472 *
4473 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4474 * must provide its Verification Tag (Tag_A) in the Initiate
4475 * Tag field. Tag_A SHOULD be a random number in the range of
4476 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4477 */
4478
4479 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4480 if (!repl)
4481 goto nomem;
4482
4483 /* Cast away the const modifier, as we want to just
4484 * rerun it through as a sideffect.
4485 */
Vlad Yasevichab38fb02008-04-12 18:40:06 -07004486 my_asoc = (struct sctp_association *)asoc;
4487 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Frank Filz3f7a87d2005-06-20 13:14:57 -07004489 /* Choose transport for INIT. */
4490 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4491 SCTP_CHUNK(repl));
4492
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 /* After sending the INIT, "A" starts the T1-init timer and
4494 * enters the COOKIE-WAIT state.
4495 */
4496 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4497 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4498 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4499 return SCTP_DISPOSITION_CONSUME;
4500
4501nomem:
4502 return SCTP_DISPOSITION_NOMEM;
4503}
4504
4505/*
4506 * Process the SEND primitive.
4507 *
4508 * Section: 10.1 ULP-to-SCTP
4509 * E) Send
4510 *
4511 * Format: SEND(association id, buffer address, byte count [,context]
4512 * [,stream id] [,life time] [,destination transport address]
4513 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4514 * -> result
4515 *
4516 * This is the main method to send user data via SCTP.
4517 *
4518 * Mandatory attributes:
4519 *
4520 * o association id - local handle to the SCTP association
4521 *
4522 * o buffer address - the location where the user message to be
4523 * transmitted is stored;
4524 *
4525 * o byte count - The size of the user data in number of bytes;
4526 *
4527 * Optional attributes:
4528 *
4529 * o context - an optional 32 bit integer that will be carried in the
4530 * sending failure notification to the ULP if the transportation of
4531 * this User Message fails.
4532 *
4533 * o stream id - to indicate which stream to send the data on. If not
4534 * specified, stream 0 will be used.
4535 *
4536 * o life time - specifies the life time of the user data. The user data
4537 * will not be sent by SCTP after the life time expires. This
4538 * parameter can be used to avoid efforts to transmit stale
4539 * user messages. SCTP notifies the ULP if the data cannot be
4540 * initiated to transport (i.e. sent to the destination via SCTP's
4541 * send primitive) within the life time variable. However, the
4542 * user data will be transmitted if SCTP has attempted to transmit a
4543 * chunk before the life time expired.
4544 *
4545 * o destination transport address - specified as one of the destination
4546 * transport addresses of the peer endpoint to which this packet
4547 * should be sent. Whenever possible, SCTP should use this destination
4548 * transport address for sending the packets, instead of the current
4549 * primary path.
4550 *
4551 * o unorder flag - this flag, if present, indicates that the user
4552 * would like the data delivered in an unordered fashion to the peer
4553 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4554 * message).
4555 *
4556 * o no-bundle flag - instructs SCTP not to bundle this user data with
4557 * other outbound DATA chunks. SCTP MAY still bundle even when
4558 * this flag is present, when faced with network congestion.
4559 *
4560 * o payload protocol-id - A 32 bit unsigned integer that is to be
4561 * passed to the peer indicating the type of payload protocol data
4562 * being transmitted. This value is passed as opaque data by SCTP.
4563 *
4564 * The return value is the disposition.
4565 */
4566sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4567 const struct sctp_association *asoc,
4568 const sctp_subtype_t type,
4569 void *arg,
4570 sctp_cmd_seq_t *commands)
4571{
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004572 struct sctp_datamsg *msg = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
Vlad Yasevich9c5c62b2009-08-10 13:51:03 -04004574 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 return SCTP_DISPOSITION_CONSUME;
4576}
4577
4578/*
4579 * Process the SHUTDOWN primitive.
4580 *
4581 * Section: 10.1:
4582 * C) Shutdown
4583 *
4584 * Format: SHUTDOWN(association id)
4585 * -> result
4586 *
4587 * Gracefully closes an association. Any locally queued user data
4588 * will be delivered to the peer. The association will be terminated only
4589 * after the peer acknowledges all the SCTP packets sent. A success code
4590 * will be returned on successful termination of the association. If
4591 * attempting to terminate the association results in a failure, an error
4592 * code shall be returned.
4593 *
4594 * Mandatory attributes:
4595 *
4596 * o association id - local handle to the SCTP association
4597 *
4598 * Optional attributes:
4599 *
4600 * None.
4601 *
4602 * The return value is the disposition.
4603 */
4604sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4605 const struct sctp_endpoint *ep,
4606 const struct sctp_association *asoc,
4607 const sctp_subtype_t type,
4608 void *arg,
4609 sctp_cmd_seq_t *commands)
4610{
4611 int disposition;
4612
4613 /* From 9.2 Shutdown of an Association
4614 * Upon receipt of the SHUTDOWN primitive from its upper
4615 * layer, the endpoint enters SHUTDOWN-PENDING state and
4616 * remains there until all outstanding data has been
4617 * acknowledged by its peer. The endpoint accepts no new data
4618 * from its upper layer, but retransmits data to the far end
4619 * if necessary to fill gaps.
4620 */
4621 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4622 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 disposition = SCTP_DISPOSITION_CONSUME;
4625 if (sctp_outq_is_empty(&asoc->outqueue)) {
4626 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4627 arg, commands);
4628 }
4629 return disposition;
4630}
4631
4632/*
4633 * Process the ABORT primitive.
4634 *
4635 * Section: 10.1:
4636 * C) Abort
4637 *
4638 * Format: Abort(association id [, cause code])
4639 * -> result
4640 *
4641 * Ungracefully closes an association. Any locally queued user data
4642 * will be discarded and an ABORT chunk is sent to the peer. A success code
4643 * will be returned on successful abortion of the association. If
4644 * attempting to abort the association results in a failure, an error
4645 * code shall be returned.
4646 *
4647 * Mandatory attributes:
4648 *
4649 * o association id - local handle to the SCTP association
4650 *
4651 * Optional attributes:
4652 *
4653 * o cause code - reason of the abort to be passed to the peer
4654 *
4655 * None.
4656 *
4657 * The return value is the disposition.
4658 */
4659sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4660 const struct sctp_endpoint *ep,
4661 const struct sctp_association *asoc,
4662 const sctp_subtype_t type,
4663 void *arg,
4664 sctp_cmd_seq_t *commands)
4665{
4666 /* From 9.1 Abort of an Association
4667 * Upon receipt of the ABORT primitive from its upper
4668 * layer, the endpoint enters CLOSED state and
4669 * discard all outstanding data has been
4670 * acknowledged by its peer. The endpoint accepts no new data
4671 * from its upper layer, but retransmits data to the far end
4672 * if necessary to fill gaps.
4673 */
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004674 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 sctp_disposition_t retval;
4676
4677 retval = SCTP_DISPOSITION_CONSUME;
4678
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004679 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
4681 /* Even if we can't send the ABORT due to low memory delete the
4682 * TCB. This is a departure from our typical NOMEM handling.
4683 */
4684
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004685 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4686 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 /* Delete the established association. */
4688 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08004689 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
4691 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4692 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4693
4694 return retval;
4695}
4696
4697/* We tried an illegal operation on an association which is closed. */
4698sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4699 const struct sctp_association *asoc,
4700 const sctp_subtype_t type,
4701 void *arg,
4702 sctp_cmd_seq_t *commands)
4703{
4704 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4705 return SCTP_DISPOSITION_CONSUME;
4706}
4707
4708/* We tried an illegal operation on an association which is shutting
4709 * down.
4710 */
4711sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4712 const struct sctp_association *asoc,
4713 const sctp_subtype_t type,
4714 void *arg,
4715 sctp_cmd_seq_t *commands)
4716{
4717 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4718 SCTP_ERROR(-ESHUTDOWN));
4719 return SCTP_DISPOSITION_CONSUME;
4720}
4721
4722/*
4723 * sctp_cookie_wait_prm_shutdown
4724 *
4725 * Section: 4 Note: 2
4726 * Verification Tag:
4727 * Inputs
4728 * (endpoint, asoc)
4729 *
4730 * The RFC does not explicitly address this issue, but is the route through the
4731 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4732 *
4733 * Outputs
4734 * (timers)
4735 */
4736sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4737 const struct sctp_endpoint *ep,
4738 const struct sctp_association *asoc,
4739 const sctp_subtype_t type,
4740 void *arg,
4741 sctp_cmd_seq_t *commands)
4742{
4743 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4744 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4745
4746 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4747 SCTP_STATE(SCTP_STATE_CLOSED));
4748
4749 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4750
4751 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4752
4753 return SCTP_DISPOSITION_DELETE_TCB;
4754}
4755
4756/*
4757 * sctp_cookie_echoed_prm_shutdown
4758 *
4759 * Section: 4 Note: 2
4760 * Verification Tag:
4761 * Inputs
4762 * (endpoint, asoc)
4763 *
4764 * The RFC does not explcitly address this issue, but is the route through the
4765 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4766 *
4767 * Outputs
4768 * (timers)
4769 */
4770sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4771 const struct sctp_endpoint *ep,
4772 const struct sctp_association *asoc,
4773 const sctp_subtype_t type,
4774 void *arg, sctp_cmd_seq_t *commands)
4775{
4776 /* There is a single T1 timer, so we should be able to use
4777 * common function with the COOKIE-WAIT state.
4778 */
4779 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4780}
4781
4782/*
4783 * sctp_sf_cookie_wait_prm_abort
4784 *
4785 * Section: 4 Note: 2
4786 * Verification Tag:
4787 * Inputs
4788 * (endpoint, asoc)
4789 *
4790 * The RFC does not explicitly address this issue, but is the route through the
4791 * state table when someone issues an abort while in COOKIE_WAIT state.
4792 *
4793 * Outputs
4794 * (timers)
4795 */
4796sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4797 const struct sctp_endpoint *ep,
4798 const struct sctp_association *asoc,
4799 const sctp_subtype_t type,
4800 void *arg,
4801 sctp_cmd_seq_t *commands)
4802{
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004803 struct sctp_chunk *abort = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 sctp_disposition_t retval;
4805
4806 /* Stop T1-init timer */
4807 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4808 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4809 retval = SCTP_DISPOSITION_CONSUME;
4810
Sridhar Samudralac164a9b2006-08-22 11:50:39 -07004811 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812
4813 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4814 SCTP_STATE(SCTP_STATE_CLOSED));
4815
4816 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4817
4818 /* Even if we can't send the ABORT due to low memory delete the
4819 * TCB. This is a departure from our typical NOMEM handling.
4820 */
4821
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07004822 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4823 SCTP_ERROR(ECONNREFUSED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 /* Delete the established association. */
4825 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08004826 SCTP_PERR(SCTP_ERROR_USER_ABORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 return retval;
4829}
4830
4831/*
4832 * sctp_sf_cookie_echoed_prm_abort
4833 *
4834 * Section: 4 Note: 3
4835 * Verification Tag:
4836 * Inputs
4837 * (endpoint, asoc)
4838 *
4839 * The RFC does not explcitly address this issue, but is the route through the
4840 * state table when someone issues an abort while in COOKIE_ECHOED state.
4841 *
4842 * Outputs
4843 * (timers)
4844 */
4845sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4846 const struct sctp_endpoint *ep,
4847 const struct sctp_association *asoc,
4848 const sctp_subtype_t type,
4849 void *arg,
4850 sctp_cmd_seq_t *commands)
4851{
4852 /* There is a single T1 timer, so we should be able to use
4853 * common function with the COOKIE-WAIT state.
4854 */
4855 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4856}
4857
4858/*
4859 * sctp_sf_shutdown_pending_prm_abort
4860 *
4861 * Inputs
4862 * (endpoint, asoc)
4863 *
4864 * The RFC does not explicitly address this issue, but is the route through the
4865 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4866 *
4867 * Outputs
4868 * (timers)
4869 */
4870sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4871 const struct sctp_endpoint *ep,
4872 const struct sctp_association *asoc,
4873 const sctp_subtype_t type,
4874 void *arg,
4875 sctp_cmd_seq_t *commands)
4876{
4877 /* Stop the T5-shutdown guard timer. */
4878 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4879 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4880
4881 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4882}
4883
4884/*
4885 * sctp_sf_shutdown_sent_prm_abort
4886 *
4887 * Inputs
4888 * (endpoint, asoc)
4889 *
4890 * The RFC does not explicitly address this issue, but is the route through the
4891 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4892 *
4893 * Outputs
4894 * (timers)
4895 */
4896sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4897 const struct sctp_endpoint *ep,
4898 const struct sctp_association *asoc,
4899 const sctp_subtype_t type,
4900 void *arg,
4901 sctp_cmd_seq_t *commands)
4902{
4903 /* Stop the T2-shutdown timer. */
4904 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4905 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4906
4907 /* Stop the T5-shutdown guard timer. */
4908 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4909 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4910
4911 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4912}
4913
4914/*
4915 * sctp_sf_cookie_echoed_prm_abort
4916 *
4917 * Inputs
4918 * (endpoint, asoc)
4919 *
4920 * The RFC does not explcitly address this issue, but is the route through the
4921 * state table when someone issues an abort while in COOKIE_ECHOED state.
4922 *
4923 * Outputs
4924 * (timers)
4925 */
4926sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4927 const struct sctp_endpoint *ep,
4928 const struct sctp_association *asoc,
4929 const sctp_subtype_t type,
4930 void *arg,
4931 sctp_cmd_seq_t *commands)
4932{
4933 /* The same T2 timer, so we should be able to use
4934 * common function with the SHUTDOWN-SENT state.
4935 */
4936 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4937}
4938
4939/*
4940 * Process the REQUESTHEARTBEAT primitive
4941 *
4942 * 10.1 ULP-to-SCTP
4943 * J) Request Heartbeat
4944 *
4945 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4946 *
4947 * -> result
4948 *
4949 * Instructs the local endpoint to perform a HeartBeat on the specified
4950 * destination transport address of the given association. The returned
4951 * result should indicate whether the transmission of the HEARTBEAT
4952 * chunk to the destination address is successful.
4953 *
4954 * Mandatory attributes:
4955 *
4956 * o association id - local handle to the SCTP association
4957 *
4958 * o destination transport address - the transport address of the
4959 * association on which a heartbeat should be issued.
4960 */
4961sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4962 const struct sctp_endpoint *ep,
4963 const struct sctp_association *asoc,
4964 const sctp_subtype_t type,
4965 void *arg,
4966 sctp_cmd_seq_t *commands)
4967{
Vlad Yasevichfb785252007-03-19 17:02:03 -07004968 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
4969 (struct sctp_transport *)arg, commands))
4970 return SCTP_DISPOSITION_NOMEM;
4971
4972 /*
4973 * RFC 2960 (bis), section 8.3
4974 *
4975 * D) Request an on-demand HEARTBEAT on a specific destination
4976 * transport address of a given association.
4977 *
4978 * The endpoint should increment the respective error counter of
4979 * the destination transport address each time a HEARTBEAT is sent
4980 * to that address and not acknowledged within one RTO.
4981 *
4982 */
Vlad Yasevich7e990132009-03-02 09:46:14 +00004983 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
Vlad Yasevichfb785252007-03-19 17:02:03 -07004984 SCTP_TRANSPORT(arg));
4985 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986}
4987
4988/*
4989 * ADDIP Section 4.1 ASCONF Chunk Procedures
4990 * When an endpoint has an ASCONF signaled change to be sent to the
4991 * remote endpoint it should do A1 to A9
4992 */
4993sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4994 const struct sctp_association *asoc,
4995 const sctp_subtype_t type,
4996 void *arg,
4997 sctp_cmd_seq_t *commands)
4998{
4999 struct sctp_chunk *chunk = arg;
5000
5001 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5002 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5003 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5004 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5005 return SCTP_DISPOSITION_CONSUME;
5006}
5007
5008/*
5009 * Ignore the primitive event
5010 *
5011 * The return value is the disposition of the primitive.
5012 */
5013sctp_disposition_t sctp_sf_ignore_primitive(
5014 const struct sctp_endpoint *ep,
5015 const struct sctp_association *asoc,
5016 const sctp_subtype_t type,
5017 void *arg,
5018 sctp_cmd_seq_t *commands)
5019{
5020 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5021 return SCTP_DISPOSITION_DISCARD;
5022}
5023
5024/***************************************************************************
5025 * These are the state functions for the OTHER events.
5026 ***************************************************************************/
5027
5028/*
5029 * Start the shutdown negotiation.
5030 *
5031 * From Section 9.2:
5032 * Once all its outstanding data has been acknowledged, the endpoint
5033 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5034 * TSN Ack field the last sequential TSN it has received from the peer.
5035 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5036 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5037 * with the updated last sequential TSN received from its peer.
5038 *
5039 * The return value is the disposition.
5040 */
5041sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5042 const struct sctp_endpoint *ep,
5043 const struct sctp_association *asoc,
5044 const sctp_subtype_t type,
5045 void *arg,
5046 sctp_cmd_seq_t *commands)
5047{
5048 struct sctp_chunk *reply;
5049
5050 /* Once all its outstanding data has been acknowledged, the
5051 * endpoint shall send a SHUTDOWN chunk to its peer including
5052 * in the Cumulative TSN Ack field the last sequential TSN it
5053 * has received from the peer.
5054 */
5055 reply = sctp_make_shutdown(asoc, NULL);
5056 if (!reply)
5057 goto nomem;
5058
5059 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5060 * T2-shutdown timer.
5061 */
5062 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5063
5064 /* It shall then start the T2-shutdown timer */
5065 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5066 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5067
Wei Yongjun536428a2008-09-05 08:55:26 +08005068 /* RFC 4960 Section 9.2
5069 * The sender of the SHUTDOWN MAY also start an overall guard timer
5070 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5071 */
5072 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5073 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5074
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 if (asoc->autoclose)
5076 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5077 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5078
5079 /* and enter the SHUTDOWN-SENT state. */
5080 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5081 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5082
5083 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5084 *
5085 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005086 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 */
5088 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5089
5090 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5091
5092 return SCTP_DISPOSITION_CONSUME;
5093
5094nomem:
5095 return SCTP_DISPOSITION_NOMEM;
5096}
5097
5098/*
5099 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5100 *
5101 * From Section 9.2:
5102 *
5103 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5104 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5105 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5106 * endpoint must re-send the SHUTDOWN ACK.
5107 *
5108 * The return value is the disposition.
5109 */
5110sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5111 const struct sctp_endpoint *ep,
5112 const struct sctp_association *asoc,
5113 const sctp_subtype_t type,
5114 void *arg,
5115 sctp_cmd_seq_t *commands)
5116{
5117 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5118 struct sctp_chunk *reply;
5119
5120 /* There are 2 ways of getting here:
5121 * 1) called in response to a SHUTDOWN chunk
5122 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5123 *
5124 * For the case (2), the arg parameter is set to NULL. We need
5125 * to check that we have a chunk before accessing it's fields.
5126 */
5127 if (chunk) {
5128 if (!sctp_vtag_verify(chunk, asoc))
5129 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5130
5131 /* Make sure that the SHUTDOWN chunk has a valid length. */
5132 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5133 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5134 commands);
5135 }
5136
5137 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5138 * shall send a SHUTDOWN ACK ...
5139 */
5140 reply = sctp_make_shutdown_ack(asoc, chunk);
5141 if (!reply)
5142 goto nomem;
5143
5144 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5145 * the T2-shutdown timer.
5146 */
5147 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5148
5149 /* and start/restart a T2-shutdown timer of its own, */
5150 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5151 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5152
5153 if (asoc->autoclose)
5154 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5155 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5156
5157 /* Enter the SHUTDOWN-ACK-SENT state. */
5158 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5159 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5160
5161 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5162 *
5163 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005164 * or SHUTDOWN-ACK.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 */
5166 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5167
5168 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5169
5170 return SCTP_DISPOSITION_CONSUME;
5171
5172nomem:
5173 return SCTP_DISPOSITION_NOMEM;
5174}
5175
5176/*
5177 * Ignore the event defined as other
5178 *
5179 * The return value is the disposition of the event.
5180 */
5181sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5182 const struct sctp_association *asoc,
5183 const sctp_subtype_t type,
5184 void *arg,
5185 sctp_cmd_seq_t *commands)
5186{
5187 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5188 return SCTP_DISPOSITION_DISCARD;
5189}
5190
5191/************************************************************
5192 * These are the state functions for handling timeout events.
5193 ************************************************************/
5194
5195/*
5196 * RTX Timeout
5197 *
5198 * Section: 6.3.3 Handle T3-rtx Expiration
5199 *
5200 * Whenever the retransmission timer T3-rtx expires for a destination
5201 * address, do the following:
5202 * [See below]
5203 *
5204 * The return value is the disposition of the chunk.
5205 */
5206sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5207 const struct sctp_association *asoc,
5208 const sctp_subtype_t type,
5209 void *arg,
5210 sctp_cmd_seq_t *commands)
5211{
5212 struct sctp_transport *transport = arg;
5213
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005214 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5215
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005217 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5218 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5220 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005221 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5223 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5224 return SCTP_DISPOSITION_DELETE_TCB;
5225 }
5226
5227 /* E1) For the destination address for which the timer
5228 * expires, adjust its ssthresh with rules defined in Section
5229 * 7.2.3 and set the cwnd <- MTU.
5230 */
5231
5232 /* E2) For the destination address for which the timer
5233 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5234 * maximum value discussed in rule C7 above (RTO.max) may be
5235 * used to provide an upper bound to this doubling operation.
5236 */
5237
5238 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5239 * outstanding DATA chunks for the address for which the
5240 * T3-rtx has expired will fit into a single packet, subject
5241 * to the MTU constraint for the path corresponding to the
5242 * destination transport address to which the retransmission
5243 * is being sent (this may be different from the address for
5244 * which the timer expires [see Section 6.4]). Call this
5245 * value K. Bundle and retransmit those K DATA chunks in a
5246 * single packet to the destination endpoint.
5247 *
5248 * Note: Any DATA chunks that were sent to the address for
5249 * which the T3-rtx timer expired but did not fit in one MTU
5250 * (rule E3 above), should be marked for retransmission and
5251 * sent as soon as cwnd allows (normally when a SACK arrives).
5252 */
5253
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 /* Do some failure management (Section 8.2). */
5255 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5256
Vlad Yasevich1845a572007-02-21 02:06:19 -08005257 /* NB: Rules E4 and F1 are implicit in R1. */
5258 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 return SCTP_DISPOSITION_CONSUME;
5261}
5262
5263/*
5264 * Generate delayed SACK on timeout
5265 *
5266 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5267 *
5268 * The guidelines on delayed acknowledgement algorithm specified in
5269 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5270 * acknowledgement SHOULD be generated for at least every second packet
5271 * (not every second DATA chunk) received, and SHOULD be generated
5272 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5273 * some situations it may be beneficial for an SCTP transmitter to be
5274 * more conservative than the algorithms detailed in this document
5275 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5276 * the following algorithms allow.
5277 */
5278sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5279 const struct sctp_association *asoc,
5280 const sctp_subtype_t type,
5281 void *arg,
5282 sctp_cmd_seq_t *commands)
5283{
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005284 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5286 return SCTP_DISPOSITION_CONSUME;
5287}
5288
5289/*
Frank Filz3f7a87d2005-06-20 13:14:57 -07005290 * sctp_sf_t1_init_timer_expire
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 *
5292 * Section: 4 Note: 2
5293 * Verification Tag:
5294 * Inputs
5295 * (endpoint, asoc)
5296 *
5297 * RFC 2960 Section 4 Notes
5298 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5299 * and re-start the T1-init timer without changing state. This MUST
5300 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5301 * endpoint MUST abort the initialization process and report the
5302 * error to SCTP user.
5303 *
Frank Filz3f7a87d2005-06-20 13:14:57 -07005304 * Outputs
5305 * (timers, events)
5306 *
5307 */
5308sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5309 const struct sctp_association *asoc,
5310 const sctp_subtype_t type,
5311 void *arg,
5312 sctp_cmd_seq_t *commands)
5313{
5314 struct sctp_chunk *repl = NULL;
5315 struct sctp_bind_addr *bp;
5316 int attempts = asoc->init_err_counter + 1;
5317
5318 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005319 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
Frank Filz3f7a87d2005-06-20 13:14:57 -07005320
Vlad Yasevich81845c22006-01-30 15:59:54 -08005321 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005322 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5323 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5324 if (!repl)
5325 return SCTP_DISPOSITION_NOMEM;
5326
5327 /* Choose transport for INIT. */
5328 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5329 SCTP_CHUNK(repl));
5330
5331 /* Issue a sideeffect to do the needed accounting. */
5332 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5333 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5334
5335 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5336 } else {
5337 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5338 " max_init_attempts: %d\n",
5339 attempts, asoc->max_init_attempts);
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005340 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5341 SCTP_ERROR(ETIMEDOUT));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005342 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005343 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Frank Filz3f7a87d2005-06-20 13:14:57 -07005344 return SCTP_DISPOSITION_DELETE_TCB;
5345 }
5346
5347 return SCTP_DISPOSITION_CONSUME;
5348}
5349
5350/*
5351 * sctp_sf_t1_cookie_timer_expire
5352 *
5353 * Section: 4 Note: 2
5354 * Verification Tag:
5355 * Inputs
5356 * (endpoint, asoc)
5357 *
5358 * RFC 2960 Section 4 Notes
5359 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 * COOKIE ECHO and re-start the T1-cookie timer without changing
5361 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5362 * After that, the endpoint MUST abort the initialization process and
5363 * report the error to SCTP user.
5364 *
5365 * Outputs
5366 * (timers, events)
5367 *
5368 */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005369sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 const struct sctp_association *asoc,
5371 const sctp_subtype_t type,
5372 void *arg,
5373 sctp_cmd_seq_t *commands)
5374{
Frank Filz3f7a87d2005-06-20 13:14:57 -07005375 struct sctp_chunk *repl = NULL;
5376 int attempts = asoc->init_err_counter + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Frank Filz3f7a87d2005-06-20 13:14:57 -07005378 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005379 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380
Vlad Yasevich81845c22006-01-30 15:59:54 -08005381 if (attempts <= asoc->max_init_attempts) {
Frank Filz3f7a87d2005-06-20 13:14:57 -07005382 repl = sctp_make_cookie_echo(asoc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 if (!repl)
Frank Filz3f7a87d2005-06-20 13:14:57 -07005384 return SCTP_DISPOSITION_NOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385
Vlad Yasevich96cd0d32008-09-08 14:00:26 -04005386 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5387 SCTP_CHUNK(repl));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 /* Issue a sideeffect to do the needed accounting. */
Frank Filz3f7a87d2005-06-20 13:14:57 -07005389 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5390 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5391
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5393 } else {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005394 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5395 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
Al Virodc251b22006-11-20 17:00:44 -08005397 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 return SCTP_DISPOSITION_DELETE_TCB;
5399 }
5400
5401 return SCTP_DISPOSITION_CONSUME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
5404/* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5405 * with the updated last sequential TSN received from its peer.
5406 *
5407 * An endpoint should limit the number of retransmissions of the
5408 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5409 * If this threshold is exceeded the endpoint should destroy the TCB and
5410 * MUST report the peer endpoint unreachable to the upper layer (and
5411 * thus the association enters the CLOSED state). The reception of any
5412 * packet from its peer (i.e. as the peer sends all of its queued DATA
5413 * chunks) should clear the endpoint's retransmission count and restart
5414 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5415 * all of its queued DATA chunks that have not yet been sent.
5416 */
5417sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5418 const struct sctp_association *asoc,
5419 const sctp_subtype_t type,
5420 void *arg,
5421 sctp_cmd_seq_t *commands)
5422{
5423 struct sctp_chunk *reply = NULL;
5424
5425 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005426 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5427
Neil Horman58fbbed2008-02-29 11:40:56 -08005428 ((struct sctp_association *)asoc)->shutdown_retries++;
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 if (asoc->overall_error_count >= asoc->max_retrans) {
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005431 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5432 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5434 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005435 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5437 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5438 return SCTP_DISPOSITION_DELETE_TCB;
5439 }
5440
5441 switch (asoc->state) {
5442 case SCTP_STATE_SHUTDOWN_SENT:
5443 reply = sctp_make_shutdown(asoc, NULL);
5444 break;
5445
5446 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5447 reply = sctp_make_shutdown_ack(asoc, NULL);
5448 break;
5449
5450 default:
5451 BUG();
5452 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -07005453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454
5455 if (!reply)
5456 goto nomem;
5457
Wei Yongjun6345b192009-04-26 23:13:35 +08005458 /* Do some failure management (Section 8.2).
5459 * If we remove the transport an SHUTDOWN was last sent to, don't
5460 * do failure management.
5461 */
5462 if (asoc->shutdown_last_sent_to)
5463 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5464 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5467 * the T2-shutdown timer.
5468 */
5469 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5470
5471 /* Restart the T2-shutdown timer. */
5472 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5473 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5474 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5475 return SCTP_DISPOSITION_CONSUME;
5476
5477nomem:
5478 return SCTP_DISPOSITION_NOMEM;
5479}
5480
5481/*
5482 * ADDIP Section 4.1 ASCONF CHunk Procedures
5483 * If the T4 RTO timer expires the endpoint should do B1 to B5
5484 */
5485sctp_disposition_t sctp_sf_t4_timer_expire(
5486 const struct sctp_endpoint *ep,
5487 const struct sctp_association *asoc,
5488 const sctp_subtype_t type,
5489 void *arg,
5490 sctp_cmd_seq_t *commands)
5491{
5492 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5493 struct sctp_transport *transport = chunk->transport;
5494
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005495 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5498 * detection on the appropriate destination address as defined in
5499 * RFC2960 [5] section 8.1 and 8.2.
5500 */
Wei Yongjun10a43ce2009-04-26 23:14:42 +08005501 if (transport)
5502 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5503 SCTP_TRANSPORT(transport));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 /* Reconfig T4 timer and transport. */
5506 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5507
5508 /* ADDIP 4.1 B2) Increment the association error counters and perform
5509 * endpoint failure detection on the association as defined in
5510 * RFC2960 [5] section 8.1 and 8.2.
5511 * association error counter is incremented in SCTP_CMD_STRIKE.
5512 */
5513 if (asoc->overall_error_count >= asoc->max_retrans) {
5514 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5515 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005516 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5517 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005519 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
Wei Yongjun3d5a0192008-10-09 14:32:24 -07005521 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 return SCTP_DISPOSITION_ABORT;
5523 }
5524
5525 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5526 * the ASCONF chunk was sent by doubling the RTO timer value.
5527 * This is done in SCTP_CMD_STRIKE.
5528 */
5529
5530 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5531 * choose an alternate destination address (please refer to RFC2960
5532 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005533 * chunk, it MUST be the same (including its serial number) as the last
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 * ASCONF sent.
5535 */
5536 sctp_chunk_hold(asoc->addip_last_asconf);
5537 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5538 SCTP_CHUNK(asoc->addip_last_asconf));
5539
5540 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5541 * destination is selected, then the RTO used will be that of the new
5542 * destination address.
5543 */
5544 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5545 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5546
5547 return SCTP_DISPOSITION_CONSUME;
5548}
5549
5550/* sctpimpguide-05 Section 2.12.2
5551 * The sender of the SHUTDOWN MAY also start an overall guard timer
5552 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5553 * At the expiration of this timer the sender SHOULD abort the association
5554 * by sending an ABORT chunk.
5555 */
5556sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5557 const struct sctp_association *asoc,
5558 const sctp_subtype_t type,
5559 void *arg,
5560 sctp_cmd_seq_t *commands)
5561{
5562 struct sctp_chunk *reply = NULL;
5563
5564 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005565 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566
5567 reply = sctp_make_abort(asoc, NULL, 0);
5568 if (!reply)
5569 goto nomem;
5570
5571 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005572 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5573 SCTP_ERROR(ETIMEDOUT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005575 SCTP_PERR(SCTP_ERROR_NO_ERROR));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
Vlad Yasevicha1080a82008-10-09 14:33:26 -07005577 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5578 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5579
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 return SCTP_DISPOSITION_DELETE_TCB;
5581nomem:
5582 return SCTP_DISPOSITION_NOMEM;
5583}
5584
5585/* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5586 * the association is automatically closed by starting the shutdown process.
5587 * The work that needs to be done is same as when SHUTDOWN is initiated by
5588 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5589 */
5590sctp_disposition_t sctp_sf_autoclose_timer_expire(
5591 const struct sctp_endpoint *ep,
5592 const struct sctp_association *asoc,
5593 const sctp_subtype_t type,
5594 void *arg,
5595 sctp_cmd_seq_t *commands)
5596{
5597 int disposition;
5598
Sridhar Samudralaac0b0462006-08-22 00:15:33 -07005599 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5600
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 /* From 9.2 Shutdown of an Association
5602 * Upon receipt of the SHUTDOWN primitive from its upper
5603 * layer, the endpoint enters SHUTDOWN-PENDING state and
5604 * remains there until all outstanding data has been
5605 * acknowledged by its peer. The endpoint accepts no new data
5606 * from its upper layer, but retransmits data to the far end
5607 * if necessary to fill gaps.
5608 */
5609 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5610 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 disposition = SCTP_DISPOSITION_CONSUME;
5613 if (sctp_outq_is_empty(&asoc->outqueue)) {
5614 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5615 arg, commands);
5616 }
5617 return disposition;
5618}
5619
5620/*****************************************************************************
5621 * These are sa state functions which could apply to all types of events.
5622 ****************************************************************************/
5623
5624/*
5625 * This table entry is not implemented.
5626 *
5627 * Inputs
5628 * (endpoint, asoc, chunk)
5629 *
5630 * The return value is the disposition of the chunk.
5631 */
5632sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5633 const struct sctp_association *asoc,
5634 const sctp_subtype_t type,
5635 void *arg,
5636 sctp_cmd_seq_t *commands)
5637{
5638 return SCTP_DISPOSITION_NOT_IMPL;
5639}
5640
5641/*
5642 * This table entry represents a bug.
5643 *
5644 * Inputs
5645 * (endpoint, asoc, chunk)
5646 *
5647 * The return value is the disposition of the chunk.
5648 */
5649sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5650 const struct sctp_association *asoc,
5651 const sctp_subtype_t type,
5652 void *arg,
5653 sctp_cmd_seq_t *commands)
5654{
5655 return SCTP_DISPOSITION_BUG;
5656}
5657
5658/*
5659 * This table entry represents the firing of a timer in the wrong state.
5660 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5661 * when the association is in the wrong state. This event should
5662 * be ignored, so as to prevent any rearming of the timer.
5663 *
5664 * Inputs
5665 * (endpoint, asoc, chunk)
5666 *
5667 * The return value is the disposition of the chunk.
5668 */
5669sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5670 const struct sctp_association *asoc,
5671 const sctp_subtype_t type,
5672 void *arg,
5673 sctp_cmd_seq_t *commands)
5674{
5675 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5676 return SCTP_DISPOSITION_CONSUME;
5677}
5678
5679/********************************************************************
5680 * 2nd Level Abstractions
5681 ********************************************************************/
5682
5683/* Pull the SACK chunk based on the SACK header. */
5684static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5685{
5686 struct sctp_sackhdr *sack;
5687 unsigned int len;
5688 __u16 num_blocks;
5689 __u16 num_dup_tsns;
5690
5691 /* Protect ourselves from reading too far into
5692 * the skb from a bogus sender.
5693 */
5694 sack = (struct sctp_sackhdr *) chunk->skb->data;
5695
5696 num_blocks = ntohs(sack->num_gap_ack_blocks);
5697 num_dup_tsns = ntohs(sack->num_dup_tsns);
5698 len = sizeof(struct sctp_sackhdr);
5699 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5700 if (len > chunk->skb->len)
5701 return NULL;
5702
5703 skb_pull(chunk->skb, len);
5704
5705 return sack;
5706}
5707
5708/* Create an ABORT packet to be sent as a response, with the specified
5709 * error causes.
5710 */
5711static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5712 const struct sctp_association *asoc,
5713 struct sctp_chunk *chunk,
5714 const void *payload,
5715 size_t paylen)
5716{
5717 struct sctp_packet *packet;
5718 struct sctp_chunk *abort;
5719
5720 packet = sctp_ootb_pkt_new(asoc, chunk);
5721
5722 if (packet) {
5723 /* Make an ABORT.
5724 * The T bit will be set if the asoc is NULL.
5725 */
5726 abort = sctp_make_abort(asoc, chunk, paylen);
5727 if (!abort) {
5728 sctp_ootb_pkt_free(packet);
5729 return NULL;
5730 }
Jerome Forissier047a2422005-04-28 11:58:43 -07005731
5732 /* Reflect vtag if T-Bit is set */
5733 if (sctp_test_T_bit(abort))
5734 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5735
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 /* Add specified error causes, i.e., payload, to the
5737 * end of the chunk.
5738 */
5739 sctp_addto_chunk(abort, paylen, payload);
5740
5741 /* Set the skb to the belonging sock for accounting. */
5742 abort->skb->sk = ep->base.sk;
5743
5744 sctp_packet_append_chunk(packet, abort);
5745
5746 }
5747
5748 return packet;
5749}
5750
5751/* Allocate a packet for responding in the OOTB conditions. */
5752static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5753 const struct sctp_chunk *chunk)
5754{
5755 struct sctp_packet *packet;
5756 struct sctp_transport *transport;
5757 __u16 sport;
5758 __u16 dport;
5759 __u32 vtag;
5760
5761 /* Get the source and destination port from the inbound packet. */
5762 sport = ntohs(chunk->sctp_hdr->dest);
5763 dport = ntohs(chunk->sctp_hdr->source);
5764
5765 /* The V-tag is going to be the same as the inbound packet if no
5766 * association exists, otherwise, use the peer's vtag.
5767 */
5768 if (asoc) {
Wei Yongjun02c4e122007-08-31 10:03:58 +08005769 /* Special case the INIT-ACK as there is no peer's vtag
5770 * yet.
5771 */
5772 switch(chunk->chunk_hdr->type) {
5773 case SCTP_CID_INIT_ACK:
5774 {
5775 sctp_initack_chunk_t *initack;
5776
5777 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5778 vtag = ntohl(initack->init_hdr.init_tag);
5779 break;
5780 }
5781 default:
5782 vtag = asoc->peer.i.init_tag;
5783 break;
5784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 } else {
5786 /* Special case the INIT and stale COOKIE_ECHO as there is no
5787 * vtag yet.
5788 */
5789 switch(chunk->chunk_hdr->type) {
5790 case SCTP_CID_INIT:
5791 {
5792 sctp_init_chunk_t *init;
5793
5794 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5795 vtag = ntohl(init->init_hdr.init_tag);
5796 break;
5797 }
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005798 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 vtag = ntohl(chunk->sctp_hdr->vtag);
5800 break;
5801 }
5802 }
5803
5804 /* Make a transport for the bucket, Eliza... */
Al Viro6a1e5f32006-11-20 17:12:25 -08005805 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 if (!transport)
5807 goto nomem;
5808
5809 /* Cache a route for the transport with the chunk's destination as
5810 * the source address.
5811 */
Al Viro16b0a032006-11-20 17:13:38 -08005812 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 sctp_sk(sctp_get_ctl_sock()));
5814
5815 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5816 packet = sctp_packet_config(packet, vtag, 0);
5817
5818 return packet;
5819
5820nomem:
5821 return NULL;
5822}
5823
5824/* Free the packet allocated earlier for responding in the OOTB condition. */
5825void sctp_ootb_pkt_free(struct sctp_packet *packet)
5826{
5827 sctp_transport_free(packet->transport);
5828}
5829
5830/* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5831static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5832 const struct sctp_association *asoc,
5833 const struct sctp_chunk *chunk,
5834 sctp_cmd_seq_t *commands,
5835 struct sctp_chunk *err_chunk)
5836{
5837 struct sctp_packet *packet;
5838
5839 if (err_chunk) {
5840 packet = sctp_ootb_pkt_new(asoc, chunk);
5841 if (packet) {
5842 struct sctp_signed_cookie *cookie;
5843
5844 /* Override the OOTB vtag from the cookie. */
5845 cookie = chunk->subh.cookie_hdr;
5846 packet->vtag = cookie->c.peer_vtag;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005847
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 /* Set the skb to the belonging sock for accounting. */
5849 err_chunk->skb->sk = ep->base.sk;
5850 sctp_packet_append_chunk(packet, err_chunk);
5851 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5852 SCTP_PACKET(packet));
5853 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5854 } else
5855 sctp_chunk_free (err_chunk);
5856 }
5857}
5858
5859
5860/* Process a data chunk */
5861static int sctp_eat_data(const struct sctp_association *asoc,
5862 struct sctp_chunk *chunk,
5863 sctp_cmd_seq_t *commands)
5864{
5865 sctp_datahdr_t *data_hdr;
5866 struct sctp_chunk *err;
5867 size_t datalen;
5868 sctp_verb_t deliver;
5869 int tmp;
5870 __u32 tsn;
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005871 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
Neil Horman049b3ff2005-11-11 16:08:24 -08005872 struct sock *sk = asoc->base.sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
5874 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5875 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5876
5877 tsn = ntohl(data_hdr->tsn);
5878 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5879
5880 /* ASSERT: Now skb->data is really the user data. */
5881
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 /* Process ECN based congestion.
5883 *
5884 * Since the chunk structure is reused for all chunks within
5885 * a packet, we use ecn_ce_done to track if we've already
5886 * done CE processing for this packet.
5887 *
5888 * We need to do ECN processing even if we plan to discard the
5889 * chunk later.
5890 */
5891
5892 if (!chunk->ecn_ce_done) {
5893 struct sctp_af *af;
5894 chunk->ecn_ce_done = 1;
5895
5896 af = sctp_get_af_specific(
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07005897 ipver2af(ip_hdr(chunk->skb)->version));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
5899 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5900 /* Do real work as sideffect. */
5901 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5902 SCTP_U32(tsn));
5903 }
5904 }
5905
5906 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5907 if (tmp < 0) {
5908 /* The TSN is too high--silently discard the chunk and
5909 * count on it getting retransmitted later.
5910 */
5911 return SCTP_IERROR_HIGH_TSN;
5912 } else if (tmp > 0) {
5913 /* This is a duplicate. Record it. */
5914 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5915 return SCTP_IERROR_DUP_TSN;
5916 }
5917
5918 /* This is a new TSN. */
5919
5920 /* Discard if there is no room in the receive window.
5921 * Actually, allow a little bit of overflow (up to a MTU).
5922 */
5923 datalen = ntohs(chunk->chunk_hdr->length);
5924 datalen -= sizeof(sctp_data_chunk_t);
5925
5926 deliver = SCTP_CMD_CHUNK_ULP;
5927
5928 /* Think about partial delivery. */
5929 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5930
5931 /* Even if we don't accept this chunk there is
5932 * memory pressure.
5933 */
5934 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5935 }
5936
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +09005937 /* Spill over rwnd a little bit. Note: While allowed, this spill over
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 * seems a bit troublesome in that frag_point varies based on
5939 * PMTU. In cases, such as loopback, this might be a rather
5940 * large spill over.
5941 */
Neil Horman4d93df02007-08-15 16:07:44 -07005942 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
5943 (datalen > asoc->rwnd + asoc->frag_point))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944
5945 /* If this is the next TSN, consider reneging to make
5946 * room. Note: Playing nice with a confused sender. A
5947 * malicious sender can still eat up all our buffer
5948 * space and in the future we may want to detect and
5949 * do more drastic reneging.
5950 */
Neil Horman7c3ceb4f2006-05-05 17:02:09 -07005951 if (sctp_tsnmap_has_gap(map) &&
5952 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5954 deliver = SCTP_CMD_RENEGE;
5955 } else {
5956 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5957 "rwnd: %d\n", tsn, datalen,
5958 asoc->rwnd);
5959 return SCTP_IERROR_IGNORE_TSN;
5960 }
5961 }
5962
5963 /*
Neil Horman4d93df02007-08-15 16:07:44 -07005964 * Also try to renege to limit our memory usage in the event that
5965 * we are under memory pressure
Hideo Aoki3ab224b2007-12-31 00:11:19 -08005966 * If we can't renege, don't worry about it, the sk_rmem_schedule
Neil Horman4d93df02007-08-15 16:07:44 -07005967 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
5968 * memory usage too much
5969 */
5970 if (*sk->sk_prot_creator->memory_pressure) {
5971 if (sctp_tsnmap_has_gap(map) &&
5972 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5973 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
5974 deliver = SCTP_CMD_RENEGE;
5975 }
5976 }
5977
5978 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 * Section 3.3.10.9 No User Data (9)
5980 *
5981 * Cause of error
5982 * ---------------
5983 * No User Data: This error cause is returned to the originator of a
5984 * DATA chunk if a received DATA chunk has no user data.
5985 */
5986 if (unlikely(0 == datalen)) {
5987 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5988 if (err) {
5989 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5990 SCTP_CHUNK(err));
5991 }
5992 /* We are going to ABORT, so we might as well stop
5993 * processing the rest of the chunks in the packet.
5994 */
5995 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
Sridhar Samudrala8de8c872006-05-19 10:58:12 -07005996 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5997 SCTP_ERROR(ECONNABORTED));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
Al Viro5be291f2006-11-20 17:01:06 -08005999 SCTP_PERR(SCTP_ERROR_NO_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6001 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6002 return SCTP_IERROR_NO_DATA;
6003 }
6004
Sridhar Samudrala9faa7302006-07-21 14:49:07 -07006005 chunk->data_accepted = 1;
6006
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6008 * if we renege and the chunk arrives again.
6009 */
6010 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6011 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6012 else
6013 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6014
6015 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6016 *
6017 * If an endpoint receive a DATA chunk with an invalid stream
6018 * identifier, it shall acknowledge the reception of the DATA chunk
6019 * following the normal procedure, immediately send an ERROR chunk
6020 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6021 * and discard the DATA chunk.
6022 */
6023 if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
Vlad Yasevich3888e9e2008-07-08 02:28:39 -07006024 /* Mark tsn as received even though we drop it */
6025 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6026
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6028 &data_hdr->stream,
6029 sizeof(data_hdr->stream));
6030 if (err)
6031 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6032 SCTP_CHUNK(err));
6033 return SCTP_IERROR_BAD_STREAM;
6034 }
6035
6036 /* Send the data up to the user. Note: Schedule the
6037 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6038 * chunk needs the updated rwnd.
6039 */
6040 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6041
6042 return SCTP_IERROR_NO_ERROR;
6043}