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