1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* 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.
9 * This is part of the SCTP Linux Kernel Implementation.
11 * These are the state functions for the state machine.
13 * Please send any bug reports or fixes you make to the
15 * lksctp developers <linux-sctp@vger.kernel.org>
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>
31 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/types.h>
34 #include <linux/kernel.h>
36 #include <linux/ipv6.h>
37 #include <linux/net.h>
38 #include <linux/inet.h>
39 #include <linux/slab.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>
47 #define CREATE_TRACE_POINTS
48 #include <trace/events/sctp.h>
50 static struct sctp_packet *sctp_abort_pkt_new(
52 const struct sctp_endpoint *ep,
53 const struct sctp_association *asoc,
54 struct sctp_chunk *chunk,
55 const void *payload, size_t paylen);
56 static int sctp_eat_data(const struct sctp_association *asoc,
57 struct sctp_chunk *chunk,
58 struct sctp_cmd_seq *commands);
59 static struct sctp_packet *sctp_ootb_pkt_new(
61 const struct sctp_association *asoc,
62 const struct sctp_chunk *chunk);
63 static 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);
69 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
71 const struct sctp_endpoint *ep,
72 const struct sctp_association *asoc,
73 const union sctp_subtype type,
75 struct sctp_cmd_seq *commands);
76 static enum sctp_disposition sctp_sf_shut_8_4_5(
78 const struct sctp_endpoint *ep,
79 const struct sctp_association *asoc,
80 const union sctp_subtype type,
82 struct sctp_cmd_seq *commands);
83 static enum sctp_disposition sctp_sf_tabort_8_4_8(
85 const struct sctp_endpoint *ep,
86 const struct sctp_association *asoc,
87 const union sctp_subtype type,
89 struct sctp_cmd_seq *commands);
90 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
92 static enum sctp_disposition sctp_stop_t1_and_abort(
94 struct sctp_cmd_seq *commands,
95 __be16 error, int sk_err,
96 const struct sctp_association *asoc,
97 struct sctp_transport *transport);
99 static enum sctp_disposition sctp_sf_abort_violation(
101 const struct sctp_endpoint *ep,
102 const struct sctp_association *asoc,
104 struct sctp_cmd_seq *commands,
106 const size_t paylen);
108 static enum sctp_disposition sctp_sf_violation_chunklen(
110 const struct sctp_endpoint *ep,
111 const struct sctp_association *asoc,
112 const union sctp_subtype type,
114 struct sctp_cmd_seq *commands);
116 static enum sctp_disposition sctp_sf_violation_paramlen(
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);
124 static enum sctp_disposition sctp_sf_violation_ctsn(
126 const struct sctp_endpoint *ep,
127 const struct sctp_association *asoc,
128 const union sctp_subtype type,
130 struct sctp_cmd_seq *commands);
132 static enum sctp_disposition sctp_sf_violation_chunk(
134 const struct sctp_endpoint *ep,
135 const struct sctp_association *asoc,
136 const union sctp_subtype type,
138 struct sctp_cmd_seq *commands);
140 static enum sctp_ierror sctp_sf_authenticate(
141 const struct sctp_association *asoc,
142 struct sctp_chunk *chunk);
144 static enum sctp_disposition __sctp_sf_do_9_1_abort(
146 const struct sctp_endpoint *ep,
147 const struct sctp_association *asoc,
148 const union sctp_subtype type,
150 struct sctp_cmd_seq *commands);
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
159 static inline bool sctp_chunk_length_valid(struct sctp_chunk *chunk,
160 __u16 required_length)
162 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
164 /* Previously already marked? */
165 if (unlikely(chunk->pdiscard))
167 if (unlikely(chunk_length < required_length))
173 /* Check for format error in an ABORT chunk */
174 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
176 struct sctp_errhdr *err;
178 sctp_walk_errors(err, chunk->chunk_hdr);
180 return (void *)err == (void *)chunk->chunk_end;
183 /**********************************************************
184 * These are the state functions for handling chunk events.
185 **********************************************************/
188 * Process the final SHUTDOWN COMPLETE.
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).
197 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
198 * C) Rules for packet carrying SHUTDOWN COMPLETE:
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
204 * it is set to its peer's tag and the T bit is set in the Chunk
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.
211 * (endpoint, asoc, chunk)
214 * (asoc, reply_msg, msg_up, timers, counters)
216 * The return value is the disposition of the chunk.
218 enum 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)
224 struct sctp_chunk *chunk = arg;
225 struct sctp_ulpevent *ev;
227 if (!sctp_vtag_verify_either(chunk, asoc))
228 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
230 /* RFC 2960 6.10 Bundling
232 * An endpoint MUST NOT bundle INIT, INIT ACK or
233 * SHUTDOWN COMPLETE with any other chunks.
235 if (!chunk->singleton)
236 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
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,
243 /* RFC 2960 10.2 SCTP-to-ULP
245 * H) SHUTDOWN COMPLETE notification
247 * When SCTP completes the shutdown procedures (section 9.2) this
248 * notification is passed to the upper layer.
250 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
251 0, 0, 0, NULL, GFP_ATOMIC);
253 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
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
264 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
265 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
270 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
271 SCTP_STATE(SCTP_STATE_CLOSED));
273 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
274 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
276 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
278 return SCTP_DISPOSITION_DELETE_TCB;
282 * Respond to a normal INIT chunk.
283 * We are the side that is being asked for an association.
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.
293 * Verification Tag: Must be 0.
296 * (endpoint, asoc, chunk)
299 * (asoc, reply_msg, msg_up, timers, counters)
301 * The return value is the disposition of the chunk.
303 enum 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,
308 struct sctp_cmd_seq *commands)
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;
316 /* Update socket peer label if first association. */
317 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
319 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
322 * An endpoint MUST NOT bundle INIT, INIT ACK or
323 * SHUTDOWN COMPLETE with any other chunks.
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.
330 if (!chunk->singleton)
331 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
333 /* If the packet is an OOTB packet which is temporarily on the
334 * control endpoint, respond with an ABORT.
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);
341 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
344 if (chunk->sctp_hdr->vtag != 0)
345 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
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.
352 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
353 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
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
361 if (sctp_sstate(ep->base.sk, CLOSING))
362 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
364 /* Verify the INIT chunk before processing it. */
366 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
367 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
369 /* This chunk contains fatal error. It is to be discarded.
370 * Send an ABORT, with causes if there is any.
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));
379 sctp_chunk_free(err_chunk);
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;
387 return SCTP_DISPOSITION_NOMEM;
390 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
395 /* Grab the INIT header. */
396 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
398 /* Tag the variable length parameters. */
399 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
401 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
405 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
406 sctp_scope(sctp_source(chunk)),
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,
416 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
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.
423 len = ntohs(err_chunk->chunk_hdr->length) -
424 sizeof(struct sctp_chunkhdr);
426 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
430 /* If there are errors need to be reported for unknown parameters,
431 * include them in the outgoing INIT ACK as "Unrecognized parameter"
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
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"
448 sctp_addto_chunk(repl, len, unk_param);
449 sctp_chunk_free(err_chunk);
452 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
454 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
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
462 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
464 return SCTP_DISPOSITION_DELETE_TCB;
467 sctp_association_free(new_asoc);
470 sctp_chunk_free(err_chunk);
471 return SCTP_DISPOSITION_NOMEM;
475 * Respond to a normal INIT ACK chunk.
476 * We are the side that is initiating the association.
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.
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.
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.
495 * (endpoint, asoc, chunk)
498 * (asoc, reply_msg, msg_up, timers, counters)
500 * The return value is the disposition of the chunk.
502 enum 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,
507 struct sctp_cmd_seq *commands)
509 struct sctp_init_chunk *initchunk;
510 struct sctp_chunk *chunk = arg;
511 struct sctp_chunk *err_chunk;
512 struct sctp_packet *packet;
514 if (!sctp_vtag_verify(chunk, asoc))
515 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
518 * An endpoint MUST NOT bundle INIT, INIT ACK or
519 * SHUTDOWN COMPLETE with any other chunks.
521 if (!chunk->singleton)
522 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
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,
528 /* Grab the INIT header. */
529 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
531 /* Verify the INIT chunk before processing it. */
533 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
534 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
537 enum sctp_error error = SCTP_ERROR_NO_RESOURCE;
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
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));
551 sctp_chunk_free(err_chunk);
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;
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.
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
572 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
573 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
575 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
576 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
577 asoc, chunk->transport);
580 /* Tag the variable length parameters. Note that we never
581 * convert the parameters in an INIT chunk.
583 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
585 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
587 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
588 SCTP_PEER_INIT(initchunk));
590 /* Reset init error count upon receipt of INIT-ACK. */
591 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
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.
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));
604 /* SCTP-AUTH: genereate the assocition shared keys so that
605 * we can potentially signe the COOKIE-ECHO.
607 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
609 /* 5.1 C) "A" shall then send the State Cookie received in the
610 * INIT ACK chunk in a COOKIE ECHO chunk, ...
612 /* If there is any errors to report, send the ERROR chunk generated
613 * for unknown parameters as well.
615 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
616 SCTP_CHUNK(err_chunk));
618 return SCTP_DISPOSITION_CONSUME;
621 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
622 const struct sctp_association *asoc)
624 struct sctp_chunk auth;
626 if (!chunk->auth_chunk)
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.
636 /* Make sure that we and the peer are AUTH capable */
637 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
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;
650 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
654 * Respond to a normal COOKIE ECHO chunk.
655 * We are the side that is being asked for an association.
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.
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.
668 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
669 * D) Rules for packet carrying a COOKIE ECHO
671 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
672 * Initial Tag received in the INIT ACK.
674 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
677 * (endpoint, asoc, chunk)
680 * (asoc, reply_msg, msg_up, timers, counters)
682 * The return value is the disposition of the chunk.
684 enum 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,
689 struct sctp_cmd_seq *commands)
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;
700 /* If the packet is an OOTB packet which is temporarily on the
701 * control endpoint, respond with an ABORT.
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);
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().
713 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
714 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
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
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);
725 /* "Decode" the chunk. We have no optional parameters so we
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)))
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.
738 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
742 * If the re-build failed, what is the proper error path
745 * [We should abort the association. --piggy]
748 /* FIXME: Several errors are possible. A bad cookie should
749 * be silently discarded, but think about logging it too.
752 case -SCTP_IERROR_NOMEM:
755 case -SCTP_IERROR_STALE_COOKIE:
756 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
758 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
760 case -SCTP_IERROR_BAD_SIG:
762 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
767 /* Delay state machine commands until later.
769 * Re-build the bind address for the association is done in
770 * the sctp_unpack_cookie() already.
772 /* This is a brand-new association, so these are not yet side
773 * effects--it is safe to run them here.
775 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
777 if (!sctp_process_init(new_asoc, chunk,
778 &chunk->subh.cookie_hdr->c.peer_addr,
779 peer_init, GFP_ATOMIC))
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
786 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
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);
795 repl = sctp_make_cookie_ack(new_asoc, chunk);
799 /* RFC 2960 5.1 Normal Establishment of an Association
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.
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,
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.
817 if (new_asoc->peer.adaptation_ind) {
818 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
824 if (!new_asoc->peer.auth_capable) {
825 auth_ev = sctp_ulpevent_make_authkey(new_asoc, 0,
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
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());
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));
848 /* This will send the COOKIE ACK */
849 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
851 /* Queue the ASSOC_CHANGE event */
852 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
854 /* Send up the Adaptation Layer Indication event */
856 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
857 SCTP_ULPEVENT(ai_ev));
860 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
861 SCTP_ULPEVENT(auth_ev));
863 return SCTP_DISPOSITION_CONSUME;
866 sctp_ulpevent_free(ai_ev);
868 sctp_ulpevent_free(ev);
870 sctp_chunk_free(repl);
872 sctp_association_free(new_asoc);
874 return SCTP_DISPOSITION_NOMEM;
878 * Respond to a normal COOKIE ACK chunk.
879 * We are the side that is asking for an association.
881 * RFC 2960 5.1 Normal Establishment of an Association
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).
891 * (endpoint, asoc, chunk)
894 * (asoc, reply_msg, msg_up, timers, counters)
896 * The return value is the disposition of the chunk.
898 enum 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,
903 struct sctp_cmd_seq *commands)
905 struct sctp_chunk *chunk = arg;
906 struct sctp_ulpevent *ev;
908 if (!sctp_vtag_verify(chunk, asoc))
909 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
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.
914 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
915 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
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.
924 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
926 /* Set peer label for connection. */
927 security_inet_conn_established(ep->base.sk, chunk->skb);
929 /* RFC 2960 5.1 Normal Establishment of an Association
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.
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));
946 /* It may also notify its ULP about the successful
947 * establishment of the association with a Communication Up
948 * notification (see Section 10).
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,
958 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
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.
965 if (asoc->peer.adaptation_ind) {
966 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
970 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
974 if (!asoc->peer.auth_capable) {
975 ev = sctp_ulpevent_make_authkey(asoc, 0, SCTP_AUTH_NO_AUTH,
979 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
983 return SCTP_DISPOSITION_CONSUME;
985 return SCTP_DISPOSITION_NOMEM;
988 /* Generate and sendout a heartbeat packet. */
989 static enum sctp_disposition sctp_sf_heartbeat(
990 const struct sctp_endpoint *ep,
991 const struct sctp_association *asoc,
992 const union sctp_subtype type,
994 struct sctp_cmd_seq *commands)
996 struct sctp_transport *transport = (struct sctp_transport *) arg;
997 struct sctp_chunk *reply;
999 /* Send a heartbeat to our peer. */
1000 reply = sctp_make_heartbeat(asoc, transport);
1002 return SCTP_DISPOSITION_NOMEM;
1004 /* Set rto_pending indicating that an RTT measurement
1005 * is started with this heartbeat chunk.
1007 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
1008 SCTP_TRANSPORT(transport));
1010 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1011 return SCTP_DISPOSITION_CONSUME;
1014 /* Generate a HEARTBEAT packet on the given transport. */
1015 enum 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,
1020 struct sctp_cmd_seq *commands)
1022 struct sctp_transport *transport = (struct sctp_transport *) arg;
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;
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).
1042 if (transport->param_flags & SPP_HB_ENABLE) {
1043 if (SCTP_DISPOSITION_NOMEM ==
1044 sctp_sf_heartbeat(ep, asoc, type, arg,
1046 return SCTP_DISPOSITION_NOMEM;
1048 /* Set transport error counter and association error counter
1049 * when sending heartbeat.
1051 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1052 SCTP_TRANSPORT(transport));
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));
1059 return SCTP_DISPOSITION_CONSUME;
1062 /* resend asoc strreset_chunk. */
1063 enum 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,
1068 struct sctp_cmd_seq *commands)
1070 struct sctp_transport *transport = arg;
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;
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));
1088 return SCTP_DISPOSITION_CONSUME;
1092 * Process an heartbeat request.
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.
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.
1108 * (endpoint, asoc, chunk)
1111 * (asoc, reply_msg, msg_up, timers, counters)
1113 * The return value is the disposition of the chunk.
1115 enum 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)
1121 struct sctp_paramhdr *param_hdr;
1122 struct sctp_chunk *chunk = arg;
1123 struct sctp_chunk *reply;
1126 if (!sctp_vtag_verify(chunk, asoc))
1127 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
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,
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.
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);
1143 if (ntohs(param_hdr->length) > paylen)
1144 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1145 param_hdr, commands);
1147 if (!pskb_pull(chunk->skb, paylen))
1150 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1154 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1155 return SCTP_DISPOSITION_CONSUME;
1158 return SCTP_DISPOSITION_NOMEM;
1162 * Process the returning HEARTBEAT ACK.
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
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.
1179 * Verification Tag: 8.5 Verification Tag [Normal verification]
1182 * (endpoint, asoc, chunk)
1185 * (asoc, reply_msg, msg_up, timers, counters)
1187 * The return value is the disposition of the chunk.
1189 enum 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,
1194 struct sctp_cmd_seq *commands)
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;
1202 if (!sctp_vtag_verify(chunk, asoc))
1203 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1205 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1206 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1208 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
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;
1216 from_addr = hbinfo->daddr;
1217 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
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",
1225 &from_addr.v6.sin6_addr);
1227 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1230 &from_addr.v4.sin_addr.s_addr);
1232 return SCTP_DISPOSITION_DISCARD;
1235 /* Validate the 64-bit random nonce. */
1236 if (hbinfo->hb_nonce != link->hb_nonce)
1237 return SCTP_DISPOSITION_DISCARD;
1239 max_interval = link->hbinterval + link->rto;
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);
1247 return SCTP_DISPOSITION_DISCARD;
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.
1256 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1258 return SCTP_DISPOSITION_CONSUME;
1261 /* Helper function to send out an abort for the restart
1264 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1265 struct sctp_chunk *init,
1266 struct sctp_cmd_seq *commands)
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;
1276 /* Build the error on the stack. We are way to malloc crazy
1277 * throughout the code today.
1279 errhdr = (struct sctp_errhdr *)buffer;
1280 addrparm = (union sctp_addr_param *)errhdr->variable;
1282 /* Copy into a parm format. */
1283 len = af->to_addr_param(ssa, addrparm);
1284 len += sizeof(*errhdr);
1286 errhdr->cause = SCTP_ERROR_RESTART;
1287 errhdr->length = htons(len);
1289 /* Assign to the control socket. */
1290 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1292 /* Association is NULL since this may be a restart attack and we
1293 * want to send back the attacker's vtag.
1295 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1299 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1301 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1303 /* Discard the rest of the inbound packet. */
1304 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1307 /* Even if there is no memory, treat as a failure so
1308 * the packet will get dropped.
1313 static bool list_has_sctp_addr(const struct list_head *list,
1314 union sctp_addr *ipaddr)
1316 struct sctp_transport *addr;
1318 list_for_each_entry(addr, list, transports) {
1319 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1325 /* A restart is occurring, check to make sure no new addresses
1326 * are being added as we may be under a takeover attack.
1328 static 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)
1333 struct net *net = new_asoc->base.net;
1334 struct sctp_transport *new_addr;
1337 /* Implementor's Guide - Section 5.2.2
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
1345 /* Search through all current addresses and make sure
1346 * we aren't adding any new ones.
1348 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
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,
1359 /* Return success if all addresses were found. */
1363 /* Populate the verification/tie tags based on overlapping INIT
1366 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1368 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1369 const struct sctp_association *asoc)
1371 switch (asoc->state) {
1373 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
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;
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;
1387 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1388 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1391 new_asoc->c.my_ttag = asoc->c.my_vtag;
1392 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
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.
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;
1407 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1410 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1412 * Returns value representing action to be taken. These action values
1413 * correspond to Action/Description values in RFC 2960, Table 2.
1415 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1416 const struct sctp_association *asoc)
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))
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))) {
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))
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))
1444 /* No match to any of the special cases; discard this packet. */
1448 /* Common helper routine for both duplicate and simulataneous INIT
1451 static enum sctp_disposition sctp_sf_do_unexpected_init(
1453 const struct sctp_endpoint *ep,
1454 const struct sctp_association *asoc,
1455 const union sctp_subtype type,
1457 struct sctp_cmd_seq *commands)
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;
1466 /* Update socket peer label if first association. */
1467 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
1469 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1472 * An endpoint MUST NOT bundle INIT, INIT ACK or
1473 * SHUTDOWN COMPLETE with any other chunks.
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.
1480 if (!chunk->singleton)
1481 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1483 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1486 if (chunk->sctp_hdr->vtag != 0)
1487 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
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.
1493 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1494 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1496 /* Grab the INIT header. */
1497 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1499 /* Tag the variable length parameters. */
1500 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1502 /* Verify the INIT chunk before processing it. */
1504 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1505 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1507 /* This chunk contains fatal error. It is to be discarded.
1508 * Send an ABORT, with causes if there is any.
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));
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;
1523 retval = SCTP_DISPOSITION_NOMEM;
1527 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
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
1539 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1543 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1544 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
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.
1551 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1552 (struct sctp_init_chunk *)chunk->chunk_hdr,
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.
1561 if (!sctp_state(asoc, COOKIE_WAIT)) {
1562 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1564 retval = SCTP_DISPOSITION_CONSUME;
1569 sctp_tietags_populate(new_asoc, asoc);
1571 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
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.
1578 len = ntohs(err_chunk->chunk_hdr->length) -
1579 sizeof(struct sctp_chunkhdr);
1582 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1586 /* If there are errors need to be reported for unknown parameters,
1587 * include them in the outgoing INIT ACK as "Unrecognized parameter"
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.
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"
1604 sctp_addto_chunk(repl, len, unk_param);
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));
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.
1615 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1616 retval = SCTP_DISPOSITION_CONSUME;
1621 retval = SCTP_DISPOSITION_NOMEM;
1624 sctp_association_free(new_asoc);
1627 sctp_chunk_free(err_chunk);
1632 * Handle simultaneous INIT.
1633 * This means we started an INIT and then we got an INIT request from
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.
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.
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.
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).
1658 * Verification Tag: Not explicit, but an INIT can not have a valid
1659 * verification tag, so we skip the check.
1662 * (endpoint, asoc, chunk)
1665 * (asoc, reply_msg, msg_up, timers, counters)
1667 * The return value is the disposition of the chunk.
1669 enum sctp_disposition sctp_sf_do_5_2_1_siminit(
1671 const struct sctp_endpoint *ep,
1672 const struct sctp_association *asoc,
1673 const union sctp_subtype type,
1675 struct sctp_cmd_seq *commands)
1677 /* Call helper to do the real work for both simulataneous and
1678 * duplicate INIT chunk handling.
1680 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1684 * Handle duplicated INIT messages. These are usually delayed
1687 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1688 * COOKIE-ECHOED and COOKIE-WAIT
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.
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.
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.
1713 * Verification Tag: Not specified, but an INIT has no way of knowing
1714 * what the verification tag could be, so we ignore it.
1717 * (endpoint, asoc, chunk)
1720 * (asoc, reply_msg, msg_up, timers, counters)
1722 * The return value is the disposition of the chunk.
1724 enum sctp_disposition sctp_sf_do_5_2_2_dupinit(
1726 const struct sctp_endpoint *ep,
1727 const struct sctp_association *asoc,
1728 const union sctp_subtype type,
1730 struct sctp_cmd_seq *commands)
1732 /* Call helper to do the real work for both simulataneous and
1733 * duplicate INIT chunk handling.
1735 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1740 * Unexpected INIT-ACK handler.
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.
1748 enum sctp_disposition sctp_sf_do_5_2_3_initack(
1750 const struct sctp_endpoint *ep,
1751 const struct sctp_association *asoc,
1752 const union sctp_subtype type,
1754 struct sctp_cmd_seq *commands)
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.
1759 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1760 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1762 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1765 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1768 * A) In this case, the peer may have restarted.
1770 static enum sctp_disposition sctp_sf_do_dupcook_a(
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)
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;
1784 /* new_asoc is a brand-new association, so these are not yet
1785 * side effects--it is safe to run them here.
1787 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1789 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1793 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1796 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1797 return SCTP_DISPOSITION_DISCARD;
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.
1803 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1804 return SCTP_DISPOSITION_CONSUME;
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
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),
1816 if (SCTP_DISPOSITION_NOMEM == disposition)
1819 err = sctp_make_op_error(asoc, chunk,
1820 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1823 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1826 return SCTP_DISPOSITION_CONSUME;
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.
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());
1837 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1838 * and ASCONF-ACK cache.
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());
1844 repl = sctp_make_cookie_ack(new_asoc, chunk);
1848 /* Report association restart to upper layer. */
1849 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1850 new_asoc->c.sinit_num_ostreams,
1851 new_asoc->c.sinit_max_instreams,
1856 /* Update the content of current association. */
1857 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1858 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1859 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1860 sctp_state(asoc, SHUTDOWN_SENT)) &&
1861 (sctp_sstate(asoc->base.sk, CLOSING) ||
1862 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1863 /* If the socket has been closed by user, don't
1864 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1865 * bundled with COOKIE_ACK.
1867 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1868 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1869 SCTP_ST_CHUNK(0), repl,
1872 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1873 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1874 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1876 return SCTP_DISPOSITION_CONSUME;
1879 sctp_chunk_free(repl);
1881 return SCTP_DISPOSITION_NOMEM;
1884 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1887 * B) In this case, both sides may be attempting to start an association
1888 * at about the same time but the peer endpoint started its INIT
1889 * after responding to the local endpoint's INIT
1891 /* This case represents an initialization collision. */
1892 static enum sctp_disposition sctp_sf_do_dupcook_b(
1894 const struct sctp_endpoint *ep,
1895 const struct sctp_association *asoc,
1896 struct sctp_chunk *chunk,
1897 struct sctp_cmd_seq *commands,
1898 struct sctp_association *new_asoc)
1900 struct sctp_init_chunk *peer_init;
1901 struct sctp_chunk *repl;
1903 /* new_asoc is a brand-new association, so these are not yet
1904 * side effects--it is safe to run them here.
1906 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1907 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1911 if (sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC))
1914 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1915 return SCTP_DISPOSITION_DISCARD;
1917 /* Update the content of current association. */
1918 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1919 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1920 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1921 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1922 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1924 repl = sctp_make_cookie_ack(new_asoc, chunk);
1928 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1930 /* RFC 2960 5.1 Normal Establishment of an Association
1932 * D) IMPLEMENTATION NOTE: An implementation may choose to
1933 * send the Communication Up notification to the SCTP user
1934 * upon reception of a valid COOKIE ECHO chunk.
1936 * Sadly, this needs to be implemented as a side-effect, because
1937 * we are not guaranteed to have set the association id of the real
1938 * association and so these notifications need to be delayed until
1939 * the association id is allocated.
1942 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1944 /* Sockets API Draft Section 5.3.1.6
1945 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1946 * delivers this notification to inform the application that of the
1947 * peers requested adaptation layer.
1949 * This also needs to be done as a side effect for the same reason as
1952 if (asoc->peer.adaptation_ind)
1953 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1955 if (!asoc->peer.auth_capable)
1956 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_NO_AUTH, SCTP_NULL());
1958 return SCTP_DISPOSITION_CONSUME;
1961 return SCTP_DISPOSITION_NOMEM;
1964 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1967 * C) In this case, the local endpoint's cookie has arrived late.
1968 * Before it arrived, the local endpoint sent an INIT and received an
1969 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1970 * but a new tag of its own.
1972 /* This case represents an initialization collision. */
1973 static enum sctp_disposition sctp_sf_do_dupcook_c(
1975 const struct sctp_endpoint *ep,
1976 const struct sctp_association *asoc,
1977 struct sctp_chunk *chunk,
1978 struct sctp_cmd_seq *commands,
1979 struct sctp_association *new_asoc)
1981 /* The cookie should be silently discarded.
1982 * The endpoint SHOULD NOT change states and should leave
1983 * any timers running.
1985 return SCTP_DISPOSITION_DISCARD;
1988 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1992 * D) When both local and remote tags match the endpoint should always
1993 * enter the ESTABLISHED state, if it has not already done so.
1995 /* This case represents an initialization collision. */
1996 static enum sctp_disposition sctp_sf_do_dupcook_d(
1998 const struct sctp_endpoint *ep,
1999 const struct sctp_association *asoc,
2000 struct sctp_chunk *chunk,
2001 struct sctp_cmd_seq *commands,
2002 struct sctp_association *new_asoc)
2004 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL, *auth_ev = NULL;
2005 struct sctp_chunk *repl;
2007 /* Clarification from Implementor's Guide:
2008 * D) When both local and remote tags match the endpoint should
2009 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
2010 * It should stop any cookie timer that may be running and send
2014 if (!sctp_auth_chunk_verify(net, chunk, asoc))
2015 return SCTP_DISPOSITION_DISCARD;
2017 /* Don't accidentally move back into established state. */
2018 if (asoc->state < SCTP_STATE_ESTABLISHED) {
2019 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2020 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2021 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2022 SCTP_STATE(SCTP_STATE_ESTABLISHED));
2023 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
2024 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
2027 /* RFC 2960 5.1 Normal Establishment of an Association
2029 * D) IMPLEMENTATION NOTE: An implementation may choose
2030 * to send the Communication Up notification to the
2031 * SCTP user upon reception of a valid COOKIE
2034 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
2036 asoc->c.sinit_num_ostreams,
2037 asoc->c.sinit_max_instreams,
2042 /* Sockets API Draft Section 5.3.1.6
2043 * When a peer sends a Adaptation Layer Indication parameter,
2044 * SCTP delivers this notification to inform the application
2045 * that of the peers requested adaptation layer.
2047 if (asoc->peer.adaptation_ind) {
2048 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
2055 if (!asoc->peer.auth_capable) {
2056 auth_ev = sctp_ulpevent_make_authkey(asoc, 0,
2064 repl = sctp_make_cookie_ack(asoc, chunk);
2068 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
2071 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2074 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2075 SCTP_ULPEVENT(ai_ev));
2077 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2078 SCTP_ULPEVENT(auth_ev));
2080 return SCTP_DISPOSITION_CONSUME;
2084 sctp_ulpevent_free(auth_ev);
2086 sctp_ulpevent_free(ai_ev);
2088 sctp_ulpevent_free(ev);
2089 return SCTP_DISPOSITION_NOMEM;
2093 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2094 * chunk was retransmitted and then delayed in the network.
2096 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2098 * Verification Tag: None. Do cookie validation.
2101 * (endpoint, asoc, chunk)
2104 * (asoc, reply_msg, msg_up, timers, counters)
2106 * The return value is the disposition of the chunk.
2108 enum sctp_disposition sctp_sf_do_5_2_4_dupcook(
2110 const struct sctp_endpoint *ep,
2111 const struct sctp_association *asoc,
2112 const union sctp_subtype type,
2114 struct sctp_cmd_seq *commands)
2116 struct sctp_association *new_asoc;
2117 struct sctp_chunk *chunk = arg;
2118 enum sctp_disposition retval;
2119 struct sctp_chunk *err_chk_p;
2123 /* Make sure that the chunk has a valid length from the protocol
2124 * perspective. In this case check to make sure we have at least
2125 * enough for the chunk header. Cookie length verification is
2128 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2129 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2132 /* "Decode" the chunk. We have no optional parameters so we
2133 * are in good shape.
2135 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2136 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2137 sizeof(struct sctp_chunkhdr)))
2140 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2141 * of a duplicate COOKIE ECHO match the Verification Tags of the
2142 * current association, consider the State Cookie valid even if
2143 * the lifespan is exceeded.
2145 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2149 * If the re-build failed, what is the proper error path
2152 * [We should abort the association. --piggy]
2155 /* FIXME: Several errors are possible. A bad cookie should
2156 * be silently discarded, but think about logging it too.
2159 case -SCTP_IERROR_NOMEM:
2162 case -SCTP_IERROR_STALE_COOKIE:
2163 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2165 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2166 case -SCTP_IERROR_BAD_SIG:
2168 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2172 /* Update socket peer label if first association. */
2173 if (security_sctp_assoc_request((struct sctp_endpoint *)ep,
2175 sctp_association_free(new_asoc);
2176 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2179 /* Set temp so that it won't be added into hashtable */
2182 /* Compare the tie_tag in cookie with the verification tag of
2183 * current association.
2185 action = sctp_tietags_compare(new_asoc, asoc);
2188 case 'A': /* Association restart. */
2189 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2193 case 'B': /* Collision case B. */
2194 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2198 case 'C': /* Collision case C. */
2199 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2203 case 'D': /* Collision case D. */
2204 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2208 default: /* Discard packet for all others. */
2209 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2213 /* Delete the tempory new association. */
2214 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2215 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2217 /* Restore association pointer to provide SCTP command interpeter
2218 * with a valid context in case it needs to manipulate
2220 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2221 SCTP_ASOC((struct sctp_association *)asoc));
2226 return SCTP_DISPOSITION_NOMEM;
2230 * Process an ABORT. (SHUTDOWN-PENDING state)
2232 * See sctp_sf_do_9_1_abort().
2234 enum sctp_disposition sctp_sf_shutdown_pending_abort(
2236 const struct sctp_endpoint *ep,
2237 const struct sctp_association *asoc,
2238 const union sctp_subtype type,
2240 struct sctp_cmd_seq *commands)
2242 struct sctp_chunk *chunk = arg;
2244 if (!sctp_vtag_verify_either(chunk, asoc))
2245 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2247 /* Make sure that the ABORT chunk has a valid length.
2248 * Since this is an ABORT chunk, we have to discard it
2249 * because of the following text:
2250 * RFC 2960, Section 3.3.7
2251 * If an endpoint receives an ABORT with a format error or for an
2252 * association that doesn't exist, it MUST silently discard it.
2253 * Because the length is "invalid", we can't really discard just
2254 * as we do not know its true length. So, to be safe, discard the
2257 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2258 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2260 /* ADD-IP: Special case for ABORT chunks
2261 * F4) One special consideration is that ABORT Chunks arriving
2262 * destined to the IP address being deleted MUST be
2263 * ignored (see Section 5.3.1 for further details).
2265 if (SCTP_ADDR_DEL ==
2266 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2267 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2269 if (!sctp_err_chunk_valid(chunk))
2270 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2272 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2276 * Process an ABORT. (SHUTDOWN-SENT state)
2278 * See sctp_sf_do_9_1_abort().
2280 enum sctp_disposition sctp_sf_shutdown_sent_abort(
2282 const struct sctp_endpoint *ep,
2283 const struct sctp_association *asoc,
2284 const union sctp_subtype type,
2286 struct sctp_cmd_seq *commands)
2288 struct sctp_chunk *chunk = arg;
2290 if (!sctp_vtag_verify_either(chunk, asoc))
2291 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2293 /* Make sure that the ABORT chunk has a valid length.
2294 * Since this is an ABORT chunk, we have to discard it
2295 * because of the following text:
2296 * RFC 2960, Section 3.3.7
2297 * If an endpoint receives an ABORT with a format error or for an
2298 * association that doesn't exist, it MUST silently discard it.
2299 * Because the length is "invalid", we can't really discard just
2300 * as we do not know its true length. So, to be safe, discard the
2303 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2304 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2306 /* ADD-IP: Special case for ABORT chunks
2307 * F4) One special consideration is that ABORT Chunks arriving
2308 * destined to the IP address being deleted MUST be
2309 * ignored (see Section 5.3.1 for further details).
2311 if (SCTP_ADDR_DEL ==
2312 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2313 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2315 if (!sctp_err_chunk_valid(chunk))
2316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2318 /* Stop the T2-shutdown timer. */
2319 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2320 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2322 /* Stop the T5-shutdown guard timer. */
2323 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2324 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2326 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2330 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2332 * See sctp_sf_do_9_1_abort().
2334 enum sctp_disposition sctp_sf_shutdown_ack_sent_abort(
2336 const struct sctp_endpoint *ep,
2337 const struct sctp_association *asoc,
2338 const union sctp_subtype type,
2340 struct sctp_cmd_seq *commands)
2342 /* The same T2 timer, so we should be able to use
2343 * common function with the SHUTDOWN-SENT state.
2345 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2349 * Handle an Error received in COOKIE_ECHOED state.
2351 * Only handle the error type of stale COOKIE Error, the other errors will
2355 * (endpoint, asoc, chunk)
2358 * (asoc, reply_msg, msg_up, timers, counters)
2360 * The return value is the disposition of the chunk.
2362 enum sctp_disposition sctp_sf_cookie_echoed_err(
2364 const struct sctp_endpoint *ep,
2365 const struct sctp_association *asoc,
2366 const union sctp_subtype type,
2368 struct sctp_cmd_seq *commands)
2370 struct sctp_chunk *chunk = arg;
2371 struct sctp_errhdr *err;
2373 if (!sctp_vtag_verify(chunk, asoc))
2374 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2376 /* Make sure that the ERROR chunk has a valid length.
2377 * The parameter walking depends on this as well.
2379 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
2380 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2383 /* Process the error here */
2384 /* FUTURE FIXME: When PR-SCTP related and other optional
2385 * parms are emitted, this will have to change to handle multiple
2388 sctp_walk_errors(err, chunk->chunk_hdr) {
2389 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2390 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2394 /* It is possible to have malformed error causes, and that
2395 * will cause us to end the walk early. However, since
2396 * we are discarding the packet, there should be no adverse
2399 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2403 * Handle a Stale COOKIE Error
2405 * Section: 5.2.6 Handle Stale COOKIE Error
2406 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2407 * one of the following three alternatives.
2409 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2410 * Preservative parameter requesting an extension to the lifetime of
2411 * the State Cookie. When calculating the time extension, an
2412 * implementation SHOULD use the RTT information measured based on the
2413 * previous COOKIE ECHO / ERROR exchange, and should add no more
2414 * than 1 second beyond the measured RTT, due to long State Cookie
2415 * lifetimes making the endpoint more subject to a replay attack.
2417 * Verification Tag: Not explicit, but safe to ignore.
2420 * (endpoint, asoc, chunk)
2423 * (asoc, reply_msg, msg_up, timers, counters)
2425 * The return value is the disposition of the chunk.
2427 static enum sctp_disposition sctp_sf_do_5_2_6_stale(
2429 const struct sctp_endpoint *ep,
2430 const struct sctp_association *asoc,
2431 const union sctp_subtype type,
2433 struct sctp_cmd_seq *commands)
2435 int attempts = asoc->init_err_counter + 1;
2436 struct sctp_chunk *chunk = arg, *reply;
2437 struct sctp_cookie_preserve_param bht;
2438 struct sctp_bind_addr *bp;
2439 struct sctp_errhdr *err;
2442 if (attempts > asoc->max_init_attempts) {
2443 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2444 SCTP_ERROR(ETIMEDOUT));
2445 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2446 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2447 return SCTP_DISPOSITION_DELETE_TCB;
2450 err = (struct sctp_errhdr *)(chunk->skb->data);
2452 /* When calculating the time extension, an implementation
2453 * SHOULD use the RTT information measured based on the
2454 * previous COOKIE ECHO / ERROR exchange, and should add no
2455 * more than 1 second beyond the measured RTT, due to long
2456 * State Cookie lifetimes making the endpoint more subject to
2458 * Measure of Staleness's unit is usec. (1/1000000 sec)
2459 * Suggested Cookie Life-span Increment's unit is msec.
2461 * In general, if you use the suggested cookie life, the value
2462 * found in the field of measure of staleness should be doubled
2463 * to give ample time to retransmit the new cookie and thus
2464 * yield a higher probability of success on the reattempt.
2466 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(*err)));
2467 stale = (stale * 2) / 1000;
2469 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2470 bht.param_hdr.length = htons(sizeof(bht));
2471 bht.lifespan_increment = htonl(stale);
2473 /* Build that new INIT chunk. */
2474 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2475 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2479 sctp_addto_chunk(reply, sizeof(bht), &bht);
2481 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2482 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2484 /* Stop pending T3-rtx and heartbeat timers */
2485 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2486 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2488 /* Delete non-primary peer ip addresses since we are transitioning
2489 * back to the COOKIE-WAIT state
2491 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2493 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2496 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2497 SCTP_TRANSPORT(asoc->peer.primary_path));
2499 /* Cast away the const modifier, as we want to just
2500 * rerun it through as a sideffect.
2502 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2504 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2505 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2506 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2507 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2508 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2509 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2511 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2513 return SCTP_DISPOSITION_CONSUME;
2516 return SCTP_DISPOSITION_NOMEM;
2523 * After checking the Verification Tag, the receiving endpoint shall
2524 * remove the association from its record, and shall report the
2525 * termination to its upper layer.
2527 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2528 * B) Rules for packet carrying ABORT:
2530 * - The endpoint shall always fill in the Verification Tag field of the
2531 * outbound packet with the destination endpoint's tag value if it
2534 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2535 * MUST follow the procedure described in Section 8.4.
2537 * - The receiver MUST accept the packet if the Verification Tag
2538 * matches either its own tag, OR the tag of its peer. Otherwise, the
2539 * receiver MUST silently discard the packet and take no further
2543 * (endpoint, asoc, chunk)
2546 * (asoc, reply_msg, msg_up, timers, counters)
2548 * The return value is the disposition of the chunk.
2550 enum sctp_disposition sctp_sf_do_9_1_abort(
2552 const struct sctp_endpoint *ep,
2553 const struct sctp_association *asoc,
2554 const union sctp_subtype type,
2556 struct sctp_cmd_seq *commands)
2558 struct sctp_chunk *chunk = arg;
2560 if (!sctp_vtag_verify_either(chunk, asoc))
2561 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2563 /* Make sure that the ABORT chunk has a valid length.
2564 * Since this is an ABORT chunk, we have to discard it
2565 * because of the following text:
2566 * RFC 2960, Section 3.3.7
2567 * If an endpoint receives an ABORT with a format error or for an
2568 * association that doesn't exist, it MUST silently discard it.
2569 * Because the length is "invalid", we can't really discard just
2570 * as we do not know its true length. So, to be safe, discard the
2573 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2574 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2576 /* ADD-IP: Special case for ABORT chunks
2577 * F4) One special consideration is that ABORT Chunks arriving
2578 * destined to the IP address being deleted MUST be
2579 * ignored (see Section 5.3.1 for further details).
2581 if (SCTP_ADDR_DEL ==
2582 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2583 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2585 if (!sctp_err_chunk_valid(chunk))
2586 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2588 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2591 static enum sctp_disposition __sctp_sf_do_9_1_abort(
2593 const struct sctp_endpoint *ep,
2594 const struct sctp_association *asoc,
2595 const union sctp_subtype type,
2597 struct sctp_cmd_seq *commands)
2599 __be16 error = SCTP_ERROR_NO_ERROR;
2600 struct sctp_chunk *chunk = arg;
2603 /* See if we have an error cause code in the chunk. */
2604 len = ntohs(chunk->chunk_hdr->length);
2605 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2606 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2608 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2609 /* ASSOC_FAILED will DELETE_TCB. */
2610 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2611 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2612 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2614 return SCTP_DISPOSITION_ABORT;
2618 * Process an ABORT. (COOKIE-WAIT state)
2620 * See sctp_sf_do_9_1_abort() above.
2622 enum sctp_disposition sctp_sf_cookie_wait_abort(
2624 const struct sctp_endpoint *ep,
2625 const struct sctp_association *asoc,
2626 const union sctp_subtype type,
2628 struct sctp_cmd_seq *commands)
2630 __be16 error = SCTP_ERROR_NO_ERROR;
2631 struct sctp_chunk *chunk = arg;
2634 if (!sctp_vtag_verify_either(chunk, asoc))
2635 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2637 /* Make sure that the ABORT chunk has a valid length.
2638 * Since this is an ABORT chunk, we have to discard it
2639 * because of the following text:
2640 * RFC 2960, Section 3.3.7
2641 * If an endpoint receives an ABORT with a format error or for an
2642 * association that doesn't exist, it MUST silently discard it.
2643 * Because the length is "invalid", we can't really discard just
2644 * as we do not know its true length. So, to be safe, discard the
2647 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_abort_chunk)))
2648 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2650 /* See if we have an error cause code in the chunk. */
2651 len = ntohs(chunk->chunk_hdr->length);
2652 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2653 error = ((struct sctp_errhdr *)chunk->skb->data)->cause;
2655 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2660 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2662 enum sctp_disposition sctp_sf_cookie_wait_icmp_abort(
2664 const struct sctp_endpoint *ep,
2665 const struct sctp_association *asoc,
2666 const union sctp_subtype type,
2668 struct sctp_cmd_seq *commands)
2670 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2672 (struct sctp_transport *)arg);
2676 * Process an ABORT. (COOKIE-ECHOED state)
2678 enum sctp_disposition sctp_sf_cookie_echoed_abort(
2680 const struct sctp_endpoint *ep,
2681 const struct sctp_association *asoc,
2682 const union sctp_subtype type,
2684 struct sctp_cmd_seq *commands)
2686 /* There is a single T1 timer, so we should be able to use
2687 * common function with the COOKIE-WAIT state.
2689 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2693 * Stop T1 timer and abort association with "INIT failed".
2695 * This is common code called by several sctp_sf_*_abort() functions above.
2697 static enum sctp_disposition sctp_stop_t1_and_abort(
2699 struct sctp_cmd_seq *commands,
2700 __be16 error, int sk_err,
2701 const struct sctp_association *asoc,
2702 struct sctp_transport *transport)
2704 pr_debug("%s: ABORT received (INIT)\n", __func__);
2706 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2707 SCTP_STATE(SCTP_STATE_CLOSED));
2708 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2709 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2710 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2711 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2712 /* CMD_INIT_FAILED will DELETE_TCB. */
2713 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2716 return SCTP_DISPOSITION_ABORT;
2720 * sctp_sf_do_9_2_shut
2723 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2724 * - enter the SHUTDOWN-RECEIVED state,
2726 * - stop accepting new data from its SCTP user
2728 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2729 * that all its outstanding DATA chunks have been received by the
2732 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2733 * send a SHUTDOWN in response to a ULP request. And should discard
2734 * subsequent SHUTDOWN chunks.
2736 * If there are still outstanding DATA chunks left, the SHUTDOWN
2737 * receiver shall continue to follow normal data transmission
2738 * procedures defined in Section 6 until all outstanding DATA chunks
2739 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2740 * new data from its SCTP user.
2742 * Verification Tag: 8.5 Verification Tag [Normal verification]
2745 * (endpoint, asoc, chunk)
2748 * (asoc, reply_msg, msg_up, timers, counters)
2750 * The return value is the disposition of the chunk.
2752 enum sctp_disposition sctp_sf_do_9_2_shutdown(
2754 const struct sctp_endpoint *ep,
2755 const struct sctp_association *asoc,
2756 const union sctp_subtype type,
2758 struct sctp_cmd_seq *commands)
2760 enum sctp_disposition disposition;
2761 struct sctp_chunk *chunk = arg;
2762 struct sctp_shutdownhdr *sdh;
2763 struct sctp_ulpevent *ev;
2766 if (!sctp_vtag_verify(chunk, asoc))
2767 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2769 /* Make sure that the SHUTDOWN chunk has a valid length. */
2770 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2771 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2774 /* Convert the elaborate header. */
2775 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2776 skb_pull(chunk->skb, sizeof(*sdh));
2777 chunk->subh.shutdown_hdr = sdh;
2778 ctsn = ntohl(sdh->cum_tsn_ack);
2780 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2781 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2782 asoc->ctsn_ack_point);
2784 return SCTP_DISPOSITION_DISCARD;
2787 /* If Cumulative TSN Ack beyond the max tsn currently
2788 * send, terminating the association and respond to the
2789 * sender with an ABORT.
2791 if (!TSN_lt(ctsn, asoc->next_tsn))
2792 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2794 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2795 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2796 * inform the application that it should cease sending data.
2798 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2800 disposition = SCTP_DISPOSITION_NOMEM;
2803 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2805 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2806 * - enter the SHUTDOWN-RECEIVED state,
2807 * - stop accepting new data from its SCTP user
2809 * [This is implicit in the new state.]
2811 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2812 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2813 disposition = SCTP_DISPOSITION_CONSUME;
2815 if (sctp_outq_is_empty(&asoc->outqueue)) {
2816 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2820 if (SCTP_DISPOSITION_NOMEM == disposition)
2823 /* - verify, by checking the Cumulative TSN Ack field of the
2824 * chunk, that all its outstanding DATA chunks have been
2825 * received by the SHUTDOWN sender.
2827 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2828 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2835 * sctp_sf_do_9_2_shut_ctsn
2837 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2838 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2839 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2840 * MUST be processed.
2842 enum sctp_disposition sctp_sf_do_9_2_shut_ctsn(
2844 const struct sctp_endpoint *ep,
2845 const struct sctp_association *asoc,
2846 const union sctp_subtype type,
2848 struct sctp_cmd_seq *commands)
2850 struct sctp_chunk *chunk = arg;
2851 struct sctp_shutdownhdr *sdh;
2854 if (!sctp_vtag_verify(chunk, asoc))
2855 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2857 /* Make sure that the SHUTDOWN chunk has a valid length. */
2858 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk)))
2859 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2862 sdh = (struct sctp_shutdownhdr *)chunk->skb->data;
2863 ctsn = ntohl(sdh->cum_tsn_ack);
2865 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2866 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2867 asoc->ctsn_ack_point);
2869 return SCTP_DISPOSITION_DISCARD;
2872 /* If Cumulative TSN Ack beyond the max tsn currently
2873 * send, terminating the association and respond to the
2874 * sender with an ABORT.
2876 if (!TSN_lt(ctsn, asoc->next_tsn))
2877 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2879 /* verify, by checking the Cumulative TSN Ack field of the
2880 * chunk, that all its outstanding DATA chunks have been
2881 * received by the SHUTDOWN sender.
2883 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2884 SCTP_BE32(sdh->cum_tsn_ack));
2886 return SCTP_DISPOSITION_CONSUME;
2890 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2891 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2892 * transport addresses (either in the IP addresses or in the INIT chunk)
2893 * that belong to this association, it should discard the INIT chunk and
2894 * retransmit the SHUTDOWN ACK chunk.
2896 enum sctp_disposition sctp_sf_do_9_2_reshutack(
2898 const struct sctp_endpoint *ep,
2899 const struct sctp_association *asoc,
2900 const union sctp_subtype type,
2902 struct sctp_cmd_seq *commands)
2904 struct sctp_chunk *chunk = arg;
2905 struct sctp_chunk *reply;
2907 /* Make sure that the chunk has a valid length */
2908 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2909 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2912 /* Since we are not going to really process this INIT, there
2913 * is no point in verifying chunk boundries. Just generate
2916 reply = sctp_make_shutdown_ack(asoc, chunk);
2920 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2921 * the T2-SHUTDOWN timer.
2923 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2925 /* and restart the T2-shutdown timer. */
2926 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2927 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2929 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2931 return SCTP_DISPOSITION_CONSUME;
2933 return SCTP_DISPOSITION_NOMEM;
2937 * sctp_sf_do_ecn_cwr
2939 * Section: Appendix A: Explicit Congestion Notification
2943 * RFC 2481 details a specific bit for a sender to send in the header of
2944 * its next outbound TCP segment to indicate to its peer that it has
2945 * reduced its congestion window. This is termed the CWR bit. For
2946 * SCTP the same indication is made by including the CWR chunk.
2947 * This chunk contains one data element, i.e. the TSN number that
2948 * was sent in the ECNE chunk. This element represents the lowest
2949 * TSN number in the datagram that was originally marked with the
2952 * Verification Tag: 8.5 Verification Tag [Normal verification]
2954 * (endpoint, asoc, chunk)
2957 * (asoc, reply_msg, msg_up, timers, counters)
2959 * The return value is the disposition of the chunk.
2961 enum sctp_disposition sctp_sf_do_ecn_cwr(struct net *net,
2962 const struct sctp_endpoint *ep,
2963 const struct sctp_association *asoc,
2964 const union sctp_subtype type,
2966 struct sctp_cmd_seq *commands)
2968 struct sctp_chunk *chunk = arg;
2969 struct sctp_cwrhdr *cwr;
2972 if (!sctp_vtag_verify(chunk, asoc))
2973 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2975 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
2976 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2979 cwr = (struct sctp_cwrhdr *)chunk->skb->data;
2980 skb_pull(chunk->skb, sizeof(*cwr));
2982 lowest_tsn = ntohl(cwr->lowest_tsn);
2984 /* Does this CWR ack the last sent congestion notification? */
2985 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2986 /* Stop sending ECNE. */
2987 sctp_add_cmd_sf(commands,
2989 SCTP_U32(lowest_tsn));
2991 return SCTP_DISPOSITION_CONSUME;
2997 * Section: Appendix A: Explicit Congestion Notification
3001 * RFC 2481 details a specific bit for a receiver to send back in its
3002 * TCP acknowledgements to notify the sender of the Congestion
3003 * Experienced (CE) bit having arrived from the network. For SCTP this
3004 * same indication is made by including the ECNE chunk. This chunk
3005 * contains one data element, i.e. the lowest TSN associated with the IP
3006 * datagram marked with the CE bit.....
3008 * Verification Tag: 8.5 Verification Tag [Normal verification]
3010 * (endpoint, asoc, chunk)
3013 * (asoc, reply_msg, msg_up, timers, counters)
3015 * The return value is the disposition of the chunk.
3017 enum sctp_disposition sctp_sf_do_ecne(struct net *net,
3018 const struct sctp_endpoint *ep,
3019 const struct sctp_association *asoc,
3020 const union sctp_subtype type,
3021 void *arg, struct sctp_cmd_seq *commands)
3023 struct sctp_chunk *chunk = arg;
3024 struct sctp_ecnehdr *ecne;
3026 if (!sctp_vtag_verify(chunk, asoc))
3027 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3029 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_ecne_chunk)))
3030 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3033 ecne = (struct sctp_ecnehdr *)chunk->skb->data;
3034 skb_pull(chunk->skb, sizeof(*ecne));
3036 /* If this is a newer ECNE than the last CWR packet we sent out */
3037 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
3038 SCTP_U32(ntohl(ecne->lowest_tsn)));
3040 return SCTP_DISPOSITION_CONSUME;
3044 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
3046 * The SCTP endpoint MUST always acknowledge the reception of each valid
3049 * The guidelines on delayed acknowledgement algorithm specified in
3050 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
3051 * acknowledgement SHOULD be generated for at least every second packet
3052 * (not every second DATA chunk) received, and SHOULD be generated within
3053 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
3054 * situations it may be beneficial for an SCTP transmitter to be more
3055 * conservative than the algorithms detailed in this document allow.
3056 * However, an SCTP transmitter MUST NOT be more aggressive than the
3057 * following algorithms allow.
3059 * A SCTP receiver MUST NOT generate more than one SACK for every
3060 * incoming packet, other than to update the offered window as the
3061 * receiving application consumes new data.
3063 * Verification Tag: 8.5 Verification Tag [Normal verification]
3066 * (endpoint, asoc, chunk)
3069 * (asoc, reply_msg, msg_up, timers, counters)
3071 * The return value is the disposition of the chunk.
3073 enum sctp_disposition sctp_sf_eat_data_6_2(struct net *net,
3074 const struct sctp_endpoint *ep,
3075 const struct sctp_association *asoc,
3076 const union sctp_subtype type,
3078 struct sctp_cmd_seq *commands)
3080 union sctp_arg force = SCTP_NOFORCE();
3081 struct sctp_chunk *chunk = arg;
3084 if (!sctp_vtag_verify(chunk, asoc)) {
3085 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3087 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3090 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3091 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3094 error = sctp_eat_data(asoc, chunk, commands);
3096 case SCTP_IERROR_NO_ERROR:
3098 case SCTP_IERROR_HIGH_TSN:
3099 case SCTP_IERROR_BAD_STREAM:
3100 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3101 goto discard_noforce;
3102 case SCTP_IERROR_DUP_TSN:
3103 case SCTP_IERROR_IGNORE_TSN:
3104 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3106 case SCTP_IERROR_NO_DATA:
3107 return SCTP_DISPOSITION_ABORT;
3108 case SCTP_IERROR_PROTO_VIOLATION:
3109 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3110 (u8 *)chunk->subh.data_hdr,
3111 sctp_datahdr_len(&asoc->stream));
3116 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3117 force = SCTP_FORCE();
3119 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3120 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3121 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3124 /* If this is the last chunk in a packet, we need to count it
3125 * toward sack generation. Note that we need to SACK every
3126 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3127 * THEM. We elect to NOT generate SACK's if the chunk fails
3128 * the verification tag test.
3130 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3132 * The SCTP endpoint MUST always acknowledge the reception of
3133 * each valid DATA chunk.
3135 * The guidelines on delayed acknowledgement algorithm
3136 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3137 * Specifically, an acknowledgement SHOULD be generated for at
3138 * least every second packet (not every second DATA chunk)
3139 * received, and SHOULD be generated within 200 ms of the
3140 * arrival of any unacknowledged DATA chunk. In some
3141 * situations it may be beneficial for an SCTP transmitter to
3142 * be more conservative than the algorithms detailed in this
3143 * document allow. However, an SCTP transmitter MUST NOT be
3144 * more aggressive than the following algorithms allow.
3146 if (chunk->end_of_packet)
3147 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3149 return SCTP_DISPOSITION_CONSUME;
3152 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3154 * When a packet arrives with duplicate DATA chunk(s) and with
3155 * no new DATA chunk(s), the endpoint MUST immediately send a
3156 * SACK with no delay. If a packet arrives with duplicate
3157 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3158 * MAY immediately send a SACK. Normally receipt of duplicate
3159 * DATA chunks will occur when the original SACK chunk was lost
3160 * and the peer's RTO has expired. The duplicate TSN number(s)
3161 * SHOULD be reported in the SACK as duplicate.
3163 /* In our case, we split the MAY SACK advice up whether or not
3164 * the last chunk is a duplicate.'
3166 if (chunk->end_of_packet)
3167 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3168 return SCTP_DISPOSITION_DISCARD;
3171 if (chunk->end_of_packet)
3172 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3174 return SCTP_DISPOSITION_DISCARD;
3178 * sctp_sf_eat_data_fast_4_4
3181 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3182 * DATA chunks without delay.
3184 * Verification Tag: 8.5 Verification Tag [Normal verification]
3186 * (endpoint, asoc, chunk)
3189 * (asoc, reply_msg, msg_up, timers, counters)
3191 * The return value is the disposition of the chunk.
3193 enum sctp_disposition sctp_sf_eat_data_fast_4_4(
3195 const struct sctp_endpoint *ep,
3196 const struct sctp_association *asoc,
3197 const union sctp_subtype type,
3199 struct sctp_cmd_seq *commands)
3201 struct sctp_chunk *chunk = arg;
3204 if (!sctp_vtag_verify(chunk, asoc)) {
3205 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3207 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3210 if (!sctp_chunk_length_valid(chunk, sctp_datachk_len(&asoc->stream)))
3211 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3214 error = sctp_eat_data(asoc, chunk, commands);
3216 case SCTP_IERROR_NO_ERROR:
3217 case SCTP_IERROR_HIGH_TSN:
3218 case SCTP_IERROR_DUP_TSN:
3219 case SCTP_IERROR_IGNORE_TSN:
3220 case SCTP_IERROR_BAD_STREAM:
3222 case SCTP_IERROR_NO_DATA:
3223 return SCTP_DISPOSITION_ABORT;
3224 case SCTP_IERROR_PROTO_VIOLATION:
3225 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3226 (u8 *)chunk->subh.data_hdr,
3227 sctp_datahdr_len(&asoc->stream));
3232 /* Go a head and force a SACK, since we are shutting down. */
3234 /* Implementor's Guide.
3236 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3237 * respond to each received packet containing one or more DATA chunk(s)
3238 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3240 if (chunk->end_of_packet) {
3241 /* We must delay the chunk creation since the cumulative
3242 * TSN has not been updated yet.
3244 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3245 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3246 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3247 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3250 return SCTP_DISPOSITION_CONSUME;
3254 * Section: 6.2 Processing a Received SACK
3255 * D) Any time a SACK arrives, the endpoint performs the following:
3257 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3258 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3259 * increasing, a SACK whose Cumulative TSN Ack is less than the
3260 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3262 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3263 * of bytes still outstanding after processing the Cumulative TSN Ack
3264 * and the Gap Ack Blocks.
3266 * iii) If the SACK is missing a TSN that was previously
3267 * acknowledged via a Gap Ack Block (e.g., the data receiver
3268 * reneged on the data), then mark the corresponding DATA chunk
3269 * as available for retransmit: Mark it as missing for fast
3270 * retransmit as described in Section 7.2.4 and if no retransmit
3271 * timer is running for the destination address to which the DATA
3272 * chunk was originally transmitted, then T3-rtx is started for
3273 * that destination address.
3275 * Verification Tag: 8.5 Verification Tag [Normal verification]
3278 * (endpoint, asoc, chunk)
3281 * (asoc, reply_msg, msg_up, timers, counters)
3283 * The return value is the disposition of the chunk.
3285 enum sctp_disposition sctp_sf_eat_sack_6_2(struct net *net,
3286 const struct sctp_endpoint *ep,
3287 const struct sctp_association *asoc,
3288 const union sctp_subtype type,
3290 struct sctp_cmd_seq *commands)
3292 struct sctp_chunk *chunk = arg;
3293 struct sctp_sackhdr *sackh;
3296 if (!sctp_vtag_verify(chunk, asoc))
3297 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3299 /* Make sure that the SACK chunk has a valid length. */
3300 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_sack_chunk)))
3301 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3304 /* Pull the SACK chunk from the data buffer */
3305 sackh = sctp_sm_pull_sack(chunk);
3306 /* Was this a bogus SACK? */
3308 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3309 chunk->subh.sack_hdr = sackh;
3310 ctsn = ntohl(sackh->cum_tsn_ack);
3312 /* If Cumulative TSN Ack beyond the max tsn currently
3313 * send, terminating the association and respond to the
3314 * sender with an ABORT.
3316 if (TSN_lte(asoc->next_tsn, ctsn))
3317 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3319 trace_sctp_probe(ep, asoc, chunk);
3321 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3322 * Ack Point, then drop the SACK. Since Cumulative TSN
3323 * Ack is monotonically increasing, a SACK whose
3324 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3325 * Point indicates an out-of-order SACK.
3327 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3328 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3329 asoc->ctsn_ack_point);
3331 return SCTP_DISPOSITION_DISCARD;
3334 /* Return this SACK for further processing. */
3335 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3337 /* Note: We do the rest of the work on the PROCESS_SACK
3340 return SCTP_DISPOSITION_CONSUME;
3344 * Generate an ABORT in response to a packet.
3346 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3348 * 8) The receiver should respond to the sender of the OOTB packet with
3349 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3350 * MUST fill in the Verification Tag field of the outbound packet
3351 * with the value found in the Verification Tag field of the OOTB
3352 * packet and set the T-bit in the Chunk Flags to indicate that the
3353 * Verification Tag is reflected. After sending this ABORT, the
3354 * receiver of the OOTB packet shall discard the OOTB packet and take
3355 * no further action.
3359 * The return value is the disposition of the chunk.
3361 static enum sctp_disposition sctp_sf_tabort_8_4_8(
3363 const struct sctp_endpoint *ep,
3364 const struct sctp_association *asoc,
3365 const union sctp_subtype type,
3367 struct sctp_cmd_seq *commands)
3369 struct sctp_packet *packet = NULL;
3370 struct sctp_chunk *chunk = arg;
3371 struct sctp_chunk *abort;
3373 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3375 return SCTP_DISPOSITION_NOMEM;
3377 /* Make an ABORT. The T bit will be set if the asoc
3380 abort = sctp_make_abort(asoc, chunk, 0);
3382 sctp_ootb_pkt_free(packet);
3383 return SCTP_DISPOSITION_NOMEM;
3386 /* Reflect vtag if T-Bit is set */
3387 if (sctp_test_T_bit(abort))
3388 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3390 /* Set the skb to the belonging sock for accounting. */
3391 abort->skb->sk = ep->base.sk;
3393 sctp_packet_append_chunk(packet, abort);
3395 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3396 SCTP_PACKET(packet));
3398 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3400 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3401 return SCTP_DISPOSITION_CONSUME;
3405 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3406 * event as ULP notification for each cause included in the chunk.
3408 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3410 * The return value is the disposition of the chunk.
3412 enum sctp_disposition sctp_sf_operr_notify(struct net *net,
3413 const struct sctp_endpoint *ep,
3414 const struct sctp_association *asoc,
3415 const union sctp_subtype type,
3417 struct sctp_cmd_seq *commands)
3419 struct sctp_chunk *chunk = arg;
3420 struct sctp_errhdr *err;
3422 if (!sctp_vtag_verify(chunk, asoc))
3423 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3425 /* Make sure that the ERROR chunk has a valid length. */
3426 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_operr_chunk)))
3427 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3429 sctp_walk_errors(err, chunk->chunk_hdr);
3430 if ((void *)err != (void *)chunk->chunk_end)
3431 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3432 (void *)err, commands);
3434 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3437 return SCTP_DISPOSITION_CONSUME;
3441 * Process an inbound SHUTDOWN ACK.
3444 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3445 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3446 * peer, and remove all record of the association.
3448 * The return value is the disposition.
3450 enum sctp_disposition sctp_sf_do_9_2_final(struct net *net,
3451 const struct sctp_endpoint *ep,
3452 const struct sctp_association *asoc,
3453 const union sctp_subtype type,
3455 struct sctp_cmd_seq *commands)
3457 struct sctp_chunk *chunk = arg;
3458 struct sctp_chunk *reply;
3459 struct sctp_ulpevent *ev;
3461 if (!sctp_vtag_verify(chunk, asoc))
3462 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3464 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3465 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3466 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3468 /* 10.2 H) SHUTDOWN COMPLETE notification
3470 * When SCTP completes the shutdown procedures (section 9.2) this
3471 * notification is passed to the upper layer.
3473 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3474 0, 0, 0, NULL, GFP_ATOMIC);
3478 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3479 reply = sctp_make_shutdown_complete(asoc, chunk);
3483 /* Do all the commands now (after allocation), so that we
3484 * have consistent state if memory allocation failes
3486 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3488 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3489 * stop the T2-shutdown timer,
3491 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3492 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3494 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3495 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3497 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3498 SCTP_STATE(SCTP_STATE_CLOSED));
3499 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3500 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3501 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3503 /* ...and remove all record of the association. */
3504 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3505 return SCTP_DISPOSITION_DELETE_TCB;
3508 sctp_ulpevent_free(ev);
3510 return SCTP_DISPOSITION_NOMEM;
3514 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3516 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3517 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3518 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3519 * packet must fill in the Verification Tag field of the outbound
3520 * packet with the Verification Tag received in the SHUTDOWN ACK and
3521 * set the T-bit in the Chunk Flags to indicate that the Verification
3524 * 8) The receiver should respond to the sender of the OOTB packet with
3525 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3526 * MUST fill in the Verification Tag field of the outbound packet
3527 * with the value found in the Verification Tag field of the OOTB
3528 * packet and set the T-bit in the Chunk Flags to indicate that the
3529 * Verification Tag is reflected. After sending this ABORT, the
3530 * receiver of the OOTB packet shall discard the OOTB packet and take
3531 * no further action.
3533 enum sctp_disposition sctp_sf_ootb(struct net *net,
3534 const struct sctp_endpoint *ep,
3535 const struct sctp_association *asoc,
3536 const union sctp_subtype type,
3537 void *arg, struct sctp_cmd_seq *commands)
3539 struct sctp_chunk *chunk = arg;
3540 struct sk_buff *skb = chunk->skb;
3541 struct sctp_chunkhdr *ch;
3542 struct sctp_errhdr *err;
3543 int ootb_cookie_ack = 0;
3544 int ootb_shut_ack = 0;
3547 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3549 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3551 /* Report violation if the chunk is less then minimal */
3552 if (ntohs(ch->length) < sizeof(*ch))
3553 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3556 /* Report violation if chunk len overflows */
3557 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3558 if (ch_end > skb_tail_pointer(skb))
3559 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3562 /* Now that we know we at least have a chunk header,
3563 * do things that are type appropriate.
3565 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3568 /* RFC 2960, Section 3.3.7
3569 * Moreover, under any circumstances, an endpoint that
3570 * receives an ABORT MUST NOT respond to that ABORT by
3571 * sending an ABORT of its own.
3573 if (SCTP_CID_ABORT == ch->type)
3574 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3576 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3577 * or a COOKIE ACK the SCTP Packet should be silently
3581 if (SCTP_CID_COOKIE_ACK == ch->type)
3582 ootb_cookie_ack = 1;
3584 if (SCTP_CID_ERROR == ch->type) {
3585 sctp_walk_errors(err, ch) {
3586 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3587 ootb_cookie_ack = 1;
3593 ch = (struct sctp_chunkhdr *)ch_end;
3594 } while (ch_end < skb_tail_pointer(skb));
3597 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3598 else if (ootb_cookie_ack)
3599 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3601 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3605 * Handle an "Out of the blue" SHUTDOWN ACK.
3607 * Section: 8.4 5, sctpimpguide 2.41.
3609 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3610 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3611 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3612 * packet must fill in the Verification Tag field of the outbound
3613 * packet with the Verification Tag received in the SHUTDOWN ACK and
3614 * set the T-bit in the Chunk Flags to indicate that the Verification
3618 * (endpoint, asoc, type, arg, commands)
3621 * (enum sctp_disposition)
3623 * The return value is the disposition of the chunk.
3625 static enum sctp_disposition sctp_sf_shut_8_4_5(
3627 const struct sctp_endpoint *ep,
3628 const struct sctp_association *asoc,
3629 const union sctp_subtype type,
3631 struct sctp_cmd_seq *commands)
3633 struct sctp_packet *packet = NULL;
3634 struct sctp_chunk *chunk = arg;
3635 struct sctp_chunk *shut;
3637 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3639 return SCTP_DISPOSITION_NOMEM;
3641 /* Make an SHUTDOWN_COMPLETE.
3642 * The T bit will be set if the asoc is NULL.
3644 shut = sctp_make_shutdown_complete(asoc, chunk);
3646 sctp_ootb_pkt_free(packet);
3647 return SCTP_DISPOSITION_NOMEM;
3650 /* Reflect vtag if T-Bit is set */
3651 if (sctp_test_T_bit(shut))
3652 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3654 /* Set the skb to the belonging sock for accounting. */
3655 shut->skb->sk = ep->base.sk;
3657 sctp_packet_append_chunk(packet, shut);
3659 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3660 SCTP_PACKET(packet));
3662 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3664 /* If the chunk length is invalid, we don't want to process
3665 * the reset of the packet.
3667 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3668 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3670 /* We need to discard the rest of the packet to prevent
3671 * potential bomming attacks from additional bundled chunks.
3672 * This is documented in SCTP Threats ID.
3674 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3678 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3680 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3681 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3682 * procedures in section 8.4 SHOULD be followed, in other words it
3683 * should be treated as an Out Of The Blue packet.
3684 * [This means that we do NOT check the Verification Tag on these
3688 enum sctp_disposition sctp_sf_do_8_5_1_E_sa(struct net *net,
3689 const struct sctp_endpoint *ep,
3690 const struct sctp_association *asoc,
3691 const union sctp_subtype type,
3693 struct sctp_cmd_seq *commands)
3695 struct sctp_chunk *chunk = arg;
3697 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3698 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3699 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3702 /* Although we do have an association in this case, it corresponds
3703 * to a restarted association. So the packet is treated as an OOTB
3704 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3705 * called with a NULL association.
3707 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3709 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3712 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3713 enum sctp_disposition sctp_sf_do_asconf(struct net *net,
3714 const struct sctp_endpoint *ep,
3715 const struct sctp_association *asoc,
3716 const union sctp_subtype type,
3718 struct sctp_cmd_seq *commands)
3720 struct sctp_paramhdr *err_param = NULL;
3721 struct sctp_chunk *asconf_ack = NULL;
3722 struct sctp_chunk *chunk = arg;
3723 struct sctp_addiphdr *hdr;
3726 if (!sctp_vtag_verify(chunk, asoc)) {
3727 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3729 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3732 /* ADD-IP: Section 4.1.1
3733 * This chunk MUST be sent in an authenticated way by using
3734 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3735 * is received unauthenticated it MUST be silently discarded as
3736 * described in [I-D.ietf-tsvwg-sctp-auth].
3738 if (!asoc->peer.asconf_capable ||
3739 (!net->sctp.addip_noauth && !chunk->auth))
3740 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3743 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3744 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_addip_chunk)))
3745 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3748 hdr = (struct sctp_addiphdr *)chunk->skb->data;
3749 serial = ntohl(hdr->serial);
3751 /* Verify the ASCONF chunk before processing it. */
3752 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3753 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3754 (void *)err_param, commands);
3756 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3757 * the endpoint stored in a new association variable
3758 * 'Peer-Serial-Number'.
3760 if (serial == asoc->peer.addip_serial + 1) {
3761 /* If this is the first instance of ASCONF in the packet,
3762 * we can clean our old ASCONF-ACKs.
3764 if (!chunk->has_asconf)
3765 sctp_assoc_clean_asconf_ack_cache(asoc);
3767 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3768 * expected, process the ASCONF as described below and after
3769 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3770 * the response packet and cache a copy of it (in the event it
3771 * later needs to be retransmitted).
3773 * Essentially, do V1-V5.
3775 asconf_ack = sctp_process_asconf((struct sctp_association *)
3778 return SCTP_DISPOSITION_NOMEM;
3779 } else if (serial < asoc->peer.addip_serial + 1) {
3781 * If the value found in the Sequence Number is less than the
3782 * ('Peer- Sequence-Number' + 1), simply skip to the next
3783 * ASCONF, and include in the outbound response packet
3784 * any previously cached ASCONF-ACK response that was
3785 * sent and saved that matches the Sequence Number of the
3786 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3787 * Chunk exists. This will occur when an older ASCONF
3788 * arrives out of order. In such a case, the receiver
3789 * should skip the ASCONF Chunk and not include ASCONF-ACK
3790 * Chunk for that chunk.
3792 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3794 return SCTP_DISPOSITION_DISCARD;
3796 /* Reset the transport so that we select the correct one
3797 * this time around. This is to make sure that we don't
3798 * accidentally use a stale transport that's been removed.
3800 asconf_ack->transport = NULL;
3802 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3803 * it must be either a stale packet or from an attacker.
3805 return SCTP_DISPOSITION_DISCARD;
3808 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3809 * containing the ASCONF-ACK Chunks MUST be the source address of
3810 * the SCTP packet that held the ASCONF Chunks.
3812 * To do this properly, we'll set the destination address of the chunk
3813 * and at the transmit time, will try look up the transport to use.
3814 * Since ASCONFs may be bundled, the correct transport may not be
3815 * created until we process the entire packet, thus this workaround.
3817 asconf_ack->dest = chunk->source;
3818 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3819 if (asoc->new_transport) {
3820 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3821 ((struct sctp_association *)asoc)->new_transport = NULL;
3824 return SCTP_DISPOSITION_CONSUME;
3827 static enum sctp_disposition sctp_send_next_asconf(
3829 const struct sctp_endpoint *ep,
3830 struct sctp_association *asoc,
3831 const union sctp_subtype type,
3832 struct sctp_cmd_seq *commands)
3834 struct sctp_chunk *asconf;
3835 struct list_head *entry;
3837 if (list_empty(&asoc->addip_chunk_list))
3838 return SCTP_DISPOSITION_CONSUME;
3840 entry = asoc->addip_chunk_list.next;
3841 asconf = list_entry(entry, struct sctp_chunk, list);
3843 list_del_init(entry);
3844 sctp_chunk_hold(asconf);
3845 asoc->addip_last_asconf = asconf;
3847 return sctp_sf_do_prm_asconf(net, ep, asoc, type, asconf, commands);
3851 * ADDIP Section 4.3 General rules for address manipulation
3852 * When building TLV parameters for the ASCONF Chunk that will add or
3853 * delete IP addresses the D0 to D13 rules should be applied:
3855 enum sctp_disposition sctp_sf_do_asconf_ack(struct net *net,
3856 const struct sctp_endpoint *ep,
3857 const struct sctp_association *asoc,
3858 const union sctp_subtype type,
3860 struct sctp_cmd_seq *commands)
3862 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3863 struct sctp_paramhdr *err_param = NULL;
3864 struct sctp_chunk *asconf_ack = arg;
3865 struct sctp_addiphdr *addip_hdr;
3866 __u32 sent_serial, rcvd_serial;
3867 struct sctp_chunk *abort;
3869 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3870 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3872 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3875 /* ADD-IP, Section 4.1.2:
3876 * This chunk MUST be sent in an authenticated way by using
3877 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3878 * is received unauthenticated it MUST be silently discarded as
3879 * described in [I-D.ietf-tsvwg-sctp-auth].
3881 if (!asoc->peer.asconf_capable ||
3882 (!net->sctp.addip_noauth && !asconf_ack->auth))
3883 return sctp_sf_discard_chunk(net, ep, asoc, type, arg,
3886 /* Make sure that the ADDIP chunk has a valid length. */
3887 if (!sctp_chunk_length_valid(asconf_ack,
3888 sizeof(struct sctp_addip_chunk)))
3889 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3892 addip_hdr = (struct sctp_addiphdr *)asconf_ack->skb->data;
3893 rcvd_serial = ntohl(addip_hdr->serial);
3895 /* Verify the ASCONF-ACK chunk before processing it. */
3896 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3897 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3898 (void *)err_param, commands);
3901 addip_hdr = (struct sctp_addiphdr *)last_asconf->subh.addip_hdr;
3902 sent_serial = ntohl(addip_hdr->serial);
3904 sent_serial = asoc->addip_serial - 1;
3907 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3908 * equal to the next serial number to be used but no ASCONF chunk is
3909 * outstanding the endpoint MUST ABORT the association. Note that a
3910 * sequence number is greater than if it is no more than 2^^31-1
3911 * larger than the current sequence number (using serial arithmetic).
3913 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3914 !(asoc->addip_last_asconf)) {
3915 abort = sctp_make_abort(asoc, asconf_ack,
3916 sizeof(struct sctp_errhdr));
3918 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3919 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3922 /* We are going to ABORT, so we might as well stop
3923 * processing the rest of the chunks in the packet.
3925 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3926 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3927 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3928 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3929 SCTP_ERROR(ECONNABORTED));
3930 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3931 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3932 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3933 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3934 return SCTP_DISPOSITION_ABORT;
3937 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3938 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3939 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3941 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3943 return sctp_send_next_asconf(net, ep,
3944 (struct sctp_association *)asoc,
3947 abort = sctp_make_abort(asoc, asconf_ack,
3948 sizeof(struct sctp_errhdr));
3950 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3951 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3954 /* We are going to ABORT, so we might as well stop
3955 * processing the rest of the chunks in the packet.
3957 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3958 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3959 SCTP_ERROR(ECONNABORTED));
3960 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3961 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3962 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3963 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3964 return SCTP_DISPOSITION_ABORT;
3967 return SCTP_DISPOSITION_DISCARD;
3970 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3971 enum sctp_disposition sctp_sf_do_reconf(struct net *net,
3972 const struct sctp_endpoint *ep,
3973 const struct sctp_association *asoc,
3974 const union sctp_subtype type,
3976 struct sctp_cmd_seq *commands)
3978 struct sctp_paramhdr *err_param = NULL;
3979 struct sctp_chunk *chunk = arg;
3980 struct sctp_reconf_chunk *hdr;
3981 union sctp_params param;
3983 if (!sctp_vtag_verify(chunk, asoc)) {
3984 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3986 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3989 /* Make sure that the RECONF chunk has a valid length. */
3990 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3991 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3994 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3995 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3996 (void *)err_param, commands);
3998 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3999 sctp_walk_params(param, hdr, params) {
4000 struct sctp_chunk *reply = NULL;
4001 struct sctp_ulpevent *ev = NULL;
4003 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
4004 reply = sctp_process_strreset_outreq(
4005 (struct sctp_association *)asoc, param, &ev);
4006 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
4007 reply = sctp_process_strreset_inreq(
4008 (struct sctp_association *)asoc, param, &ev);
4009 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
4010 reply = sctp_process_strreset_tsnreq(
4011 (struct sctp_association *)asoc, param, &ev);
4012 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
4013 reply = sctp_process_strreset_addstrm_out(
4014 (struct sctp_association *)asoc, param, &ev);
4015 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
4016 reply = sctp_process_strreset_addstrm_in(
4017 (struct sctp_association *)asoc, param, &ev);
4018 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
4019 reply = sctp_process_strreset_resp(
4020 (struct sctp_association *)asoc, param, &ev);
4023 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4027 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4031 return SCTP_DISPOSITION_CONSUME;
4035 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
4037 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
4038 * its cumulative TSN point to the value carried in the FORWARD TSN
4039 * chunk, and then MUST further advance its cumulative TSN point locally
4041 * After the above processing, the data receiver MUST stop reporting any
4042 * missing TSNs earlier than or equal to the new cumulative TSN point.
4044 * Verification Tag: 8.5 Verification Tag [Normal verification]
4046 * The return value is the disposition of the chunk.
4048 enum sctp_disposition sctp_sf_eat_fwd_tsn(struct net *net,
4049 const struct sctp_endpoint *ep,
4050 const struct sctp_association *asoc,
4051 const union sctp_subtype type,
4053 struct sctp_cmd_seq *commands)
4055 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4056 struct sctp_chunk *chunk = arg;
4060 if (!sctp_vtag_verify(chunk, asoc)) {
4061 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4063 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4066 if (!asoc->peer.prsctp_capable)
4067 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4069 /* Make sure that the FORWARD_TSN chunk has valid length. */
4070 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4071 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4074 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4075 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4076 len = ntohs(chunk->chunk_hdr->length);
4077 len -= sizeof(struct sctp_chunkhdr);
4078 skb_pull(chunk->skb, len);
4080 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4081 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4083 /* The TSN is too high--silently discard the chunk and count on it
4084 * getting retransmitted later.
4086 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4087 goto discard_noforce;
4089 if (!asoc->stream.si->validate_ftsn(chunk))
4090 goto discard_noforce;
4092 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4093 if (len > sctp_ftsnhdr_len(&asoc->stream))
4094 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4097 /* Count this as receiving DATA. */
4098 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
4099 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4100 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4103 /* FIXME: For now send a SACK, but DATA processing may
4106 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
4108 return SCTP_DISPOSITION_CONSUME;
4111 return SCTP_DISPOSITION_DISCARD;
4114 enum sctp_disposition sctp_sf_eat_fwd_tsn_fast(
4116 const struct sctp_endpoint *ep,
4117 const struct sctp_association *asoc,
4118 const union sctp_subtype type,
4120 struct sctp_cmd_seq *commands)
4122 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
4123 struct sctp_chunk *chunk = arg;
4127 if (!sctp_vtag_verify(chunk, asoc)) {
4128 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4130 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4133 if (!asoc->peer.prsctp_capable)
4134 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4136 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4137 if (!sctp_chunk_length_valid(chunk, sctp_ftsnchk_len(&asoc->stream)))
4138 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4141 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4142 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4143 len = ntohs(chunk->chunk_hdr->length);
4144 len -= sizeof(struct sctp_chunkhdr);
4145 skb_pull(chunk->skb, len);
4147 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4148 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4150 /* The TSN is too high--silently discard the chunk and count on it
4151 * getting retransmitted later.
4153 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4156 if (!asoc->stream.si->validate_ftsn(chunk))
4159 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4160 if (len > sctp_ftsnhdr_len(&asoc->stream))
4161 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4164 /* Go a head and force a SACK, since we are shutting down. */
4166 /* Implementor's Guide.
4168 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4169 * respond to each received packet containing one or more DATA chunk(s)
4170 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4172 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4173 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4174 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4175 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4177 return SCTP_DISPOSITION_CONSUME;
4181 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4183 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4184 * Identifier field. If this algorithm was not specified by the
4185 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4186 * during association setup, the AUTH chunk and all chunks after it MUST
4187 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4188 * defined in Section 4.1.
4190 * If an endpoint with no shared key receives a Shared Key Identifier
4191 * other than 0, it MUST silently discard all authenticated chunks. If
4192 * the endpoint has at least one endpoint pair shared key for the peer,
4193 * it MUST use the key specified by the Shared Key Identifier if a
4194 * key has been configured for that Shared Key Identifier. If no
4195 * endpoint pair shared key has been configured for that Shared Key
4196 * Identifier, all authenticated chunks MUST be silently discarded.
4198 * Verification Tag: 8.5 Verification Tag [Normal verification]
4200 * The return value is the disposition of the chunk.
4202 static enum sctp_ierror sctp_sf_authenticate(
4203 const struct sctp_association *asoc,
4204 struct sctp_chunk *chunk)
4206 struct sctp_shared_key *sh_key = NULL;
4207 struct sctp_authhdr *auth_hdr;
4208 __u8 *save_digest, *digest;
4209 struct sctp_hmac *hmac;
4210 unsigned int sig_len;
4213 /* Pull in the auth header, so we can do some more verification */
4214 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4215 chunk->subh.auth_hdr = auth_hdr;
4216 skb_pull(chunk->skb, sizeof(*auth_hdr));
4218 /* Make sure that we support the HMAC algorithm from the auth
4221 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4222 return SCTP_IERROR_AUTH_BAD_HMAC;
4224 /* Make sure that the provided shared key identifier has been
4227 key_id = ntohs(auth_hdr->shkey_id);
4228 if (key_id != asoc->active_key_id) {
4229 sh_key = sctp_auth_get_shkey(asoc, key_id);
4231 return SCTP_IERROR_AUTH_BAD_KEYID;
4234 /* Make sure that the length of the signature matches what
4237 sig_len = ntohs(chunk->chunk_hdr->length) -
4238 sizeof(struct sctp_auth_chunk);
4239 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4240 if (sig_len != hmac->hmac_len)
4241 return SCTP_IERROR_PROTO_VIOLATION;
4243 /* Now that we've done validation checks, we can compute and
4244 * verify the hmac. The steps involved are:
4245 * 1. Save the digest from the chunk.
4246 * 2. Zero out the digest in the chunk.
4247 * 3. Compute the new digest
4248 * 4. Compare saved and new digests.
4250 digest = auth_hdr->hmac;
4251 skb_pull(chunk->skb, sig_len);
4253 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4257 memset(digest, 0, sig_len);
4259 sctp_auth_calculate_hmac(asoc, chunk->skb,
4260 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4261 sh_key, GFP_ATOMIC);
4263 /* Discard the packet if the digests do not match */
4264 if (memcmp(save_digest, digest, sig_len)) {
4266 return SCTP_IERROR_BAD_SIG;
4272 return SCTP_IERROR_NO_ERROR;
4274 return SCTP_IERROR_NOMEM;
4277 enum sctp_disposition sctp_sf_eat_auth(struct net *net,
4278 const struct sctp_endpoint *ep,
4279 const struct sctp_association *asoc,
4280 const union sctp_subtype type,
4281 void *arg, struct sctp_cmd_seq *commands)
4283 struct sctp_chunk *chunk = arg;
4284 struct sctp_authhdr *auth_hdr;
4285 struct sctp_chunk *err_chunk;
4286 enum sctp_ierror error;
4288 /* Make sure that the peer has AUTH capable */
4289 if (!asoc->peer.auth_capable)
4290 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4292 if (!sctp_vtag_verify(chunk, asoc)) {
4293 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4295 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4298 /* Make sure that the AUTH chunk has valid length. */
4299 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4300 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4303 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4304 error = sctp_sf_authenticate(asoc, chunk);
4306 case SCTP_IERROR_AUTH_BAD_HMAC:
4307 /* Generate the ERROR chunk and discard the rest
4310 err_chunk = sctp_make_op_error(asoc, chunk,
4311 SCTP_ERROR_UNSUP_HMAC,
4315 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4316 SCTP_CHUNK(err_chunk));
4319 case SCTP_IERROR_AUTH_BAD_KEYID:
4320 case SCTP_IERROR_BAD_SIG:
4321 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4323 case SCTP_IERROR_PROTO_VIOLATION:
4324 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4327 case SCTP_IERROR_NOMEM:
4328 return SCTP_DISPOSITION_NOMEM;
4330 default: /* Prevent gcc warnings */
4334 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4335 struct sctp_ulpevent *ev;
4337 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4338 SCTP_AUTH_NEW_KEY, GFP_ATOMIC);
4343 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4347 return SCTP_DISPOSITION_CONSUME;
4351 * Process an unknown chunk.
4353 * Section: 3.2. Also, 2.1 in the implementor's guide.
4355 * Chunk Types are encoded such that the highest-order two bits specify
4356 * the action that must be taken if the processing endpoint does not
4357 * recognize the Chunk Type.
4359 * 00 - Stop processing this SCTP packet and discard it, do not process
4360 * any further chunks within it.
4362 * 01 - Stop processing this SCTP packet and discard it, do not process
4363 * any further chunks within it, and report the unrecognized
4364 * chunk in an 'Unrecognized Chunk Type'.
4366 * 10 - Skip this chunk and continue processing.
4368 * 11 - Skip this chunk and continue processing, but report in an ERROR
4369 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4371 * The return value is the disposition of the chunk.
4373 enum sctp_disposition sctp_sf_unk_chunk(struct net *net,
4374 const struct sctp_endpoint *ep,
4375 const struct sctp_association *asoc,
4376 const union sctp_subtype type,
4378 struct sctp_cmd_seq *commands)
4380 struct sctp_chunk *unk_chunk = arg;
4381 struct sctp_chunk *err_chunk;
4382 struct sctp_chunkhdr *hdr;
4384 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4386 if (!sctp_vtag_verify(unk_chunk, asoc))
4387 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4389 /* Make sure that the chunk has a valid length.
4390 * Since we don't know the chunk type, we use a general
4391 * chunkhdr structure to make a comparison.
4393 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4394 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4397 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4398 case SCTP_CID_ACTION_DISCARD:
4399 /* Discard the packet. */
4400 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4401 case SCTP_CID_ACTION_DISCARD_ERR:
4402 /* Generate an ERROR chunk as response. */
4403 hdr = unk_chunk->chunk_hdr;
4404 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4405 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4406 SCTP_PAD4(ntohs(hdr->length)),
4409 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4410 SCTP_CHUNK(err_chunk));
4413 /* Discard the packet. */
4414 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4415 return SCTP_DISPOSITION_CONSUME;
4416 case SCTP_CID_ACTION_SKIP:
4417 /* Skip the chunk. */
4418 return SCTP_DISPOSITION_DISCARD;
4419 case SCTP_CID_ACTION_SKIP_ERR:
4420 /* Generate an ERROR chunk as response. */
4421 hdr = unk_chunk->chunk_hdr;
4422 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4423 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4424 SCTP_PAD4(ntohs(hdr->length)),
4427 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4428 SCTP_CHUNK(err_chunk));
4430 /* Skip the chunk. */
4431 return SCTP_DISPOSITION_CONSUME;
4436 return SCTP_DISPOSITION_DISCARD;
4440 * Discard the chunk.
4442 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4443 * [Too numerous to mention...]
4444 * Verification Tag: No verification needed.
4446 * (endpoint, asoc, chunk)
4449 * (asoc, reply_msg, msg_up, timers, counters)
4451 * The return value is the disposition of the chunk.
4453 enum sctp_disposition sctp_sf_discard_chunk(struct net *net,
4454 const struct sctp_endpoint *ep,
4455 const struct sctp_association *asoc,
4456 const union sctp_subtype type,
4458 struct sctp_cmd_seq *commands)
4460 struct sctp_chunk *chunk = arg;
4462 /* Make sure that the chunk has a valid length.
4463 * Since we don't know the chunk type, we use a general
4464 * chunkhdr structure to make a comparison.
4466 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4467 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4470 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4472 return SCTP_DISPOSITION_DISCARD;
4476 * Discard the whole packet.
4480 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4481 * silently discard the OOTB packet and take no further action.
4483 * Verification Tag: No verification necessary
4486 * (endpoint, asoc, chunk)
4489 * (asoc, reply_msg, msg_up, timers, counters)
4491 * The return value is the disposition of the chunk.
4493 enum sctp_disposition sctp_sf_pdiscard(struct net *net,
4494 const struct sctp_endpoint *ep,
4495 const struct sctp_association *asoc,
4496 const union sctp_subtype type,
4497 void *arg, struct sctp_cmd_seq *commands)
4499 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4500 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4502 return SCTP_DISPOSITION_CONSUME;
4507 * The other end is violating protocol.
4509 * Section: Not specified
4510 * Verification Tag: Not specified
4512 * (endpoint, asoc, chunk)
4515 * (asoc, reply_msg, msg_up, timers, counters)
4517 * We simply tag the chunk as a violation. The state machine will log
4518 * the violation and continue.
4520 enum sctp_disposition sctp_sf_violation(struct net *net,
4521 const struct sctp_endpoint *ep,
4522 const struct sctp_association *asoc,
4523 const union sctp_subtype type,
4525 struct sctp_cmd_seq *commands)
4527 struct sctp_chunk *chunk = arg;
4529 /* Make sure that the chunk has a valid length. */
4530 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4531 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4534 return SCTP_DISPOSITION_VIOLATION;
4538 * Common function to handle a protocol violation.
4540 static enum sctp_disposition sctp_sf_abort_violation(
4542 const struct sctp_endpoint *ep,
4543 const struct sctp_association *asoc,
4545 struct sctp_cmd_seq *commands,
4546 const __u8 *payload,
4547 const size_t paylen)
4549 struct sctp_packet *packet = NULL;
4550 struct sctp_chunk *chunk = arg;
4551 struct sctp_chunk *abort = NULL;
4553 /* SCTP-AUTH, Section 6.3:
4554 * It should be noted that if the receiver wants to tear
4555 * down an association in an authenticated way only, the
4556 * handling of malformed packets should not result in
4557 * tearing down the association.
4559 * This means that if we only want to abort associations
4560 * in an authenticated way (i.e AUTH+ABORT), then we
4561 * can't destroy this association just because the packet
4564 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4567 /* Make the abort chunk. */
4568 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4573 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4574 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4575 !asoc->peer.i.init_tag) {
4576 struct sctp_initack_chunk *initack;
4578 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
4579 if (!sctp_chunk_length_valid(chunk, sizeof(*initack)))
4580 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4582 unsigned int inittag;
4584 inittag = ntohl(initack->init_hdr.init_tag);
4585 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4590 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4591 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4593 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4594 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4595 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4596 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4597 SCTP_ERROR(ECONNREFUSED));
4598 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4599 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4601 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4602 SCTP_ERROR(ECONNABORTED));
4603 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4604 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4605 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4608 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4613 if (sctp_test_T_bit(abort))
4614 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4616 abort->skb->sk = ep->base.sk;
4618 sctp_packet_append_chunk(packet, abort);
4620 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4621 SCTP_PACKET(packet));
4623 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4626 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4629 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4630 return SCTP_DISPOSITION_ABORT;
4633 sctp_chunk_free(abort);
4635 return SCTP_DISPOSITION_NOMEM;
4639 * Handle a protocol violation when the chunk length is invalid.
4640 * "Invalid" length is identified as smaller than the minimal length a
4641 * given chunk can be. For example, a SACK chunk has invalid length
4642 * if its length is set to be smaller than the size of struct sctp_sack_chunk.
4644 * We inform the other end by sending an ABORT with a Protocol Violation
4647 * Section: Not specified
4648 * Verification Tag: Nothing to do
4650 * (endpoint, asoc, chunk)
4653 * (reply_msg, msg_up, counters)
4655 * Generate an ABORT chunk and terminate the association.
4657 static enum sctp_disposition sctp_sf_violation_chunklen(
4659 const struct sctp_endpoint *ep,
4660 const struct sctp_association *asoc,
4661 const union sctp_subtype type,
4663 struct sctp_cmd_seq *commands)
4665 static const char err_str[] = "The following chunk had invalid length:";
4667 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4672 * Handle a protocol violation when the parameter length is invalid.
4673 * If the length is smaller than the minimum length of a given parameter,
4674 * or accumulated length in multi parameters exceeds the end of the chunk,
4675 * the length is considered as invalid.
4677 static enum sctp_disposition sctp_sf_violation_paramlen(
4679 const struct sctp_endpoint *ep,
4680 const struct sctp_association *asoc,
4681 const union sctp_subtype type,
4682 void *arg, void *ext,
4683 struct sctp_cmd_seq *commands)
4685 struct sctp_paramhdr *param = ext;
4686 struct sctp_chunk *abort = NULL;
4687 struct sctp_chunk *chunk = arg;
4689 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4692 /* Make the abort chunk. */
4693 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4697 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4698 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4700 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4701 SCTP_ERROR(ECONNABORTED));
4702 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4703 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4704 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4705 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4708 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4709 return SCTP_DISPOSITION_ABORT;
4711 return SCTP_DISPOSITION_NOMEM;
4714 /* Handle a protocol violation when the peer trying to advance the
4715 * cumulative tsn ack to a point beyond the max tsn currently sent.
4717 * We inform the other end by sending an ABORT with a Protocol Violation
4720 static enum sctp_disposition sctp_sf_violation_ctsn(
4722 const struct sctp_endpoint *ep,
4723 const struct sctp_association *asoc,
4724 const union sctp_subtype type,
4726 struct sctp_cmd_seq *commands)
4728 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4730 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4734 /* Handle protocol violation of an invalid chunk bundling. For example,
4735 * when we have an association and we receive bundled INIT-ACK, or
4736 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4737 * statement from the specs. Additionally, there might be an attacker
4738 * on the path and we may not want to continue this communication.
4740 static enum sctp_disposition sctp_sf_violation_chunk(
4742 const struct sctp_endpoint *ep,
4743 const struct sctp_association *asoc,
4744 const union sctp_subtype type,
4746 struct sctp_cmd_seq *commands)
4748 static const char err_str[] = "The following chunk violates protocol:";
4751 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4753 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4756 /***************************************************************************
4757 * These are the state functions for handling primitive (Section 10) events.
4758 ***************************************************************************/
4760 * sctp_sf_do_prm_asoc
4762 * Section: 10.1 ULP-to-SCTP
4765 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4766 * outbound stream count)
4767 * -> association id [,destination transport addr list] [,outbound stream
4770 * This primitive allows the upper layer to initiate an association to a
4771 * specific peer endpoint.
4773 * The peer endpoint shall be specified by one of the transport addresses
4774 * which defines the endpoint (see Section 1.4). If the local SCTP
4775 * instance has not been initialized, the ASSOCIATE is considered an
4777 * [This is not relevant for the kernel implementation since we do all
4778 * initialization at boot time. It we hadn't initialized we wouldn't
4779 * get anywhere near this code.]
4781 * An association id, which is a local handle to the SCTP association,
4782 * will be returned on successful establishment of the association. If
4783 * SCTP is not able to open an SCTP association with the peer endpoint,
4784 * an error is returned.
4785 * [In the kernel implementation, the struct sctp_association needs to
4786 * be created BEFORE causing this primitive to run.]
4788 * Other association parameters may be returned, including the
4789 * complete destination transport addresses of the peer as well as the
4790 * outbound stream count of the local endpoint. One of the transport
4791 * address from the returned destination addresses will be selected by
4792 * the local endpoint as default primary path for sending SCTP packets
4793 * to this peer. The returned "destination transport addr list" can
4794 * be used by the ULP to change the default primary path or to force
4795 * sending a packet to a specific transport address. [All of this
4796 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4799 * Mandatory attributes:
4801 * o local SCTP instance name - obtained from the INITIALIZE operation.
4802 * [This is the argument asoc.]
4803 * o destination transport addr - specified as one of the transport
4804 * addresses of the peer endpoint with which the association is to be
4806 * [This is asoc->peer.active_path.]
4807 * o outbound stream count - the number of outbound streams the ULP
4808 * would like to open towards this peer endpoint.
4809 * [BUG: This is not currently implemented.]
4810 * Optional attributes:
4814 * The return value is a disposition.
4816 enum sctp_disposition sctp_sf_do_prm_asoc(struct net *net,
4817 const struct sctp_endpoint *ep,
4818 const struct sctp_association *asoc,
4819 const union sctp_subtype type,
4821 struct sctp_cmd_seq *commands)
4823 struct sctp_association *my_asoc;
4824 struct sctp_chunk *repl;
4826 /* The comment below says that we enter COOKIE-WAIT AFTER
4827 * sending the INIT, but that doesn't actually work in our
4830 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4831 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4833 /* RFC 2960 5.1 Normal Establishment of an Association
4835 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4836 * must provide its Verification Tag (Tag_A) in the Initiate
4837 * Tag field. Tag_A SHOULD be a random number in the range of
4838 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4841 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4845 /* Choose transport for INIT. */
4846 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4849 /* Cast away the const modifier, as we want to just
4850 * rerun it through as a sideffect.
4852 my_asoc = (struct sctp_association *)asoc;
4853 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4855 /* After sending the INIT, "A" starts the T1-init timer and
4856 * enters the COOKIE-WAIT state.
4858 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4859 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4860 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4861 return SCTP_DISPOSITION_CONSUME;
4864 return SCTP_DISPOSITION_NOMEM;
4868 * Process the SEND primitive.
4870 * Section: 10.1 ULP-to-SCTP
4873 * Format: SEND(association id, buffer address, byte count [,context]
4874 * [,stream id] [,life time] [,destination transport address]
4875 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4878 * This is the main method to send user data via SCTP.
4880 * Mandatory attributes:
4882 * o association id - local handle to the SCTP association
4884 * o buffer address - the location where the user message to be
4885 * transmitted is stored;
4887 * o byte count - The size of the user data in number of bytes;
4889 * Optional attributes:
4891 * o context - an optional 32 bit integer that will be carried in the
4892 * sending failure notification to the ULP if the transportation of
4893 * this User Message fails.
4895 * o stream id - to indicate which stream to send the data on. If not
4896 * specified, stream 0 will be used.
4898 * o life time - specifies the life time of the user data. The user data
4899 * will not be sent by SCTP after the life time expires. This
4900 * parameter can be used to avoid efforts to transmit stale
4901 * user messages. SCTP notifies the ULP if the data cannot be
4902 * initiated to transport (i.e. sent to the destination via SCTP's
4903 * send primitive) within the life time variable. However, the
4904 * user data will be transmitted if SCTP has attempted to transmit a
4905 * chunk before the life time expired.
4907 * o destination transport address - specified as one of the destination
4908 * transport addresses of the peer endpoint to which this packet
4909 * should be sent. Whenever possible, SCTP should use this destination
4910 * transport address for sending the packets, instead of the current
4913 * o unorder flag - this flag, if present, indicates that the user
4914 * would like the data delivered in an unordered fashion to the peer
4915 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4918 * o no-bundle flag - instructs SCTP not to bundle this user data with
4919 * other outbound DATA chunks. SCTP MAY still bundle even when
4920 * this flag is present, when faced with network congestion.
4922 * o payload protocol-id - A 32 bit unsigned integer that is to be
4923 * passed to the peer indicating the type of payload protocol data
4924 * being transmitted. This value is passed as opaque data by SCTP.
4926 * The return value is the disposition.
4928 enum sctp_disposition sctp_sf_do_prm_send(struct net *net,
4929 const struct sctp_endpoint *ep,
4930 const struct sctp_association *asoc,
4931 const union sctp_subtype type,
4933 struct sctp_cmd_seq *commands)
4935 struct sctp_datamsg *msg = arg;
4937 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4938 return SCTP_DISPOSITION_CONSUME;
4942 * Process the SHUTDOWN primitive.
4947 * Format: SHUTDOWN(association id)
4950 * Gracefully closes an association. Any locally queued user data
4951 * will be delivered to the peer. The association will be terminated only
4952 * after the peer acknowledges all the SCTP packets sent. A success code
4953 * will be returned on successful termination of the association. If
4954 * attempting to terminate the association results in a failure, an error
4955 * code shall be returned.
4957 * Mandatory attributes:
4959 * o association id - local handle to the SCTP association
4961 * Optional attributes:
4965 * The return value is the disposition.
4967 enum sctp_disposition sctp_sf_do_9_2_prm_shutdown(
4969 const struct sctp_endpoint *ep,
4970 const struct sctp_association *asoc,
4971 const union sctp_subtype type,
4973 struct sctp_cmd_seq *commands)
4975 enum sctp_disposition disposition;
4977 /* From 9.2 Shutdown of an Association
4978 * Upon receipt of the SHUTDOWN primitive from its upper
4979 * layer, the endpoint enters SHUTDOWN-PENDING state and
4980 * remains there until all outstanding data has been
4981 * acknowledged by its peer. The endpoint accepts no new data
4982 * from its upper layer, but retransmits data to the far end
4983 * if necessary to fill gaps.
4985 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4986 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4988 disposition = SCTP_DISPOSITION_CONSUME;
4989 if (sctp_outq_is_empty(&asoc->outqueue)) {
4990 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4998 * Process the ABORT primitive.
5003 * Format: Abort(association id [, cause code])
5006 * Ungracefully closes an association. Any locally queued user data
5007 * will be discarded and an ABORT chunk is sent to the peer. A success code
5008 * will be returned on successful abortion of the association. If
5009 * attempting to abort the association results in a failure, an error
5010 * code shall be returned.
5012 * Mandatory attributes:
5014 * o association id - local handle to the SCTP association
5016 * Optional attributes:
5018 * o cause code - reason of the abort to be passed to the peer
5022 * The return value is the disposition.
5024 enum sctp_disposition sctp_sf_do_9_1_prm_abort(
5026 const struct sctp_endpoint *ep,
5027 const struct sctp_association *asoc,
5028 const union sctp_subtype type,
5030 struct sctp_cmd_seq *commands)
5032 /* From 9.1 Abort of an Association
5033 * Upon receipt of the ABORT primitive from its upper
5034 * layer, the endpoint enters CLOSED state and
5035 * discard all outstanding data has been
5036 * acknowledged by its peer. The endpoint accepts no new data
5037 * from its upper layer, but retransmits data to the far end
5038 * if necessary to fill gaps.
5040 struct sctp_chunk *abort = arg;
5043 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5045 /* Even if we can't send the ABORT due to low memory delete the
5046 * TCB. This is a departure from our typical NOMEM handling.
5049 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5050 SCTP_ERROR(ECONNABORTED));
5051 /* Delete the established association. */
5052 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5053 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5055 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5056 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5058 return SCTP_DISPOSITION_ABORT;
5061 /* We tried an illegal operation on an association which is closed. */
5062 enum sctp_disposition sctp_sf_error_closed(struct net *net,
5063 const struct sctp_endpoint *ep,
5064 const struct sctp_association *asoc,
5065 const union sctp_subtype type,
5067 struct sctp_cmd_seq *commands)
5069 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
5070 return SCTP_DISPOSITION_CONSUME;
5073 /* We tried an illegal operation on an association which is shutting
5076 enum sctp_disposition sctp_sf_error_shutdown(
5078 const struct sctp_endpoint *ep,
5079 const struct sctp_association *asoc,
5080 const union sctp_subtype type,
5082 struct sctp_cmd_seq *commands)
5084 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
5085 SCTP_ERROR(-ESHUTDOWN));
5086 return SCTP_DISPOSITION_CONSUME;
5090 * sctp_cookie_wait_prm_shutdown
5092 * Section: 4 Note: 2
5097 * The RFC does not explicitly address this issue, but is the route through the
5098 * state table when someone issues a shutdown while in COOKIE_WAIT state.
5103 enum sctp_disposition sctp_sf_cookie_wait_prm_shutdown(
5105 const struct sctp_endpoint *ep,
5106 const struct sctp_association *asoc,
5107 const union sctp_subtype type,
5109 struct sctp_cmd_seq *commands)
5111 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5112 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5114 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5115 SCTP_STATE(SCTP_STATE_CLOSED));
5117 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
5119 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5121 return SCTP_DISPOSITION_DELETE_TCB;
5125 * sctp_cookie_echoed_prm_shutdown
5127 * Section: 4 Note: 2
5132 * The RFC does not explcitly address this issue, but is the route through the
5133 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5138 enum sctp_disposition sctp_sf_cookie_echoed_prm_shutdown(
5140 const struct sctp_endpoint *ep,
5141 const struct sctp_association *asoc,
5142 const union sctp_subtype type,
5144 struct sctp_cmd_seq *commands)
5146 /* There is a single T1 timer, so we should be able to use
5147 * common function with the COOKIE-WAIT state.
5149 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5153 * sctp_sf_cookie_wait_prm_abort
5155 * Section: 4 Note: 2
5160 * The RFC does not explicitly address this issue, but is the route through the
5161 * state table when someone issues an abort while in COOKIE_WAIT state.
5166 enum sctp_disposition sctp_sf_cookie_wait_prm_abort(
5168 const struct sctp_endpoint *ep,
5169 const struct sctp_association *asoc,
5170 const union sctp_subtype type,
5172 struct sctp_cmd_seq *commands)
5174 struct sctp_chunk *abort = arg;
5176 /* Stop T1-init timer */
5177 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5178 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5181 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5183 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5184 SCTP_STATE(SCTP_STATE_CLOSED));
5186 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5188 /* Even if we can't send the ABORT due to low memory delete the
5189 * TCB. This is a departure from our typical NOMEM handling.
5192 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5193 SCTP_ERROR(ECONNREFUSED));
5194 /* Delete the established association. */
5195 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5196 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5198 return SCTP_DISPOSITION_ABORT;
5202 * sctp_sf_cookie_echoed_prm_abort
5204 * Section: 4 Note: 3
5209 * The RFC does not explcitly address this issue, but is the route through the
5210 * state table when someone issues an abort while in COOKIE_ECHOED state.
5215 enum sctp_disposition sctp_sf_cookie_echoed_prm_abort(
5217 const struct sctp_endpoint *ep,
5218 const struct sctp_association *asoc,
5219 const union sctp_subtype type,
5221 struct sctp_cmd_seq *commands)
5223 /* There is a single T1 timer, so we should be able to use
5224 * common function with the COOKIE-WAIT state.
5226 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5230 * sctp_sf_shutdown_pending_prm_abort
5235 * The RFC does not explicitly address this issue, but is the route through the
5236 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5241 enum sctp_disposition sctp_sf_shutdown_pending_prm_abort(
5243 const struct sctp_endpoint *ep,
5244 const struct sctp_association *asoc,
5245 const union sctp_subtype type,
5247 struct sctp_cmd_seq *commands)
5249 /* Stop the T5-shutdown guard timer. */
5250 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5251 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5253 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5257 * sctp_sf_shutdown_sent_prm_abort
5262 * The RFC does not explicitly address this issue, but is the route through the
5263 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5268 enum sctp_disposition sctp_sf_shutdown_sent_prm_abort(
5270 const struct sctp_endpoint *ep,
5271 const struct sctp_association *asoc,
5272 const union sctp_subtype type,
5274 struct sctp_cmd_seq *commands)
5276 /* Stop the T2-shutdown timer. */
5277 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5278 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5280 /* Stop the T5-shutdown guard timer. */
5281 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5282 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5284 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5288 * sctp_sf_cookie_echoed_prm_abort
5293 * The RFC does not explcitly address this issue, but is the route through the
5294 * state table when someone issues an abort while in COOKIE_ECHOED state.
5299 enum sctp_disposition sctp_sf_shutdown_ack_sent_prm_abort(
5301 const struct sctp_endpoint *ep,
5302 const struct sctp_association *asoc,
5303 const union sctp_subtype type,
5305 struct sctp_cmd_seq *commands)
5307 /* The same T2 timer, so we should be able to use
5308 * common function with the SHUTDOWN-SENT state.
5310 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5314 * Process the REQUESTHEARTBEAT primitive
5317 * J) Request Heartbeat
5319 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5323 * Instructs the local endpoint to perform a HeartBeat on the specified
5324 * destination transport address of the given association. The returned
5325 * result should indicate whether the transmission of the HEARTBEAT
5326 * chunk to the destination address is successful.
5328 * Mandatory attributes:
5330 * o association id - local handle to the SCTP association
5332 * o destination transport address - the transport address of the
5333 * association on which a heartbeat should be issued.
5335 enum sctp_disposition sctp_sf_do_prm_requestheartbeat(
5337 const struct sctp_endpoint *ep,
5338 const struct sctp_association *asoc,
5339 const union sctp_subtype type,
5341 struct sctp_cmd_seq *commands)
5343 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5344 (struct sctp_transport *)arg, commands))
5345 return SCTP_DISPOSITION_NOMEM;
5348 * RFC 2960 (bis), section 8.3
5350 * D) Request an on-demand HEARTBEAT on a specific destination
5351 * transport address of a given association.
5353 * The endpoint should increment the respective error counter of
5354 * the destination transport address each time a HEARTBEAT is sent
5355 * to that address and not acknowledged within one RTO.
5358 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5359 SCTP_TRANSPORT(arg));
5360 return SCTP_DISPOSITION_CONSUME;
5364 * ADDIP Section 4.1 ASCONF Chunk Procedures
5365 * When an endpoint has an ASCONF signaled change to be sent to the
5366 * remote endpoint it should do A1 to A9
5368 enum sctp_disposition sctp_sf_do_prm_asconf(struct net *net,
5369 const struct sctp_endpoint *ep,
5370 const struct sctp_association *asoc,
5371 const union sctp_subtype type,
5373 struct sctp_cmd_seq *commands)
5375 struct sctp_chunk *chunk = arg;
5377 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5378 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5379 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5380 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5381 return SCTP_DISPOSITION_CONSUME;
5384 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5385 enum sctp_disposition sctp_sf_do_prm_reconf(struct net *net,
5386 const struct sctp_endpoint *ep,
5387 const struct sctp_association *asoc,
5388 const union sctp_subtype type,
5390 struct sctp_cmd_seq *commands)
5392 struct sctp_chunk *chunk = arg;
5394 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5395 return SCTP_DISPOSITION_CONSUME;
5399 * Ignore the primitive event
5401 * The return value is the disposition of the primitive.
5403 enum sctp_disposition sctp_sf_ignore_primitive(
5405 const struct sctp_endpoint *ep,
5406 const struct sctp_association *asoc,
5407 const union sctp_subtype type,
5409 struct sctp_cmd_seq *commands)
5411 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5414 return SCTP_DISPOSITION_DISCARD;
5417 /***************************************************************************
5418 * These are the state functions for the OTHER events.
5419 ***************************************************************************/
5422 * When the SCTP stack has no more user data to send or retransmit, this
5423 * notification is given to the user. Also, at the time when a user app
5424 * subscribes to this event, if there is no data to be sent or
5425 * retransmit, the stack will immediately send up this notification.
5427 enum sctp_disposition sctp_sf_do_no_pending_tsn(
5429 const struct sctp_endpoint *ep,
5430 const struct sctp_association *asoc,
5431 const union sctp_subtype type,
5433 struct sctp_cmd_seq *commands)
5435 struct sctp_ulpevent *event;
5437 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5439 return SCTP_DISPOSITION_NOMEM;
5441 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5443 return SCTP_DISPOSITION_CONSUME;
5447 * Start the shutdown negotiation.
5450 * Once all its outstanding data has been acknowledged, the endpoint
5451 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5452 * TSN Ack field the last sequential TSN it has received from the peer.
5453 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5454 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5455 * with the updated last sequential TSN received from its peer.
5457 * The return value is the disposition.
5459 enum sctp_disposition sctp_sf_do_9_2_start_shutdown(
5461 const struct sctp_endpoint *ep,
5462 const struct sctp_association *asoc,
5463 const union sctp_subtype type,
5465 struct sctp_cmd_seq *commands)
5467 struct sctp_chunk *reply;
5469 /* Once all its outstanding data has been acknowledged, the
5470 * endpoint shall send a SHUTDOWN chunk to its peer including
5471 * in the Cumulative TSN Ack field the last sequential TSN it
5472 * has received from the peer.
5474 reply = sctp_make_shutdown(asoc, arg);
5478 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5479 * T2-shutdown timer.
5481 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5483 /* It shall then start the T2-shutdown timer */
5484 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5485 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5487 /* RFC 4960 Section 9.2
5488 * The sender of the SHUTDOWN MAY also start an overall guard timer
5489 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5491 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5492 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5494 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5495 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5496 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5498 /* and enter the SHUTDOWN-SENT state. */
5499 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5500 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5502 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5504 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5507 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5509 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5511 return SCTP_DISPOSITION_CONSUME;
5514 return SCTP_DISPOSITION_NOMEM;
5518 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5522 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5523 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5524 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5525 * endpoint must re-send the SHUTDOWN ACK.
5527 * The return value is the disposition.
5529 enum sctp_disposition sctp_sf_do_9_2_shutdown_ack(
5531 const struct sctp_endpoint *ep,
5532 const struct sctp_association *asoc,
5533 const union sctp_subtype type,
5535 struct sctp_cmd_seq *commands)
5537 struct sctp_chunk *chunk = arg;
5538 struct sctp_chunk *reply;
5540 /* There are 2 ways of getting here:
5541 * 1) called in response to a SHUTDOWN chunk
5542 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5544 * For the case (2), the arg parameter is set to NULL. We need
5545 * to check that we have a chunk before accessing it's fields.
5548 if (!sctp_vtag_verify(chunk, asoc))
5549 return sctp_sf_pdiscard(net, ep, asoc, type, arg,
5552 /* Make sure that the SHUTDOWN chunk has a valid length. */
5553 if (!sctp_chunk_length_valid(
5554 chunk, sizeof(struct sctp_shutdown_chunk)))
5555 return sctp_sf_violation_chunklen(net, ep, asoc, type,
5559 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5560 * shall send a SHUTDOWN ACK ...
5562 reply = sctp_make_shutdown_ack(asoc, chunk);
5566 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5567 * the T2-shutdown timer.
5569 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5571 /* and start/restart a T2-shutdown timer of its own, */
5572 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5573 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5575 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5576 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5577 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5579 /* Enter the SHUTDOWN-ACK-SENT state. */
5580 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5581 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5583 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5585 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5588 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5590 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5592 return SCTP_DISPOSITION_CONSUME;
5595 return SCTP_DISPOSITION_NOMEM;
5599 * Ignore the event defined as other
5601 * The return value is the disposition of the event.
5603 enum sctp_disposition sctp_sf_ignore_other(struct net *net,
5604 const struct sctp_endpoint *ep,
5605 const struct sctp_association *asoc,
5606 const union sctp_subtype type,
5608 struct sctp_cmd_seq *commands)
5610 pr_debug("%s: the event other type:%d is ignored\n",
5611 __func__, type.other);
5613 return SCTP_DISPOSITION_DISCARD;
5616 /************************************************************
5617 * These are the state functions for handling timeout events.
5618 ************************************************************/
5623 * Section: 6.3.3 Handle T3-rtx Expiration
5625 * Whenever the retransmission timer T3-rtx expires for a destination
5626 * address, do the following:
5629 * The return value is the disposition of the chunk.
5631 enum sctp_disposition sctp_sf_do_6_3_3_rtx(struct net *net,
5632 const struct sctp_endpoint *ep,
5633 const struct sctp_association *asoc,
5634 const union sctp_subtype type,
5636 struct sctp_cmd_seq *commands)
5638 struct sctp_transport *transport = arg;
5640 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5642 if (asoc->overall_error_count >= asoc->max_retrans) {
5643 if (asoc->peer.zero_window_announced &&
5644 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5646 * We are here likely because the receiver had its rwnd
5647 * closed for a while and we have not been able to
5648 * transmit the locally queued data within the maximum
5649 * retransmission attempts limit. Start the T5
5650 * shutdown guard timer to give the receiver one last
5651 * chance and some additional time to recover before
5654 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5655 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5657 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5658 SCTP_ERROR(ETIMEDOUT));
5659 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5660 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5661 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5662 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5663 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5664 return SCTP_DISPOSITION_DELETE_TCB;
5668 /* E1) For the destination address for which the timer
5669 * expires, adjust its ssthresh with rules defined in Section
5670 * 7.2.3 and set the cwnd <- MTU.
5673 /* E2) For the destination address for which the timer
5674 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5675 * maximum value discussed in rule C7 above (RTO.max) may be
5676 * used to provide an upper bound to this doubling operation.
5679 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5680 * outstanding DATA chunks for the address for which the
5681 * T3-rtx has expired will fit into a single packet, subject
5682 * to the MTU constraint for the path corresponding to the
5683 * destination transport address to which the retransmission
5684 * is being sent (this may be different from the address for
5685 * which the timer expires [see Section 6.4]). Call this
5686 * value K. Bundle and retransmit those K DATA chunks in a
5687 * single packet to the destination endpoint.
5689 * Note: Any DATA chunks that were sent to the address for
5690 * which the T3-rtx timer expired but did not fit in one MTU
5691 * (rule E3 above), should be marked for retransmission and
5692 * sent as soon as cwnd allows (normally when a SACK arrives).
5695 /* Do some failure management (Section 8.2). */
5696 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5698 /* NB: Rules E4 and F1 are implicit in R1. */
5699 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5701 return SCTP_DISPOSITION_CONSUME;
5705 * Generate delayed SACK on timeout
5707 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5709 * The guidelines on delayed acknowledgement algorithm specified in
5710 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5711 * acknowledgement SHOULD be generated for at least every second packet
5712 * (not every second DATA chunk) received, and SHOULD be generated
5713 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5714 * some situations it may be beneficial for an SCTP transmitter to be
5715 * more conservative than the algorithms detailed in this document
5716 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5717 * the following algorithms allow.
5719 enum sctp_disposition sctp_sf_do_6_2_sack(struct net *net,
5720 const struct sctp_endpoint *ep,
5721 const struct sctp_association *asoc,
5722 const union sctp_subtype type,
5724 struct sctp_cmd_seq *commands)
5726 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5727 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5728 return SCTP_DISPOSITION_CONSUME;
5732 * sctp_sf_t1_init_timer_expire
5734 * Section: 4 Note: 2
5739 * RFC 2960 Section 4 Notes
5740 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5741 * and re-start the T1-init timer without changing state. This MUST
5742 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5743 * endpoint MUST abort the initialization process and report the
5744 * error to SCTP user.
5750 enum sctp_disposition sctp_sf_t1_init_timer_expire(
5752 const struct sctp_endpoint *ep,
5753 const struct sctp_association *asoc,
5754 const union sctp_subtype type,
5756 struct sctp_cmd_seq *commands)
5758 int attempts = asoc->init_err_counter + 1;
5759 struct sctp_chunk *repl = NULL;
5760 struct sctp_bind_addr *bp;
5762 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5764 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5766 if (attempts <= asoc->max_init_attempts) {
5767 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5768 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5770 return SCTP_DISPOSITION_NOMEM;
5772 /* Choose transport for INIT. */
5773 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5776 /* Issue a sideeffect to do the needed accounting. */
5777 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5778 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5780 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5782 pr_debug("%s: giving up on INIT, attempts:%d "
5783 "max_init_attempts:%d\n", __func__, attempts,
5784 asoc->max_init_attempts);
5786 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5787 SCTP_ERROR(ETIMEDOUT));
5788 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5789 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5790 return SCTP_DISPOSITION_DELETE_TCB;
5793 return SCTP_DISPOSITION_CONSUME;
5797 * sctp_sf_t1_cookie_timer_expire
5799 * Section: 4 Note: 2
5804 * RFC 2960 Section 4 Notes
5805 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5806 * COOKIE ECHO and re-start the T1-cookie timer without changing
5807 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5808 * After that, the endpoint MUST abort the initialization process and
5809 * report the error to SCTP user.
5815 enum sctp_disposition sctp_sf_t1_cookie_timer_expire(
5817 const struct sctp_endpoint *ep,
5818 const struct sctp_association *asoc,
5819 const union sctp_subtype type,
5821 struct sctp_cmd_seq *commands)
5823 int attempts = asoc->init_err_counter + 1;
5824 struct sctp_chunk *repl = NULL;
5826 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5828 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5830 if (attempts <= asoc->max_init_attempts) {
5831 repl = sctp_make_cookie_echo(asoc, NULL);
5833 return SCTP_DISPOSITION_NOMEM;
5835 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5837 /* Issue a sideeffect to do the needed accounting. */
5838 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5839 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5841 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5843 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5844 SCTP_ERROR(ETIMEDOUT));
5845 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5846 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5847 return SCTP_DISPOSITION_DELETE_TCB;
5850 return SCTP_DISPOSITION_CONSUME;
5853 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5854 * with the updated last sequential TSN received from its peer.
5856 * An endpoint should limit the number of retransmissions of the
5857 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5858 * If this threshold is exceeded the endpoint should destroy the TCB and
5859 * MUST report the peer endpoint unreachable to the upper layer (and
5860 * thus the association enters the CLOSED state). The reception of any
5861 * packet from its peer (i.e. as the peer sends all of its queued DATA
5862 * chunks) should clear the endpoint's retransmission count and restart
5863 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5864 * all of its queued DATA chunks that have not yet been sent.
5866 enum sctp_disposition sctp_sf_t2_timer_expire(
5868 const struct sctp_endpoint *ep,
5869 const struct sctp_association *asoc,
5870 const union sctp_subtype type,
5872 struct sctp_cmd_seq *commands)
5874 struct sctp_chunk *reply = NULL;
5876 pr_debug("%s: timer T2 expired\n", __func__);
5878 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5880 ((struct sctp_association *)asoc)->shutdown_retries++;
5882 if (asoc->overall_error_count >= asoc->max_retrans) {
5883 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5884 SCTP_ERROR(ETIMEDOUT));
5885 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5886 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5887 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5888 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5889 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5890 return SCTP_DISPOSITION_DELETE_TCB;
5893 switch (asoc->state) {
5894 case SCTP_STATE_SHUTDOWN_SENT:
5895 reply = sctp_make_shutdown(asoc, NULL);
5898 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5899 reply = sctp_make_shutdown_ack(asoc, NULL);
5910 /* Do some failure management (Section 8.2).
5911 * If we remove the transport an SHUTDOWN was last sent to, don't
5912 * do failure management.
5914 if (asoc->shutdown_last_sent_to)
5915 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5916 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5918 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5919 * the T2-shutdown timer.
5921 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5923 /* Restart the T2-shutdown timer. */
5924 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5925 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5926 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5927 return SCTP_DISPOSITION_CONSUME;
5930 return SCTP_DISPOSITION_NOMEM;
5934 * ADDIP Section 4.1 ASCONF CHunk Procedures
5935 * If the T4 RTO timer expires the endpoint should do B1 to B5
5937 enum sctp_disposition sctp_sf_t4_timer_expire(
5939 const struct sctp_endpoint *ep,
5940 const struct sctp_association *asoc,
5941 const union sctp_subtype type,
5943 struct sctp_cmd_seq *commands)
5945 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5946 struct sctp_transport *transport = chunk->transport;
5948 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5950 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5951 * detection on the appropriate destination address as defined in
5952 * RFC2960 [5] section 8.1 and 8.2.
5955 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5956 SCTP_TRANSPORT(transport));
5958 /* Reconfig T4 timer and transport. */
5959 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5961 /* ADDIP 4.1 B2) Increment the association error counters and perform
5962 * endpoint failure detection on the association as defined in
5963 * RFC2960 [5] section 8.1 and 8.2.
5964 * association error counter is incremented in SCTP_CMD_STRIKE.
5966 if (asoc->overall_error_count >= asoc->max_retrans) {
5967 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5968 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5969 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5970 SCTP_ERROR(ETIMEDOUT));
5971 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5972 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5973 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5974 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5975 return SCTP_DISPOSITION_ABORT;
5978 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5979 * the ASCONF chunk was sent by doubling the RTO timer value.
5980 * This is done in SCTP_CMD_STRIKE.
5983 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5984 * choose an alternate destination address (please refer to RFC2960
5985 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5986 * chunk, it MUST be the same (including its serial number) as the last
5989 sctp_chunk_hold(asoc->addip_last_asconf);
5990 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5991 SCTP_CHUNK(asoc->addip_last_asconf));
5993 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5994 * destination is selected, then the RTO used will be that of the new
5995 * destination address.
5997 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5998 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
6000 return SCTP_DISPOSITION_CONSUME;
6003 /* sctpimpguide-05 Section 2.12.2
6004 * The sender of the SHUTDOWN MAY also start an overall guard timer
6005 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
6006 * At the expiration of this timer the sender SHOULD abort the association
6007 * by sending an ABORT chunk.
6009 enum sctp_disposition sctp_sf_t5_timer_expire(
6011 const struct sctp_endpoint *ep,
6012 const struct sctp_association *asoc,
6013 const union sctp_subtype type,
6015 struct sctp_cmd_seq *commands)
6017 struct sctp_chunk *reply = NULL;
6019 pr_debug("%s: timer T5 expired\n", __func__);
6021 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
6023 reply = sctp_make_abort(asoc, NULL, 0);
6027 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
6028 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6029 SCTP_ERROR(ETIMEDOUT));
6030 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6031 SCTP_PERR(SCTP_ERROR_NO_ERROR));
6033 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6034 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6036 return SCTP_DISPOSITION_DELETE_TCB;
6038 return SCTP_DISPOSITION_NOMEM;
6041 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
6042 * the association is automatically closed by starting the shutdown process.
6043 * The work that needs to be done is same as when SHUTDOWN is initiated by
6044 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
6046 enum sctp_disposition sctp_sf_autoclose_timer_expire(
6048 const struct sctp_endpoint *ep,
6049 const struct sctp_association *asoc,
6050 const union sctp_subtype type,
6052 struct sctp_cmd_seq *commands)
6054 enum sctp_disposition disposition;
6056 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
6058 /* From 9.2 Shutdown of an Association
6059 * Upon receipt of the SHUTDOWN primitive from its upper
6060 * layer, the endpoint enters SHUTDOWN-PENDING state and
6061 * remains there until all outstanding data has been
6062 * acknowledged by its peer. The endpoint accepts no new data
6063 * from its upper layer, but retransmits data to the far end
6064 * if necessary to fill gaps.
6066 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
6067 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
6069 disposition = SCTP_DISPOSITION_CONSUME;
6070 if (sctp_outq_is_empty(&asoc->outqueue)) {
6071 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
6078 /*****************************************************************************
6079 * These are sa state functions which could apply to all types of events.
6080 ****************************************************************************/
6083 * This table entry is not implemented.
6086 * (endpoint, asoc, chunk)
6088 * The return value is the disposition of the chunk.
6090 enum sctp_disposition sctp_sf_not_impl(struct net *net,
6091 const struct sctp_endpoint *ep,
6092 const struct sctp_association *asoc,
6093 const union sctp_subtype type,
6094 void *arg, struct sctp_cmd_seq *commands)
6096 return SCTP_DISPOSITION_NOT_IMPL;
6100 * This table entry represents a bug.
6103 * (endpoint, asoc, chunk)
6105 * The return value is the disposition of the chunk.
6107 enum sctp_disposition sctp_sf_bug(struct net *net,
6108 const struct sctp_endpoint *ep,
6109 const struct sctp_association *asoc,
6110 const union sctp_subtype type,
6111 void *arg, struct sctp_cmd_seq *commands)
6113 return SCTP_DISPOSITION_BUG;
6117 * This table entry represents the firing of a timer in the wrong state.
6118 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
6119 * when the association is in the wrong state. This event should
6120 * be ignored, so as to prevent any rearming of the timer.
6123 * (endpoint, asoc, chunk)
6125 * The return value is the disposition of the chunk.
6127 enum sctp_disposition sctp_sf_timer_ignore(struct net *net,
6128 const struct sctp_endpoint *ep,
6129 const struct sctp_association *asoc,
6130 const union sctp_subtype type,
6132 struct sctp_cmd_seq *commands)
6134 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6136 return SCTP_DISPOSITION_CONSUME;
6139 /********************************************************************
6140 * 2nd Level Abstractions
6141 ********************************************************************/
6143 /* Pull the SACK chunk based on the SACK header. */
6144 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6146 struct sctp_sackhdr *sack;
6151 /* Protect ourselves from reading too far into
6152 * the skb from a bogus sender.
6154 sack = (struct sctp_sackhdr *) chunk->skb->data;
6156 num_blocks = ntohs(sack->num_gap_ack_blocks);
6157 num_dup_tsns = ntohs(sack->num_dup_tsns);
6158 len = sizeof(struct sctp_sackhdr);
6159 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6160 if (len > chunk->skb->len)
6163 skb_pull(chunk->skb, len);
6168 /* Create an ABORT packet to be sent as a response, with the specified
6171 static struct sctp_packet *sctp_abort_pkt_new(
6173 const struct sctp_endpoint *ep,
6174 const struct sctp_association *asoc,
6175 struct sctp_chunk *chunk,
6176 const void *payload, size_t paylen)
6178 struct sctp_packet *packet;
6179 struct sctp_chunk *abort;
6181 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6185 * The T bit will be set if the asoc is NULL.
6187 abort = sctp_make_abort(asoc, chunk, paylen);
6189 sctp_ootb_pkt_free(packet);
6193 /* Reflect vtag if T-Bit is set */
6194 if (sctp_test_T_bit(abort))
6195 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6197 /* Add specified error causes, i.e., payload, to the
6200 sctp_addto_chunk(abort, paylen, payload);
6202 /* Set the skb to the belonging sock for accounting. */
6203 abort->skb->sk = ep->base.sk;
6205 sctp_packet_append_chunk(packet, abort);
6212 /* Allocate a packet for responding in the OOTB conditions. */
6213 static struct sctp_packet *sctp_ootb_pkt_new(
6215 const struct sctp_association *asoc,
6216 const struct sctp_chunk *chunk)
6218 struct sctp_transport *transport;
6219 struct sctp_packet *packet;
6223 /* Get the source and destination port from the inbound packet. */
6224 sport = ntohs(chunk->sctp_hdr->dest);
6225 dport = ntohs(chunk->sctp_hdr->source);
6227 /* The V-tag is going to be the same as the inbound packet if no
6228 * association exists, otherwise, use the peer's vtag.
6231 /* Special case the INIT-ACK as there is no peer's vtag
6234 switch (chunk->chunk_hdr->type) {
6235 case SCTP_CID_INIT_ACK:
6237 struct sctp_initack_chunk *initack;
6239 initack = (struct sctp_initack_chunk *)chunk->chunk_hdr;
6240 vtag = ntohl(initack->init_hdr.init_tag);
6244 vtag = asoc->peer.i.init_tag;
6248 /* Special case the INIT and stale COOKIE_ECHO as there is no
6251 switch (chunk->chunk_hdr->type) {
6254 struct sctp_init_chunk *init;
6256 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6257 vtag = ntohl(init->init_hdr.init_tag);
6261 vtag = ntohl(chunk->sctp_hdr->vtag);
6266 /* Make a transport for the bucket, Eliza... */
6267 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6271 /* Cache a route for the transport with the chunk's destination as
6272 * the source address.
6274 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6275 sctp_sk(net->sctp.ctl_sock));
6277 packet = &transport->packet;
6278 sctp_packet_init(packet, transport, sport, dport);
6279 sctp_packet_config(packet, vtag, 0);
6287 /* Free the packet allocated earlier for responding in the OOTB condition. */
6288 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6290 sctp_transport_free(packet->transport);
6293 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6294 static void sctp_send_stale_cookie_err(struct net *net,
6295 const struct sctp_endpoint *ep,
6296 const struct sctp_association *asoc,
6297 const struct sctp_chunk *chunk,
6298 struct sctp_cmd_seq *commands,
6299 struct sctp_chunk *err_chunk)
6301 struct sctp_packet *packet;
6304 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6306 struct sctp_signed_cookie *cookie;
6308 /* Override the OOTB vtag from the cookie. */
6309 cookie = chunk->subh.cookie_hdr;
6310 packet->vtag = cookie->c.peer_vtag;
6312 /* Set the skb to the belonging sock for accounting. */
6313 err_chunk->skb->sk = ep->base.sk;
6314 sctp_packet_append_chunk(packet, err_chunk);
6315 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6316 SCTP_PACKET(packet));
6317 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6319 sctp_chunk_free (err_chunk);
6324 /* Process a data chunk */
6325 static int sctp_eat_data(const struct sctp_association *asoc,
6326 struct sctp_chunk *chunk,
6327 struct sctp_cmd_seq *commands)
6329 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6330 struct sock *sk = asoc->base.sk;
6331 struct net *net = sock_net(sk);
6332 struct sctp_datahdr *data_hdr;
6333 struct sctp_chunk *err;
6334 enum sctp_verb deliver;
6339 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6340 chunk->subh.data_hdr = data_hdr;
6341 skb_pull(chunk->skb, sctp_datahdr_len(&asoc->stream));
6343 tsn = ntohl(data_hdr->tsn);
6344 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6346 /* ASSERT: Now skb->data is really the user data. */
6348 /* Process ECN based congestion.
6350 * Since the chunk structure is reused for all chunks within
6351 * a packet, we use ecn_ce_done to track if we've already
6352 * done CE processing for this packet.
6354 * We need to do ECN processing even if we plan to discard the
6358 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6359 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6360 chunk->ecn_ce_done = 1;
6362 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6363 /* Do real work as sideffect. */
6364 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6369 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6371 /* The TSN is too high--silently discard the chunk and
6372 * count on it getting retransmitted later.
6375 chunk->asoc->stats.outofseqtsns++;
6376 return SCTP_IERROR_HIGH_TSN;
6377 } else if (tmp > 0) {
6378 /* This is a duplicate. Record it. */
6379 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6380 return SCTP_IERROR_DUP_TSN;
6383 /* This is a new TSN. */
6385 /* Discard if there is no room in the receive window.
6386 * Actually, allow a little bit of overflow (up to a MTU).
6388 datalen = ntohs(chunk->chunk_hdr->length);
6389 datalen -= sctp_datachk_len(&asoc->stream);
6391 deliver = SCTP_CMD_CHUNK_ULP;
6393 /* Think about partial delivery. */
6394 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6396 /* Even if we don't accept this chunk there is
6399 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6402 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6403 * seems a bit troublesome in that frag_point varies based on
6404 * PMTU. In cases, such as loopback, this might be a rather
6407 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6408 (datalen > asoc->rwnd + asoc->frag_point))) {
6410 /* If this is the next TSN, consider reneging to make
6411 * room. Note: Playing nice with a confused sender. A
6412 * malicious sender can still eat up all our buffer
6413 * space and in the future we may want to detect and
6414 * do more drastic reneging.
6416 if (sctp_tsnmap_has_gap(map) &&
6417 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6418 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6419 deliver = SCTP_CMD_RENEGE;
6421 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6422 __func__, tsn, datalen, asoc->rwnd);
6424 return SCTP_IERROR_IGNORE_TSN;
6429 * Also try to renege to limit our memory usage in the event that
6430 * we are under memory pressure
6431 * If we can't renege, don't worry about it, the sk_rmem_schedule
6432 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6433 * memory usage too much
6435 if (sk_under_memory_pressure(sk)) {
6436 if (sctp_tsnmap_has_gap(map) &&
6437 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6438 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6440 deliver = SCTP_CMD_RENEGE;
6447 * Section 3.3.10.9 No User Data (9)
6451 * No User Data: This error cause is returned to the originator of a
6452 * DATA chunk if a received DATA chunk has no user data.
6454 if (unlikely(0 == datalen)) {
6455 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6457 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6460 /* We are going to ABORT, so we might as well stop
6461 * processing the rest of the chunks in the packet.
6463 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6464 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6465 SCTP_ERROR(ECONNABORTED));
6466 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6467 SCTP_PERR(SCTP_ERROR_NO_DATA));
6468 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6469 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6470 return SCTP_IERROR_NO_DATA;
6473 chunk->data_accepted = 1;
6475 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6476 * if we renege and the chunk arrives again.
6478 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6479 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6481 chunk->asoc->stats.iuodchunks++;
6483 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6485 chunk->asoc->stats.iodchunks++;
6488 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6490 * If an endpoint receive a DATA chunk with an invalid stream
6491 * identifier, it shall acknowledge the reception of the DATA chunk
6492 * following the normal procedure, immediately send an ERROR chunk
6493 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6494 * and discard the DATA chunk.
6496 if (ntohs(data_hdr->stream) >= asoc->stream.incnt) {
6497 /* Mark tsn as received even though we drop it */
6498 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6500 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6502 sizeof(data_hdr->stream),
6505 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6507 return SCTP_IERROR_BAD_STREAM;
6510 /* Check to see if the SSN is possible for this TSN.
6511 * The biggest gap we can record is 4K wide. Since SSNs wrap
6512 * at an unsigned short, there is no way that an SSN can
6513 * wrap and for a valid TSN. We can simply check if the current
6514 * SSN is smaller then the next expected one. If it is, it wrapped
6517 if (!asoc->stream.si->validate_data(chunk))
6518 return SCTP_IERROR_PROTO_VIOLATION;
6520 /* Send the data up to the user. Note: Schedule the
6521 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6522 * chunk needs the updated rwnd.
6524 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6526 return SCTP_IERROR_NO_ERROR;