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