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