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