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