1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
8 * This is part of the SCTP Linux Kernel Implementation.
10 * These are the state functions for the state machine.
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <linux-sctp@vger.kernel.org>
32 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <linux/types.h>
49 #include <linux/kernel.h>
51 #include <linux/ipv6.h>
52 #include <linux/net.h>
53 #include <linux/inet.h>
54 #include <linux/slab.h>
56 #include <net/inet_ecn.h>
57 #include <linux/skbuff.h>
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
60 #include <net/sctp/structs.h>
62 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
63 const struct sctp_endpoint *ep,
64 const struct sctp_association *asoc,
65 struct sctp_chunk *chunk,
68 static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 sctp_cmd_seq_t *commands);
71 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
72 const struct sctp_association *asoc,
73 const struct sctp_chunk *chunk);
74 static void sctp_send_stale_cookie_err(struct net *net,
75 const struct sctp_endpoint *ep,
76 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk,
78 sctp_cmd_seq_t *commands,
79 struct sctp_chunk *err_chunk);
80 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
81 const struct sctp_endpoint *ep,
82 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
85 sctp_cmd_seq_t *commands);
86 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
87 const struct sctp_endpoint *ep,
88 const struct sctp_association *asoc,
89 const sctp_subtype_t type,
91 sctp_cmd_seq_t *commands);
92 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
93 const struct sctp_endpoint *ep,
94 const struct sctp_association *asoc,
95 const sctp_subtype_t type,
97 sctp_cmd_seq_t *commands);
98 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
100 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
101 sctp_cmd_seq_t *commands,
102 __be16 error, int sk_err,
103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
106 static sctp_disposition_t sctp_sf_abort_violation(
108 const struct sctp_endpoint *ep,
109 const struct sctp_association *asoc,
111 sctp_cmd_seq_t *commands,
113 const size_t paylen);
115 static sctp_disposition_t sctp_sf_violation_chunklen(
117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const sctp_subtype_t type,
121 sctp_cmd_seq_t *commands);
123 static sctp_disposition_t sctp_sf_violation_paramlen(
125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
128 void *arg, void *ext,
129 sctp_cmd_seq_t *commands);
131 static sctp_disposition_t sctp_sf_violation_ctsn(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
137 sctp_cmd_seq_t *commands);
139 static sctp_disposition_t sctp_sf_violation_chunk(
141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const sctp_subtype_t type,
145 sctp_cmd_seq_t *commands);
147 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
148 const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 struct sctp_chunk *chunk);
153 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
154 const struct sctp_endpoint *ep,
155 const struct sctp_association *asoc,
156 const sctp_subtype_t type,
158 sctp_cmd_seq_t *commands);
160 /* Small helper function that checks if the chunk length
161 * is of the appropriate length. The 'required_length' argument
162 * is set to be the size of a specific chunk we are testing.
163 * Return Values: 1 = Valid length
168 sctp_chunk_length_valid(struct sctp_chunk *chunk,
169 __u16 required_length)
171 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
173 if (unlikely(chunk_length < required_length))
179 /**********************************************************
180 * These are the state functions for handling chunk events.
181 **********************************************************/
184 * Process the final SHUTDOWN COMPLETE.
186 * Section: 4 (C) (diagram), 9.2
187 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
188 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
189 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
190 * should stop the T2-shutdown timer and remove all knowledge of the
191 * association (and thus the association enters the CLOSED state).
193 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
194 * C) Rules for packet carrying SHUTDOWN COMPLETE:
196 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
197 * if the Verification Tag field of the packet matches its own tag and
198 * the T bit is not set
200 * it is set to its peer's tag and the T bit is set in the Chunk
202 * Otherwise, the receiver MUST silently discard the packet
203 * and take no further action. An endpoint MUST ignore the
204 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
207 * (endpoint, asoc, chunk)
210 * (asoc, reply_msg, msg_up, timers, counters)
212 * The return value is the disposition of the chunk.
214 sctp_disposition_t sctp_sf_do_4_C(struct net *net,
215 const struct sctp_endpoint *ep,
216 const struct sctp_association *asoc,
217 const sctp_subtype_t type,
219 sctp_cmd_seq_t *commands)
221 struct sctp_chunk *chunk = arg;
222 struct sctp_ulpevent *ev;
224 if (!sctp_vtag_verify_either(chunk, asoc))
225 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
227 /* RFC 2960 6.10 Bundling
229 * An endpoint MUST NOT bundle INIT, INIT ACK or
230 * SHUTDOWN COMPLETE with any other chunks.
232 if (!chunk->singleton)
233 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
235 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
236 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
237 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
240 /* RFC 2960 10.2 SCTP-to-ULP
242 * H) SHUTDOWN COMPLETE notification
244 * When SCTP completes the shutdown procedures (section 9.2) this
245 * notification is passed to the upper layer.
247 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
248 0, 0, 0, NULL, GFP_ATOMIC);
250 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
253 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
254 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
255 * not the chunk should be discarded. If the endpoint is in
256 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
257 * T2-shutdown timer and remove all knowledge of the
258 * association (and thus the association enters the CLOSED
261 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
262 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
264 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
265 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
267 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
268 SCTP_STATE(SCTP_STATE_CLOSED));
270 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
271 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
273 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
275 return SCTP_DISPOSITION_DELETE_TCB;
279 * Respond to a normal INIT chunk.
280 * We are the side that is being asked for an association.
282 * Section: 5.1 Normal Establishment of an Association, B
283 * B) "Z" shall respond immediately with an INIT ACK chunk. The
284 * destination IP address of the INIT ACK MUST be set to the source
285 * IP address of the INIT to which this INIT ACK is responding. In
286 * the response, besides filling in other parameters, "Z" must set the
287 * Verification Tag field to Tag_A, and also provide its own
288 * Verification Tag (Tag_Z) in the Initiate Tag field.
290 * Verification Tag: Must be 0.
293 * (endpoint, asoc, chunk)
296 * (asoc, reply_msg, msg_up, timers, counters)
298 * The return value is the disposition of the chunk.
300 sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
301 const struct sctp_endpoint *ep,
302 const struct sctp_association *asoc,
303 const sctp_subtype_t type,
305 sctp_cmd_seq_t *commands)
307 struct sctp_chunk *chunk = arg;
308 struct sctp_chunk *repl;
309 struct sctp_association *new_asoc;
310 struct sctp_chunk *err_chunk;
311 struct sctp_packet *packet;
312 sctp_unrecognized_param_t *unk_param;
316 * An endpoint MUST NOT bundle INIT, INIT ACK or
317 * SHUTDOWN COMPLETE with any other chunks.
320 * Furthermore, we require that the receiver of an INIT chunk MUST
321 * enforce these rules by silently discarding an arriving packet
322 * with an INIT chunk that is bundled with other chunks.
324 if (!chunk->singleton)
325 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
327 /* If the packet is an OOTB packet which is temporarily on the
328 * control endpoint, respond with an ABORT.
330 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
331 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
332 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
335 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
338 if (chunk->sctp_hdr->vtag != 0)
339 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
341 /* Make sure that the INIT chunk has a valid length.
342 * Normally, this would cause an ABORT with a Protocol Violation
343 * error, but since we don't have an association, we'll
344 * just discard the packet.
346 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
347 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
349 /* If the INIT is coming toward a closing socket, we'll send back
350 * and ABORT. Essentially, this catches the race of INIT being
351 * backloged to the socket at the same time as the user isses close().
352 * Since the socket and all its associations are going away, we
353 * can treat this OOTB
355 if (sctp_sstate(ep->base.sk, CLOSING))
356 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
358 /* Verify the INIT chunk before processing it. */
360 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
361 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
363 /* This chunk contains fatal error. It is to be discarded.
364 * Send an ABORT, with causes if there is any.
367 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
368 (__u8 *)(err_chunk->chunk_hdr) +
369 sizeof(sctp_chunkhdr_t),
370 ntohs(err_chunk->chunk_hdr->length) -
371 sizeof(sctp_chunkhdr_t));
373 sctp_chunk_free(err_chunk);
376 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
377 SCTP_PACKET(packet));
378 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
379 return SCTP_DISPOSITION_CONSUME;
381 return SCTP_DISPOSITION_NOMEM;
384 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
389 /* Grab the INIT header. */
390 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
392 /* Tag the variable length parameters. */
393 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
395 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
399 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
400 sctp_scope(sctp_source(chunk)),
404 /* The call, sctp_process_init(), can fail on memory allocation. */
405 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
406 (sctp_init_chunk_t *)chunk->chunk_hdr,
410 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
412 /* If there are errors need to be reported for unknown parameters,
413 * make sure to reserve enough room in the INIT ACK for them.
417 len = ntohs(err_chunk->chunk_hdr->length) -
418 sizeof(sctp_chunkhdr_t);
420 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
424 /* If there are errors need to be reported for unknown parameters,
425 * include them in the outgoing INIT ACK as "Unrecognized parameter"
429 /* Get the "Unrecognized parameter" parameter(s) out of the
430 * ERROR chunk generated by sctp_verify_init(). Since the
431 * error cause code for "unknown parameter" and the
432 * "Unrecognized parameter" type is the same, we can
433 * construct the parameters in INIT ACK by copying the
436 unk_param = (sctp_unrecognized_param_t *)
437 ((__u8 *)(err_chunk->chunk_hdr) +
438 sizeof(sctp_chunkhdr_t));
439 /* Replace the cause code with the "Unrecognized parameter"
442 sctp_addto_chunk(repl, len, unk_param);
443 sctp_chunk_free(err_chunk);
446 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
448 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
451 * Note: After sending out INIT ACK with the State Cookie parameter,
452 * "Z" MUST NOT allocate any resources, nor keep any states for the
453 * new association. Otherwise, "Z" will be vulnerable to resource
456 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
458 return SCTP_DISPOSITION_DELETE_TCB;
461 sctp_association_free(new_asoc);
464 sctp_chunk_free(err_chunk);
465 return SCTP_DISPOSITION_NOMEM;
469 * Respond to a normal INIT ACK chunk.
470 * We are the side that is initiating the association.
472 * Section: 5.1 Normal Establishment of an Association, C
473 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
474 * timer and leave COOKIE-WAIT state. "A" shall then send the State
475 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
476 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
478 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
479 * DATA chunks, but it MUST be the first chunk in the packet and
480 * until the COOKIE ACK is returned the sender MUST NOT send any
481 * other packets to the peer.
483 * Verification Tag: 3.3.3
484 * If the value of the Initiate Tag in a received INIT ACK chunk is
485 * found to be 0, the receiver MUST treat it as an error and close the
486 * association by transmitting an ABORT.
489 * (endpoint, asoc, chunk)
492 * (asoc, reply_msg, msg_up, timers, counters)
494 * The return value is the disposition of the chunk.
496 sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
497 const struct sctp_endpoint *ep,
498 const struct sctp_association *asoc,
499 const sctp_subtype_t type,
501 sctp_cmd_seq_t *commands)
503 struct sctp_chunk *chunk = arg;
504 sctp_init_chunk_t *initchunk;
505 struct sctp_chunk *err_chunk;
506 struct sctp_packet *packet;
508 if (!sctp_vtag_verify(chunk, asoc))
509 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
512 * An endpoint MUST NOT bundle INIT, INIT ACK or
513 * SHUTDOWN COMPLETE with any other chunks.
515 if (!chunk->singleton)
516 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
518 /* Make sure that the INIT-ACK chunk has a valid length */
519 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
520 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
522 /* Grab the INIT header. */
523 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
525 /* Verify the INIT chunk before processing it. */
527 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
528 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
531 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
533 /* This chunk contains fatal error. It is to be discarded.
534 * Send an ABORT, with causes. If there are no causes,
535 * then there wasn't enough memory. Just terminate
539 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
540 (__u8 *)(err_chunk->chunk_hdr) +
541 sizeof(sctp_chunkhdr_t),
542 ntohs(err_chunk->chunk_hdr->length) -
543 sizeof(sctp_chunkhdr_t));
545 sctp_chunk_free(err_chunk);
548 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
549 SCTP_PACKET(packet));
550 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
551 error = SCTP_ERROR_INV_PARAM;
555 /* SCTP-AUTH, Section 6.3:
556 * It should be noted that if the receiver wants to tear
557 * down an association in an authenticated way only, the
558 * handling of malformed packets should not result in
559 * tearing down the association.
561 * This means that if we only want to abort associations
562 * in an authenticated way (i.e AUTH+ABORT), then we
563 * can't destroy this association just because the packet
566 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
567 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
569 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
570 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
571 asoc, chunk->transport);
574 /* Tag the variable length parameters. Note that we never
575 * convert the parameters in an INIT chunk.
577 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
579 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
581 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
582 SCTP_PEER_INIT(initchunk));
584 /* Reset init error count upon receipt of INIT-ACK. */
585 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
587 /* 5.1 C) "A" shall stop the T1-init timer and leave
588 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
589 * timer, and enter the COOKIE-ECHOED state.
591 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
592 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
593 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
594 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
595 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
596 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
598 /* SCTP-AUTH: genereate the assocition shared keys so that
599 * we can potentially signe the COOKIE-ECHO.
601 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
603 /* 5.1 C) "A" shall then send the State Cookie received in the
604 * INIT ACK chunk in a COOKIE ECHO chunk, ...
606 /* If there is any errors to report, send the ERROR chunk generated
607 * for unknown parameters as well.
609 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
610 SCTP_CHUNK(err_chunk));
612 return SCTP_DISPOSITION_CONSUME;
616 * Respond to a normal COOKIE ECHO chunk.
617 * We are the side that is being asked for an association.
619 * Section: 5.1 Normal Establishment of an Association, D
620 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
621 * with a COOKIE ACK chunk after building a TCB and moving to
622 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
623 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
624 * chunk MUST be the first chunk in the packet.
626 * IMPLEMENTATION NOTE: An implementation may choose to send the
627 * Communication Up notification to the SCTP user upon reception
628 * of a valid COOKIE ECHO chunk.
630 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
631 * D) Rules for packet carrying a COOKIE ECHO
633 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
634 * Initial Tag received in the INIT ACK.
636 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
639 * (endpoint, asoc, chunk)
642 * (asoc, reply_msg, msg_up, timers, counters)
644 * The return value is the disposition of the chunk.
646 sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
647 const struct sctp_endpoint *ep,
648 const struct sctp_association *asoc,
649 const sctp_subtype_t type, void *arg,
650 sctp_cmd_seq_t *commands)
652 struct sctp_chunk *chunk = arg;
653 struct sctp_association *new_asoc;
654 sctp_init_chunk_t *peer_init;
655 struct sctp_chunk *repl;
656 struct sctp_ulpevent *ev, *ai_ev = NULL;
658 struct sctp_chunk *err_chk_p;
661 /* If the packet is an OOTB packet which is temporarily on the
662 * control endpoint, respond with an ABORT.
664 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
665 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
666 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
669 /* Make sure that the COOKIE_ECHO chunk has a valid length.
670 * In this case, we check that we have enough for at least a
671 * chunk header. More detailed verification is done
672 * in sctp_unpack_cookie().
674 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
675 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
677 /* If the endpoint is not listening or if the number of associations
678 * on the TCP-style socket exceed the max backlog, respond with an
682 if (!sctp_sstate(sk, LISTENING) ||
683 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
684 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
686 /* "Decode" the chunk. We have no optional parameters so we
689 chunk->subh.cookie_hdr =
690 (struct sctp_signed_cookie *)chunk->skb->data;
691 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
692 sizeof(sctp_chunkhdr_t)))
695 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
696 * "Z" will reply with a COOKIE ACK chunk after building a TCB
697 * and moving to the ESTABLISHED state.
699 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
703 * If the re-build failed, what is the proper error path
706 * [We should abort the association. --piggy]
709 /* FIXME: Several errors are possible. A bad cookie should
710 * be silently discarded, but think about logging it too.
713 case -SCTP_IERROR_NOMEM:
716 case -SCTP_IERROR_STALE_COOKIE:
717 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
719 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
721 case -SCTP_IERROR_BAD_SIG:
723 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
728 /* Delay state machine commands until later.
730 * Re-build the bind address for the association is done in
731 * the sctp_unpack_cookie() already.
733 /* This is a brand-new association, so these are not yet side
734 * effects--it is safe to run them here.
736 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
738 if (!sctp_process_init(new_asoc, chunk,
739 &chunk->subh.cookie_hdr->c.peer_addr,
740 peer_init, GFP_ATOMIC))
743 /* SCTP-AUTH: Now that we've populate required fields in
744 * sctp_process_init, set up the assocaition shared keys as
745 * necessary so that we can potentially authenticate the ACK
747 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
751 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
752 * is supposed to be authenticated and we have to do delayed
753 * authentication. We've just recreated the association using
754 * the information in the cookie and now it's much easier to
755 * do the authentication.
757 if (chunk->auth_chunk) {
758 struct sctp_chunk auth;
761 /* set-up our fake chunk so that we can process it */
762 auth.skb = chunk->auth_chunk;
763 auth.asoc = chunk->asoc;
764 auth.sctp_hdr = chunk->sctp_hdr;
765 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
766 sizeof(sctp_chunkhdr_t));
767 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
768 auth.transport = chunk->transport;
770 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
772 /* We can now safely free the auth_chunk clone */
773 kfree_skb(chunk->auth_chunk);
775 if (ret != SCTP_IERROR_NO_ERROR) {
776 sctp_association_free(new_asoc);
777 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
781 repl = sctp_make_cookie_ack(new_asoc, chunk);
785 /* RFC 2960 5.1 Normal Establishment of an Association
787 * D) IMPLEMENTATION NOTE: An implementation may choose to
788 * send the Communication Up notification to the SCTP user
789 * upon reception of a valid COOKIE ECHO chunk.
791 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
792 new_asoc->c.sinit_num_ostreams,
793 new_asoc->c.sinit_max_instreams,
798 /* Sockets API Draft Section 5.3.1.6
799 * When a peer sends a Adaptation Layer Indication parameter , SCTP
800 * delivers this notification to inform the application that of the
801 * peers requested adaptation layer.
803 if (new_asoc->peer.adaptation_ind) {
804 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
810 /* Add all the state machine commands now since we've created
811 * everything. This way we don't introduce memory corruptions
812 * during side-effect processing and correclty count established
815 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
816 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
817 SCTP_STATE(SCTP_STATE_ESTABLISHED));
818 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
819 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
820 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
822 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
823 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
824 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
826 /* This will send the COOKIE ACK */
827 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
829 /* Queue the ASSOC_CHANGE event */
830 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
832 /* Send up the Adaptation Layer Indication event */
834 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
835 SCTP_ULPEVENT(ai_ev));
837 return SCTP_DISPOSITION_CONSUME;
840 sctp_ulpevent_free(ev);
842 sctp_chunk_free(repl);
844 sctp_association_free(new_asoc);
846 return SCTP_DISPOSITION_NOMEM;
850 * Respond to a normal COOKIE ACK chunk.
851 * We are the side that is being asked for an association.
853 * RFC 2960 5.1 Normal Establishment of an Association
855 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
856 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
857 * timer. It may also notify its ULP about the successful
858 * establishment of the association with a Communication Up
859 * notification (see Section 10).
863 * (endpoint, asoc, chunk)
866 * (asoc, reply_msg, msg_up, timers, counters)
868 * The return value is the disposition of the chunk.
870 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
871 const struct sctp_endpoint *ep,
872 const struct sctp_association *asoc,
873 const sctp_subtype_t type, void *arg,
874 sctp_cmd_seq_t *commands)
876 struct sctp_chunk *chunk = arg;
877 struct sctp_ulpevent *ev;
879 if (!sctp_vtag_verify(chunk, asoc))
880 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
882 /* Verify that the chunk length for the COOKIE-ACK is OK.
883 * If we don't do this, any bundled chunks may be junked.
885 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
886 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
889 /* Reset init error count upon receipt of COOKIE-ACK,
890 * to avoid problems with the managemement of this
891 * counter in stale cookie situations when a transition back
892 * from the COOKIE-ECHOED state to the COOKIE-WAIT
893 * state is performed.
895 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
897 /* RFC 2960 5.1 Normal Establishment of an Association
899 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
900 * from the COOKIE-ECHOED state to the ESTABLISHED state,
901 * stopping the T1-cookie timer.
903 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
904 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
905 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
906 SCTP_STATE(SCTP_STATE_ESTABLISHED));
907 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
908 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
909 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
910 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
911 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
912 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
914 /* It may also notify its ULP about the successful
915 * establishment of the association with a Communication Up
916 * notification (see Section 10).
918 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
919 0, asoc->c.sinit_num_ostreams,
920 asoc->c.sinit_max_instreams,
926 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
928 /* Sockets API Draft Section 5.3.1.6
929 * When a peer sends a Adaptation Layer Indication parameter , SCTP
930 * delivers this notification to inform the application that of the
931 * peers requested adaptation layer.
933 if (asoc->peer.adaptation_ind) {
934 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
938 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
942 return SCTP_DISPOSITION_CONSUME;
944 return SCTP_DISPOSITION_NOMEM;
947 /* Generate and sendout a heartbeat packet. */
948 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
949 const struct sctp_association *asoc,
950 const sctp_subtype_t type,
952 sctp_cmd_seq_t *commands)
954 struct sctp_transport *transport = (struct sctp_transport *) arg;
955 struct sctp_chunk *reply;
957 /* Send a heartbeat to our peer. */
958 reply = sctp_make_heartbeat(asoc, transport);
960 return SCTP_DISPOSITION_NOMEM;
962 /* Set rto_pending indicating that an RTT measurement
963 * is started with this heartbeat chunk.
965 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
966 SCTP_TRANSPORT(transport));
968 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
969 return SCTP_DISPOSITION_CONSUME;
972 /* Generate a HEARTBEAT packet on the given transport. */
973 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
974 const struct sctp_endpoint *ep,
975 const struct sctp_association *asoc,
976 const sctp_subtype_t type,
978 sctp_cmd_seq_t *commands)
980 struct sctp_transport *transport = (struct sctp_transport *) arg;
982 if (asoc->overall_error_count >= asoc->max_retrans) {
983 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
984 SCTP_ERROR(ETIMEDOUT));
985 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
986 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
987 SCTP_PERR(SCTP_ERROR_NO_ERROR));
988 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
989 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
990 return SCTP_DISPOSITION_DELETE_TCB;
994 * The Sender-specific Heartbeat Info field should normally include
995 * information about the sender's current time when this HEARTBEAT
996 * chunk is sent and the destination transport address to which this
997 * HEARTBEAT is sent (see Section 8.3).
1000 if (transport->param_flags & SPP_HB_ENABLE) {
1001 if (SCTP_DISPOSITION_NOMEM ==
1002 sctp_sf_heartbeat(ep, asoc, type, arg,
1004 return SCTP_DISPOSITION_NOMEM;
1006 /* Set transport error counter and association error counter
1007 * when sending heartbeat.
1009 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1010 SCTP_TRANSPORT(transport));
1012 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1013 SCTP_TRANSPORT(transport));
1014 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1015 SCTP_TRANSPORT(transport));
1017 return SCTP_DISPOSITION_CONSUME;
1021 * Process an heartbeat request.
1023 * Section: 8.3 Path Heartbeat
1024 * The receiver of the HEARTBEAT should immediately respond with a
1025 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1026 * from the received HEARTBEAT chunk.
1028 * Verification Tag: 8.5 Verification Tag [Normal verification]
1029 * When receiving an SCTP packet, the endpoint MUST ensure that the
1030 * value in the Verification Tag field of the received SCTP packet
1031 * matches its own Tag. If the received Verification Tag value does not
1032 * match the receiver's own tag value, the receiver shall silently
1033 * discard the packet and shall not process it any further except for
1034 * those cases listed in Section 8.5.1 below.
1037 * (endpoint, asoc, chunk)
1040 * (asoc, reply_msg, msg_up, timers, counters)
1042 * The return value is the disposition of the chunk.
1044 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1045 const struct sctp_endpoint *ep,
1046 const struct sctp_association *asoc,
1047 const sctp_subtype_t type,
1049 sctp_cmd_seq_t *commands)
1051 sctp_paramhdr_t *param_hdr;
1052 struct sctp_chunk *chunk = arg;
1053 struct sctp_chunk *reply;
1056 if (!sctp_vtag_verify(chunk, asoc))
1057 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1059 /* Make sure that the HEARTBEAT chunk has a valid length. */
1060 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1061 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1064 /* 8.3 The receiver of the HEARTBEAT should immediately
1065 * respond with a HEARTBEAT ACK that contains the Heartbeat
1066 * Information field copied from the received HEARTBEAT chunk.
1068 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1069 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
1070 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1072 if (ntohs(param_hdr->length) > paylen)
1073 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1074 param_hdr, commands);
1076 if (!pskb_pull(chunk->skb, paylen))
1079 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1083 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1084 return SCTP_DISPOSITION_CONSUME;
1087 return SCTP_DISPOSITION_NOMEM;
1091 * Process the returning HEARTBEAT ACK.
1093 * Section: 8.3 Path Heartbeat
1094 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1095 * should clear the error counter of the destination transport
1096 * address to which the HEARTBEAT was sent, and mark the destination
1097 * transport address as active if it is not so marked. The endpoint may
1098 * optionally report to the upper layer when an inactive destination
1099 * address is marked as active due to the reception of the latest
1100 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1101 * clear the association overall error count as well (as defined
1104 * The receiver of the HEARTBEAT ACK should also perform an RTT
1105 * measurement for that destination transport address using the time
1106 * value carried in the HEARTBEAT ACK chunk.
1108 * Verification Tag: 8.5 Verification Tag [Normal verification]
1111 * (endpoint, asoc, chunk)
1114 * (asoc, reply_msg, msg_up, timers, counters)
1116 * The return value is the disposition of the chunk.
1118 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1119 const struct sctp_endpoint *ep,
1120 const struct sctp_association *asoc,
1121 const sctp_subtype_t type,
1123 sctp_cmd_seq_t *commands)
1125 struct sctp_chunk *chunk = arg;
1126 union sctp_addr from_addr;
1127 struct sctp_transport *link;
1128 sctp_sender_hb_info_t *hbinfo;
1129 unsigned long max_interval;
1131 if (!sctp_vtag_verify(chunk, asoc))
1132 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1134 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1135 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1136 sizeof(sctp_sender_hb_info_t)))
1137 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1140 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1141 /* Make sure that the length of the parameter is what we expect */
1142 if (ntohs(hbinfo->param_hdr.length) !=
1143 sizeof(sctp_sender_hb_info_t)) {
1144 return SCTP_DISPOSITION_DISCARD;
1147 from_addr = hbinfo->daddr;
1148 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1150 /* This should never happen, but lets log it if so. */
1151 if (unlikely(!link)) {
1152 if (from_addr.sa.sa_family == AF_INET6) {
1153 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1156 &from_addr.v6.sin6_addr);
1158 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1161 &from_addr.v4.sin_addr.s_addr);
1163 return SCTP_DISPOSITION_DISCARD;
1166 /* Validate the 64-bit random nonce. */
1167 if (hbinfo->hb_nonce != link->hb_nonce)
1168 return SCTP_DISPOSITION_DISCARD;
1170 max_interval = link->hbinterval + link->rto;
1172 /* Check if the timestamp looks valid. */
1173 if (time_after(hbinfo->sent_at, jiffies) ||
1174 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1175 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1176 "for transport:%p\n", __func__, link);
1178 return SCTP_DISPOSITION_DISCARD;
1181 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1182 * the HEARTBEAT should clear the error counter of the
1183 * destination transport address to which the HEARTBEAT was
1184 * sent and mark the destination transport address as active if
1185 * it is not so marked.
1187 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1189 return SCTP_DISPOSITION_CONSUME;
1192 /* Helper function to send out an abort for the restart
1195 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1196 struct sctp_chunk *init,
1197 sctp_cmd_seq_t *commands)
1200 struct sctp_packet *pkt;
1201 union sctp_addr_param *addrparm;
1202 struct sctp_errhdr *errhdr;
1203 struct sctp_endpoint *ep;
1204 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1205 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1207 /* Build the error on the stack. We are way to malloc crazy
1208 * throughout the code today.
1210 errhdr = (struct sctp_errhdr *)buffer;
1211 addrparm = (union sctp_addr_param *)errhdr->variable;
1213 /* Copy into a parm format. */
1214 len = af->to_addr_param(ssa, addrparm);
1215 len += sizeof(sctp_errhdr_t);
1217 errhdr->cause = SCTP_ERROR_RESTART;
1218 errhdr->length = htons(len);
1220 /* Assign to the control socket. */
1221 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1223 /* Association is NULL since this may be a restart attack and we
1224 * want to send back the attacker's vtag.
1226 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1230 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1232 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1234 /* Discard the rest of the inbound packet. */
1235 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1238 /* Even if there is no memory, treat as a failure so
1239 * the packet will get dropped.
1244 static bool list_has_sctp_addr(const struct list_head *list,
1245 union sctp_addr *ipaddr)
1247 struct sctp_transport *addr;
1249 list_for_each_entry(addr, list, transports) {
1250 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1256 /* A restart is occurring, check to make sure no new addresses
1257 * are being added as we may be under a takeover attack.
1259 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1260 const struct sctp_association *asoc,
1261 struct sctp_chunk *init,
1262 sctp_cmd_seq_t *commands)
1264 struct net *net = sock_net(new_asoc->base.sk);
1265 struct sctp_transport *new_addr;
1268 /* Implementor's Guide - Section 5.2.2
1270 * Before responding the endpoint MUST check to see if the
1271 * unexpected INIT adds new addresses to the association. If new
1272 * addresses are added to the association, the endpoint MUST respond
1276 /* Search through all current addresses and make sure
1277 * we aren't adding any new ones.
1279 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1281 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1282 &new_addr->ipaddr)) {
1283 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1290 /* Return success if all addresses were found. */
1294 /* Populate the verification/tie tags based on overlapping INIT
1297 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1299 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1300 const struct sctp_association *asoc)
1302 switch (asoc->state) {
1304 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1306 case SCTP_STATE_COOKIE_WAIT:
1307 new_asoc->c.my_vtag = asoc->c.my_vtag;
1308 new_asoc->c.my_ttag = asoc->c.my_vtag;
1309 new_asoc->c.peer_ttag = 0;
1312 case SCTP_STATE_COOKIE_ECHOED:
1313 new_asoc->c.my_vtag = asoc->c.my_vtag;
1314 new_asoc->c.my_ttag = asoc->c.my_vtag;
1315 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1318 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1319 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1322 new_asoc->c.my_ttag = asoc->c.my_vtag;
1323 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1327 /* Other parameters for the endpoint SHOULD be copied from the
1328 * existing parameters of the association (e.g. number of
1329 * outbound streams) into the INIT ACK and cookie.
1331 new_asoc->rwnd = asoc->rwnd;
1332 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1333 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1334 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1338 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1341 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1343 * Returns value representing action to be taken. These action values
1344 * correspond to Action/Description values in RFC 2960, Table 2.
1346 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1347 const struct sctp_association *asoc)
1349 /* In this case, the peer may have restarted. */
1350 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1351 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1352 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1353 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1356 /* Collision case B. */
1357 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1358 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1359 (0 == asoc->c.peer_vtag))) {
1363 /* Collision case D. */
1364 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1365 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1368 /* Collision case C. */
1369 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1370 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1371 (0 == new_asoc->c.my_ttag) &&
1372 (0 == new_asoc->c.peer_ttag))
1375 /* No match to any of the special cases; discard this packet. */
1379 /* Common helper routine for both duplicate and simulataneous INIT
1382 static sctp_disposition_t sctp_sf_do_unexpected_init(
1384 const struct sctp_endpoint *ep,
1385 const struct sctp_association *asoc,
1386 const sctp_subtype_t type,
1387 void *arg, sctp_cmd_seq_t *commands)
1389 sctp_disposition_t retval;
1390 struct sctp_chunk *chunk = arg;
1391 struct sctp_chunk *repl;
1392 struct sctp_association *new_asoc;
1393 struct sctp_chunk *err_chunk;
1394 struct sctp_packet *packet;
1395 sctp_unrecognized_param_t *unk_param;
1399 * An endpoint MUST NOT bundle INIT, INIT ACK or
1400 * SHUTDOWN COMPLETE with any other chunks.
1403 * Furthermore, we require that the receiver of an INIT chunk MUST
1404 * enforce these rules by silently discarding an arriving packet
1405 * with an INIT chunk that is bundled with other chunks.
1407 if (!chunk->singleton)
1408 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1410 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1413 if (chunk->sctp_hdr->vtag != 0)
1414 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1416 /* Make sure that the INIT chunk has a valid length.
1417 * In this case, we generate a protocol violation since we have
1418 * an association established.
1420 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1421 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1423 /* Grab the INIT header. */
1424 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1426 /* Tag the variable length parameters. */
1427 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1429 /* Verify the INIT chunk before processing it. */
1431 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
1432 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1434 /* This chunk contains fatal error. It is to be discarded.
1435 * Send an ABORT, with causes if there is any.
1438 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1439 (__u8 *)(err_chunk->chunk_hdr) +
1440 sizeof(sctp_chunkhdr_t),
1441 ntohs(err_chunk->chunk_hdr->length) -
1442 sizeof(sctp_chunkhdr_t));
1445 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1446 SCTP_PACKET(packet));
1447 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1448 retval = SCTP_DISPOSITION_CONSUME;
1450 retval = SCTP_DISPOSITION_NOMEM;
1454 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1460 * Other parameters for the endpoint SHOULD be copied from the
1461 * existing parameters of the association (e.g. number of
1462 * outbound streams) into the INIT ACK and cookie.
1463 * FIXME: We are copying parameters from the endpoint not the
1466 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1470 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1471 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1474 /* In the outbound INIT ACK the endpoint MUST copy its current
1475 * Verification Tag and Peers Verification tag into a reserved
1476 * place (local tie-tag and per tie-tag) within the state cookie.
1478 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1479 (sctp_init_chunk_t *)chunk->chunk_hdr,
1483 /* Make sure no new addresses are being added during the
1484 * restart. Do not do this check for COOKIE-WAIT state,
1485 * since there are no peer addresses to check against.
1486 * Upon return an ABORT will have been sent if needed.
1488 if (!sctp_state(asoc, COOKIE_WAIT)) {
1489 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1491 retval = SCTP_DISPOSITION_CONSUME;
1496 sctp_tietags_populate(new_asoc, asoc);
1498 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1500 /* If there are errors need to be reported for unknown parameters,
1501 * make sure to reserve enough room in the INIT ACK for them.
1505 len = ntohs(err_chunk->chunk_hdr->length) -
1506 sizeof(sctp_chunkhdr_t);
1509 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1513 /* If there are errors need to be reported for unknown parameters,
1514 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1518 /* Get the "Unrecognized parameter" parameter(s) out of the
1519 * ERROR chunk generated by sctp_verify_init(). Since the
1520 * error cause code for "unknown parameter" and the
1521 * "Unrecognized parameter" type is the same, we can
1522 * construct the parameters in INIT ACK by copying the
1523 * ERROR causes over.
1525 unk_param = (sctp_unrecognized_param_t *)
1526 ((__u8 *)(err_chunk->chunk_hdr) +
1527 sizeof(sctp_chunkhdr_t));
1528 /* Replace the cause code with the "Unrecognized parameter"
1531 sctp_addto_chunk(repl, len, unk_param);
1534 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1535 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1538 * Note: After sending out INIT ACK with the State Cookie parameter,
1539 * "Z" MUST NOT allocate any resources for this new association.
1540 * Otherwise, "Z" will be vulnerable to resource attacks.
1542 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1543 retval = SCTP_DISPOSITION_CONSUME;
1548 retval = SCTP_DISPOSITION_NOMEM;
1551 sctp_association_free(new_asoc);
1554 sctp_chunk_free(err_chunk);
1559 * Handle simultaneous INIT.
1560 * This means we started an INIT and then we got an INIT request from
1563 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1564 * This usually indicates an initialization collision, i.e., each
1565 * endpoint is attempting, at about the same time, to establish an
1566 * association with the other endpoint.
1568 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1569 * endpoint MUST respond with an INIT ACK using the same parameters it
1570 * sent in its original INIT chunk (including its Verification Tag,
1571 * unchanged). These original parameters are combined with those from the
1572 * newly received INIT chunk. The endpoint shall also generate a State
1573 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1574 * INIT to calculate the State Cookie.
1576 * After that, the endpoint MUST NOT change its state, the T1-init
1577 * timer shall be left running and the corresponding TCB MUST NOT be
1578 * destroyed. The normal procedures for handling State Cookies when
1579 * a TCB exists will resolve the duplicate INITs to a single association.
1581 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1582 * its Tie-Tags with the Tag information of itself and its peer (see
1583 * section 5.2.2 for a description of the Tie-Tags).
1585 * Verification Tag: Not explicit, but an INIT can not have a valid
1586 * verification tag, so we skip the check.
1589 * (endpoint, asoc, chunk)
1592 * (asoc, reply_msg, msg_up, timers, counters)
1594 * The return value is the disposition of the chunk.
1596 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1597 const struct sctp_endpoint *ep,
1598 const struct sctp_association *asoc,
1599 const sctp_subtype_t type,
1601 sctp_cmd_seq_t *commands)
1603 /* Call helper to do the real work for both simulataneous and
1604 * duplicate INIT chunk handling.
1606 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1610 * Handle duplicated INIT messages. These are usually delayed
1613 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1614 * COOKIE-ECHOED and COOKIE-WAIT
1616 * Unless otherwise stated, upon reception of an unexpected INIT for
1617 * this association, the endpoint shall generate an INIT ACK with a
1618 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1619 * current Verification Tag and peer's Verification Tag into a reserved
1620 * place within the state cookie. We shall refer to these locations as
1621 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1622 * containing this INIT ACK MUST carry a Verification Tag value equal to
1623 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1624 * MUST contain a new Initiation Tag (randomly generated see Section
1625 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1626 * existing parameters of the association (e.g. number of outbound
1627 * streams) into the INIT ACK and cookie.
1629 * After sending out the INIT ACK, the endpoint shall take no further
1630 * actions, i.e., the existing association, including its current state,
1631 * and the corresponding TCB MUST NOT be changed.
1633 * Note: Only when a TCB exists and the association is not in a COOKIE-
1634 * WAIT state are the Tie-Tags populated. For a normal association INIT
1635 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1636 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1637 * State Cookie are populated as specified in section 5.2.1.
1639 * Verification Tag: Not specified, but an INIT has no way of knowing
1640 * what the verification tag could be, so we ignore it.
1643 * (endpoint, asoc, chunk)
1646 * (asoc, reply_msg, msg_up, timers, counters)
1648 * The return value is the disposition of the chunk.
1650 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1651 const struct sctp_endpoint *ep,
1652 const struct sctp_association *asoc,
1653 const sctp_subtype_t type,
1655 sctp_cmd_seq_t *commands)
1657 /* Call helper to do the real work for both simulataneous and
1658 * duplicate INIT chunk handling.
1660 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1665 * Unexpected INIT-ACK handler.
1668 * If an INIT ACK received by an endpoint in any state other than the
1669 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1670 * An unexpected INIT ACK usually indicates the processing of an old or
1671 * duplicated INIT chunk.
1673 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1674 const struct sctp_endpoint *ep,
1675 const struct sctp_association *asoc,
1676 const sctp_subtype_t type,
1677 void *arg, sctp_cmd_seq_t *commands)
1679 /* Per the above section, we'll discard the chunk if we have an
1680 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1682 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1683 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1685 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1688 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1691 * A) In this case, the peer may have restarted.
1693 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1694 const struct sctp_endpoint *ep,
1695 const struct sctp_association *asoc,
1696 struct sctp_chunk *chunk,
1697 sctp_cmd_seq_t *commands,
1698 struct sctp_association *new_asoc)
1700 sctp_init_chunk_t *peer_init;
1701 struct sctp_ulpevent *ev;
1702 struct sctp_chunk *repl;
1703 struct sctp_chunk *err;
1704 sctp_disposition_t disposition;
1706 /* new_asoc is a brand-new association, so these are not yet
1707 * side effects--it is safe to run them here.
1709 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1711 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1715 /* Make sure no new addresses are being added during the
1716 * restart. Though this is a pretty complicated attack
1717 * since you'd have to get inside the cookie.
1719 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1720 return SCTP_DISPOSITION_CONSUME;
1723 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1724 * the peer has restarted (Action A), it MUST NOT setup a new
1725 * association but instead resend the SHUTDOWN ACK and send an ERROR
1726 * chunk with a "Cookie Received while Shutting Down" error cause to
1729 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1730 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1731 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1733 if (SCTP_DISPOSITION_NOMEM == disposition)
1736 err = sctp_make_op_error(asoc, chunk,
1737 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1740 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1743 return SCTP_DISPOSITION_CONSUME;
1746 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1747 * data. Consider the optional choice of resending of this data.
1749 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1750 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1751 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1752 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1754 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1755 * and ASCONF-ACK cache.
1757 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1758 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1759 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1761 repl = sctp_make_cookie_ack(new_asoc, chunk);
1765 /* Report association restart to upper layer. */
1766 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1767 new_asoc->c.sinit_num_ostreams,
1768 new_asoc->c.sinit_max_instreams,
1773 /* Update the content of current association. */
1774 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1775 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1776 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1777 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1778 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1779 return SCTP_DISPOSITION_CONSUME;
1782 sctp_chunk_free(repl);
1784 return SCTP_DISPOSITION_NOMEM;
1787 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1790 * B) In this case, both sides may be attempting to start an association
1791 * at about the same time but the peer endpoint started its INIT
1792 * after responding to the local endpoint's INIT
1794 /* This case represents an initialization collision. */
1795 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1796 const struct sctp_endpoint *ep,
1797 const struct sctp_association *asoc,
1798 struct sctp_chunk *chunk,
1799 sctp_cmd_seq_t *commands,
1800 struct sctp_association *new_asoc)
1802 sctp_init_chunk_t *peer_init;
1803 struct sctp_chunk *repl;
1805 /* new_asoc is a brand-new association, so these are not yet
1806 * side effects--it is safe to run them here.
1808 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1809 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1813 /* Update the content of current association. */
1814 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1815 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1816 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1817 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1818 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1820 repl = sctp_make_cookie_ack(new_asoc, chunk);
1824 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1826 /* RFC 2960 5.1 Normal Establishment of an Association
1828 * D) IMPLEMENTATION NOTE: An implementation may choose to
1829 * send the Communication Up notification to the SCTP user
1830 * upon reception of a valid COOKIE ECHO chunk.
1832 * Sadly, this needs to be implemented as a side-effect, because
1833 * we are not guaranteed to have set the association id of the real
1834 * association and so these notifications need to be delayed until
1835 * the association id is allocated.
1838 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1840 /* Sockets API Draft Section 5.3.1.6
1841 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1842 * delivers this notification to inform the application that of the
1843 * peers requested adaptation layer.
1845 * This also needs to be done as a side effect for the same reason as
1848 if (asoc->peer.adaptation_ind)
1849 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1851 return SCTP_DISPOSITION_CONSUME;
1854 return SCTP_DISPOSITION_NOMEM;
1857 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1860 * C) In this case, the local endpoint's cookie has arrived late.
1861 * Before it arrived, the local endpoint sent an INIT and received an
1862 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1863 * but a new tag of its own.
1865 /* This case represents an initialization collision. */
1866 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1867 const struct sctp_endpoint *ep,
1868 const struct sctp_association *asoc,
1869 struct sctp_chunk *chunk,
1870 sctp_cmd_seq_t *commands,
1871 struct sctp_association *new_asoc)
1873 /* The cookie should be silently discarded.
1874 * The endpoint SHOULD NOT change states and should leave
1875 * any timers running.
1877 return SCTP_DISPOSITION_DISCARD;
1880 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1884 * D) When both local and remote tags match the endpoint should always
1885 * enter the ESTABLISHED state, if it has not already done so.
1887 /* This case represents an initialization collision. */
1888 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1889 const struct sctp_endpoint *ep,
1890 const struct sctp_association *asoc,
1891 struct sctp_chunk *chunk,
1892 sctp_cmd_seq_t *commands,
1893 struct sctp_association *new_asoc)
1895 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1896 struct sctp_chunk *repl;
1898 /* Clarification from Implementor's Guide:
1899 * D) When both local and remote tags match the endpoint should
1900 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1901 * It should stop any cookie timer that may be running and send
1905 /* Don't accidentally move back into established state. */
1906 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1907 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1908 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1909 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1910 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1911 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1912 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1915 /* RFC 2960 5.1 Normal Establishment of an Association
1917 * D) IMPLEMENTATION NOTE: An implementation may choose
1918 * to send the Communication Up notification to the
1919 * SCTP user upon reception of a valid COOKIE
1922 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1924 asoc->c.sinit_num_ostreams,
1925 asoc->c.sinit_max_instreams,
1930 /* Sockets API Draft Section 5.3.1.6
1931 * When a peer sends a Adaptation Layer Indication parameter,
1932 * SCTP delivers this notification to inform the application
1933 * that of the peers requested adaptation layer.
1935 if (asoc->peer.adaptation_ind) {
1936 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1944 repl = sctp_make_cookie_ack(new_asoc, chunk);
1948 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1951 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1954 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1955 SCTP_ULPEVENT(ai_ev));
1957 return SCTP_DISPOSITION_CONSUME;
1961 sctp_ulpevent_free(ai_ev);
1963 sctp_ulpevent_free(ev);
1964 return SCTP_DISPOSITION_NOMEM;
1968 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1969 * chunk was retransmitted and then delayed in the network.
1971 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1973 * Verification Tag: None. Do cookie validation.
1976 * (endpoint, asoc, chunk)
1979 * (asoc, reply_msg, msg_up, timers, counters)
1981 * The return value is the disposition of the chunk.
1983 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1984 const struct sctp_endpoint *ep,
1985 const struct sctp_association *asoc,
1986 const sctp_subtype_t type,
1988 sctp_cmd_seq_t *commands)
1990 sctp_disposition_t retval;
1991 struct sctp_chunk *chunk = arg;
1992 struct sctp_association *new_asoc;
1995 struct sctp_chunk *err_chk_p;
1997 /* Make sure that the chunk has a valid length from the protocol
1998 * perspective. In this case check to make sure we have at least
1999 * enough for the chunk header. Cookie length verification is
2002 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2003 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2006 /* "Decode" the chunk. We have no optional parameters so we
2007 * are in good shape.
2009 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2010 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2011 sizeof(sctp_chunkhdr_t)))
2014 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2015 * of a duplicate COOKIE ECHO match the Verification Tags of the
2016 * current association, consider the State Cookie valid even if
2017 * the lifespan is exceeded.
2019 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2023 * If the re-build failed, what is the proper error path
2026 * [We should abort the association. --piggy]
2029 /* FIXME: Several errors are possible. A bad cookie should
2030 * be silently discarded, but think about logging it too.
2033 case -SCTP_IERROR_NOMEM:
2036 case -SCTP_IERROR_STALE_COOKIE:
2037 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2039 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2040 case -SCTP_IERROR_BAD_SIG:
2042 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2046 /* Compare the tie_tag in cookie with the verification tag of
2047 * current association.
2049 action = sctp_tietags_compare(new_asoc, asoc);
2052 case 'A': /* Association restart. */
2053 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2057 case 'B': /* Collision case B. */
2058 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2062 case 'C': /* Collision case C. */
2063 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2067 case 'D': /* Collision case D. */
2068 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2072 default: /* Discard packet for all others. */
2073 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2077 /* Delete the tempory new association. */
2078 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2079 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2081 /* Restore association pointer to provide SCTP command interpeter
2082 * with a valid context in case it needs to manipulate
2084 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2085 SCTP_ASOC((struct sctp_association *)asoc));
2090 return SCTP_DISPOSITION_NOMEM;
2094 * Process an ABORT. (SHUTDOWN-PENDING state)
2096 * See sctp_sf_do_9_1_abort().
2098 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2100 const struct sctp_endpoint *ep,
2101 const struct sctp_association *asoc,
2102 const sctp_subtype_t type,
2104 sctp_cmd_seq_t *commands)
2106 struct sctp_chunk *chunk = arg;
2108 if (!sctp_vtag_verify_either(chunk, asoc))
2109 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2111 /* Make sure that the ABORT chunk has a valid length.
2112 * Since this is an ABORT chunk, we have to discard it
2113 * because of the following text:
2114 * RFC 2960, Section 3.3.7
2115 * If an endpoint receives an ABORT with a format error or for an
2116 * association that doesn't exist, it MUST silently discard it.
2117 * Because the length is "invalid", we can't really discard just
2118 * as we do not know its true length. So, to be safe, discard the
2121 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2122 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2124 /* ADD-IP: Special case for ABORT chunks
2125 * F4) One special consideration is that ABORT Chunks arriving
2126 * destined to the IP address being deleted MUST be
2127 * ignored (see Section 5.3.1 for further details).
2129 if (SCTP_ADDR_DEL ==
2130 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2131 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2133 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2137 * Process an ABORT. (SHUTDOWN-SENT state)
2139 * See sctp_sf_do_9_1_abort().
2141 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2142 const struct sctp_endpoint *ep,
2143 const struct sctp_association *asoc,
2144 const sctp_subtype_t type,
2146 sctp_cmd_seq_t *commands)
2148 struct sctp_chunk *chunk = arg;
2150 if (!sctp_vtag_verify_either(chunk, asoc))
2151 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2153 /* Make sure that the ABORT chunk has a valid length.
2154 * Since this is an ABORT chunk, we have to discard it
2155 * because of the following text:
2156 * RFC 2960, Section 3.3.7
2157 * If an endpoint receives an ABORT with a format error or for an
2158 * association that doesn't exist, it MUST silently discard it.
2159 * Because the length is "invalid", we can't really discard just
2160 * as we do not know its true length. So, to be safe, discard the
2163 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2164 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2166 /* ADD-IP: Special case for ABORT chunks
2167 * F4) One special consideration is that ABORT Chunks arriving
2168 * destined to the IP address being deleted MUST be
2169 * ignored (see Section 5.3.1 for further details).
2171 if (SCTP_ADDR_DEL ==
2172 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2173 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2175 /* Stop the T2-shutdown timer. */
2176 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2177 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2179 /* Stop the T5-shutdown guard timer. */
2180 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2181 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2183 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2187 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2189 * See sctp_sf_do_9_1_abort().
2191 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2193 const struct sctp_endpoint *ep,
2194 const struct sctp_association *asoc,
2195 const sctp_subtype_t type,
2197 sctp_cmd_seq_t *commands)
2199 /* The same T2 timer, so we should be able to use
2200 * common function with the SHUTDOWN-SENT state.
2202 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2206 * Handle an Error received in COOKIE_ECHOED state.
2208 * Only handle the error type of stale COOKIE Error, the other errors will
2212 * (endpoint, asoc, chunk)
2215 * (asoc, reply_msg, msg_up, timers, counters)
2217 * The return value is the disposition of the chunk.
2219 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2220 const struct sctp_endpoint *ep,
2221 const struct sctp_association *asoc,
2222 const sctp_subtype_t type,
2224 sctp_cmd_seq_t *commands)
2226 struct sctp_chunk *chunk = arg;
2229 if (!sctp_vtag_verify(chunk, asoc))
2230 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2232 /* Make sure that the ERROR chunk has a valid length.
2233 * The parameter walking depends on this as well.
2235 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2236 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2239 /* Process the error here */
2240 /* FUTURE FIXME: When PR-SCTP related and other optional
2241 * parms are emitted, this will have to change to handle multiple
2244 sctp_walk_errors(err, chunk->chunk_hdr) {
2245 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2246 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2250 /* It is possible to have malformed error causes, and that
2251 * will cause us to end the walk early. However, since
2252 * we are discarding the packet, there should be no adverse
2255 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2259 * Handle a Stale COOKIE Error
2261 * Section: 5.2.6 Handle Stale COOKIE Error
2262 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2263 * one of the following three alternatives.
2265 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2266 * Preservative parameter requesting an extension to the lifetime of
2267 * the State Cookie. When calculating the time extension, an
2268 * implementation SHOULD use the RTT information measured based on the
2269 * previous COOKIE ECHO / ERROR exchange, and should add no more
2270 * than 1 second beyond the measured RTT, due to long State Cookie
2271 * lifetimes making the endpoint more subject to a replay attack.
2273 * Verification Tag: Not explicit, but safe to ignore.
2276 * (endpoint, asoc, chunk)
2279 * (asoc, reply_msg, msg_up, timers, counters)
2281 * The return value is the disposition of the chunk.
2283 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2284 const struct sctp_endpoint *ep,
2285 const struct sctp_association *asoc,
2286 const sctp_subtype_t type,
2288 sctp_cmd_seq_t *commands)
2290 struct sctp_chunk *chunk = arg;
2292 sctp_cookie_preserve_param_t bht;
2294 struct sctp_chunk *reply;
2295 struct sctp_bind_addr *bp;
2296 int attempts = asoc->init_err_counter + 1;
2298 if (attempts > asoc->max_init_attempts) {
2299 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2300 SCTP_ERROR(ETIMEDOUT));
2301 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2302 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2303 return SCTP_DISPOSITION_DELETE_TCB;
2306 err = (sctp_errhdr_t *)(chunk->skb->data);
2308 /* When calculating the time extension, an implementation
2309 * SHOULD use the RTT information measured based on the
2310 * previous COOKIE ECHO / ERROR exchange, and should add no
2311 * more than 1 second beyond the measured RTT, due to long
2312 * State Cookie lifetimes making the endpoint more subject to
2314 * Measure of Staleness's unit is usec. (1/1000000 sec)
2315 * Suggested Cookie Life-span Increment's unit is msec.
2317 * In general, if you use the suggested cookie life, the value
2318 * found in the field of measure of staleness should be doubled
2319 * to give ample time to retransmit the new cookie and thus
2320 * yield a higher probability of success on the reattempt.
2322 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2323 stale = (stale * 2) / 1000;
2325 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2326 bht.param_hdr.length = htons(sizeof(bht));
2327 bht.lifespan_increment = htonl(stale);
2329 /* Build that new INIT chunk. */
2330 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2331 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2335 sctp_addto_chunk(reply, sizeof(bht), &bht);
2337 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2338 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2340 /* Stop pending T3-rtx and heartbeat timers */
2341 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2342 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2344 /* Delete non-primary peer ip addresses since we are transitioning
2345 * back to the COOKIE-WAIT state
2347 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2349 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2352 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2353 SCTP_TRANSPORT(asoc->peer.primary_path));
2355 /* Cast away the const modifier, as we want to just
2356 * rerun it through as a sideffect.
2358 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2360 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2361 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2362 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2363 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2364 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2365 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2367 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2369 return SCTP_DISPOSITION_CONSUME;
2372 return SCTP_DISPOSITION_NOMEM;
2379 * After checking the Verification Tag, the receiving endpoint shall
2380 * remove the association from its record, and shall report the
2381 * termination to its upper layer.
2383 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2384 * B) Rules for packet carrying ABORT:
2386 * - The endpoint shall always fill in the Verification Tag field of the
2387 * outbound packet with the destination endpoint's tag value if it
2390 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2391 * MUST follow the procedure described in Section 8.4.
2393 * - The receiver MUST accept the packet if the Verification Tag
2394 * matches either its own tag, OR the tag of its peer. Otherwise, the
2395 * receiver MUST silently discard the packet and take no further
2399 * (endpoint, asoc, chunk)
2402 * (asoc, reply_msg, msg_up, timers, counters)
2404 * The return value is the disposition of the chunk.
2406 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2407 const struct sctp_endpoint *ep,
2408 const struct sctp_association *asoc,
2409 const sctp_subtype_t type,
2411 sctp_cmd_seq_t *commands)
2413 struct sctp_chunk *chunk = arg;
2415 if (!sctp_vtag_verify_either(chunk, asoc))
2416 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2418 /* Make sure that the ABORT chunk has a valid length.
2419 * Since this is an ABORT chunk, we have to discard it
2420 * because of the following text:
2421 * RFC 2960, Section 3.3.7
2422 * If an endpoint receives an ABORT with a format error or for an
2423 * association that doesn't exist, it MUST silently discard it.
2424 * Because the length is "invalid", we can't really discard just
2425 * as we do not know its true length. So, to be safe, discard the
2428 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2429 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2431 /* ADD-IP: Special case for ABORT chunks
2432 * F4) One special consideration is that ABORT Chunks arriving
2433 * destined to the IP address being deleted MUST be
2434 * ignored (see Section 5.3.1 for further details).
2436 if (SCTP_ADDR_DEL ==
2437 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2438 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2440 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2443 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2444 const struct sctp_endpoint *ep,
2445 const struct sctp_association *asoc,
2446 const sctp_subtype_t type,
2448 sctp_cmd_seq_t *commands)
2450 struct sctp_chunk *chunk = arg;
2452 __be16 error = SCTP_ERROR_NO_ERROR;
2454 /* See if we have an error cause code in the chunk. */
2455 len = ntohs(chunk->chunk_hdr->length);
2456 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2459 sctp_walk_errors(err, chunk->chunk_hdr);
2460 if ((void *)err != (void *)chunk->chunk_end)
2461 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2463 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2466 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2467 /* ASSOC_FAILED will DELETE_TCB. */
2468 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2469 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2470 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2472 return SCTP_DISPOSITION_ABORT;
2476 * Process an ABORT. (COOKIE-WAIT state)
2478 * See sctp_sf_do_9_1_abort() above.
2480 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2481 const struct sctp_endpoint *ep,
2482 const struct sctp_association *asoc,
2483 const sctp_subtype_t type,
2485 sctp_cmd_seq_t *commands)
2487 struct sctp_chunk *chunk = arg;
2489 __be16 error = SCTP_ERROR_NO_ERROR;
2491 if (!sctp_vtag_verify_either(chunk, asoc))
2492 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2494 /* Make sure that the ABORT chunk has a valid length.
2495 * Since this is an ABORT chunk, we have to discard it
2496 * because of the following text:
2497 * RFC 2960, Section 3.3.7
2498 * If an endpoint receives an ABORT with a format error or for an
2499 * association that doesn't exist, it MUST silently discard it.
2500 * Because the length is "invalid", we can't really discard just
2501 * as we do not know its true length. So, to be safe, discard the
2504 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2505 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2507 /* See if we have an error cause code in the chunk. */
2508 len = ntohs(chunk->chunk_hdr->length);
2509 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2510 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2512 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2517 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2519 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2520 const struct sctp_endpoint *ep,
2521 const struct sctp_association *asoc,
2522 const sctp_subtype_t type,
2524 sctp_cmd_seq_t *commands)
2526 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2528 (struct sctp_transport *)arg);
2532 * Process an ABORT. (COOKIE-ECHOED state)
2534 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2535 const struct sctp_endpoint *ep,
2536 const struct sctp_association *asoc,
2537 const sctp_subtype_t type,
2539 sctp_cmd_seq_t *commands)
2541 /* There is a single T1 timer, so we should be able to use
2542 * common function with the COOKIE-WAIT state.
2544 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2548 * Stop T1 timer and abort association with "INIT failed".
2550 * This is common code called by several sctp_sf_*_abort() functions above.
2552 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2553 sctp_cmd_seq_t *commands,
2554 __be16 error, int sk_err,
2555 const struct sctp_association *asoc,
2556 struct sctp_transport *transport)
2558 pr_debug("%s: ABORT received (INIT)\n", __func__);
2560 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2561 SCTP_STATE(SCTP_STATE_CLOSED));
2562 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2563 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2564 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2565 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2566 /* CMD_INIT_FAILED will DELETE_TCB. */
2567 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2570 return SCTP_DISPOSITION_ABORT;
2574 * sctp_sf_do_9_2_shut
2577 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2578 * - enter the SHUTDOWN-RECEIVED state,
2580 * - stop accepting new data from its SCTP user
2582 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2583 * that all its outstanding DATA chunks have been received by the
2586 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2587 * send a SHUTDOWN in response to a ULP request. And should discard
2588 * subsequent SHUTDOWN chunks.
2590 * If there are still outstanding DATA chunks left, the SHUTDOWN
2591 * receiver shall continue to follow normal data transmission
2592 * procedures defined in Section 6 until all outstanding DATA chunks
2593 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2594 * new data from its SCTP user.
2596 * Verification Tag: 8.5 Verification Tag [Normal verification]
2599 * (endpoint, asoc, chunk)
2602 * (asoc, reply_msg, msg_up, timers, counters)
2604 * The return value is the disposition of the chunk.
2606 sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2607 const struct sctp_endpoint *ep,
2608 const struct sctp_association *asoc,
2609 const sctp_subtype_t type,
2611 sctp_cmd_seq_t *commands)
2613 struct sctp_chunk *chunk = arg;
2614 sctp_shutdownhdr_t *sdh;
2615 sctp_disposition_t disposition;
2616 struct sctp_ulpevent *ev;
2619 if (!sctp_vtag_verify(chunk, asoc))
2620 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2622 /* Make sure that the SHUTDOWN chunk has a valid length. */
2623 if (!sctp_chunk_length_valid(chunk,
2624 sizeof(struct sctp_shutdown_chunk_t)))
2625 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2628 /* Convert the elaborate header. */
2629 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2630 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2631 chunk->subh.shutdown_hdr = sdh;
2632 ctsn = ntohl(sdh->cum_tsn_ack);
2634 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2635 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2636 asoc->ctsn_ack_point);
2638 return SCTP_DISPOSITION_DISCARD;
2641 /* If Cumulative TSN Ack beyond the max tsn currently
2642 * send, terminating the association and respond to the
2643 * sender with an ABORT.
2645 if (!TSN_lt(ctsn, asoc->next_tsn))
2646 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2648 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2649 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2650 * inform the application that it should cease sending data.
2652 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2654 disposition = SCTP_DISPOSITION_NOMEM;
2657 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2659 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2660 * - enter the SHUTDOWN-RECEIVED state,
2661 * - stop accepting new data from its SCTP user
2663 * [This is implicit in the new state.]
2665 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2666 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2667 disposition = SCTP_DISPOSITION_CONSUME;
2669 if (sctp_outq_is_empty(&asoc->outqueue)) {
2670 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2674 if (SCTP_DISPOSITION_NOMEM == disposition)
2677 /* - verify, by checking the Cumulative TSN Ack field of the
2678 * chunk, that all its outstanding DATA chunks have been
2679 * received by the SHUTDOWN sender.
2681 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2682 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2689 * sctp_sf_do_9_2_shut_ctsn
2691 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2692 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2693 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2694 * MUST be processed.
2696 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2697 const struct sctp_endpoint *ep,
2698 const struct sctp_association *asoc,
2699 const sctp_subtype_t type,
2701 sctp_cmd_seq_t *commands)
2703 struct sctp_chunk *chunk = arg;
2704 sctp_shutdownhdr_t *sdh;
2707 if (!sctp_vtag_verify(chunk, asoc))
2708 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2710 /* Make sure that the SHUTDOWN chunk has a valid length. */
2711 if (!sctp_chunk_length_valid(chunk,
2712 sizeof(struct sctp_shutdown_chunk_t)))
2713 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2716 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2717 ctsn = ntohl(sdh->cum_tsn_ack);
2719 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2720 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2721 asoc->ctsn_ack_point);
2723 return SCTP_DISPOSITION_DISCARD;
2726 /* If Cumulative TSN Ack beyond the max tsn currently
2727 * send, terminating the association and respond to the
2728 * sender with an ABORT.
2730 if (!TSN_lt(ctsn, asoc->next_tsn))
2731 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2733 /* verify, by checking the Cumulative TSN Ack field of the
2734 * chunk, that all its outstanding DATA chunks have been
2735 * received by the SHUTDOWN sender.
2737 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2738 SCTP_BE32(sdh->cum_tsn_ack));
2740 return SCTP_DISPOSITION_CONSUME;
2744 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2745 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2746 * transport addresses (either in the IP addresses or in the INIT chunk)
2747 * that belong to this association, it should discard the INIT chunk and
2748 * retransmit the SHUTDOWN ACK chunk.
2750 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2751 const struct sctp_endpoint *ep,
2752 const struct sctp_association *asoc,
2753 const sctp_subtype_t type,
2755 sctp_cmd_seq_t *commands)
2757 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2758 struct sctp_chunk *reply;
2760 /* Make sure that the chunk has a valid length */
2761 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2762 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2765 /* Since we are not going to really process this INIT, there
2766 * is no point in verifying chunk boundries. Just generate
2769 reply = sctp_make_shutdown_ack(asoc, chunk);
2773 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2774 * the T2-SHUTDOWN timer.
2776 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2778 /* and restart the T2-shutdown timer. */
2779 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2780 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2782 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2784 return SCTP_DISPOSITION_CONSUME;
2786 return SCTP_DISPOSITION_NOMEM;
2790 * sctp_sf_do_ecn_cwr
2792 * Section: Appendix A: Explicit Congestion Notification
2796 * RFC 2481 details a specific bit for a sender to send in the header of
2797 * its next outbound TCP segment to indicate to its peer that it has
2798 * reduced its congestion window. This is termed the CWR bit. For
2799 * SCTP the same indication is made by including the CWR chunk.
2800 * This chunk contains one data element, i.e. the TSN number that
2801 * was sent in the ECNE chunk. This element represents the lowest
2802 * TSN number in the datagram that was originally marked with the
2805 * Verification Tag: 8.5 Verification Tag [Normal verification]
2807 * (endpoint, asoc, chunk)
2810 * (asoc, reply_msg, msg_up, timers, counters)
2812 * The return value is the disposition of the chunk.
2814 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2815 const struct sctp_endpoint *ep,
2816 const struct sctp_association *asoc,
2817 const sctp_subtype_t type,
2819 sctp_cmd_seq_t *commands)
2822 struct sctp_chunk *chunk = arg;
2825 if (!sctp_vtag_verify(chunk, asoc))
2826 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2828 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2829 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2832 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2833 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2835 lowest_tsn = ntohl(cwr->lowest_tsn);
2837 /* Does this CWR ack the last sent congestion notification? */
2838 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2839 /* Stop sending ECNE. */
2840 sctp_add_cmd_sf(commands,
2842 SCTP_U32(lowest_tsn));
2844 return SCTP_DISPOSITION_CONSUME;
2850 * Section: Appendix A: Explicit Congestion Notification
2854 * RFC 2481 details a specific bit for a receiver to send back in its
2855 * TCP acknowledgements to notify the sender of the Congestion
2856 * Experienced (CE) bit having arrived from the network. For SCTP this
2857 * same indication is made by including the ECNE chunk. This chunk
2858 * contains one data element, i.e. the lowest TSN associated with the IP
2859 * datagram marked with the CE bit.....
2861 * Verification Tag: 8.5 Verification Tag [Normal verification]
2863 * (endpoint, asoc, chunk)
2866 * (asoc, reply_msg, msg_up, timers, counters)
2868 * The return value is the disposition of the chunk.
2870 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2871 const struct sctp_endpoint *ep,
2872 const struct sctp_association *asoc,
2873 const sctp_subtype_t type,
2875 sctp_cmd_seq_t *commands)
2877 sctp_ecnehdr_t *ecne;
2878 struct sctp_chunk *chunk = arg;
2880 if (!sctp_vtag_verify(chunk, asoc))
2881 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2883 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2884 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2887 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2888 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2890 /* If this is a newer ECNE than the last CWR packet we sent out */
2891 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2892 SCTP_U32(ntohl(ecne->lowest_tsn)));
2894 return SCTP_DISPOSITION_CONSUME;
2898 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2900 * The SCTP endpoint MUST always acknowledge the reception of each valid
2903 * The guidelines on delayed acknowledgement algorithm specified in
2904 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2905 * acknowledgement SHOULD be generated for at least every second packet
2906 * (not every second DATA chunk) received, and SHOULD be generated within
2907 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2908 * situations it may be beneficial for an SCTP transmitter to be more
2909 * conservative than the algorithms detailed in this document allow.
2910 * However, an SCTP transmitter MUST NOT be more aggressive than the
2911 * following algorithms allow.
2913 * A SCTP receiver MUST NOT generate more than one SACK for every
2914 * incoming packet, other than to update the offered window as the
2915 * receiving application consumes new data.
2917 * Verification Tag: 8.5 Verification Tag [Normal verification]
2920 * (endpoint, asoc, chunk)
2923 * (asoc, reply_msg, msg_up, timers, counters)
2925 * The return value is the disposition of the chunk.
2927 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2928 const struct sctp_endpoint *ep,
2929 const struct sctp_association *asoc,
2930 const sctp_subtype_t type,
2932 sctp_cmd_seq_t *commands)
2934 struct sctp_chunk *chunk = arg;
2935 sctp_arg_t force = SCTP_NOFORCE();
2938 if (!sctp_vtag_verify(chunk, asoc)) {
2939 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2941 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2944 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2945 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2948 error = sctp_eat_data(asoc, chunk, commands );
2950 case SCTP_IERROR_NO_ERROR:
2952 case SCTP_IERROR_HIGH_TSN:
2953 case SCTP_IERROR_BAD_STREAM:
2954 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2955 goto discard_noforce;
2956 case SCTP_IERROR_DUP_TSN:
2957 case SCTP_IERROR_IGNORE_TSN:
2958 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2960 case SCTP_IERROR_NO_DATA:
2962 case SCTP_IERROR_PROTO_VIOLATION:
2963 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
2964 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2969 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2970 force = SCTP_FORCE();
2972 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
2973 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2974 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2977 /* If this is the last chunk in a packet, we need to count it
2978 * toward sack generation. Note that we need to SACK every
2979 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2980 * THEM. We elect to NOT generate SACK's if the chunk fails
2981 * the verification tag test.
2983 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2985 * The SCTP endpoint MUST always acknowledge the reception of
2986 * each valid DATA chunk.
2988 * The guidelines on delayed acknowledgement algorithm
2989 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2990 * Specifically, an acknowledgement SHOULD be generated for at
2991 * least every second packet (not every second DATA chunk)
2992 * received, and SHOULD be generated within 200 ms of the
2993 * arrival of any unacknowledged DATA chunk. In some
2994 * situations it may be beneficial for an SCTP transmitter to
2995 * be more conservative than the algorithms detailed in this
2996 * document allow. However, an SCTP transmitter MUST NOT be
2997 * more aggressive than the following algorithms allow.
2999 if (chunk->end_of_packet)
3000 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3002 return SCTP_DISPOSITION_CONSUME;
3005 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3007 * When a packet arrives with duplicate DATA chunk(s) and with
3008 * no new DATA chunk(s), the endpoint MUST immediately send a
3009 * SACK with no delay. If a packet arrives with duplicate
3010 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3011 * MAY immediately send a SACK. Normally receipt of duplicate
3012 * DATA chunks will occur when the original SACK chunk was lost
3013 * and the peer's RTO has expired. The duplicate TSN number(s)
3014 * SHOULD be reported in the SACK as duplicate.
3016 /* In our case, we split the MAY SACK advice up whether or not
3017 * the last chunk is a duplicate.'
3019 if (chunk->end_of_packet)
3020 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3021 return SCTP_DISPOSITION_DISCARD;
3024 if (chunk->end_of_packet)
3025 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3027 return SCTP_DISPOSITION_DISCARD;
3029 return SCTP_DISPOSITION_CONSUME;
3034 * sctp_sf_eat_data_fast_4_4
3037 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3038 * DATA chunks without delay.
3040 * Verification Tag: 8.5 Verification Tag [Normal verification]
3042 * (endpoint, asoc, chunk)
3045 * (asoc, reply_msg, msg_up, timers, counters)
3047 * The return value is the disposition of the chunk.
3049 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3050 const struct sctp_endpoint *ep,
3051 const struct sctp_association *asoc,
3052 const sctp_subtype_t type,
3054 sctp_cmd_seq_t *commands)
3056 struct sctp_chunk *chunk = arg;
3059 if (!sctp_vtag_verify(chunk, asoc)) {
3060 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3062 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3065 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3066 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3069 error = sctp_eat_data(asoc, chunk, commands );
3071 case SCTP_IERROR_NO_ERROR:
3072 case SCTP_IERROR_HIGH_TSN:
3073 case SCTP_IERROR_DUP_TSN:
3074 case SCTP_IERROR_IGNORE_TSN:
3075 case SCTP_IERROR_BAD_STREAM:
3077 case SCTP_IERROR_NO_DATA:
3079 case SCTP_IERROR_PROTO_VIOLATION:
3080 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3081 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3086 /* Go a head and force a SACK, since we are shutting down. */
3088 /* Implementor's Guide.
3090 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3091 * respond to each received packet containing one or more DATA chunk(s)
3092 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3094 if (chunk->end_of_packet) {
3095 /* We must delay the chunk creation since the cumulative
3096 * TSN has not been updated yet.
3098 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3099 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3100 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3101 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3105 return SCTP_DISPOSITION_CONSUME;
3109 * Section: 6.2 Processing a Received SACK
3110 * D) Any time a SACK arrives, the endpoint performs the following:
3112 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3113 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3114 * increasing, a SACK whose Cumulative TSN Ack is less than the
3115 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3117 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3118 * of bytes still outstanding after processing the Cumulative TSN Ack
3119 * and the Gap Ack Blocks.
3121 * iii) If the SACK is missing a TSN that was previously
3122 * acknowledged via a Gap Ack Block (e.g., the data receiver
3123 * reneged on the data), then mark the corresponding DATA chunk
3124 * as available for retransmit: Mark it as missing for fast
3125 * retransmit as described in Section 7.2.4 and if no retransmit
3126 * timer is running for the destination address to which the DATA
3127 * chunk was originally transmitted, then T3-rtx is started for
3128 * that destination address.
3130 * Verification Tag: 8.5 Verification Tag [Normal verification]
3133 * (endpoint, asoc, chunk)
3136 * (asoc, reply_msg, msg_up, timers, counters)
3138 * The return value is the disposition of the chunk.
3140 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3141 const struct sctp_endpoint *ep,
3142 const struct sctp_association *asoc,
3143 const sctp_subtype_t type,
3145 sctp_cmd_seq_t *commands)
3147 struct sctp_chunk *chunk = arg;
3148 sctp_sackhdr_t *sackh;
3151 if (!sctp_vtag_verify(chunk, asoc))
3152 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3154 /* Make sure that the SACK chunk has a valid length. */
3155 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3156 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3159 /* Pull the SACK chunk from the data buffer */
3160 sackh = sctp_sm_pull_sack(chunk);
3161 /* Was this a bogus SACK? */
3163 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3164 chunk->subh.sack_hdr = sackh;
3165 ctsn = ntohl(sackh->cum_tsn_ack);
3167 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3168 * Ack Point, then drop the SACK. Since Cumulative TSN
3169 * Ack is monotonically increasing, a SACK whose
3170 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3171 * Point indicates an out-of-order SACK.
3173 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3174 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3175 asoc->ctsn_ack_point);
3177 return SCTP_DISPOSITION_DISCARD;
3180 /* If Cumulative TSN Ack beyond the max tsn currently
3181 * send, terminating the association and respond to the
3182 * sender with an ABORT.
3184 if (!TSN_lt(ctsn, asoc->next_tsn))
3185 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3187 /* Return this SACK for further processing. */
3188 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3190 /* Note: We do the rest of the work on the PROCESS_SACK
3193 return SCTP_DISPOSITION_CONSUME;
3197 * Generate an ABORT in response to a packet.
3199 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3201 * 8) The receiver should respond to the sender of the OOTB packet with
3202 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3203 * MUST fill in the Verification Tag field of the outbound packet
3204 * with the value found in the Verification Tag field of the OOTB
3205 * packet and set the T-bit in the Chunk Flags to indicate that the
3206 * Verification Tag is reflected. After sending this ABORT, the
3207 * receiver of the OOTB packet shall discard the OOTB packet and take
3208 * no further action.
3212 * The return value is the disposition of the chunk.
3214 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3215 const struct sctp_endpoint *ep,
3216 const struct sctp_association *asoc,
3217 const sctp_subtype_t type,
3219 sctp_cmd_seq_t *commands)
3221 struct sctp_packet *packet = NULL;
3222 struct sctp_chunk *chunk = arg;
3223 struct sctp_chunk *abort;
3225 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3228 /* Make an ABORT. The T bit will be set if the asoc
3231 abort = sctp_make_abort(asoc, chunk, 0);
3233 sctp_ootb_pkt_free(packet);
3234 return SCTP_DISPOSITION_NOMEM;
3237 /* Reflect vtag if T-Bit is set */
3238 if (sctp_test_T_bit(abort))
3239 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3241 /* Set the skb to the belonging sock for accounting. */
3242 abort->skb->sk = ep->base.sk;
3244 sctp_packet_append_chunk(packet, abort);
3246 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3247 SCTP_PACKET(packet));
3249 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3251 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3252 return SCTP_DISPOSITION_CONSUME;
3255 return SCTP_DISPOSITION_NOMEM;
3259 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3260 * event as ULP notification for each cause included in the chunk.
3262 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3264 * The return value is the disposition of the chunk.
3266 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3267 const struct sctp_endpoint *ep,
3268 const struct sctp_association *asoc,
3269 const sctp_subtype_t type,
3271 sctp_cmd_seq_t *commands)
3273 struct sctp_chunk *chunk = arg;
3276 if (!sctp_vtag_verify(chunk, asoc))
3277 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3279 /* Make sure that the ERROR chunk has a valid length. */
3280 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3281 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3283 sctp_walk_errors(err, chunk->chunk_hdr);
3284 if ((void *)err != (void *)chunk->chunk_end)
3285 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3286 (void *)err, commands);
3288 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3291 return SCTP_DISPOSITION_CONSUME;
3295 * Process an inbound SHUTDOWN ACK.
3298 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3299 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3300 * peer, and remove all record of the association.
3302 * The return value is the disposition.
3304 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3305 const struct sctp_endpoint *ep,
3306 const struct sctp_association *asoc,
3307 const sctp_subtype_t type,
3309 sctp_cmd_seq_t *commands)
3311 struct sctp_chunk *chunk = arg;
3312 struct sctp_chunk *reply;
3313 struct sctp_ulpevent *ev;
3315 if (!sctp_vtag_verify(chunk, asoc))
3316 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3318 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3319 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3320 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3322 /* 10.2 H) SHUTDOWN COMPLETE notification
3324 * When SCTP completes the shutdown procedures (section 9.2) this
3325 * notification is passed to the upper layer.
3327 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3328 0, 0, 0, NULL, GFP_ATOMIC);
3332 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3333 reply = sctp_make_shutdown_complete(asoc, chunk);
3337 /* Do all the commands now (after allocation), so that we
3338 * have consistent state if memory allocation failes
3340 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3342 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3343 * stop the T2-shutdown timer,
3345 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3346 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3348 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3349 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3351 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3352 SCTP_STATE(SCTP_STATE_CLOSED));
3353 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3354 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3355 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3357 /* ...and remove all record of the association. */
3358 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3359 return SCTP_DISPOSITION_DELETE_TCB;
3362 sctp_ulpevent_free(ev);
3364 return SCTP_DISPOSITION_NOMEM;
3368 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3370 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3371 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3372 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3373 * packet must fill in the Verification Tag field of the outbound
3374 * packet with the Verification Tag received in the SHUTDOWN ACK and
3375 * set the T-bit in the Chunk Flags to indicate that the Verification
3378 * 8) The receiver should respond to the sender of the OOTB packet with
3379 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3380 * MUST fill in the Verification Tag field of the outbound packet
3381 * with the value found in the Verification Tag field of the OOTB
3382 * packet and set the T-bit in the Chunk Flags to indicate that the
3383 * Verification Tag is reflected. After sending this ABORT, the
3384 * receiver of the OOTB packet shall discard the OOTB packet and take
3385 * no further action.
3387 sctp_disposition_t sctp_sf_ootb(struct net *net,
3388 const struct sctp_endpoint *ep,
3389 const struct sctp_association *asoc,
3390 const sctp_subtype_t type,
3392 sctp_cmd_seq_t *commands)
3394 struct sctp_chunk *chunk = arg;
3395 struct sk_buff *skb = chunk->skb;
3396 sctp_chunkhdr_t *ch;
3399 int ootb_shut_ack = 0;
3400 int ootb_cookie_ack = 0;
3402 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3404 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3406 /* Report violation if the chunk is less then minimal */
3407 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3408 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3411 /* Now that we know we at least have a chunk header,
3412 * do things that are type appropriate.
3414 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3417 /* RFC 2960, Section 3.3.7
3418 * Moreover, under any circumstances, an endpoint that
3419 * receives an ABORT MUST NOT respond to that ABORT by
3420 * sending an ABORT of its own.
3422 if (SCTP_CID_ABORT == ch->type)
3423 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3425 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3426 * or a COOKIE ACK the SCTP Packet should be silently
3430 if (SCTP_CID_COOKIE_ACK == ch->type)
3431 ootb_cookie_ack = 1;
3433 if (SCTP_CID_ERROR == ch->type) {
3434 sctp_walk_errors(err, ch) {
3435 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3436 ootb_cookie_ack = 1;
3442 /* Report violation if chunk len overflows */
3443 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3444 if (ch_end > skb_tail_pointer(skb))
3445 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3448 ch = (sctp_chunkhdr_t *) ch_end;
3449 } while (ch_end < skb_tail_pointer(skb));
3452 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3453 else if (ootb_cookie_ack)
3454 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3456 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3460 * Handle an "Out of the blue" SHUTDOWN ACK.
3462 * Section: 8.4 5, sctpimpguide 2.41.
3464 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3465 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3466 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3467 * packet must fill in the Verification Tag field of the outbound
3468 * packet with the Verification Tag received in the SHUTDOWN ACK and
3469 * set the T-bit in the Chunk Flags to indicate that the Verification
3473 * (endpoint, asoc, type, arg, commands)
3476 * (sctp_disposition_t)
3478 * The return value is the disposition of the chunk.
3480 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3481 const struct sctp_endpoint *ep,
3482 const struct sctp_association *asoc,
3483 const sctp_subtype_t type,
3485 sctp_cmd_seq_t *commands)
3487 struct sctp_packet *packet = NULL;
3488 struct sctp_chunk *chunk = arg;
3489 struct sctp_chunk *shut;
3491 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3494 /* Make an SHUTDOWN_COMPLETE.
3495 * The T bit will be set if the asoc is NULL.
3497 shut = sctp_make_shutdown_complete(asoc, chunk);
3499 sctp_ootb_pkt_free(packet);
3500 return SCTP_DISPOSITION_NOMEM;
3503 /* Reflect vtag if T-Bit is set */
3504 if (sctp_test_T_bit(shut))
3505 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3507 /* Set the skb to the belonging sock for accounting. */
3508 shut->skb->sk = ep->base.sk;
3510 sctp_packet_append_chunk(packet, shut);
3512 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3513 SCTP_PACKET(packet));
3515 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3517 /* If the chunk length is invalid, we don't want to process
3518 * the reset of the packet.
3520 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3521 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3523 /* We need to discard the rest of the packet to prevent
3524 * potential bomming attacks from additional bundled chunks.
3525 * This is documented in SCTP Threats ID.
3527 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3530 return SCTP_DISPOSITION_NOMEM;
3534 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3536 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3537 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3538 * procedures in section 8.4 SHOULD be followed, in other words it
3539 * should be treated as an Out Of The Blue packet.
3540 * [This means that we do NOT check the Verification Tag on these
3544 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3545 const struct sctp_endpoint *ep,
3546 const struct sctp_association *asoc,
3547 const sctp_subtype_t type,
3549 sctp_cmd_seq_t *commands)
3551 struct sctp_chunk *chunk = arg;
3553 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3554 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3555 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3558 /* Although we do have an association in this case, it corresponds
3559 * to a restarted association. So the packet is treated as an OOTB
3560 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3561 * called with a NULL association.
3563 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3565 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3568 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3569 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3570 const struct sctp_endpoint *ep,
3571 const struct sctp_association *asoc,
3572 const sctp_subtype_t type, void *arg,
3573 sctp_cmd_seq_t *commands)
3575 struct sctp_chunk *chunk = arg;
3576 struct sctp_chunk *asconf_ack = NULL;
3577 struct sctp_paramhdr *err_param = NULL;
3578 sctp_addiphdr_t *hdr;
3579 union sctp_addr_param *addr_param;
3583 if (!sctp_vtag_verify(chunk, asoc)) {
3584 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3586 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3589 /* ADD-IP: Section 4.1.1
3590 * This chunk MUST be sent in an authenticated way by using
3591 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3592 * is received unauthenticated it MUST be silently discarded as
3593 * described in [I-D.ietf-tsvwg-sctp-auth].
3595 if (!net->sctp.addip_noauth && !chunk->auth)
3596 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3598 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3599 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3600 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3603 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3604 serial = ntohl(hdr->serial);
3606 addr_param = (union sctp_addr_param *)hdr->params;
3607 length = ntohs(addr_param->p.length);
3608 if (length < sizeof(sctp_paramhdr_t))
3609 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3610 (void *)addr_param, commands);
3612 /* Verify the ASCONF chunk before processing it. */
3613 if (!sctp_verify_asconf(asoc,
3614 (sctp_paramhdr_t *)((void *)addr_param + length),
3615 (void *)chunk->chunk_end,
3617 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3618 (void *)err_param, commands);
3620 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3621 * the endpoint stored in a new association variable
3622 * 'Peer-Serial-Number'.
3624 if (serial == asoc->peer.addip_serial + 1) {
3625 /* If this is the first instance of ASCONF in the packet,
3626 * we can clean our old ASCONF-ACKs.
3628 if (!chunk->has_asconf)
3629 sctp_assoc_clean_asconf_ack_cache(asoc);
3631 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3632 * expected, process the ASCONF as described below and after
3633 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3634 * the response packet and cache a copy of it (in the event it
3635 * later needs to be retransmitted).
3637 * Essentially, do V1-V5.
3639 asconf_ack = sctp_process_asconf((struct sctp_association *)
3642 return SCTP_DISPOSITION_NOMEM;
3643 } else if (serial < asoc->peer.addip_serial + 1) {
3645 * If the value found in the Sequence Number is less than the
3646 * ('Peer- Sequence-Number' + 1), simply skip to the next
3647 * ASCONF, and include in the outbound response packet
3648 * any previously cached ASCONF-ACK response that was
3649 * sent and saved that matches the Sequence Number of the
3650 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3651 * Chunk exists. This will occur when an older ASCONF
3652 * arrives out of order. In such a case, the receiver
3653 * should skip the ASCONF Chunk and not include ASCONF-ACK
3654 * Chunk for that chunk.
3656 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3658 return SCTP_DISPOSITION_DISCARD;
3660 /* Reset the transport so that we select the correct one
3661 * this time around. This is to make sure that we don't
3662 * accidentally use a stale transport that's been removed.
3664 asconf_ack->transport = NULL;
3666 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3667 * it must be either a stale packet or from an attacker.
3669 return SCTP_DISPOSITION_DISCARD;
3672 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3673 * containing the ASCONF-ACK Chunks MUST be the source address of
3674 * the SCTP packet that held the ASCONF Chunks.
3676 * To do this properly, we'll set the destination address of the chunk
3677 * and at the transmit time, will try look up the transport to use.
3678 * Since ASCONFs may be bundled, the correct transport may not be
3679 * created until we process the entire packet, thus this workaround.
3681 asconf_ack->dest = chunk->source;
3682 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3683 if (asoc->new_transport) {
3684 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3686 ((struct sctp_association *)asoc)->new_transport = NULL;
3689 return SCTP_DISPOSITION_CONSUME;
3693 * ADDIP Section 4.3 General rules for address manipulation
3694 * When building TLV parameters for the ASCONF Chunk that will add or
3695 * delete IP addresses the D0 to D13 rules should be applied:
3697 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3698 const struct sctp_endpoint *ep,
3699 const struct sctp_association *asoc,
3700 const sctp_subtype_t type, void *arg,
3701 sctp_cmd_seq_t *commands)
3703 struct sctp_chunk *asconf_ack = arg;
3704 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3705 struct sctp_chunk *abort;
3706 struct sctp_paramhdr *err_param = NULL;
3707 sctp_addiphdr_t *addip_hdr;
3708 __u32 sent_serial, rcvd_serial;
3710 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3711 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3713 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3716 /* ADD-IP, Section 4.1.2:
3717 * This chunk MUST be sent in an authenticated way by using
3718 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3719 * is received unauthenticated it MUST be silently discarded as
3720 * described in [I-D.ietf-tsvwg-sctp-auth].
3722 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3723 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3725 /* Make sure that the ADDIP chunk has a valid length. */
3726 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3727 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3730 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3731 rcvd_serial = ntohl(addip_hdr->serial);
3733 /* Verify the ASCONF-ACK chunk before processing it. */
3734 if (!sctp_verify_asconf(asoc,
3735 (sctp_paramhdr_t *)addip_hdr->params,
3736 (void *)asconf_ack->chunk_end,
3738 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3739 (void *)err_param, commands);
3742 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3743 sent_serial = ntohl(addip_hdr->serial);
3745 sent_serial = asoc->addip_serial - 1;
3748 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3749 * equal to the next serial number to be used but no ASCONF chunk is
3750 * outstanding the endpoint MUST ABORT the association. Note that a
3751 * sequence number is greater than if it is no more than 2^^31-1
3752 * larger than the current sequence number (using serial arithmetic).
3754 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3755 !(asoc->addip_last_asconf)) {
3756 abort = sctp_make_abort(asoc, asconf_ack,
3757 sizeof(sctp_errhdr_t));
3759 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3763 /* We are going to ABORT, so we might as well stop
3764 * processing the rest of the chunks in the packet.
3766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3768 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3769 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3770 SCTP_ERROR(ECONNABORTED));
3771 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3772 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3773 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3774 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3775 return SCTP_DISPOSITION_ABORT;
3778 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3779 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3780 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3782 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3784 /* Successfully processed ASCONF_ACK. We can
3785 * release the next asconf if we have one.
3787 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3789 return SCTP_DISPOSITION_CONSUME;
3792 abort = sctp_make_abort(asoc, asconf_ack,
3793 sizeof(sctp_errhdr_t));
3795 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3796 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3799 /* We are going to ABORT, so we might as well stop
3800 * processing the rest of the chunks in the packet.
3802 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3803 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3804 SCTP_ERROR(ECONNABORTED));
3805 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3806 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3807 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3808 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3809 return SCTP_DISPOSITION_ABORT;
3812 return SCTP_DISPOSITION_DISCARD;
3816 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3818 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3819 * its cumulative TSN point to the value carried in the FORWARD TSN
3820 * chunk, and then MUST further advance its cumulative TSN point locally
3822 * After the above processing, the data receiver MUST stop reporting any
3823 * missing TSNs earlier than or equal to the new cumulative TSN point.
3825 * Verification Tag: 8.5 Verification Tag [Normal verification]
3827 * The return value is the disposition of the chunk.
3829 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3830 const struct sctp_endpoint *ep,
3831 const struct sctp_association *asoc,
3832 const sctp_subtype_t type,
3834 sctp_cmd_seq_t *commands)
3836 struct sctp_chunk *chunk = arg;
3837 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3838 struct sctp_fwdtsn_skip *skip;
3842 if (!sctp_vtag_verify(chunk, asoc)) {
3843 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3845 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3848 /* Make sure that the FORWARD_TSN chunk has valid length. */
3849 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3850 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3853 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3854 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3855 len = ntohs(chunk->chunk_hdr->length);
3856 len -= sizeof(struct sctp_chunkhdr);
3857 skb_pull(chunk->skb, len);
3859 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3860 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3862 /* The TSN is too high--silently discard the chunk and count on it
3863 * getting retransmitted later.
3865 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3866 goto discard_noforce;
3868 /* Silently discard the chunk if stream-id is not valid */
3869 sctp_walk_fwdtsn(skip, chunk) {
3870 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3871 goto discard_noforce;
3874 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3875 if (len > sizeof(struct sctp_fwdtsn_hdr))
3876 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3879 /* Count this as receiving DATA. */
3880 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3881 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3882 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3885 /* FIXME: For now send a SACK, but DATA processing may
3888 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3890 return SCTP_DISPOSITION_CONSUME;
3893 return SCTP_DISPOSITION_DISCARD;
3896 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3898 const struct sctp_endpoint *ep,
3899 const struct sctp_association *asoc,
3900 const sctp_subtype_t type,
3902 sctp_cmd_seq_t *commands)
3904 struct sctp_chunk *chunk = arg;
3905 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3906 struct sctp_fwdtsn_skip *skip;
3910 if (!sctp_vtag_verify(chunk, asoc)) {
3911 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3913 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3916 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3917 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3918 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3921 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3922 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3923 len = ntohs(chunk->chunk_hdr->length);
3924 len -= sizeof(struct sctp_chunkhdr);
3925 skb_pull(chunk->skb, len);
3927 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3928 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3930 /* The TSN is too high--silently discard the chunk and count on it
3931 * getting retransmitted later.
3933 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3936 /* Silently discard the chunk if stream-id is not valid */
3937 sctp_walk_fwdtsn(skip, chunk) {
3938 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3942 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3943 if (len > sizeof(struct sctp_fwdtsn_hdr))
3944 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3947 /* Go a head and force a SACK, since we are shutting down. */
3949 /* Implementor's Guide.
3951 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3952 * respond to each received packet containing one or more DATA chunk(s)
3953 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3955 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3956 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3957 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3958 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3960 return SCTP_DISPOSITION_CONSUME;
3964 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3966 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3967 * Identifier field. If this algorithm was not specified by the
3968 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3969 * during association setup, the AUTH chunk and all chunks after it MUST
3970 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3971 * defined in Section 4.1.
3973 * If an endpoint with no shared key receives a Shared Key Identifier
3974 * other than 0, it MUST silently discard all authenticated chunks. If
3975 * the endpoint has at least one endpoint pair shared key for the peer,
3976 * it MUST use the key specified by the Shared Key Identifier if a
3977 * key has been configured for that Shared Key Identifier. If no
3978 * endpoint pair shared key has been configured for that Shared Key
3979 * Identifier, all authenticated chunks MUST be silently discarded.
3981 * Verification Tag: 8.5 Verification Tag [Normal verification]
3983 * The return value is the disposition of the chunk.
3985 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3986 const struct sctp_endpoint *ep,
3987 const struct sctp_association *asoc,
3988 const sctp_subtype_t type,
3989 struct sctp_chunk *chunk)
3991 struct sctp_authhdr *auth_hdr;
3992 struct sctp_hmac *hmac;
3993 unsigned int sig_len;
3998 /* Pull in the auth header, so we can do some more verification */
3999 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4000 chunk->subh.auth_hdr = auth_hdr;
4001 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4003 /* Make sure that we support the HMAC algorithm from the auth
4006 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4007 return SCTP_IERROR_AUTH_BAD_HMAC;
4009 /* Make sure that the provided shared key identifier has been
4012 key_id = ntohs(auth_hdr->shkey_id);
4013 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4014 return SCTP_IERROR_AUTH_BAD_KEYID;
4017 /* Make sure that the length of the signature matches what
4020 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4021 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4022 if (sig_len != hmac->hmac_len)
4023 return SCTP_IERROR_PROTO_VIOLATION;
4025 /* Now that we've done validation checks, we can compute and
4026 * verify the hmac. The steps involved are:
4027 * 1. Save the digest from the chunk.
4028 * 2. Zero out the digest in the chunk.
4029 * 3. Compute the new digest
4030 * 4. Compare saved and new digests.
4032 digest = auth_hdr->hmac;
4033 skb_pull(chunk->skb, sig_len);
4035 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4039 memset(digest, 0, sig_len);
4041 sctp_auth_calculate_hmac(asoc, chunk->skb,
4042 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4045 /* Discard the packet if the digests do not match */
4046 if (memcmp(save_digest, digest, sig_len)) {
4048 return SCTP_IERROR_BAD_SIG;
4054 return SCTP_IERROR_NO_ERROR;
4056 return SCTP_IERROR_NOMEM;
4059 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4060 const struct sctp_endpoint *ep,
4061 const struct sctp_association *asoc,
4062 const sctp_subtype_t type,
4064 sctp_cmd_seq_t *commands)
4066 struct sctp_authhdr *auth_hdr;
4067 struct sctp_chunk *chunk = arg;
4068 struct sctp_chunk *err_chunk;
4069 sctp_ierror_t error;
4071 /* Make sure that the peer has AUTH capable */
4072 if (!asoc->peer.auth_capable)
4073 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4075 if (!sctp_vtag_verify(chunk, asoc)) {
4076 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4078 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4081 /* Make sure that the AUTH chunk has valid length. */
4082 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4083 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4086 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4087 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
4089 case SCTP_IERROR_AUTH_BAD_HMAC:
4090 /* Generate the ERROR chunk and discard the rest
4093 err_chunk = sctp_make_op_error(asoc, chunk,
4094 SCTP_ERROR_UNSUP_HMAC,
4098 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4099 SCTP_CHUNK(err_chunk));
4102 case SCTP_IERROR_AUTH_BAD_KEYID:
4103 case SCTP_IERROR_BAD_SIG:
4104 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4106 case SCTP_IERROR_PROTO_VIOLATION:
4107 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4110 case SCTP_IERROR_NOMEM:
4111 return SCTP_DISPOSITION_NOMEM;
4113 default: /* Prevent gcc warnings */
4117 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4118 struct sctp_ulpevent *ev;
4120 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4121 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4126 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4130 return SCTP_DISPOSITION_CONSUME;
4134 * Process an unknown chunk.
4136 * Section: 3.2. Also, 2.1 in the implementor's guide.
4138 * Chunk Types are encoded such that the highest-order two bits specify
4139 * the action that must be taken if the processing endpoint does not
4140 * recognize the Chunk Type.
4142 * 00 - Stop processing this SCTP packet and discard it, do not process
4143 * any further chunks within it.
4145 * 01 - Stop processing this SCTP packet and discard it, do not process
4146 * any further chunks within it, and report the unrecognized
4147 * chunk in an 'Unrecognized Chunk Type'.
4149 * 10 - Skip this chunk and continue processing.
4151 * 11 - Skip this chunk and continue processing, but report in an ERROR
4152 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4154 * The return value is the disposition of the chunk.
4156 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4157 const struct sctp_endpoint *ep,
4158 const struct sctp_association *asoc,
4159 const sctp_subtype_t type,
4161 sctp_cmd_seq_t *commands)
4163 struct sctp_chunk *unk_chunk = arg;
4164 struct sctp_chunk *err_chunk;
4165 sctp_chunkhdr_t *hdr;
4167 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4169 if (!sctp_vtag_verify(unk_chunk, asoc))
4170 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4172 /* Make sure that the chunk has a valid length.
4173 * Since we don't know the chunk type, we use a general
4174 * chunkhdr structure to make a comparison.
4176 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4177 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4180 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4181 case SCTP_CID_ACTION_DISCARD:
4182 /* Discard the packet. */
4183 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4185 case SCTP_CID_ACTION_DISCARD_ERR:
4186 /* Generate an ERROR chunk as response. */
4187 hdr = unk_chunk->chunk_hdr;
4188 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4189 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4190 WORD_ROUND(ntohs(hdr->length)),
4193 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4194 SCTP_CHUNK(err_chunk));
4197 /* Discard the packet. */
4198 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4199 return SCTP_DISPOSITION_CONSUME;
4201 case SCTP_CID_ACTION_SKIP:
4202 /* Skip the chunk. */
4203 return SCTP_DISPOSITION_DISCARD;
4205 case SCTP_CID_ACTION_SKIP_ERR:
4206 /* Generate an ERROR chunk as response. */
4207 hdr = unk_chunk->chunk_hdr;
4208 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4209 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4210 WORD_ROUND(ntohs(hdr->length)),
4213 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4214 SCTP_CHUNK(err_chunk));
4216 /* Skip the chunk. */
4217 return SCTP_DISPOSITION_CONSUME;
4223 return SCTP_DISPOSITION_DISCARD;
4227 * Discard the chunk.
4229 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4230 * [Too numerous to mention...]
4231 * Verification Tag: No verification needed.
4233 * (endpoint, asoc, chunk)
4236 * (asoc, reply_msg, msg_up, timers, counters)
4238 * The return value is the disposition of the chunk.
4240 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4241 const struct sctp_endpoint *ep,
4242 const struct sctp_association *asoc,
4243 const sctp_subtype_t type,
4245 sctp_cmd_seq_t *commands)
4247 struct sctp_chunk *chunk = arg;
4249 /* Make sure that the chunk has a valid length.
4250 * Since we don't know the chunk type, we use a general
4251 * chunkhdr structure to make a comparison.
4253 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4254 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4257 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4259 return SCTP_DISPOSITION_DISCARD;
4263 * Discard the whole packet.
4267 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4268 * silently discard the OOTB packet and take no further action.
4270 * Verification Tag: No verification necessary
4273 * (endpoint, asoc, chunk)
4276 * (asoc, reply_msg, msg_up, timers, counters)
4278 * The return value is the disposition of the chunk.
4280 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4281 const struct sctp_endpoint *ep,
4282 const struct sctp_association *asoc,
4283 const sctp_subtype_t type,
4285 sctp_cmd_seq_t *commands)
4287 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4288 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4290 return SCTP_DISPOSITION_CONSUME;
4295 * The other end is violating protocol.
4297 * Section: Not specified
4298 * Verification Tag: Not specified
4300 * (endpoint, asoc, chunk)
4303 * (asoc, reply_msg, msg_up, timers, counters)
4305 * We simply tag the chunk as a violation. The state machine will log
4306 * the violation and continue.
4308 sctp_disposition_t sctp_sf_violation(struct net *net,
4309 const struct sctp_endpoint *ep,
4310 const struct sctp_association *asoc,
4311 const sctp_subtype_t type,
4313 sctp_cmd_seq_t *commands)
4315 struct sctp_chunk *chunk = arg;
4317 /* Make sure that the chunk has a valid length. */
4318 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4319 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4322 return SCTP_DISPOSITION_VIOLATION;
4326 * Common function to handle a protocol violation.
4328 static sctp_disposition_t sctp_sf_abort_violation(
4330 const struct sctp_endpoint *ep,
4331 const struct sctp_association *asoc,
4333 sctp_cmd_seq_t *commands,
4334 const __u8 *payload,
4335 const size_t paylen)
4337 struct sctp_packet *packet = NULL;
4338 struct sctp_chunk *chunk = arg;
4339 struct sctp_chunk *abort = NULL;
4341 /* SCTP-AUTH, Section 6.3:
4342 * It should be noted that if the receiver wants to tear
4343 * down an association in an authenticated way only, the
4344 * handling of malformed packets should not result in
4345 * tearing down the association.
4347 * This means that if we only want to abort associations
4348 * in an authenticated way (i.e AUTH+ABORT), then we
4349 * can't destroy this association just because the packet
4352 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4355 /* Make the abort chunk. */
4356 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4361 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4362 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4363 !asoc->peer.i.init_tag) {
4364 sctp_initack_chunk_t *initack;
4366 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4367 if (!sctp_chunk_length_valid(chunk,
4368 sizeof(sctp_initack_chunk_t)))
4369 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4371 unsigned int inittag;
4373 inittag = ntohl(initack->init_hdr.init_tag);
4374 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4379 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4380 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4382 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4383 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4384 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4385 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4386 SCTP_ERROR(ECONNREFUSED));
4387 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4388 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4390 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4391 SCTP_ERROR(ECONNABORTED));
4392 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4393 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4394 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4397 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4402 if (sctp_test_T_bit(abort))
4403 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4405 abort->skb->sk = ep->base.sk;
4407 sctp_packet_append_chunk(packet, abort);
4409 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4410 SCTP_PACKET(packet));
4412 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4415 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4418 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4419 return SCTP_DISPOSITION_ABORT;
4422 sctp_chunk_free(abort);
4424 return SCTP_DISPOSITION_NOMEM;
4428 * Handle a protocol violation when the chunk length is invalid.
4429 * "Invalid" length is identified as smaller than the minimal length a
4430 * given chunk can be. For example, a SACK chunk has invalid length
4431 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4433 * We inform the other end by sending an ABORT with a Protocol Violation
4436 * Section: Not specified
4437 * Verification Tag: Nothing to do
4439 * (endpoint, asoc, chunk)
4442 * (reply_msg, msg_up, counters)
4444 * Generate an ABORT chunk and terminate the association.
4446 static sctp_disposition_t sctp_sf_violation_chunklen(
4448 const struct sctp_endpoint *ep,
4449 const struct sctp_association *asoc,
4450 const sctp_subtype_t type,
4452 sctp_cmd_seq_t *commands)
4454 static const char err_str[]="The following chunk had invalid length:";
4456 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4461 * Handle a protocol violation when the parameter length is invalid.
4462 * If the length is smaller than the minimum length of a given parameter,
4463 * or accumulated length in multi parameters exceeds the end of the chunk,
4464 * the length is considered as invalid.
4466 static sctp_disposition_t sctp_sf_violation_paramlen(
4468 const struct sctp_endpoint *ep,
4469 const struct sctp_association *asoc,
4470 const sctp_subtype_t type,
4471 void *arg, void *ext,
4472 sctp_cmd_seq_t *commands)
4474 struct sctp_chunk *chunk = arg;
4475 struct sctp_paramhdr *param = ext;
4476 struct sctp_chunk *abort = NULL;
4478 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4481 /* Make the abort chunk. */
4482 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4486 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4487 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4489 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4490 SCTP_ERROR(ECONNABORTED));
4491 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4492 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4493 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4494 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4497 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4498 return SCTP_DISPOSITION_ABORT;
4500 return SCTP_DISPOSITION_NOMEM;
4503 /* Handle a protocol violation when the peer trying to advance the
4504 * cumulative tsn ack to a point beyond the max tsn currently sent.
4506 * We inform the other end by sending an ABORT with a Protocol Violation
4509 static sctp_disposition_t sctp_sf_violation_ctsn(
4511 const struct sctp_endpoint *ep,
4512 const struct sctp_association *asoc,
4513 const sctp_subtype_t type,
4515 sctp_cmd_seq_t *commands)
4517 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4519 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4523 /* Handle protocol violation of an invalid chunk bundling. For example,
4524 * when we have an association and we receive bundled INIT-ACK, or
4525 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4526 * statement from the specs. Additionally, there might be an attacker
4527 * on the path and we may not want to continue this communication.
4529 static sctp_disposition_t sctp_sf_violation_chunk(
4531 const struct sctp_endpoint *ep,
4532 const struct sctp_association *asoc,
4533 const sctp_subtype_t type,
4535 sctp_cmd_seq_t *commands)
4537 static const char err_str[]="The following chunk violates protocol:";
4540 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4542 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4545 /***************************************************************************
4546 * These are the state functions for handling primitive (Section 10) events.
4547 ***************************************************************************/
4549 * sctp_sf_do_prm_asoc
4551 * Section: 10.1 ULP-to-SCTP
4554 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4555 * outbound stream count)
4556 * -> association id [,destination transport addr list] [,outbound stream
4559 * This primitive allows the upper layer to initiate an association to a
4560 * specific peer endpoint.
4562 * The peer endpoint shall be specified by one of the transport addresses
4563 * which defines the endpoint (see Section 1.4). If the local SCTP
4564 * instance has not been initialized, the ASSOCIATE is considered an
4566 * [This is not relevant for the kernel implementation since we do all
4567 * initialization at boot time. It we hadn't initialized we wouldn't
4568 * get anywhere near this code.]
4570 * An association id, which is a local handle to the SCTP association,
4571 * will be returned on successful establishment of the association. If
4572 * SCTP is not able to open an SCTP association with the peer endpoint,
4573 * an error is returned.
4574 * [In the kernel implementation, the struct sctp_association needs to
4575 * be created BEFORE causing this primitive to run.]
4577 * Other association parameters may be returned, including the
4578 * complete destination transport addresses of the peer as well as the
4579 * outbound stream count of the local endpoint. One of the transport
4580 * address from the returned destination addresses will be selected by
4581 * the local endpoint as default primary path for sending SCTP packets
4582 * to this peer. The returned "destination transport addr list" can
4583 * be used by the ULP to change the default primary path or to force
4584 * sending a packet to a specific transport address. [All of this
4585 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4588 * Mandatory attributes:
4590 * o local SCTP instance name - obtained from the INITIALIZE operation.
4591 * [This is the argument asoc.]
4592 * o destination transport addr - specified as one of the transport
4593 * addresses of the peer endpoint with which the association is to be
4595 * [This is asoc->peer.active_path.]
4596 * o outbound stream count - the number of outbound streams the ULP
4597 * would like to open towards this peer endpoint.
4598 * [BUG: This is not currently implemented.]
4599 * Optional attributes:
4603 * The return value is a disposition.
4605 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4606 const struct sctp_endpoint *ep,
4607 const struct sctp_association *asoc,
4608 const sctp_subtype_t type,
4610 sctp_cmd_seq_t *commands)
4612 struct sctp_chunk *repl;
4613 struct sctp_association* my_asoc;
4615 /* The comment below says that we enter COOKIE-WAIT AFTER
4616 * sending the INIT, but that doesn't actually work in our
4619 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4620 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4622 /* RFC 2960 5.1 Normal Establishment of an Association
4624 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4625 * must provide its Verification Tag (Tag_A) in the Initiate
4626 * Tag field. Tag_A SHOULD be a random number in the range of
4627 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4630 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4634 /* Choose transport for INIT. */
4635 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4638 /* Cast away the const modifier, as we want to just
4639 * rerun it through as a sideffect.
4641 my_asoc = (struct sctp_association *)asoc;
4642 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4644 /* After sending the INIT, "A" starts the T1-init timer and
4645 * enters the COOKIE-WAIT state.
4647 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4648 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4649 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4650 return SCTP_DISPOSITION_CONSUME;
4653 return SCTP_DISPOSITION_NOMEM;
4657 * Process the SEND primitive.
4659 * Section: 10.1 ULP-to-SCTP
4662 * Format: SEND(association id, buffer address, byte count [,context]
4663 * [,stream id] [,life time] [,destination transport address]
4664 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4667 * This is the main method to send user data via SCTP.
4669 * Mandatory attributes:
4671 * o association id - local handle to the SCTP association
4673 * o buffer address - the location where the user message to be
4674 * transmitted is stored;
4676 * o byte count - The size of the user data in number of bytes;
4678 * Optional attributes:
4680 * o context - an optional 32 bit integer that will be carried in the
4681 * sending failure notification to the ULP if the transportation of
4682 * this User Message fails.
4684 * o stream id - to indicate which stream to send the data on. If not
4685 * specified, stream 0 will be used.
4687 * o life time - specifies the life time of the user data. The user data
4688 * will not be sent by SCTP after the life time expires. This
4689 * parameter can be used to avoid efforts to transmit stale
4690 * user messages. SCTP notifies the ULP if the data cannot be
4691 * initiated to transport (i.e. sent to the destination via SCTP's
4692 * send primitive) within the life time variable. However, the
4693 * user data will be transmitted if SCTP has attempted to transmit a
4694 * chunk before the life time expired.
4696 * o destination transport address - specified as one of the destination
4697 * transport addresses of the peer endpoint to which this packet
4698 * should be sent. Whenever possible, SCTP should use this destination
4699 * transport address for sending the packets, instead of the current
4702 * o unorder flag - this flag, if present, indicates that the user
4703 * would like the data delivered in an unordered fashion to the peer
4704 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4707 * o no-bundle flag - instructs SCTP not to bundle this user data with
4708 * other outbound DATA chunks. SCTP MAY still bundle even when
4709 * this flag is present, when faced with network congestion.
4711 * o payload protocol-id - A 32 bit unsigned integer that is to be
4712 * passed to the peer indicating the type of payload protocol data
4713 * being transmitted. This value is passed as opaque data by SCTP.
4715 * The return value is the disposition.
4717 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4718 const struct sctp_endpoint *ep,
4719 const struct sctp_association *asoc,
4720 const sctp_subtype_t type,
4722 sctp_cmd_seq_t *commands)
4724 struct sctp_datamsg *msg = arg;
4726 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4727 return SCTP_DISPOSITION_CONSUME;
4731 * Process the SHUTDOWN primitive.
4736 * Format: SHUTDOWN(association id)
4739 * Gracefully closes an association. Any locally queued user data
4740 * will be delivered to the peer. The association will be terminated only
4741 * after the peer acknowledges all the SCTP packets sent. A success code
4742 * will be returned on successful termination of the association. If
4743 * attempting to terminate the association results in a failure, an error
4744 * code shall be returned.
4746 * Mandatory attributes:
4748 * o association id - local handle to the SCTP association
4750 * Optional attributes:
4754 * The return value is the disposition.
4756 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4758 const struct sctp_endpoint *ep,
4759 const struct sctp_association *asoc,
4760 const sctp_subtype_t type,
4762 sctp_cmd_seq_t *commands)
4766 /* From 9.2 Shutdown of an Association
4767 * Upon receipt of the SHUTDOWN primitive from its upper
4768 * layer, the endpoint enters SHUTDOWN-PENDING state and
4769 * remains there until all outstanding data has been
4770 * acknowledged by its peer. The endpoint accepts no new data
4771 * from its upper layer, but retransmits data to the far end
4772 * if necessary to fill gaps.
4774 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4775 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4777 disposition = SCTP_DISPOSITION_CONSUME;
4778 if (sctp_outq_is_empty(&asoc->outqueue)) {
4779 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4786 * Process the ABORT primitive.
4791 * Format: Abort(association id [, cause code])
4794 * Ungracefully closes an association. Any locally queued user data
4795 * will be discarded and an ABORT chunk is sent to the peer. A success code
4796 * will be returned on successful abortion of the association. If
4797 * attempting to abort the association results in a failure, an error
4798 * code shall be returned.
4800 * Mandatory attributes:
4802 * o association id - local handle to the SCTP association
4804 * Optional attributes:
4806 * o cause code - reason of the abort to be passed to the peer
4810 * The return value is the disposition.
4812 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4814 const struct sctp_endpoint *ep,
4815 const struct sctp_association *asoc,
4816 const sctp_subtype_t type,
4818 sctp_cmd_seq_t *commands)
4820 /* From 9.1 Abort of an Association
4821 * Upon receipt of the ABORT primitive from its upper
4822 * layer, the endpoint enters CLOSED state and
4823 * discard all outstanding data has been
4824 * acknowledged by its peer. The endpoint accepts no new data
4825 * from its upper layer, but retransmits data to the far end
4826 * if necessary to fill gaps.
4828 struct sctp_chunk *abort = arg;
4829 sctp_disposition_t retval;
4831 retval = SCTP_DISPOSITION_CONSUME;
4833 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4835 /* Even if we can't send the ABORT due to low memory delete the
4836 * TCB. This is a departure from our typical NOMEM handling.
4839 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4840 SCTP_ERROR(ECONNABORTED));
4841 /* Delete the established association. */
4842 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4843 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4845 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4846 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4851 /* We tried an illegal operation on an association which is closed. */
4852 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4853 const struct sctp_endpoint *ep,
4854 const struct sctp_association *asoc,
4855 const sctp_subtype_t type,
4857 sctp_cmd_seq_t *commands)
4859 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4860 return SCTP_DISPOSITION_CONSUME;
4863 /* We tried an illegal operation on an association which is shutting
4866 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4867 const struct sctp_endpoint *ep,
4868 const struct sctp_association *asoc,
4869 const sctp_subtype_t type,
4871 sctp_cmd_seq_t *commands)
4873 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4874 SCTP_ERROR(-ESHUTDOWN));
4875 return SCTP_DISPOSITION_CONSUME;
4879 * sctp_cookie_wait_prm_shutdown
4881 * Section: 4 Note: 2
4886 * The RFC does not explicitly address this issue, but is the route through the
4887 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4892 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4894 const struct sctp_endpoint *ep,
4895 const struct sctp_association *asoc,
4896 const sctp_subtype_t type,
4898 sctp_cmd_seq_t *commands)
4900 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4901 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4903 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4904 SCTP_STATE(SCTP_STATE_CLOSED));
4906 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4908 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4910 return SCTP_DISPOSITION_DELETE_TCB;
4914 * sctp_cookie_echoed_prm_shutdown
4916 * Section: 4 Note: 2
4921 * The RFC does not explcitly address this issue, but is the route through the
4922 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4927 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4929 const struct sctp_endpoint *ep,
4930 const struct sctp_association *asoc,
4931 const sctp_subtype_t type,
4932 void *arg, sctp_cmd_seq_t *commands)
4934 /* There is a single T1 timer, so we should be able to use
4935 * common function with the COOKIE-WAIT state.
4937 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4941 * sctp_sf_cookie_wait_prm_abort
4943 * Section: 4 Note: 2
4948 * The RFC does not explicitly address this issue, but is the route through the
4949 * state table when someone issues an abort while in COOKIE_WAIT state.
4954 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4956 const struct sctp_endpoint *ep,
4957 const struct sctp_association *asoc,
4958 const sctp_subtype_t type,
4960 sctp_cmd_seq_t *commands)
4962 struct sctp_chunk *abort = arg;
4963 sctp_disposition_t retval;
4965 /* Stop T1-init timer */
4966 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4967 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4968 retval = SCTP_DISPOSITION_CONSUME;
4970 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4972 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4973 SCTP_STATE(SCTP_STATE_CLOSED));
4975 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4977 /* Even if we can't send the ABORT due to low memory delete the
4978 * TCB. This is a departure from our typical NOMEM handling.
4981 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4982 SCTP_ERROR(ECONNREFUSED));
4983 /* Delete the established association. */
4984 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4985 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4991 * sctp_sf_cookie_echoed_prm_abort
4993 * Section: 4 Note: 3
4998 * The RFC does not explcitly address this issue, but is the route through the
4999 * state table when someone issues an abort while in COOKIE_ECHOED state.
5004 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5006 const struct sctp_endpoint *ep,
5007 const struct sctp_association *asoc,
5008 const sctp_subtype_t type,
5010 sctp_cmd_seq_t *commands)
5012 /* There is a single T1 timer, so we should be able to use
5013 * common function with the COOKIE-WAIT state.
5015 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5019 * sctp_sf_shutdown_pending_prm_abort
5024 * The RFC does not explicitly address this issue, but is the route through the
5025 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5030 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5032 const struct sctp_endpoint *ep,
5033 const struct sctp_association *asoc,
5034 const sctp_subtype_t type,
5036 sctp_cmd_seq_t *commands)
5038 /* Stop the T5-shutdown guard timer. */
5039 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5040 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5042 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5046 * sctp_sf_shutdown_sent_prm_abort
5051 * The RFC does not explicitly address this issue, but is the route through the
5052 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5057 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5059 const struct sctp_endpoint *ep,
5060 const struct sctp_association *asoc,
5061 const sctp_subtype_t type,
5063 sctp_cmd_seq_t *commands)
5065 /* Stop the T2-shutdown timer. */
5066 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5067 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5069 /* Stop the T5-shutdown guard timer. */
5070 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5071 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5073 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5077 * sctp_sf_cookie_echoed_prm_abort
5082 * The RFC does not explcitly address this issue, but is the route through the
5083 * state table when someone issues an abort while in COOKIE_ECHOED state.
5088 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5090 const struct sctp_endpoint *ep,
5091 const struct sctp_association *asoc,
5092 const sctp_subtype_t type,
5094 sctp_cmd_seq_t *commands)
5096 /* The same T2 timer, so we should be able to use
5097 * common function with the SHUTDOWN-SENT state.
5099 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5103 * Process the REQUESTHEARTBEAT primitive
5106 * J) Request Heartbeat
5108 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5112 * Instructs the local endpoint to perform a HeartBeat on the specified
5113 * destination transport address of the given association. The returned
5114 * result should indicate whether the transmission of the HEARTBEAT
5115 * chunk to the destination address is successful.
5117 * Mandatory attributes:
5119 * o association id - local handle to the SCTP association
5121 * o destination transport address - the transport address of the
5122 * association on which a heartbeat should be issued.
5124 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5126 const struct sctp_endpoint *ep,
5127 const struct sctp_association *asoc,
5128 const sctp_subtype_t type,
5130 sctp_cmd_seq_t *commands)
5132 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5133 (struct sctp_transport *)arg, commands))
5134 return SCTP_DISPOSITION_NOMEM;
5137 * RFC 2960 (bis), section 8.3
5139 * D) Request an on-demand HEARTBEAT on a specific destination
5140 * transport address of a given association.
5142 * The endpoint should increment the respective error counter of
5143 * the destination transport address each time a HEARTBEAT is sent
5144 * to that address and not acknowledged within one RTO.
5147 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5148 SCTP_TRANSPORT(arg));
5149 return SCTP_DISPOSITION_CONSUME;
5153 * ADDIP Section 4.1 ASCONF Chunk Procedures
5154 * When an endpoint has an ASCONF signaled change to be sent to the
5155 * remote endpoint it should do A1 to A9
5157 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5158 const struct sctp_endpoint *ep,
5159 const struct sctp_association *asoc,
5160 const sctp_subtype_t type,
5162 sctp_cmd_seq_t *commands)
5164 struct sctp_chunk *chunk = arg;
5166 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5167 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5168 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5169 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5170 return SCTP_DISPOSITION_CONSUME;
5174 * Ignore the primitive event
5176 * The return value is the disposition of the primitive.
5178 sctp_disposition_t sctp_sf_ignore_primitive(
5180 const struct sctp_endpoint *ep,
5181 const struct sctp_association *asoc,
5182 const sctp_subtype_t type,
5184 sctp_cmd_seq_t *commands)
5186 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5189 return SCTP_DISPOSITION_DISCARD;
5192 /***************************************************************************
5193 * These are the state functions for the OTHER events.
5194 ***************************************************************************/
5197 * When the SCTP stack has no more user data to send or retransmit, this
5198 * notification is given to the user. Also, at the time when a user app
5199 * subscribes to this event, if there is no data to be sent or
5200 * retransmit, the stack will immediately send up this notification.
5202 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5204 const struct sctp_endpoint *ep,
5205 const struct sctp_association *asoc,
5206 const sctp_subtype_t type,
5208 sctp_cmd_seq_t *commands)
5210 struct sctp_ulpevent *event;
5212 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5214 return SCTP_DISPOSITION_NOMEM;
5216 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5218 return SCTP_DISPOSITION_CONSUME;
5222 * Start the shutdown negotiation.
5225 * Once all its outstanding data has been acknowledged, the endpoint
5226 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5227 * TSN Ack field the last sequential TSN it has received from the peer.
5228 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5229 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5230 * with the updated last sequential TSN received from its peer.
5232 * The return value is the disposition.
5234 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5236 const struct sctp_endpoint *ep,
5237 const struct sctp_association *asoc,
5238 const sctp_subtype_t type,
5240 sctp_cmd_seq_t *commands)
5242 struct sctp_chunk *reply;
5244 /* Once all its outstanding data has been acknowledged, the
5245 * endpoint shall send a SHUTDOWN chunk to its peer including
5246 * in the Cumulative TSN Ack field the last sequential TSN it
5247 * has received from the peer.
5249 reply = sctp_make_shutdown(asoc, NULL);
5253 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5254 * T2-shutdown timer.
5256 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5258 /* It shall then start the T2-shutdown timer */
5259 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5260 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5262 /* RFC 4960 Section 9.2
5263 * The sender of the SHUTDOWN MAY also start an overall guard timer
5264 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5267 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5269 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5270 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5271 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5273 /* and enter the SHUTDOWN-SENT state. */
5274 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5275 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5277 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5279 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5282 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5284 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5286 return SCTP_DISPOSITION_CONSUME;
5289 return SCTP_DISPOSITION_NOMEM;
5293 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5297 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5298 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5299 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5300 * endpoint must re-send the SHUTDOWN ACK.
5302 * The return value is the disposition.
5304 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5306 const struct sctp_endpoint *ep,
5307 const struct sctp_association *asoc,
5308 const sctp_subtype_t type,
5310 sctp_cmd_seq_t *commands)
5312 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5313 struct sctp_chunk *reply;
5315 /* There are 2 ways of getting here:
5316 * 1) called in response to a SHUTDOWN chunk
5317 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5319 * For the case (2), the arg parameter is set to NULL. We need
5320 * to check that we have a chunk before accessing it's fields.
5323 if (!sctp_vtag_verify(chunk, asoc))
5324 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5326 /* Make sure that the SHUTDOWN chunk has a valid length. */
5327 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5328 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5332 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5333 * shall send a SHUTDOWN ACK ...
5335 reply = sctp_make_shutdown_ack(asoc, chunk);
5339 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5340 * the T2-shutdown timer.
5342 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5344 /* and start/restart a T2-shutdown timer of its own, */
5345 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5346 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5348 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5349 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5350 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5352 /* Enter the SHUTDOWN-ACK-SENT state. */
5353 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5354 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5356 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5358 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5361 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5363 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5365 return SCTP_DISPOSITION_CONSUME;
5368 return SCTP_DISPOSITION_NOMEM;
5372 * Ignore the event defined as other
5374 * The return value is the disposition of the event.
5376 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5377 const struct sctp_endpoint *ep,
5378 const struct sctp_association *asoc,
5379 const sctp_subtype_t type,
5381 sctp_cmd_seq_t *commands)
5383 pr_debug("%s: the event other type:%d is ignored\n",
5384 __func__, type.other);
5386 return SCTP_DISPOSITION_DISCARD;
5389 /************************************************************
5390 * These are the state functions for handling timeout events.
5391 ************************************************************/
5396 * Section: 6.3.3 Handle T3-rtx Expiration
5398 * Whenever the retransmission timer T3-rtx expires for a destination
5399 * address, do the following:
5402 * The return value is the disposition of the chunk.
5404 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5405 const struct sctp_endpoint *ep,
5406 const struct sctp_association *asoc,
5407 const sctp_subtype_t type,
5409 sctp_cmd_seq_t *commands)
5411 struct sctp_transport *transport = arg;
5413 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5415 if (asoc->overall_error_count >= asoc->max_retrans) {
5416 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5418 * We are here likely because the receiver had its rwnd
5419 * closed for a while and we have not been able to
5420 * transmit the locally queued data within the maximum
5421 * retransmission attempts limit. Start the T5
5422 * shutdown guard timer to give the receiver one last
5423 * chance and some additional time to recover before
5426 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5427 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5429 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5430 SCTP_ERROR(ETIMEDOUT));
5431 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5432 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5433 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5434 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5435 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5436 return SCTP_DISPOSITION_DELETE_TCB;
5440 /* E1) For the destination address for which the timer
5441 * expires, adjust its ssthresh with rules defined in Section
5442 * 7.2.3 and set the cwnd <- MTU.
5445 /* E2) For the destination address for which the timer
5446 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5447 * maximum value discussed in rule C7 above (RTO.max) may be
5448 * used to provide an upper bound to this doubling operation.
5451 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5452 * outstanding DATA chunks for the address for which the
5453 * T3-rtx has expired will fit into a single packet, subject
5454 * to the MTU constraint for the path corresponding to the
5455 * destination transport address to which the retransmission
5456 * is being sent (this may be different from the address for
5457 * which the timer expires [see Section 6.4]). Call this
5458 * value K. Bundle and retransmit those K DATA chunks in a
5459 * single packet to the destination endpoint.
5461 * Note: Any DATA chunks that were sent to the address for
5462 * which the T3-rtx timer expired but did not fit in one MTU
5463 * (rule E3 above), should be marked for retransmission and
5464 * sent as soon as cwnd allows (normally when a SACK arrives).
5467 /* Do some failure management (Section 8.2). */
5468 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5470 /* NB: Rules E4 and F1 are implicit in R1. */
5471 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5473 return SCTP_DISPOSITION_CONSUME;
5477 * Generate delayed SACK on timeout
5479 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5481 * The guidelines on delayed acknowledgement algorithm specified in
5482 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5483 * acknowledgement SHOULD be generated for at least every second packet
5484 * (not every second DATA chunk) received, and SHOULD be generated
5485 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5486 * some situations it may be beneficial for an SCTP transmitter to be
5487 * more conservative than the algorithms detailed in this document
5488 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5489 * the following algorithms allow.
5491 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5492 const struct sctp_endpoint *ep,
5493 const struct sctp_association *asoc,
5494 const sctp_subtype_t type,
5496 sctp_cmd_seq_t *commands)
5498 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5499 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5500 return SCTP_DISPOSITION_CONSUME;
5504 * sctp_sf_t1_init_timer_expire
5506 * Section: 4 Note: 2
5511 * RFC 2960 Section 4 Notes
5512 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5513 * and re-start the T1-init timer without changing state. This MUST
5514 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5515 * endpoint MUST abort the initialization process and report the
5516 * error to SCTP user.
5522 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5523 const struct sctp_endpoint *ep,
5524 const struct sctp_association *asoc,
5525 const sctp_subtype_t type,
5527 sctp_cmd_seq_t *commands)
5529 struct sctp_chunk *repl = NULL;
5530 struct sctp_bind_addr *bp;
5531 int attempts = asoc->init_err_counter + 1;
5533 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5535 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5537 if (attempts <= asoc->max_init_attempts) {
5538 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5539 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5541 return SCTP_DISPOSITION_NOMEM;
5543 /* Choose transport for INIT. */
5544 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5547 /* Issue a sideeffect to do the needed accounting. */
5548 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5549 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5551 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5553 pr_debug("%s: giving up on INIT, attempts:%d "
5554 "max_init_attempts:%d\n", __func__, attempts,
5555 asoc->max_init_attempts);
5557 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5558 SCTP_ERROR(ETIMEDOUT));
5559 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5560 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5561 return SCTP_DISPOSITION_DELETE_TCB;
5564 return SCTP_DISPOSITION_CONSUME;
5568 * sctp_sf_t1_cookie_timer_expire
5570 * Section: 4 Note: 2
5575 * RFC 2960 Section 4 Notes
5576 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5577 * COOKIE ECHO and re-start the T1-cookie timer without changing
5578 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5579 * After that, the endpoint MUST abort the initialization process and
5580 * report the error to SCTP user.
5586 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5587 const struct sctp_endpoint *ep,
5588 const struct sctp_association *asoc,
5589 const sctp_subtype_t type,
5591 sctp_cmd_seq_t *commands)
5593 struct sctp_chunk *repl = NULL;
5594 int attempts = asoc->init_err_counter + 1;
5596 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5598 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5600 if (attempts <= asoc->max_init_attempts) {
5601 repl = sctp_make_cookie_echo(asoc, NULL);
5603 return SCTP_DISPOSITION_NOMEM;
5605 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5607 /* Issue a sideeffect to do the needed accounting. */
5608 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5609 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5611 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5613 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5614 SCTP_ERROR(ETIMEDOUT));
5615 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5616 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5617 return SCTP_DISPOSITION_DELETE_TCB;
5620 return SCTP_DISPOSITION_CONSUME;
5623 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5624 * with the updated last sequential TSN received from its peer.
5626 * An endpoint should limit the number of retransmissions of the
5627 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5628 * If this threshold is exceeded the endpoint should destroy the TCB and
5629 * MUST report the peer endpoint unreachable to the upper layer (and
5630 * thus the association enters the CLOSED state). The reception of any
5631 * packet from its peer (i.e. as the peer sends all of its queued DATA
5632 * chunks) should clear the endpoint's retransmission count and restart
5633 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5634 * all of its queued DATA chunks that have not yet been sent.
5636 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5637 const struct sctp_endpoint *ep,
5638 const struct sctp_association *asoc,
5639 const sctp_subtype_t type,
5641 sctp_cmd_seq_t *commands)
5643 struct sctp_chunk *reply = NULL;
5645 pr_debug("%s: timer T2 expired\n", __func__);
5647 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5649 ((struct sctp_association *)asoc)->shutdown_retries++;
5651 if (asoc->overall_error_count >= asoc->max_retrans) {
5652 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5653 SCTP_ERROR(ETIMEDOUT));
5654 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5655 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5656 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5657 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5658 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5659 return SCTP_DISPOSITION_DELETE_TCB;
5662 switch (asoc->state) {
5663 case SCTP_STATE_SHUTDOWN_SENT:
5664 reply = sctp_make_shutdown(asoc, NULL);
5667 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5668 reply = sctp_make_shutdown_ack(asoc, NULL);
5679 /* Do some failure management (Section 8.2).
5680 * If we remove the transport an SHUTDOWN was last sent to, don't
5681 * do failure management.
5683 if (asoc->shutdown_last_sent_to)
5684 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5685 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5687 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5688 * the T2-shutdown timer.
5690 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5692 /* Restart the T2-shutdown timer. */
5693 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5694 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5695 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5696 return SCTP_DISPOSITION_CONSUME;
5699 return SCTP_DISPOSITION_NOMEM;
5703 * ADDIP Section 4.1 ASCONF CHunk Procedures
5704 * If the T4 RTO timer expires the endpoint should do B1 to B5
5706 sctp_disposition_t sctp_sf_t4_timer_expire(
5708 const struct sctp_endpoint *ep,
5709 const struct sctp_association *asoc,
5710 const sctp_subtype_t type,
5712 sctp_cmd_seq_t *commands)
5714 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5715 struct sctp_transport *transport = chunk->transport;
5717 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5719 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5720 * detection on the appropriate destination address as defined in
5721 * RFC2960 [5] section 8.1 and 8.2.
5724 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5725 SCTP_TRANSPORT(transport));
5727 /* Reconfig T4 timer and transport. */
5728 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5730 /* ADDIP 4.1 B2) Increment the association error counters and perform
5731 * endpoint failure detection on the association as defined in
5732 * RFC2960 [5] section 8.1 and 8.2.
5733 * association error counter is incremented in SCTP_CMD_STRIKE.
5735 if (asoc->overall_error_count >= asoc->max_retrans) {
5736 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5737 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5738 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5739 SCTP_ERROR(ETIMEDOUT));
5740 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5741 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5742 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5743 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5744 return SCTP_DISPOSITION_ABORT;
5747 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5748 * the ASCONF chunk was sent by doubling the RTO timer value.
5749 * This is done in SCTP_CMD_STRIKE.
5752 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5753 * choose an alternate destination address (please refer to RFC2960
5754 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5755 * chunk, it MUST be the same (including its serial number) as the last
5758 sctp_chunk_hold(asoc->addip_last_asconf);
5759 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5760 SCTP_CHUNK(asoc->addip_last_asconf));
5762 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5763 * destination is selected, then the RTO used will be that of the new
5764 * destination address.
5766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5769 return SCTP_DISPOSITION_CONSUME;
5772 /* sctpimpguide-05 Section 2.12.2
5773 * The sender of the SHUTDOWN MAY also start an overall guard timer
5774 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5775 * At the expiration of this timer the sender SHOULD abort the association
5776 * by sending an ABORT chunk.
5778 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5779 const struct sctp_endpoint *ep,
5780 const struct sctp_association *asoc,
5781 const sctp_subtype_t type,
5783 sctp_cmd_seq_t *commands)
5785 struct sctp_chunk *reply = NULL;
5787 pr_debug("%s: timer T5 expired\n", __func__);
5789 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5791 reply = sctp_make_abort(asoc, NULL, 0);
5795 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5796 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5797 SCTP_ERROR(ETIMEDOUT));
5798 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5799 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5801 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5802 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5804 return SCTP_DISPOSITION_DELETE_TCB;
5806 return SCTP_DISPOSITION_NOMEM;
5809 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5810 * the association is automatically closed by starting the shutdown process.
5811 * The work that needs to be done is same as when SHUTDOWN is initiated by
5812 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5814 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5816 const struct sctp_endpoint *ep,
5817 const struct sctp_association *asoc,
5818 const sctp_subtype_t type,
5820 sctp_cmd_seq_t *commands)
5824 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5826 /* From 9.2 Shutdown of an Association
5827 * Upon receipt of the SHUTDOWN primitive from its upper
5828 * layer, the endpoint enters SHUTDOWN-PENDING state and
5829 * remains there until all outstanding data has been
5830 * acknowledged by its peer. The endpoint accepts no new data
5831 * from its upper layer, but retransmits data to the far end
5832 * if necessary to fill gaps.
5834 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5835 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5837 disposition = SCTP_DISPOSITION_CONSUME;
5838 if (sctp_outq_is_empty(&asoc->outqueue)) {
5839 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5845 /*****************************************************************************
5846 * These are sa state functions which could apply to all types of events.
5847 ****************************************************************************/
5850 * This table entry is not implemented.
5853 * (endpoint, asoc, chunk)
5855 * The return value is the disposition of the chunk.
5857 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5858 const struct sctp_endpoint *ep,
5859 const struct sctp_association *asoc,
5860 const sctp_subtype_t type,
5862 sctp_cmd_seq_t *commands)
5864 return SCTP_DISPOSITION_NOT_IMPL;
5868 * This table entry represents a bug.
5871 * (endpoint, asoc, chunk)
5873 * The return value is the disposition of the chunk.
5875 sctp_disposition_t sctp_sf_bug(struct net *net,
5876 const struct sctp_endpoint *ep,
5877 const struct sctp_association *asoc,
5878 const sctp_subtype_t type,
5880 sctp_cmd_seq_t *commands)
5882 return SCTP_DISPOSITION_BUG;
5886 * This table entry represents the firing of a timer in the wrong state.
5887 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5888 * when the association is in the wrong state. This event should
5889 * be ignored, so as to prevent any rearming of the timer.
5892 * (endpoint, asoc, chunk)
5894 * The return value is the disposition of the chunk.
5896 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5897 const struct sctp_endpoint *ep,
5898 const struct sctp_association *asoc,
5899 const sctp_subtype_t type,
5901 sctp_cmd_seq_t *commands)
5903 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5905 return SCTP_DISPOSITION_CONSUME;
5908 /********************************************************************
5909 * 2nd Level Abstractions
5910 ********************************************************************/
5912 /* Pull the SACK chunk based on the SACK header. */
5913 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5915 struct sctp_sackhdr *sack;
5920 /* Protect ourselves from reading too far into
5921 * the skb from a bogus sender.
5923 sack = (struct sctp_sackhdr *) chunk->skb->data;
5925 num_blocks = ntohs(sack->num_gap_ack_blocks);
5926 num_dup_tsns = ntohs(sack->num_dup_tsns);
5927 len = sizeof(struct sctp_sackhdr);
5928 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5929 if (len > chunk->skb->len)
5932 skb_pull(chunk->skb, len);
5937 /* Create an ABORT packet to be sent as a response, with the specified
5940 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5941 const struct sctp_endpoint *ep,
5942 const struct sctp_association *asoc,
5943 struct sctp_chunk *chunk,
5944 const void *payload,
5947 struct sctp_packet *packet;
5948 struct sctp_chunk *abort;
5950 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5954 * The T bit will be set if the asoc is NULL.
5956 abort = sctp_make_abort(asoc, chunk, paylen);
5958 sctp_ootb_pkt_free(packet);
5962 /* Reflect vtag if T-Bit is set */
5963 if (sctp_test_T_bit(abort))
5964 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5966 /* Add specified error causes, i.e., payload, to the
5969 sctp_addto_chunk(abort, paylen, payload);
5971 /* Set the skb to the belonging sock for accounting. */
5972 abort->skb->sk = ep->base.sk;
5974 sctp_packet_append_chunk(packet, abort);
5981 /* Allocate a packet for responding in the OOTB conditions. */
5982 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5983 const struct sctp_association *asoc,
5984 const struct sctp_chunk *chunk)
5986 struct sctp_packet *packet;
5987 struct sctp_transport *transport;
5992 /* Get the source and destination port from the inbound packet. */
5993 sport = ntohs(chunk->sctp_hdr->dest);
5994 dport = ntohs(chunk->sctp_hdr->source);
5996 /* The V-tag is going to be the same as the inbound packet if no
5997 * association exists, otherwise, use the peer's vtag.
6000 /* Special case the INIT-ACK as there is no peer's vtag
6003 switch(chunk->chunk_hdr->type) {
6004 case SCTP_CID_INIT_ACK:
6006 sctp_initack_chunk_t *initack;
6008 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6009 vtag = ntohl(initack->init_hdr.init_tag);
6013 vtag = asoc->peer.i.init_tag;
6017 /* Special case the INIT and stale COOKIE_ECHO as there is no
6020 switch(chunk->chunk_hdr->type) {
6023 sctp_init_chunk_t *init;
6025 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6026 vtag = ntohl(init->init_hdr.init_tag);
6030 vtag = ntohl(chunk->sctp_hdr->vtag);
6035 /* Make a transport for the bucket, Eliza... */
6036 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6040 /* Cache a route for the transport with the chunk's destination as
6041 * the source address.
6043 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6044 sctp_sk(net->sctp.ctl_sock));
6046 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6047 packet = sctp_packet_config(packet, vtag, 0);
6055 /* Free the packet allocated earlier for responding in the OOTB condition. */
6056 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6058 sctp_transport_free(packet->transport);
6061 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6062 static void sctp_send_stale_cookie_err(struct net *net,
6063 const struct sctp_endpoint *ep,
6064 const struct sctp_association *asoc,
6065 const struct sctp_chunk *chunk,
6066 sctp_cmd_seq_t *commands,
6067 struct sctp_chunk *err_chunk)
6069 struct sctp_packet *packet;
6072 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6074 struct sctp_signed_cookie *cookie;
6076 /* Override the OOTB vtag from the cookie. */
6077 cookie = chunk->subh.cookie_hdr;
6078 packet->vtag = cookie->c.peer_vtag;
6080 /* Set the skb to the belonging sock for accounting. */
6081 err_chunk->skb->sk = ep->base.sk;
6082 sctp_packet_append_chunk(packet, err_chunk);
6083 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6084 SCTP_PACKET(packet));
6085 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6087 sctp_chunk_free (err_chunk);
6092 /* Process a data chunk */
6093 static int sctp_eat_data(const struct sctp_association *asoc,
6094 struct sctp_chunk *chunk,
6095 sctp_cmd_seq_t *commands)
6097 sctp_datahdr_t *data_hdr;
6098 struct sctp_chunk *err;
6100 sctp_verb_t deliver;
6103 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6104 struct sock *sk = asoc->base.sk;
6105 struct net *net = sock_net(sk);
6110 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6111 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6113 tsn = ntohl(data_hdr->tsn);
6114 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6116 /* ASSERT: Now skb->data is really the user data. */
6118 /* Process ECN based congestion.
6120 * Since the chunk structure is reused for all chunks within
6121 * a packet, we use ecn_ce_done to track if we've already
6122 * done CE processing for this packet.
6124 * We need to do ECN processing even if we plan to discard the
6128 if (!chunk->ecn_ce_done) {
6130 chunk->ecn_ce_done = 1;
6132 af = sctp_get_af_specific(
6133 ipver2af(ip_hdr(chunk->skb)->version));
6135 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6136 /* Do real work as sideffect. */
6137 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6142 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6144 /* The TSN is too high--silently discard the chunk and
6145 * count on it getting retransmitted later.
6148 chunk->asoc->stats.outofseqtsns++;
6149 return SCTP_IERROR_HIGH_TSN;
6150 } else if (tmp > 0) {
6151 /* This is a duplicate. Record it. */
6152 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6153 return SCTP_IERROR_DUP_TSN;
6156 /* This is a new TSN. */
6158 /* Discard if there is no room in the receive window.
6159 * Actually, allow a little bit of overflow (up to a MTU).
6161 datalen = ntohs(chunk->chunk_hdr->length);
6162 datalen -= sizeof(sctp_data_chunk_t);
6164 deliver = SCTP_CMD_CHUNK_ULP;
6166 /* Think about partial delivery. */
6167 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6169 /* Even if we don't accept this chunk there is
6172 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6175 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6176 * seems a bit troublesome in that frag_point varies based on
6177 * PMTU. In cases, such as loopback, this might be a rather
6180 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6181 (datalen > asoc->rwnd + asoc->frag_point))) {
6183 /* If this is the next TSN, consider reneging to make
6184 * room. Note: Playing nice with a confused sender. A
6185 * malicious sender can still eat up all our buffer
6186 * space and in the future we may want to detect and
6187 * do more drastic reneging.
6189 if (sctp_tsnmap_has_gap(map) &&
6190 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6191 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6192 deliver = SCTP_CMD_RENEGE;
6194 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6195 __func__, tsn, datalen, asoc->rwnd);
6197 return SCTP_IERROR_IGNORE_TSN;
6202 * Also try to renege to limit our memory usage in the event that
6203 * we are under memory pressure
6204 * If we can't renege, don't worry about it, the sk_rmem_schedule
6205 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6206 * memory usage too much
6208 if (*sk->sk_prot_creator->memory_pressure) {
6209 if (sctp_tsnmap_has_gap(map) &&
6210 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6211 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6213 deliver = SCTP_CMD_RENEGE;
6218 * Section 3.3.10.9 No User Data (9)
6222 * No User Data: This error cause is returned to the originator of a
6223 * DATA chunk if a received DATA chunk has no user data.
6225 if (unlikely(0 == datalen)) {
6226 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6228 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6231 /* We are going to ABORT, so we might as well stop
6232 * processing the rest of the chunks in the packet.
6234 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6235 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6236 SCTP_ERROR(ECONNABORTED));
6237 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6238 SCTP_PERR(SCTP_ERROR_NO_DATA));
6239 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6240 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6241 return SCTP_IERROR_NO_DATA;
6244 chunk->data_accepted = 1;
6246 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6247 * if we renege and the chunk arrives again.
6249 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6250 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6252 chunk->asoc->stats.iuodchunks++;
6254 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6256 chunk->asoc->stats.iodchunks++;
6260 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6262 * If an endpoint receive a DATA chunk with an invalid stream
6263 * identifier, it shall acknowledge the reception of the DATA chunk
6264 * following the normal procedure, immediately send an ERROR chunk
6265 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6266 * and discard the DATA chunk.
6268 sid = ntohs(data_hdr->stream);
6269 if (sid >= asoc->c.sinit_max_instreams) {
6270 /* Mark tsn as received even though we drop it */
6271 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6273 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6275 sizeof(data_hdr->stream),
6278 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6280 return SCTP_IERROR_BAD_STREAM;
6283 /* Check to see if the SSN is possible for this TSN.
6284 * The biggest gap we can record is 4K wide. Since SSNs wrap
6285 * at an unsigned short, there is no way that an SSN can
6286 * wrap and for a valid TSN. We can simply check if the current
6287 * SSN is smaller then the next expected one. If it is, it wrapped
6290 ssn = ntohs(data_hdr->ssn);
6291 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6292 return SCTP_IERROR_PROTO_VIOLATION;
6295 /* Send the data up to the user. Note: Schedule the
6296 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6297 * chunk needs the updated rwnd.
6299 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6301 return SCTP_IERROR_NO_ERROR;