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