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