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