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, write to
26 * the Free Software Foundation, 59 Temple Place - Suite 330,
27 * Boston, MA 02111-1307, USA.
29 * Please send any bug reports or fixes you make to the
31 * lksctp developers <lksctp-developers@lists.sourceforge.net>
33 * Or submit a bug report through the following website:
34 * http://www.sf.net/projects/lksctp
36 * Written or modified by:
37 * La Monte H.P. Yarroll <piggy@acm.org>
38 * Karl Knutson <karl@athena.chicago.il.us>
39 * Mathew Kotowsky <kotowsky@sctp.org>
40 * Sridhar Samudrala <samudrala@us.ibm.com>
41 * Jon Grimm <jgrimm@us.ibm.com>
42 * Hui Huang <hui.huang@nokia.com>
43 * Dajiang Zhang <dajiang.zhang@nokia.com>
44 * Daisy Chang <daisyc@us.ibm.com>
45 * Ardelle Fan <ardelle.fan@intel.com>
46 * Ryan Layer <rmlayer@us.ibm.com>
47 * Kevin Gao <kevin.gao@intel.com>
49 * Any bugs reported given to us we will try to fix... any fixes shared will
50 * be incorporated into the next SCTP release.
53 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
55 #include <linux/types.h>
56 #include <linux/kernel.h>
58 #include <linux/ipv6.h>
59 #include <linux/net.h>
60 #include <linux/inet.h>
61 #include <linux/slab.h>
63 #include <net/inet_ecn.h>
64 #include <linux/skbuff.h>
65 #include <net/sctp/sctp.h>
66 #include <net/sctp/sm.h>
67 #include <net/sctp/structs.h>
69 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
70 const struct sctp_association *asoc,
71 struct sctp_chunk *chunk,
74 static int sctp_eat_data(const struct sctp_association *asoc,
75 struct sctp_chunk *chunk,
76 sctp_cmd_seq_t *commands);
77 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
78 const struct sctp_association *asoc,
79 const struct sctp_chunk *chunk);
80 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
81 const struct sctp_association *asoc,
82 const struct sctp_chunk *chunk,
83 sctp_cmd_seq_t *commands,
84 struct sctp_chunk *err_chunk);
85 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
86 const struct sctp_association *asoc,
87 const sctp_subtype_t type,
89 sctp_cmd_seq_t *commands);
90 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
91 const struct sctp_association *asoc,
92 const sctp_subtype_t type,
94 sctp_cmd_seq_t *commands);
95 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
96 const struct sctp_association *asoc,
97 const sctp_subtype_t type,
99 sctp_cmd_seq_t *commands);
100 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
102 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
103 __be16 error, int sk_err,
104 const struct sctp_association *asoc,
105 struct sctp_transport *transport);
107 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(
116 const struct sctp_endpoint *ep,
117 const struct sctp_association *asoc,
118 const sctp_subtype_t type,
120 sctp_cmd_seq_t *commands);
122 static sctp_disposition_t sctp_sf_violation_paramlen(
123 const struct sctp_endpoint *ep,
124 const struct sctp_association *asoc,
125 const sctp_subtype_t type,
126 void *arg, void *ext,
127 sctp_cmd_seq_t *commands);
129 static sctp_disposition_t sctp_sf_violation_ctsn(
130 const struct sctp_endpoint *ep,
131 const struct sctp_association *asoc,
132 const sctp_subtype_t type,
134 sctp_cmd_seq_t *commands);
136 static sctp_disposition_t sctp_sf_violation_chunk(
137 const struct sctp_endpoint *ep,
138 const struct sctp_association *asoc,
139 const sctp_subtype_t type,
141 sctp_cmd_seq_t *commands);
143 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
144 const struct sctp_association *asoc,
145 const sctp_subtype_t type,
146 struct sctp_chunk *chunk);
148 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
152 sctp_cmd_seq_t *commands);
154 /* Small helper function that checks if the chunk length
155 * is of the appropriate length. The 'required_length' argument
156 * is set to be the size of a specific chunk we are testing.
157 * Return Values: 1 = Valid length
162 sctp_chunk_length_valid(struct sctp_chunk *chunk,
163 __u16 required_length)
165 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
167 if (unlikely(chunk_length < required_length))
173 /**********************************************************
174 * These are the state functions for handling chunk events.
175 **********************************************************/
178 * Process the final SHUTDOWN COMPLETE.
180 * Section: 4 (C) (diagram), 9.2
181 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
182 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
183 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
184 * should stop the T2-shutdown timer and remove all knowledge of the
185 * association (and thus the association enters the CLOSED state).
187 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
188 * C) Rules for packet carrying SHUTDOWN COMPLETE:
190 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
191 * if the Verification Tag field of the packet matches its own tag and
192 * the T bit is not set
194 * it is set to its peer's tag and the T bit is set in the Chunk
196 * Otherwise, the receiver MUST silently discard the packet
197 * and take no further action. An endpoint MUST ignore the
198 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
201 * (endpoint, asoc, chunk)
204 * (asoc, reply_msg, msg_up, timers, counters)
206 * The return value is the disposition of the chunk.
208 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
209 const struct sctp_association *asoc,
210 const sctp_subtype_t type,
212 sctp_cmd_seq_t *commands)
214 struct sctp_chunk *chunk = arg;
215 struct sctp_ulpevent *ev;
217 if (!sctp_vtag_verify_either(chunk, asoc))
218 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
220 /* RFC 2960 6.10 Bundling
222 * An endpoint MUST NOT bundle INIT, INIT ACK or
223 * SHUTDOWN COMPLETE with any other chunks.
225 if (!chunk->singleton)
226 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
228 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
229 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
230 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
233 /* RFC 2960 10.2 SCTP-to-ULP
235 * H) SHUTDOWN COMPLETE notification
237 * When SCTP completes the shutdown procedures (section 9.2) this
238 * notification is passed to the upper layer.
240 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
241 0, 0, 0, NULL, GFP_ATOMIC);
243 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
246 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
247 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
248 * not the chunk should be discarded. If the endpoint is in
249 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
250 * T2-shutdown timer and remove all knowledge of the
251 * association (and thus the association enters the CLOSED
254 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
255 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
257 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
258 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
260 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
261 SCTP_STATE(SCTP_STATE_CLOSED));
263 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
264 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
266 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
268 return SCTP_DISPOSITION_DELETE_TCB;
272 * Respond to a normal INIT chunk.
273 * We are the side that is being asked for an association.
275 * Section: 5.1 Normal Establishment of an Association, B
276 * B) "Z" shall respond immediately with an INIT ACK chunk. The
277 * destination IP address of the INIT ACK MUST be set to the source
278 * IP address of the INIT to which this INIT ACK is responding. In
279 * the response, besides filling in other parameters, "Z" must set the
280 * Verification Tag field to Tag_A, and also provide its own
281 * Verification Tag (Tag_Z) in the Initiate Tag field.
283 * Verification Tag: Must be 0.
286 * (endpoint, asoc, chunk)
289 * (asoc, reply_msg, msg_up, timers, counters)
291 * The return value is the disposition of the chunk.
293 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
294 const struct sctp_association *asoc,
295 const sctp_subtype_t type,
297 sctp_cmd_seq_t *commands)
299 struct sctp_chunk *chunk = arg;
300 struct sctp_chunk *repl;
301 struct sctp_association *new_asoc;
302 struct sctp_chunk *err_chunk;
303 struct sctp_packet *packet;
304 sctp_unrecognized_param_t *unk_param;
309 * An endpoint MUST NOT bundle INIT, INIT ACK or
310 * SHUTDOWN COMPLETE with any other chunks.
313 * Furthermore, we require that the receiver of an INIT chunk MUST
314 * enforce these rules by silently discarding an arriving packet
315 * with an INIT chunk that is bundled with other chunks.
317 if (!chunk->singleton)
318 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
320 /* If the packet is an OOTB packet which is temporarily on the
321 * control endpoint, respond with an ABORT.
323 net = sock_net(ep->base.sk);
324 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
325 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
326 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
329 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
332 if (chunk->sctp_hdr->vtag != 0)
333 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
335 /* Make sure that the INIT chunk has a valid length.
336 * Normally, this would cause an ABORT with a Protocol Violation
337 * error, but since we don't have an association, we'll
338 * just discard the packet.
340 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
341 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
343 /* If the INIT is coming toward a closing socket, we'll send back
344 * and ABORT. Essentially, this catches the race of INIT being
345 * backloged to the socket at the same time as the user isses close().
346 * Since the socket and all its associations are going away, we
347 * can treat this OOTB
349 if (sctp_sstate(ep->base.sk, CLOSING))
350 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
352 /* Verify the INIT chunk before processing it. */
354 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
355 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
357 /* This chunk contains fatal error. It is to be discarded.
358 * Send an ABORT, with causes if there is any.
361 packet = sctp_abort_pkt_new(ep, asoc, arg,
362 (__u8 *)(err_chunk->chunk_hdr) +
363 sizeof(sctp_chunkhdr_t),
364 ntohs(err_chunk->chunk_hdr->length) -
365 sizeof(sctp_chunkhdr_t));
367 sctp_chunk_free(err_chunk);
370 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
371 SCTP_PACKET(packet));
372 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
373 return SCTP_DISPOSITION_CONSUME;
375 return SCTP_DISPOSITION_NOMEM;
378 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
383 /* Grab the INIT header. */
384 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
386 /* Tag the variable length parameters. */
387 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
389 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
393 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
394 sctp_scope(sctp_source(chunk)),
398 /* The call, sctp_process_init(), can fail on memory allocation. */
399 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
400 (sctp_init_chunk_t *)chunk->chunk_hdr,
404 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
406 /* If there are errors need to be reported for unknown parameters,
407 * make sure to reserve enough room in the INIT ACK for them.
411 len = ntohs(err_chunk->chunk_hdr->length) -
412 sizeof(sctp_chunkhdr_t);
414 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
418 /* If there are errors need to be reported for unknown parameters,
419 * include them in the outgoing INIT ACK as "Unrecognized parameter"
423 /* Get the "Unrecognized parameter" parameter(s) out of the
424 * ERROR chunk generated by sctp_verify_init(). Since the
425 * error cause code for "unknown parameter" and the
426 * "Unrecognized parameter" type is the same, we can
427 * construct the parameters in INIT ACK by copying the
430 unk_param = (sctp_unrecognized_param_t *)
431 ((__u8 *)(err_chunk->chunk_hdr) +
432 sizeof(sctp_chunkhdr_t));
433 /* Replace the cause code with the "Unrecognized parameter"
436 sctp_addto_chunk(repl, len, unk_param);
437 sctp_chunk_free(err_chunk);
440 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
442 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
445 * Note: After sending out INIT ACK with the State Cookie parameter,
446 * "Z" MUST NOT allocate any resources, nor keep any states for the
447 * new association. Otherwise, "Z" will be vulnerable to resource
450 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
452 return SCTP_DISPOSITION_DELETE_TCB;
455 sctp_association_free(new_asoc);
458 sctp_chunk_free(err_chunk);
459 return SCTP_DISPOSITION_NOMEM;
463 * Respond to a normal INIT ACK chunk.
464 * We are the side that is initiating the association.
466 * Section: 5.1 Normal Establishment of an Association, C
467 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
468 * timer and leave COOKIE-WAIT state. "A" shall then send the State
469 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
470 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
472 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
473 * DATA chunks, but it MUST be the first chunk in the packet and
474 * until the COOKIE ACK is returned the sender MUST NOT send any
475 * other packets to the peer.
477 * Verification Tag: 3.3.3
478 * If the value of the Initiate Tag in a received INIT ACK chunk is
479 * found to be 0, the receiver MUST treat it as an error and close the
480 * association by transmitting an ABORT.
483 * (endpoint, asoc, chunk)
486 * (asoc, reply_msg, msg_up, timers, counters)
488 * The return value is the disposition of the chunk.
490 sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
491 const struct sctp_association *asoc,
492 const sctp_subtype_t type,
494 sctp_cmd_seq_t *commands)
496 struct sctp_chunk *chunk = arg;
497 sctp_init_chunk_t *initchunk;
498 struct sctp_chunk *err_chunk;
499 struct sctp_packet *packet;
501 if (!sctp_vtag_verify(chunk, asoc))
502 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
505 * An endpoint MUST NOT bundle INIT, INIT ACK or
506 * SHUTDOWN COMPLETE with any other chunks.
508 if (!chunk->singleton)
509 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
511 /* Make sure that the INIT-ACK chunk has a valid length */
512 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
513 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
515 /* Grab the INIT header. */
516 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
518 /* Verify the INIT chunk before processing it. */
520 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
521 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
524 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
526 /* This chunk contains fatal error. It is to be discarded.
527 * Send an ABORT, with causes. If there are no causes,
528 * then there wasn't enough memory. Just terminate
532 packet = sctp_abort_pkt_new(ep, asoc, arg,
533 (__u8 *)(err_chunk->chunk_hdr) +
534 sizeof(sctp_chunkhdr_t),
535 ntohs(err_chunk->chunk_hdr->length) -
536 sizeof(sctp_chunkhdr_t));
538 sctp_chunk_free(err_chunk);
541 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
542 SCTP_PACKET(packet));
543 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
544 error = SCTP_ERROR_INV_PARAM;
548 /* SCTP-AUTH, Section 6.3:
549 * It should be noted that if the receiver wants to tear
550 * down an association in an authenticated way only, the
551 * handling of malformed packets should not result in
552 * tearing down the association.
554 * This means that if we only want to abort associations
555 * in an authenticated way (i.e AUTH+ABORT), then we
556 * can't destroy this association just because the packet
559 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
560 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
562 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
563 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
564 asoc, chunk->transport);
567 /* Tag the variable length parameters. Note that we never
568 * convert the parameters in an INIT chunk.
570 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
572 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
574 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
575 SCTP_PEER_INIT(initchunk));
577 /* Reset init error count upon receipt of INIT-ACK. */
578 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
580 /* 5.1 C) "A" shall stop the T1-init timer and leave
581 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
582 * timer, and enter the COOKIE-ECHOED state.
584 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
585 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
586 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
587 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
588 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
589 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
591 /* SCTP-AUTH: genereate the assocition shared keys so that
592 * we can potentially signe the COOKIE-ECHO.
594 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
596 /* 5.1 C) "A" shall then send the State Cookie received in the
597 * INIT ACK chunk in a COOKIE ECHO chunk, ...
599 /* If there is any errors to report, send the ERROR chunk generated
600 * for unknown parameters as well.
602 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
603 SCTP_CHUNK(err_chunk));
605 return SCTP_DISPOSITION_CONSUME;
609 * Respond to a normal COOKIE ECHO chunk.
610 * We are the side that is being asked for an association.
612 * Section: 5.1 Normal Establishment of an Association, D
613 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
614 * with a COOKIE ACK chunk after building a TCB and moving to
615 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
616 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
617 * chunk MUST be the first chunk in the packet.
619 * IMPLEMENTATION NOTE: An implementation may choose to send the
620 * Communication Up notification to the SCTP user upon reception
621 * of a valid COOKIE ECHO chunk.
623 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
624 * D) Rules for packet carrying a COOKIE ECHO
626 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
627 * Initial Tag received in the INIT ACK.
629 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
632 * (endpoint, asoc, chunk)
635 * (asoc, reply_msg, msg_up, timers, counters)
637 * The return value is the disposition of the chunk.
639 sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
640 const struct sctp_association *asoc,
641 const sctp_subtype_t type, void *arg,
642 sctp_cmd_seq_t *commands)
644 struct sctp_chunk *chunk = arg;
645 struct sctp_association *new_asoc;
646 sctp_init_chunk_t *peer_init;
647 struct sctp_chunk *repl;
648 struct sctp_ulpevent *ev, *ai_ev = NULL;
650 struct sctp_chunk *err_chk_p;
654 /* If the packet is an OOTB packet which is temporarily on the
655 * control endpoint, respond with an ABORT.
657 net = sock_net(ep->base.sk);
658 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
659 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
660 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
663 /* Make sure that the COOKIE_ECHO chunk has a valid length.
664 * In this case, we check that we have enough for at least a
665 * chunk header. More detailed verification is done
666 * in sctp_unpack_cookie().
668 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
669 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
671 /* If the endpoint is not listening or if the number of associations
672 * on the TCP-style socket exceed the max backlog, respond with an
676 if (!sctp_sstate(sk, LISTENING) ||
677 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
678 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
680 /* "Decode" the chunk. We have no optional parameters so we
683 chunk->subh.cookie_hdr =
684 (struct sctp_signed_cookie *)chunk->skb->data;
685 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
686 sizeof(sctp_chunkhdr_t)))
689 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
690 * "Z" will reply with a COOKIE ACK chunk after building a TCB
691 * and moving to the ESTABLISHED state.
693 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
697 * If the re-build failed, what is the proper error path
700 * [We should abort the association. --piggy]
703 /* FIXME: Several errors are possible. A bad cookie should
704 * be silently discarded, but think about logging it too.
707 case -SCTP_IERROR_NOMEM:
710 case -SCTP_IERROR_STALE_COOKIE:
711 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
713 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
715 case -SCTP_IERROR_BAD_SIG:
717 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
722 /* Delay state machine commands until later.
724 * Re-build the bind address for the association is done in
725 * the sctp_unpack_cookie() already.
727 /* This is a brand-new association, so these are not yet side
728 * effects--it is safe to run them here.
730 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
732 if (!sctp_process_init(new_asoc, chunk,
733 &chunk->subh.cookie_hdr->c.peer_addr,
734 peer_init, GFP_ATOMIC))
737 /* SCTP-AUTH: Now that we've populate required fields in
738 * sctp_process_init, set up the assocaition shared keys as
739 * necessary so that we can potentially authenticate the ACK
741 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
745 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
746 * is supposed to be authenticated and we have to do delayed
747 * authentication. We've just recreated the association using
748 * the information in the cookie and now it's much easier to
749 * do the authentication.
751 if (chunk->auth_chunk) {
752 struct sctp_chunk auth;
755 /* set-up our fake chunk so that we can process it */
756 auth.skb = chunk->auth_chunk;
757 auth.asoc = chunk->asoc;
758 auth.sctp_hdr = chunk->sctp_hdr;
759 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
760 sizeof(sctp_chunkhdr_t));
761 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
762 auth.transport = chunk->transport;
764 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
766 /* We can now safely free the auth_chunk clone */
767 kfree_skb(chunk->auth_chunk);
769 if (ret != SCTP_IERROR_NO_ERROR) {
770 sctp_association_free(new_asoc);
771 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
775 repl = sctp_make_cookie_ack(new_asoc, chunk);
779 /* RFC 2960 5.1 Normal Establishment of an Association
781 * D) IMPLEMENTATION NOTE: An implementation may choose to
782 * send the Communication Up notification to the SCTP user
783 * upon reception of a valid COOKIE ECHO chunk.
785 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
786 new_asoc->c.sinit_num_ostreams,
787 new_asoc->c.sinit_max_instreams,
792 /* Sockets API Draft Section 5.3.1.6
793 * When a peer sends a Adaptation Layer Indication parameter , SCTP
794 * delivers this notification to inform the application that of the
795 * peers requested adaptation layer.
797 if (new_asoc->peer.adaptation_ind) {
798 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
804 /* Add all the state machine commands now since we've created
805 * everything. This way we don't introduce memory corruptions
806 * during side-effect processing and correclty count established
809 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
810 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
811 SCTP_STATE(SCTP_STATE_ESTABLISHED));
812 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
813 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
814 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
816 if (new_asoc->autoclose)
817 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
818 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
820 /* This will send the COOKIE ACK */
821 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
823 /* Queue the ASSOC_CHANGE event */
824 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
826 /* Send up the Adaptation Layer Indication event */
828 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
829 SCTP_ULPEVENT(ai_ev));
831 return SCTP_DISPOSITION_CONSUME;
834 sctp_ulpevent_free(ev);
836 sctp_chunk_free(repl);
838 sctp_association_free(new_asoc);
840 return SCTP_DISPOSITION_NOMEM;
844 * Respond to a normal COOKIE ACK chunk.
845 * We are the side that is being asked for an association.
847 * RFC 2960 5.1 Normal Establishment of an Association
849 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
850 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
851 * timer. It may also notify its ULP about the successful
852 * establishment of the association with a Communication Up
853 * notification (see Section 10).
857 * (endpoint, asoc, chunk)
860 * (asoc, reply_msg, msg_up, timers, counters)
862 * The return value is the disposition of the chunk.
864 sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
865 const struct sctp_association *asoc,
866 const sctp_subtype_t type, void *arg,
867 sctp_cmd_seq_t *commands)
869 struct sctp_chunk *chunk = arg;
870 struct sctp_ulpevent *ev;
872 if (!sctp_vtag_verify(chunk, asoc))
873 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
875 /* Verify that the chunk length for the COOKIE-ACK is OK.
876 * If we don't do this, any bundled chunks may be junked.
878 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
879 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
882 /* Reset init error count upon receipt of COOKIE-ACK,
883 * to avoid problems with the managemement of this
884 * counter in stale cookie situations when a transition back
885 * from the COOKIE-ECHOED state to the COOKIE-WAIT
886 * state is performed.
888 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
890 /* RFC 2960 5.1 Normal Establishment of an Association
892 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
893 * from the COOKIE-ECHOED state to the ESTABLISHED state,
894 * stopping the T1-cookie timer.
896 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
897 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
898 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
899 SCTP_STATE(SCTP_STATE_ESTABLISHED));
900 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
901 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
902 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
904 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
905 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
907 /* It may also notify its ULP about the successful
908 * establishment of the association with a Communication Up
909 * notification (see Section 10).
911 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
912 0, asoc->c.sinit_num_ostreams,
913 asoc->c.sinit_max_instreams,
919 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
921 /* Sockets API Draft Section 5.3.1.6
922 * When a peer sends a Adaptation Layer Indication parameter , SCTP
923 * delivers this notification to inform the application that of the
924 * peers requested adaptation layer.
926 if (asoc->peer.adaptation_ind) {
927 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
931 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
935 return SCTP_DISPOSITION_CONSUME;
937 return SCTP_DISPOSITION_NOMEM;
940 /* Generate and sendout a heartbeat packet. */
941 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
942 const struct sctp_association *asoc,
943 const sctp_subtype_t type,
945 sctp_cmd_seq_t *commands)
947 struct sctp_transport *transport = (struct sctp_transport *) arg;
948 struct sctp_chunk *reply;
950 /* Send a heartbeat to our peer. */
951 reply = sctp_make_heartbeat(asoc, transport);
953 return SCTP_DISPOSITION_NOMEM;
955 /* Set rto_pending indicating that an RTT measurement
956 * is started with this heartbeat chunk.
958 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
959 SCTP_TRANSPORT(transport));
961 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
962 return SCTP_DISPOSITION_CONSUME;
965 /* Generate a HEARTBEAT packet on the given transport. */
966 sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
967 const struct sctp_association *asoc,
968 const sctp_subtype_t type,
970 sctp_cmd_seq_t *commands)
972 struct sctp_transport *transport = (struct sctp_transport *) arg;
974 if (asoc->overall_error_count >= asoc->max_retrans) {
975 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
976 SCTP_ERROR(ETIMEDOUT));
977 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
978 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
979 SCTP_PERR(SCTP_ERROR_NO_ERROR));
980 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
981 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
982 return SCTP_DISPOSITION_DELETE_TCB;
986 * The Sender-specific Heartbeat Info field should normally include
987 * information about the sender's current time when this HEARTBEAT
988 * chunk is sent and the destination transport address to which this
989 * HEARTBEAT is sent (see Section 8.3).
992 if (transport->param_flags & SPP_HB_ENABLE) {
993 if (SCTP_DISPOSITION_NOMEM ==
994 sctp_sf_heartbeat(ep, asoc, type, arg,
996 return SCTP_DISPOSITION_NOMEM;
998 /* Set transport error counter and association error counter
999 * when sending heartbeat.
1001 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1002 SCTP_TRANSPORT(transport));
1004 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1005 SCTP_TRANSPORT(transport));
1006 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1007 SCTP_TRANSPORT(transport));
1009 return SCTP_DISPOSITION_CONSUME;
1013 * Process an heartbeat request.
1015 * Section: 8.3 Path Heartbeat
1016 * The receiver of the HEARTBEAT should immediately respond with a
1017 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1018 * from the received HEARTBEAT chunk.
1020 * Verification Tag: 8.5 Verification Tag [Normal verification]
1021 * When receiving an SCTP packet, the endpoint MUST ensure that the
1022 * value in the Verification Tag field of the received SCTP packet
1023 * matches its own Tag. If the received Verification Tag value does not
1024 * match the receiver's own tag value, the receiver shall silently
1025 * discard the packet and shall not process it any further except for
1026 * those cases listed in Section 8.5.1 below.
1029 * (endpoint, asoc, chunk)
1032 * (asoc, reply_msg, msg_up, timers, counters)
1034 * The return value is the disposition of the chunk.
1036 sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
1037 const struct sctp_association *asoc,
1038 const sctp_subtype_t type,
1040 sctp_cmd_seq_t *commands)
1042 struct sctp_chunk *chunk = arg;
1043 struct sctp_chunk *reply;
1046 if (!sctp_vtag_verify(chunk, asoc))
1047 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1049 /* Make sure that the HEARTBEAT chunk has a valid length. */
1050 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1051 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1054 /* 8.3 The receiver of the HEARTBEAT should immediately
1055 * respond with a HEARTBEAT ACK that contains the Heartbeat
1056 * Information field copied from the received HEARTBEAT chunk.
1058 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1059 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1060 if (!pskb_pull(chunk->skb, paylen))
1063 reply = sctp_make_heartbeat_ack(asoc, chunk,
1064 chunk->subh.hb_hdr, paylen);
1068 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1069 return SCTP_DISPOSITION_CONSUME;
1072 return SCTP_DISPOSITION_NOMEM;
1076 * Process the returning HEARTBEAT ACK.
1078 * Section: 8.3 Path Heartbeat
1079 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1080 * should clear the error counter of the destination transport
1081 * address to which the HEARTBEAT was sent, and mark the destination
1082 * transport address as active if it is not so marked. The endpoint may
1083 * optionally report to the upper layer when an inactive destination
1084 * address is marked as active due to the reception of the latest
1085 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1086 * clear the association overall error count as well (as defined
1089 * The receiver of the HEARTBEAT ACK should also perform an RTT
1090 * measurement for that destination transport address using the time
1091 * value carried in the HEARTBEAT ACK chunk.
1093 * Verification Tag: 8.5 Verification Tag [Normal verification]
1096 * (endpoint, asoc, chunk)
1099 * (asoc, reply_msg, msg_up, timers, counters)
1101 * The return value is the disposition of the chunk.
1103 sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
1104 const struct sctp_association *asoc,
1105 const sctp_subtype_t type,
1107 sctp_cmd_seq_t *commands)
1109 struct sctp_chunk *chunk = arg;
1110 union sctp_addr from_addr;
1111 struct sctp_transport *link;
1112 sctp_sender_hb_info_t *hbinfo;
1113 unsigned long max_interval;
1115 if (!sctp_vtag_verify(chunk, asoc))
1116 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1118 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1119 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1120 sizeof(sctp_sender_hb_info_t)))
1121 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1124 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1125 /* Make sure that the length of the parameter is what we expect */
1126 if (ntohs(hbinfo->param_hdr.length) !=
1127 sizeof(sctp_sender_hb_info_t)) {
1128 return SCTP_DISPOSITION_DISCARD;
1131 from_addr = hbinfo->daddr;
1132 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1134 /* This should never happen, but lets log it if so. */
1135 if (unlikely(!link)) {
1136 if (from_addr.sa.sa_family == AF_INET6) {
1137 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1140 &from_addr.v6.sin6_addr);
1142 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1145 &from_addr.v4.sin_addr.s_addr);
1147 return SCTP_DISPOSITION_DISCARD;
1150 /* Validate the 64-bit random nonce. */
1151 if (hbinfo->hb_nonce != link->hb_nonce)
1152 return SCTP_DISPOSITION_DISCARD;
1154 max_interval = link->hbinterval + link->rto;
1156 /* Check if the timestamp looks valid. */
1157 if (time_after(hbinfo->sent_at, jiffies) ||
1158 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1159 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1160 "received for transport: %p\n",
1162 return SCTP_DISPOSITION_DISCARD;
1165 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1166 * the HEARTBEAT should clear the error counter of the
1167 * destination transport address to which the HEARTBEAT was
1168 * sent and mark the destination transport address as active if
1169 * it is not so marked.
1171 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1173 return SCTP_DISPOSITION_CONSUME;
1176 /* Helper function to send out an abort for the restart
1179 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1180 struct sctp_chunk *init,
1181 sctp_cmd_seq_t *commands)
1184 struct sctp_packet *pkt;
1185 union sctp_addr_param *addrparm;
1186 struct sctp_errhdr *errhdr;
1187 struct sctp_endpoint *ep;
1188 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1189 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1191 /* Build the error on the stack. We are way to malloc crazy
1192 * throughout the code today.
1194 errhdr = (struct sctp_errhdr *)buffer;
1195 addrparm = (union sctp_addr_param *)errhdr->variable;
1197 /* Copy into a parm format. */
1198 len = af->to_addr_param(ssa, addrparm);
1199 len += sizeof(sctp_errhdr_t);
1201 errhdr->cause = SCTP_ERROR_RESTART;
1202 errhdr->length = htons(len);
1204 /* Assign to the control socket. */
1205 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1207 /* Association is NULL since this may be a restart attack and we
1208 * want to send back the attacker's vtag.
1210 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1214 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1216 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1218 /* Discard the rest of the inbound packet. */
1219 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1222 /* Even if there is no memory, treat as a failure so
1223 * the packet will get dropped.
1228 static bool list_has_sctp_addr(const struct list_head *list,
1229 union sctp_addr *ipaddr)
1231 struct sctp_transport *addr;
1233 list_for_each_entry(addr, list, transports) {
1234 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1240 /* A restart is occurring, check to make sure no new addresses
1241 * are being added as we may be under a takeover attack.
1243 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1244 const struct sctp_association *asoc,
1245 struct sctp_chunk *init,
1246 sctp_cmd_seq_t *commands)
1248 struct net *net = sock_net(new_asoc->base.sk);
1249 struct sctp_transport *new_addr;
1252 /* Implementor's Guide - Section 5.2.2
1254 * Before responding the endpoint MUST check to see if the
1255 * unexpected INIT adds new addresses to the association. If new
1256 * addresses are added to the association, the endpoint MUST respond
1260 /* Search through all current addresses and make sure
1261 * we aren't adding any new ones.
1263 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1265 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1266 &new_addr->ipaddr)) {
1267 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1274 /* Return success if all addresses were found. */
1278 /* Populate the verification/tie tags based on overlapping INIT
1281 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1283 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1284 const struct sctp_association *asoc)
1286 switch (asoc->state) {
1288 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1290 case SCTP_STATE_COOKIE_WAIT:
1291 new_asoc->c.my_vtag = asoc->c.my_vtag;
1292 new_asoc->c.my_ttag = asoc->c.my_vtag;
1293 new_asoc->c.peer_ttag = 0;
1296 case SCTP_STATE_COOKIE_ECHOED:
1297 new_asoc->c.my_vtag = asoc->c.my_vtag;
1298 new_asoc->c.my_ttag = asoc->c.my_vtag;
1299 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1302 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1303 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1306 new_asoc->c.my_ttag = asoc->c.my_vtag;
1307 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1311 /* Other parameters for the endpoint SHOULD be copied from the
1312 * existing parameters of the association (e.g. number of
1313 * outbound streams) into the INIT ACK and cookie.
1315 new_asoc->rwnd = asoc->rwnd;
1316 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1317 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1318 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1322 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1325 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1327 * Returns value representing action to be taken. These action values
1328 * correspond to Action/Description values in RFC 2960, Table 2.
1330 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1331 const struct sctp_association *asoc)
1333 /* In this case, the peer may have restarted. */
1334 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1335 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1336 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1337 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1340 /* Collision case B. */
1341 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1342 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1343 (0 == asoc->c.peer_vtag))) {
1347 /* Collision case D. */
1348 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1349 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1352 /* Collision case C. */
1353 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1354 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1355 (0 == new_asoc->c.my_ttag) &&
1356 (0 == new_asoc->c.peer_ttag))
1359 /* No match to any of the special cases; discard this packet. */
1363 /* Common helper routine for both duplicate and simulataneous INIT
1366 static sctp_disposition_t sctp_sf_do_unexpected_init(
1367 const struct sctp_endpoint *ep,
1368 const struct sctp_association *asoc,
1369 const sctp_subtype_t type,
1370 void *arg, sctp_cmd_seq_t *commands)
1372 sctp_disposition_t retval;
1373 struct sctp_chunk *chunk = arg;
1374 struct sctp_chunk *repl;
1375 struct sctp_association *new_asoc;
1376 struct sctp_chunk *err_chunk;
1377 struct sctp_packet *packet;
1378 sctp_unrecognized_param_t *unk_param;
1382 * An endpoint MUST NOT bundle INIT, INIT ACK or
1383 * SHUTDOWN COMPLETE with any other chunks.
1386 * Furthermore, we require that the receiver of an INIT chunk MUST
1387 * enforce these rules by silently discarding an arriving packet
1388 * with an INIT chunk that is bundled with other chunks.
1390 if (!chunk->singleton)
1391 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1393 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1396 if (chunk->sctp_hdr->vtag != 0)
1397 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1399 /* Make sure that the INIT chunk has a valid length.
1400 * In this case, we generate a protocol violation since we have
1401 * an association established.
1403 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1404 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1406 /* Grab the INIT header. */
1407 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1409 /* Tag the variable length parameters. */
1410 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1412 /* Verify the INIT chunk before processing it. */
1414 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1415 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1417 /* This chunk contains fatal error. It is to be discarded.
1418 * Send an ABORT, with causes if there is any.
1421 packet = sctp_abort_pkt_new(ep, asoc, arg,
1422 (__u8 *)(err_chunk->chunk_hdr) +
1423 sizeof(sctp_chunkhdr_t),
1424 ntohs(err_chunk->chunk_hdr->length) -
1425 sizeof(sctp_chunkhdr_t));
1428 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1429 SCTP_PACKET(packet));
1430 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1431 retval = SCTP_DISPOSITION_CONSUME;
1433 retval = SCTP_DISPOSITION_NOMEM;
1437 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1443 * Other parameters for the endpoint SHOULD be copied from the
1444 * existing parameters of the association (e.g. number of
1445 * outbound streams) into the INIT ACK and cookie.
1446 * FIXME: We are copying parameters from the endpoint not the
1449 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1453 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1454 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1457 /* In the outbound INIT ACK the endpoint MUST copy its current
1458 * Verification Tag and Peers Verification tag into a reserved
1459 * place (local tie-tag and per tie-tag) within the state cookie.
1461 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1462 (sctp_init_chunk_t *)chunk->chunk_hdr,
1466 /* Make sure no new addresses are being added during the
1467 * restart. Do not do this check for COOKIE-WAIT state,
1468 * since there are no peer addresses to check against.
1469 * Upon return an ABORT will have been sent if needed.
1471 if (!sctp_state(asoc, COOKIE_WAIT)) {
1472 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1474 retval = SCTP_DISPOSITION_CONSUME;
1479 sctp_tietags_populate(new_asoc, asoc);
1481 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1483 /* If there are errors need to be reported for unknown parameters,
1484 * make sure to reserve enough room in the INIT ACK for them.
1488 len = ntohs(err_chunk->chunk_hdr->length) -
1489 sizeof(sctp_chunkhdr_t);
1492 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1496 /* If there are errors need to be reported for unknown parameters,
1497 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1501 /* Get the "Unrecognized parameter" parameter(s) out of the
1502 * ERROR chunk generated by sctp_verify_init(). Since the
1503 * error cause code for "unknown parameter" and the
1504 * "Unrecognized parameter" type is the same, we can
1505 * construct the parameters in INIT ACK by copying the
1506 * ERROR causes over.
1508 unk_param = (sctp_unrecognized_param_t *)
1509 ((__u8 *)(err_chunk->chunk_hdr) +
1510 sizeof(sctp_chunkhdr_t));
1511 /* Replace the cause code with the "Unrecognized parameter"
1514 sctp_addto_chunk(repl, len, unk_param);
1517 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1518 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1521 * Note: After sending out INIT ACK with the State Cookie parameter,
1522 * "Z" MUST NOT allocate any resources for this new association.
1523 * Otherwise, "Z" will be vulnerable to resource attacks.
1525 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1526 retval = SCTP_DISPOSITION_CONSUME;
1531 retval = SCTP_DISPOSITION_NOMEM;
1534 sctp_association_free(new_asoc);
1537 sctp_chunk_free(err_chunk);
1542 * Handle simultaneous INIT.
1543 * This means we started an INIT and then we got an INIT request from
1546 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1547 * This usually indicates an initialization collision, i.e., each
1548 * endpoint is attempting, at about the same time, to establish an
1549 * association with the other endpoint.
1551 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1552 * endpoint MUST respond with an INIT ACK using the same parameters it
1553 * sent in its original INIT chunk (including its Verification Tag,
1554 * unchanged). These original parameters are combined with those from the
1555 * newly received INIT chunk. The endpoint shall also generate a State
1556 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1557 * INIT to calculate the State Cookie.
1559 * After that, the endpoint MUST NOT change its state, the T1-init
1560 * timer shall be left running and the corresponding TCB MUST NOT be
1561 * destroyed. The normal procedures for handling State Cookies when
1562 * a TCB exists will resolve the duplicate INITs to a single association.
1564 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1565 * its Tie-Tags with the Tag information of itself and its peer (see
1566 * section 5.2.2 for a description of the Tie-Tags).
1568 * Verification Tag: Not explicit, but an INIT can not have a valid
1569 * verification tag, so we skip the check.
1572 * (endpoint, asoc, chunk)
1575 * (asoc, reply_msg, msg_up, timers, counters)
1577 * The return value is the disposition of the chunk.
1579 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1580 const struct sctp_association *asoc,
1581 const sctp_subtype_t type,
1583 sctp_cmd_seq_t *commands)
1585 /* Call helper to do the real work for both simulataneous and
1586 * duplicate INIT chunk handling.
1588 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1592 * Handle duplicated INIT messages. These are usually delayed
1595 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1596 * COOKIE-ECHOED and COOKIE-WAIT
1598 * Unless otherwise stated, upon reception of an unexpected INIT for
1599 * this association, the endpoint shall generate an INIT ACK with a
1600 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1601 * current Verification Tag and peer's Verification Tag into a reserved
1602 * place within the state cookie. We shall refer to these locations as
1603 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1604 * containing this INIT ACK MUST carry a Verification Tag value equal to
1605 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1606 * MUST contain a new Initiation Tag (randomly generated see Section
1607 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1608 * existing parameters of the association (e.g. number of outbound
1609 * streams) into the INIT ACK and cookie.
1611 * After sending out the INIT ACK, the endpoint shall take no further
1612 * actions, i.e., the existing association, including its current state,
1613 * and the corresponding TCB MUST NOT be changed.
1615 * Note: Only when a TCB exists and the association is not in a COOKIE-
1616 * WAIT state are the Tie-Tags populated. For a normal association INIT
1617 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1618 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1619 * State Cookie are populated as specified in section 5.2.1.
1621 * Verification Tag: Not specified, but an INIT has no way of knowing
1622 * what the verification tag could be, so we ignore it.
1625 * (endpoint, asoc, chunk)
1628 * (asoc, reply_msg, msg_up, timers, counters)
1630 * The return value is the disposition of the chunk.
1632 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1633 const struct sctp_association *asoc,
1634 const sctp_subtype_t type,
1636 sctp_cmd_seq_t *commands)
1638 /* Call helper to do the real work for both simulataneous and
1639 * duplicate INIT chunk handling.
1641 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1646 * Unexpected INIT-ACK handler.
1649 * If an INIT ACK received by an endpoint in any state other than the
1650 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1651 * An unexpected INIT ACK usually indicates the processing of an old or
1652 * duplicated INIT chunk.
1654 sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1655 const struct sctp_association *asoc,
1656 const sctp_subtype_t type,
1657 void *arg, sctp_cmd_seq_t *commands)
1659 struct net *net = sock_net(ep->base.sk);
1660 /* Per the above section, we'll discard the chunk if we have an
1661 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1663 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1664 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1666 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1669 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1672 * A) In this case, the peer may have restarted.
1674 static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1675 const struct sctp_association *asoc,
1676 struct sctp_chunk *chunk,
1677 sctp_cmd_seq_t *commands,
1678 struct sctp_association *new_asoc)
1680 sctp_init_chunk_t *peer_init;
1681 struct sctp_ulpevent *ev;
1682 struct sctp_chunk *repl;
1683 struct sctp_chunk *err;
1684 sctp_disposition_t disposition;
1686 /* new_asoc is a brand-new association, so these are not yet
1687 * side effects--it is safe to run them here.
1689 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1691 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1695 /* Make sure no new addresses are being added during the
1696 * restart. Though this is a pretty complicated attack
1697 * since you'd have to get inside the cookie.
1699 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1700 return SCTP_DISPOSITION_CONSUME;
1703 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1704 * the peer has restarted (Action A), it MUST NOT setup a new
1705 * association but instead resend the SHUTDOWN ACK and send an ERROR
1706 * chunk with a "Cookie Received while Shutting Down" error cause to
1709 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1710 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1711 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1713 if (SCTP_DISPOSITION_NOMEM == disposition)
1716 err = sctp_make_op_error(asoc, chunk,
1717 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1720 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1723 return SCTP_DISPOSITION_CONSUME;
1726 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1727 * data. Consider the optional choice of resending of this data.
1729 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1730 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1731 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1732 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1734 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1735 * and ASCONF-ACK cache.
1737 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1738 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1739 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1741 repl = sctp_make_cookie_ack(new_asoc, chunk);
1745 /* Report association restart to upper layer. */
1746 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1747 new_asoc->c.sinit_num_ostreams,
1748 new_asoc->c.sinit_max_instreams,
1753 /* Update the content of current association. */
1754 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1755 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1756 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1757 return SCTP_DISPOSITION_CONSUME;
1760 sctp_chunk_free(repl);
1762 return SCTP_DISPOSITION_NOMEM;
1765 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1768 * B) In this case, both sides may be attempting to start an association
1769 * at about the same time but the peer endpoint started its INIT
1770 * after responding to the local endpoint's INIT
1772 /* This case represents an initialization collision. */
1773 static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1774 const struct sctp_association *asoc,
1775 struct sctp_chunk *chunk,
1776 sctp_cmd_seq_t *commands,
1777 struct sctp_association *new_asoc)
1779 sctp_init_chunk_t *peer_init;
1780 struct sctp_chunk *repl;
1782 /* new_asoc is a brand-new association, so these are not yet
1783 * side effects--it is safe to run them here.
1785 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1786 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1790 /* Update the content of current association. */
1791 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1792 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1793 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1794 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1795 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1797 repl = sctp_make_cookie_ack(new_asoc, chunk);
1801 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1803 /* RFC 2960 5.1 Normal Establishment of an Association
1805 * D) IMPLEMENTATION NOTE: An implementation may choose to
1806 * send the Communication Up notification to the SCTP user
1807 * upon reception of a valid COOKIE ECHO chunk.
1809 * Sadly, this needs to be implemented as a side-effect, because
1810 * we are not guaranteed to have set the association id of the real
1811 * association and so these notifications need to be delayed until
1812 * the association id is allocated.
1815 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1817 /* Sockets API Draft Section 5.3.1.6
1818 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1819 * delivers this notification to inform the application that of the
1820 * peers requested adaptation layer.
1822 * This also needs to be done as a side effect for the same reason as
1825 if (asoc->peer.adaptation_ind)
1826 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1828 return SCTP_DISPOSITION_CONSUME;
1831 return SCTP_DISPOSITION_NOMEM;
1834 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1837 * C) In this case, the local endpoint's cookie has arrived late.
1838 * Before it arrived, the local endpoint sent an INIT and received an
1839 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1840 * but a new tag of its own.
1842 /* This case represents an initialization collision. */
1843 static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1844 const struct sctp_association *asoc,
1845 struct sctp_chunk *chunk,
1846 sctp_cmd_seq_t *commands,
1847 struct sctp_association *new_asoc)
1849 /* The cookie should be silently discarded.
1850 * The endpoint SHOULD NOT change states and should leave
1851 * any timers running.
1853 return SCTP_DISPOSITION_DISCARD;
1856 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1860 * D) When both local and remote tags match the endpoint should always
1861 * enter the ESTABLISHED state, if it has not already done so.
1863 /* This case represents an initialization collision. */
1864 static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1865 const struct sctp_association *asoc,
1866 struct sctp_chunk *chunk,
1867 sctp_cmd_seq_t *commands,
1868 struct sctp_association *new_asoc)
1870 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1871 struct sctp_chunk *repl;
1873 /* Clarification from Implementor's Guide:
1874 * D) When both local and remote tags match the endpoint should
1875 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1876 * It should stop any cookie timer that may be running and send
1880 /* Don't accidentally move back into established state. */
1881 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1882 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1883 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1884 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1885 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1886 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1887 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1890 /* RFC 2960 5.1 Normal Establishment of an Association
1892 * D) IMPLEMENTATION NOTE: An implementation may choose
1893 * to send the Communication Up notification to the
1894 * SCTP user upon reception of a valid COOKIE
1897 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1899 asoc->c.sinit_num_ostreams,
1900 asoc->c.sinit_max_instreams,
1905 /* Sockets API Draft Section 5.3.1.6
1906 * When a peer sends a Adaptation Layer Indication parameter,
1907 * SCTP delivers this notification to inform the application
1908 * that of the peers requested adaptation layer.
1910 if (asoc->peer.adaptation_ind) {
1911 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1919 repl = sctp_make_cookie_ack(new_asoc, chunk);
1923 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1926 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1929 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1930 SCTP_ULPEVENT(ai_ev));
1932 return SCTP_DISPOSITION_CONSUME;
1936 sctp_ulpevent_free(ai_ev);
1938 sctp_ulpevent_free(ev);
1939 return SCTP_DISPOSITION_NOMEM;
1943 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1944 * chunk was retransmitted and then delayed in the network.
1946 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1948 * Verification Tag: None. Do cookie validation.
1951 * (endpoint, asoc, chunk)
1954 * (asoc, reply_msg, msg_up, timers, counters)
1956 * The return value is the disposition of the chunk.
1958 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1959 const struct sctp_association *asoc,
1960 const sctp_subtype_t type,
1962 sctp_cmd_seq_t *commands)
1964 sctp_disposition_t retval;
1965 struct sctp_chunk *chunk = arg;
1966 struct sctp_association *new_asoc;
1969 struct sctp_chunk *err_chk_p;
1971 /* Make sure that the chunk has a valid length from the protocol
1972 * perspective. In this case check to make sure we have at least
1973 * enough for the chunk header. Cookie length verification is
1976 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1977 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1980 /* "Decode" the chunk. We have no optional parameters so we
1981 * are in good shape.
1983 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
1984 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1985 sizeof(sctp_chunkhdr_t)))
1988 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1989 * of a duplicate COOKIE ECHO match the Verification Tags of the
1990 * current association, consider the State Cookie valid even if
1991 * the lifespan is exceeded.
1993 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1997 * If the re-build failed, what is the proper error path
2000 * [We should abort the association. --piggy]
2003 /* FIXME: Several errors are possible. A bad cookie should
2004 * be silently discarded, but think about logging it too.
2007 case -SCTP_IERROR_NOMEM:
2010 case -SCTP_IERROR_STALE_COOKIE:
2011 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
2013 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2014 case -SCTP_IERROR_BAD_SIG:
2016 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2020 /* Compare the tie_tag in cookie with the verification tag of
2021 * current association.
2023 action = sctp_tietags_compare(new_asoc, asoc);
2026 case 'A': /* Association restart. */
2027 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
2031 case 'B': /* Collision case B. */
2032 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
2036 case 'C': /* Collision case C. */
2037 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
2041 case 'D': /* Collision case D. */
2042 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
2046 default: /* Discard packet for all others. */
2047 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2051 /* Delete the tempory new association. */
2052 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2053 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2055 /* Restore association pointer to provide SCTP command interpeter
2056 * with a valid context in case it needs to manipulate
2058 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2059 SCTP_ASOC((struct sctp_association *)asoc));
2064 return SCTP_DISPOSITION_NOMEM;
2068 * Process an ABORT. (SHUTDOWN-PENDING state)
2070 * See sctp_sf_do_9_1_abort().
2072 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2073 const struct sctp_endpoint *ep,
2074 const struct sctp_association *asoc,
2075 const sctp_subtype_t type,
2077 sctp_cmd_seq_t *commands)
2079 struct sctp_chunk *chunk = arg;
2081 if (!sctp_vtag_verify_either(chunk, asoc))
2082 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2084 /* Make sure that the ABORT chunk has a valid length.
2085 * Since this is an ABORT chunk, we have to discard it
2086 * because of the following text:
2087 * RFC 2960, Section 3.3.7
2088 * If an endpoint receives an ABORT with a format error or for an
2089 * association that doesn't exist, it MUST silently discard it.
2090 * Because the length is "invalid", we can't really discard just
2091 * as we do not know its true length. So, to be safe, discard the
2094 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2095 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2097 /* ADD-IP: Special case for ABORT chunks
2098 * F4) One special consideration is that ABORT Chunks arriving
2099 * destined to the IP address being deleted MUST be
2100 * ignored (see Section 5.3.1 for further details).
2102 if (SCTP_ADDR_DEL ==
2103 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2104 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2106 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2110 * Process an ABORT. (SHUTDOWN-SENT state)
2112 * See sctp_sf_do_9_1_abort().
2114 sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
2115 const struct sctp_association *asoc,
2116 const sctp_subtype_t type,
2118 sctp_cmd_seq_t *commands)
2120 struct sctp_chunk *chunk = arg;
2122 if (!sctp_vtag_verify_either(chunk, asoc))
2123 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2125 /* Make sure that the ABORT chunk has a valid length.
2126 * Since this is an ABORT chunk, we have to discard it
2127 * because of the following text:
2128 * RFC 2960, Section 3.3.7
2129 * If an endpoint receives an ABORT with a format error or for an
2130 * association that doesn't exist, it MUST silently discard it.
2131 * Because the length is "invalid", we can't really discard just
2132 * as we do not know its true length. So, to be safe, discard the
2135 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2136 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2138 /* ADD-IP: Special case for ABORT chunks
2139 * F4) One special consideration is that ABORT Chunks arriving
2140 * destined to the IP address being deleted MUST be
2141 * ignored (see Section 5.3.1 for further details).
2143 if (SCTP_ADDR_DEL ==
2144 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2145 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2147 /* Stop the T2-shutdown timer. */
2148 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2149 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2151 /* Stop the T5-shutdown guard timer. */
2152 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2153 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2155 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2159 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2161 * See sctp_sf_do_9_1_abort().
2163 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2164 const struct sctp_endpoint *ep,
2165 const struct sctp_association *asoc,
2166 const sctp_subtype_t type,
2168 sctp_cmd_seq_t *commands)
2170 /* The same T2 timer, so we should be able to use
2171 * common function with the SHUTDOWN-SENT state.
2173 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2177 * Handle an Error received in COOKIE_ECHOED state.
2179 * Only handle the error type of stale COOKIE Error, the other errors will
2183 * (endpoint, asoc, chunk)
2186 * (asoc, reply_msg, msg_up, timers, counters)
2188 * The return value is the disposition of the chunk.
2190 sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2191 const struct sctp_association *asoc,
2192 const sctp_subtype_t type,
2194 sctp_cmd_seq_t *commands)
2196 struct sctp_chunk *chunk = arg;
2199 if (!sctp_vtag_verify(chunk, asoc))
2200 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2202 /* Make sure that the ERROR chunk has a valid length.
2203 * The parameter walking depends on this as well.
2205 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2206 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2209 /* Process the error here */
2210 /* FUTURE FIXME: When PR-SCTP related and other optional
2211 * parms are emitted, this will have to change to handle multiple
2214 sctp_walk_errors(err, chunk->chunk_hdr) {
2215 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2216 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
2220 /* It is possible to have malformed error causes, and that
2221 * will cause us to end the walk early. However, since
2222 * we are discarding the packet, there should be no adverse
2225 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2229 * Handle a Stale COOKIE Error
2231 * Section: 5.2.6 Handle Stale COOKIE Error
2232 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2233 * one of the following three alternatives.
2235 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2236 * Preservative parameter requesting an extension to the lifetime of
2237 * the State Cookie. When calculating the time extension, an
2238 * implementation SHOULD use the RTT information measured based on the
2239 * previous COOKIE ECHO / ERROR exchange, and should add no more
2240 * than 1 second beyond the measured RTT, due to long State Cookie
2241 * lifetimes making the endpoint more subject to a replay attack.
2243 * Verification Tag: Not explicit, but safe to ignore.
2246 * (endpoint, asoc, chunk)
2249 * (asoc, reply_msg, msg_up, timers, counters)
2251 * The return value is the disposition of the chunk.
2253 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2254 const struct sctp_association *asoc,
2255 const sctp_subtype_t type,
2257 sctp_cmd_seq_t *commands)
2259 struct sctp_chunk *chunk = arg;
2261 sctp_cookie_preserve_param_t bht;
2263 struct sctp_chunk *reply;
2264 struct sctp_bind_addr *bp;
2265 int attempts = asoc->init_err_counter + 1;
2267 if (attempts > asoc->max_init_attempts) {
2268 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2269 SCTP_ERROR(ETIMEDOUT));
2270 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2271 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2272 return SCTP_DISPOSITION_DELETE_TCB;
2275 err = (sctp_errhdr_t *)(chunk->skb->data);
2277 /* When calculating the time extension, an implementation
2278 * SHOULD use the RTT information measured based on the
2279 * previous COOKIE ECHO / ERROR exchange, and should add no
2280 * more than 1 second beyond the measured RTT, due to long
2281 * State Cookie lifetimes making the endpoint more subject to
2283 * Measure of Staleness's unit is usec. (1/1000000 sec)
2284 * Suggested Cookie Life-span Increment's unit is msec.
2286 * In general, if you use the suggested cookie life, the value
2287 * found in the field of measure of staleness should be doubled
2288 * to give ample time to retransmit the new cookie and thus
2289 * yield a higher probability of success on the reattempt.
2291 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2292 stale = (stale * 2) / 1000;
2294 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2295 bht.param_hdr.length = htons(sizeof(bht));
2296 bht.lifespan_increment = htonl(stale);
2298 /* Build that new INIT chunk. */
2299 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2300 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2304 sctp_addto_chunk(reply, sizeof(bht), &bht);
2306 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2307 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2309 /* Stop pending T3-rtx and heartbeat timers */
2310 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2311 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2313 /* Delete non-primary peer ip addresses since we are transitioning
2314 * back to the COOKIE-WAIT state
2316 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2318 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2321 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2322 SCTP_TRANSPORT(asoc->peer.primary_path));
2324 /* Cast away the const modifier, as we want to just
2325 * rerun it through as a sideffect.
2327 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2329 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2330 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2331 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2332 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2333 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2334 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2336 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2338 return SCTP_DISPOSITION_CONSUME;
2341 return SCTP_DISPOSITION_NOMEM;
2348 * After checking the Verification Tag, the receiving endpoint shall
2349 * remove the association from its record, and shall report the
2350 * termination to its upper layer.
2352 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2353 * B) Rules for packet carrying ABORT:
2355 * - The endpoint shall always fill in the Verification Tag field of the
2356 * outbound packet with the destination endpoint's tag value if it
2359 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2360 * MUST follow the procedure described in Section 8.4.
2362 * - The receiver MUST accept the packet if the Verification Tag
2363 * matches either its own tag, OR the tag of its peer. Otherwise, the
2364 * receiver MUST silently discard the packet and take no further
2368 * (endpoint, asoc, chunk)
2371 * (asoc, reply_msg, msg_up, timers, counters)
2373 * The return value is the disposition of the chunk.
2375 sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2376 const struct sctp_association *asoc,
2377 const sctp_subtype_t type,
2379 sctp_cmd_seq_t *commands)
2381 struct sctp_chunk *chunk = arg;
2383 if (!sctp_vtag_verify_either(chunk, asoc))
2384 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2386 /* Make sure that the ABORT chunk has a valid length.
2387 * Since this is an ABORT chunk, we have to discard it
2388 * because of the following text:
2389 * RFC 2960, Section 3.3.7
2390 * If an endpoint receives an ABORT with a format error or for an
2391 * association that doesn't exist, it MUST silently discard it.
2392 * Because the length is "invalid", we can't really discard just
2393 * as we do not know its true length. So, to be safe, discard the
2396 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2397 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2399 /* ADD-IP: Special case for ABORT chunks
2400 * F4) One special consideration is that ABORT Chunks arriving
2401 * destined to the IP address being deleted MUST be
2402 * ignored (see Section 5.3.1 for further details).
2404 if (SCTP_ADDR_DEL ==
2405 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2406 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
2408 return __sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2411 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2412 const struct sctp_association *asoc,
2413 const sctp_subtype_t type,
2415 sctp_cmd_seq_t *commands)
2417 struct sctp_chunk *chunk = arg;
2419 __be16 error = SCTP_ERROR_NO_ERROR;
2421 /* See if we have an error cause code in the chunk. */
2422 len = ntohs(chunk->chunk_hdr->length);
2423 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2426 sctp_walk_errors(err, chunk->chunk_hdr);
2427 if ((void *)err != (void *)chunk->chunk_end)
2428 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2430 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2433 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2434 /* ASSOC_FAILED will DELETE_TCB. */
2435 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2436 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2437 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2439 return SCTP_DISPOSITION_ABORT;
2443 * Process an ABORT. (COOKIE-WAIT state)
2445 * See sctp_sf_do_9_1_abort() above.
2447 sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2448 const struct sctp_association *asoc,
2449 const sctp_subtype_t type,
2451 sctp_cmd_seq_t *commands)
2453 struct sctp_chunk *chunk = arg;
2455 __be16 error = SCTP_ERROR_NO_ERROR;
2457 if (!sctp_vtag_verify_either(chunk, asoc))
2458 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2460 /* Make sure that the ABORT chunk has a valid length.
2461 * Since this is an ABORT chunk, we have to discard it
2462 * because of the following text:
2463 * RFC 2960, Section 3.3.7
2464 * If an endpoint receives an ABORT with a format error or for an
2465 * association that doesn't exist, it MUST silently discard it.
2466 * Because the length is "invalid", we can't really discard just
2467 * as we do not know its true length. So, to be safe, discard the
2470 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2471 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2473 /* See if we have an error cause code in the chunk. */
2474 len = ntohs(chunk->chunk_hdr->length);
2475 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2476 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2478 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2483 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2485 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2486 const struct sctp_association *asoc,
2487 const sctp_subtype_t type,
2489 sctp_cmd_seq_t *commands)
2491 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2493 (struct sctp_transport *)arg);
2497 * Process an ABORT. (COOKIE-ECHOED state)
2499 sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2500 const struct sctp_association *asoc,
2501 const sctp_subtype_t type,
2503 sctp_cmd_seq_t *commands)
2505 /* There is a single T1 timer, so we should be able to use
2506 * common function with the COOKIE-WAIT state.
2508 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2512 * Stop T1 timer and abort association with "INIT failed".
2514 * This is common code called by several sctp_sf_*_abort() functions above.
2516 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
2517 __be16 error, int sk_err,
2518 const struct sctp_association *asoc,
2519 struct sctp_transport *transport)
2521 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2522 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2523 SCTP_STATE(SCTP_STATE_CLOSED));
2524 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2525 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2526 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2527 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2528 /* CMD_INIT_FAILED will DELETE_TCB. */
2529 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2531 return SCTP_DISPOSITION_ABORT;
2535 * sctp_sf_do_9_2_shut
2538 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2539 * - enter the SHUTDOWN-RECEIVED state,
2541 * - stop accepting new data from its SCTP user
2543 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2544 * that all its outstanding DATA chunks have been received by the
2547 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2548 * send a SHUTDOWN in response to a ULP request. And should discard
2549 * subsequent SHUTDOWN chunks.
2551 * If there are still outstanding DATA chunks left, the SHUTDOWN
2552 * receiver shall continue to follow normal data transmission
2553 * procedures defined in Section 6 until all outstanding DATA chunks
2554 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2555 * new data from its SCTP user.
2557 * Verification Tag: 8.5 Verification Tag [Normal verification]
2560 * (endpoint, asoc, chunk)
2563 * (asoc, reply_msg, msg_up, timers, counters)
2565 * The return value is the disposition of the chunk.
2567 sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2568 const struct sctp_association *asoc,
2569 const sctp_subtype_t type,
2571 sctp_cmd_seq_t *commands)
2573 struct sctp_chunk *chunk = arg;
2574 sctp_shutdownhdr_t *sdh;
2575 sctp_disposition_t disposition;
2576 struct sctp_ulpevent *ev;
2579 if (!sctp_vtag_verify(chunk, asoc))
2580 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2582 /* Make sure that the SHUTDOWN chunk has a valid length. */
2583 if (!sctp_chunk_length_valid(chunk,
2584 sizeof(struct sctp_shutdown_chunk_t)))
2585 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2588 /* Convert the elaborate header. */
2589 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2590 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2591 chunk->subh.shutdown_hdr = sdh;
2592 ctsn = ntohl(sdh->cum_tsn_ack);
2594 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2595 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2596 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2597 return SCTP_DISPOSITION_DISCARD;
2600 /* If Cumulative TSN Ack beyond the max tsn currently
2601 * send, terminating the association and respond to the
2602 * sender with an ABORT.
2604 if (!TSN_lt(ctsn, asoc->next_tsn))
2605 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2607 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2608 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2609 * inform the application that it should cease sending data.
2611 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2613 disposition = SCTP_DISPOSITION_NOMEM;
2616 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2618 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2619 * - enter the SHUTDOWN-RECEIVED state,
2620 * - stop accepting new data from its SCTP user
2622 * [This is implicit in the new state.]
2624 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2625 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2626 disposition = SCTP_DISPOSITION_CONSUME;
2628 if (sctp_outq_is_empty(&asoc->outqueue)) {
2629 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2633 if (SCTP_DISPOSITION_NOMEM == disposition)
2636 /* - verify, by checking the Cumulative TSN Ack field of the
2637 * chunk, that all its outstanding DATA chunks have been
2638 * received by the SHUTDOWN sender.
2640 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2641 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2648 * sctp_sf_do_9_2_shut_ctsn
2650 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2651 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2652 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2653 * MUST be processed.
2655 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(const struct sctp_endpoint *ep,
2656 const struct sctp_association *asoc,
2657 const sctp_subtype_t type,
2659 sctp_cmd_seq_t *commands)
2661 struct sctp_chunk *chunk = arg;
2662 sctp_shutdownhdr_t *sdh;
2665 if (!sctp_vtag_verify(chunk, asoc))
2666 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2668 /* Make sure that the SHUTDOWN chunk has a valid length. */
2669 if (!sctp_chunk_length_valid(chunk,
2670 sizeof(struct sctp_shutdown_chunk_t)))
2671 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2674 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2675 ctsn = ntohl(sdh->cum_tsn_ack);
2677 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2678 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2679 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2680 return SCTP_DISPOSITION_DISCARD;
2683 /* If Cumulative TSN Ack beyond the max tsn currently
2684 * send, terminating the association and respond to the
2685 * sender with an ABORT.
2687 if (!TSN_lt(ctsn, asoc->next_tsn))
2688 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
2690 /* verify, by checking the Cumulative TSN Ack field of the
2691 * chunk, that all its outstanding DATA chunks have been
2692 * received by the SHUTDOWN sender.
2694 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2695 SCTP_BE32(sdh->cum_tsn_ack));
2697 return SCTP_DISPOSITION_CONSUME;
2701 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2702 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2703 * transport addresses (either in the IP addresses or in the INIT chunk)
2704 * that belong to this association, it should discard the INIT chunk and
2705 * retransmit the SHUTDOWN ACK chunk.
2707 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2708 const struct sctp_association *asoc,
2709 const sctp_subtype_t type,
2711 sctp_cmd_seq_t *commands)
2713 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2714 struct sctp_chunk *reply;
2716 /* Make sure that the chunk has a valid length */
2717 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2718 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2721 /* Since we are not going to really process this INIT, there
2722 * is no point in verifying chunk boundries. Just generate
2725 reply = sctp_make_shutdown_ack(asoc, chunk);
2729 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2730 * the T2-SHUTDOWN timer.
2732 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2734 /* and restart the T2-shutdown timer. */
2735 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2736 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2738 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2740 return SCTP_DISPOSITION_CONSUME;
2742 return SCTP_DISPOSITION_NOMEM;
2746 * sctp_sf_do_ecn_cwr
2748 * Section: Appendix A: Explicit Congestion Notification
2752 * RFC 2481 details a specific bit for a sender to send in the header of
2753 * its next outbound TCP segment to indicate to its peer that it has
2754 * reduced its congestion window. This is termed the CWR bit. For
2755 * SCTP the same indication is made by including the CWR chunk.
2756 * This chunk contains one data element, i.e. the TSN number that
2757 * was sent in the ECNE chunk. This element represents the lowest
2758 * TSN number in the datagram that was originally marked with the
2761 * Verification Tag: 8.5 Verification Tag [Normal verification]
2763 * (endpoint, asoc, chunk)
2766 * (asoc, reply_msg, msg_up, timers, counters)
2768 * The return value is the disposition of the chunk.
2770 sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2771 const struct sctp_association *asoc,
2772 const sctp_subtype_t type,
2774 sctp_cmd_seq_t *commands)
2777 struct sctp_chunk *chunk = arg;
2780 if (!sctp_vtag_verify(chunk, asoc))
2781 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2783 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2784 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2787 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2788 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2790 lowest_tsn = ntohl(cwr->lowest_tsn);
2792 /* Does this CWR ack the last sent congestion notification? */
2793 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2794 /* Stop sending ECNE. */
2795 sctp_add_cmd_sf(commands,
2797 SCTP_U32(lowest_tsn));
2799 return SCTP_DISPOSITION_CONSUME;
2805 * Section: Appendix A: Explicit Congestion Notification
2809 * RFC 2481 details a specific bit for a receiver to send back in its
2810 * TCP acknowledgements to notify the sender of the Congestion
2811 * Experienced (CE) bit having arrived from the network. For SCTP this
2812 * same indication is made by including the ECNE chunk. This chunk
2813 * contains one data element, i.e. the lowest TSN associated with the IP
2814 * datagram marked with the CE bit.....
2816 * Verification Tag: 8.5 Verification Tag [Normal verification]
2818 * (endpoint, asoc, chunk)
2821 * (asoc, reply_msg, msg_up, timers, counters)
2823 * The return value is the disposition of the chunk.
2825 sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2826 const struct sctp_association *asoc,
2827 const sctp_subtype_t type,
2829 sctp_cmd_seq_t *commands)
2831 sctp_ecnehdr_t *ecne;
2832 struct sctp_chunk *chunk = arg;
2834 if (!sctp_vtag_verify(chunk, asoc))
2835 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2837 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2838 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2841 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2842 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2844 /* If this is a newer ECNE than the last CWR packet we sent out */
2845 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2846 SCTP_U32(ntohl(ecne->lowest_tsn)));
2848 return SCTP_DISPOSITION_CONSUME;
2852 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2854 * The SCTP endpoint MUST always acknowledge the reception of each valid
2857 * The guidelines on delayed acknowledgement algorithm specified in
2858 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2859 * acknowledgement SHOULD be generated for at least every second packet
2860 * (not every second DATA chunk) received, and SHOULD be generated within
2861 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2862 * situations it may be beneficial for an SCTP transmitter to be more
2863 * conservative than the algorithms detailed in this document allow.
2864 * However, an SCTP transmitter MUST NOT be more aggressive than the
2865 * following algorithms allow.
2867 * A SCTP receiver MUST NOT generate more than one SACK for every
2868 * incoming packet, other than to update the offered window as the
2869 * receiving application consumes new data.
2871 * Verification Tag: 8.5 Verification Tag [Normal verification]
2874 * (endpoint, asoc, chunk)
2877 * (asoc, reply_msg, msg_up, timers, counters)
2879 * The return value is the disposition of the chunk.
2881 sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2882 const struct sctp_association *asoc,
2883 const sctp_subtype_t type,
2885 sctp_cmd_seq_t *commands)
2887 struct sctp_chunk *chunk = arg;
2888 sctp_arg_t force = SCTP_NOFORCE();
2891 if (!sctp_vtag_verify(chunk, asoc)) {
2892 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2894 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2897 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2898 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2901 error = sctp_eat_data(asoc, chunk, commands );
2903 case SCTP_IERROR_NO_ERROR:
2905 case SCTP_IERROR_HIGH_TSN:
2906 case SCTP_IERROR_BAD_STREAM:
2907 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2908 goto discard_noforce;
2909 case SCTP_IERROR_DUP_TSN:
2910 case SCTP_IERROR_IGNORE_TSN:
2911 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2913 case SCTP_IERROR_NO_DATA:
2915 case SCTP_IERROR_PROTO_VIOLATION:
2916 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
2917 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2922 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2923 force = SCTP_FORCE();
2925 if (asoc->autoclose) {
2926 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2927 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2930 /* If this is the last chunk in a packet, we need to count it
2931 * toward sack generation. Note that we need to SACK every
2932 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2933 * THEM. We elect to NOT generate SACK's if the chunk fails
2934 * the verification tag test.
2936 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2938 * The SCTP endpoint MUST always acknowledge the reception of
2939 * each valid DATA chunk.
2941 * The guidelines on delayed acknowledgement algorithm
2942 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2943 * Specifically, an acknowledgement SHOULD be generated for at
2944 * least every second packet (not every second DATA chunk)
2945 * received, and SHOULD be generated within 200 ms of the
2946 * arrival of any unacknowledged DATA chunk. In some
2947 * situations it may be beneficial for an SCTP transmitter to
2948 * be more conservative than the algorithms detailed in this
2949 * document allow. However, an SCTP transmitter MUST NOT be
2950 * more aggressive than the following algorithms allow.
2952 if (chunk->end_of_packet)
2953 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
2955 return SCTP_DISPOSITION_CONSUME;
2958 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2960 * When a packet arrives with duplicate DATA chunk(s) and with
2961 * no new DATA chunk(s), the endpoint MUST immediately send a
2962 * SACK with no delay. If a packet arrives with duplicate
2963 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2964 * MAY immediately send a SACK. Normally receipt of duplicate
2965 * DATA chunks will occur when the original SACK chunk was lost
2966 * and the peer's RTO has expired. The duplicate TSN number(s)
2967 * SHOULD be reported in the SACK as duplicate.
2969 /* In our case, we split the MAY SACK advice up whether or not
2970 * the last chunk is a duplicate.'
2972 if (chunk->end_of_packet)
2973 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2974 return SCTP_DISPOSITION_DISCARD;
2977 if (chunk->end_of_packet)
2978 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
2980 return SCTP_DISPOSITION_DISCARD;
2982 return SCTP_DISPOSITION_CONSUME;
2987 * sctp_sf_eat_data_fast_4_4
2990 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2991 * DATA chunks without delay.
2993 * Verification Tag: 8.5 Verification Tag [Normal verification]
2995 * (endpoint, asoc, chunk)
2998 * (asoc, reply_msg, msg_up, timers, counters)
3000 * The return value is the disposition of the chunk.
3002 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
3003 const struct sctp_association *asoc,
3004 const sctp_subtype_t type,
3006 sctp_cmd_seq_t *commands)
3008 struct sctp_chunk *chunk = arg;
3011 if (!sctp_vtag_verify(chunk, asoc)) {
3012 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3014 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3017 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3018 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3021 error = sctp_eat_data(asoc, chunk, commands );
3023 case SCTP_IERROR_NO_ERROR:
3024 case SCTP_IERROR_HIGH_TSN:
3025 case SCTP_IERROR_DUP_TSN:
3026 case SCTP_IERROR_IGNORE_TSN:
3027 case SCTP_IERROR_BAD_STREAM:
3029 case SCTP_IERROR_NO_DATA:
3031 case SCTP_IERROR_PROTO_VIOLATION:
3032 return sctp_sf_abort_violation(ep, asoc, chunk, commands,
3033 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3038 /* Go a head and force a SACK, since we are shutting down. */
3040 /* Implementor's Guide.
3042 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3043 * respond to each received packet containing one or more DATA chunk(s)
3044 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3046 if (chunk->end_of_packet) {
3047 /* We must delay the chunk creation since the cumulative
3048 * TSN has not been updated yet.
3050 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3051 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3052 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3053 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3057 return SCTP_DISPOSITION_CONSUME;
3061 * Section: 6.2 Processing a Received SACK
3062 * D) Any time a SACK arrives, the endpoint performs the following:
3064 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3065 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3066 * increasing, a SACK whose Cumulative TSN Ack is less than the
3067 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3069 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3070 * of bytes still outstanding after processing the Cumulative TSN Ack
3071 * and the Gap Ack Blocks.
3073 * iii) If the SACK is missing a TSN that was previously
3074 * acknowledged via a Gap Ack Block (e.g., the data receiver
3075 * reneged on the data), then mark the corresponding DATA chunk
3076 * as available for retransmit: Mark it as missing for fast
3077 * retransmit as described in Section 7.2.4 and if no retransmit
3078 * timer is running for the destination address to which the DATA
3079 * chunk was originally transmitted, then T3-rtx is started for
3080 * that destination address.
3082 * Verification Tag: 8.5 Verification Tag [Normal verification]
3085 * (endpoint, asoc, chunk)
3088 * (asoc, reply_msg, msg_up, timers, counters)
3090 * The return value is the disposition of the chunk.
3092 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
3093 const struct sctp_association *asoc,
3094 const sctp_subtype_t type,
3096 sctp_cmd_seq_t *commands)
3098 struct sctp_chunk *chunk = arg;
3099 sctp_sackhdr_t *sackh;
3102 if (!sctp_vtag_verify(chunk, asoc))
3103 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3105 /* Make sure that the SACK chunk has a valid length. */
3106 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3107 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3110 /* Pull the SACK chunk from the data buffer */
3111 sackh = sctp_sm_pull_sack(chunk);
3112 /* Was this a bogus SACK? */
3114 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3115 chunk->subh.sack_hdr = sackh;
3116 ctsn = ntohl(sackh->cum_tsn_ack);
3118 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3119 * Ack Point, then drop the SACK. Since Cumulative TSN
3120 * Ack is monotonically increasing, a SACK whose
3121 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3122 * Point indicates an out-of-order SACK.
3124 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3125 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3126 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3127 return SCTP_DISPOSITION_DISCARD;
3130 /* If Cumulative TSN Ack beyond the max tsn currently
3131 * send, terminating the association and respond to the
3132 * sender with an ABORT.
3134 if (!TSN_lt(ctsn, asoc->next_tsn))
3135 return sctp_sf_violation_ctsn(ep, asoc, type, arg, commands);
3137 /* Return this SACK for further processing. */
3138 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
3140 /* Note: We do the rest of the work on the PROCESS_SACK
3143 return SCTP_DISPOSITION_CONSUME;
3147 * Generate an ABORT in response to a packet.
3149 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3151 * 8) The receiver should respond to the sender of the OOTB packet with
3152 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3153 * MUST fill in the Verification Tag field of the outbound packet
3154 * with the value found in the Verification Tag field of the OOTB
3155 * packet and set the T-bit in the Chunk Flags to indicate that the
3156 * Verification Tag is reflected. After sending this ABORT, the
3157 * receiver of the OOTB packet shall discard the OOTB packet and take
3158 * no further action.
3162 * The return value is the disposition of the chunk.
3164 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
3165 const struct sctp_association *asoc,
3166 const sctp_subtype_t type,
3168 sctp_cmd_seq_t *commands)
3170 struct sctp_packet *packet = NULL;
3171 struct sctp_chunk *chunk = arg;
3172 struct sctp_chunk *abort;
3175 net = sock_net(ep->base.sk);
3176 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3179 /* Make an ABORT. The T bit will be set if the asoc
3182 abort = sctp_make_abort(asoc, chunk, 0);
3184 sctp_ootb_pkt_free(packet);
3185 return SCTP_DISPOSITION_NOMEM;
3188 /* Reflect vtag if T-Bit is set */
3189 if (sctp_test_T_bit(abort))
3190 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3192 /* Set the skb to the belonging sock for accounting. */
3193 abort->skb->sk = ep->base.sk;
3195 sctp_packet_append_chunk(packet, abort);
3197 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3198 SCTP_PACKET(packet));
3200 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3202 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3203 return SCTP_DISPOSITION_CONSUME;
3206 return SCTP_DISPOSITION_NOMEM;
3210 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3211 * event as ULP notification for each cause included in the chunk.
3213 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3215 * The return value is the disposition of the chunk.
3217 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3218 const struct sctp_association *asoc,
3219 const sctp_subtype_t type,
3221 sctp_cmd_seq_t *commands)
3223 struct sctp_chunk *chunk = arg;
3226 if (!sctp_vtag_verify(chunk, asoc))
3227 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3229 /* Make sure that the ERROR chunk has a valid length. */
3230 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3231 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3233 sctp_walk_errors(err, chunk->chunk_hdr);
3234 if ((void *)err != (void *)chunk->chunk_end)
3235 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3236 (void *)err, commands);
3238 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3241 return SCTP_DISPOSITION_CONSUME;
3245 * Process an inbound SHUTDOWN ACK.
3248 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3249 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3250 * peer, and remove all record of the association.
3252 * The return value is the disposition.
3254 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3255 const struct sctp_association *asoc,
3256 const sctp_subtype_t type,
3258 sctp_cmd_seq_t *commands)
3260 struct sctp_chunk *chunk = arg;
3261 struct sctp_chunk *reply;
3262 struct sctp_ulpevent *ev;
3264 if (!sctp_vtag_verify(chunk, asoc))
3265 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3267 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3268 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3269 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3271 /* 10.2 H) SHUTDOWN COMPLETE notification
3273 * When SCTP completes the shutdown procedures (section 9.2) this
3274 * notification is passed to the upper layer.
3276 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3277 0, 0, 0, NULL, GFP_ATOMIC);
3281 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3282 reply = sctp_make_shutdown_complete(asoc, chunk);
3286 /* Do all the commands now (after allocation), so that we
3287 * have consistent state if memory allocation failes
3289 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3291 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3292 * stop the T2-shutdown timer,
3294 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3295 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3297 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3298 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3300 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3301 SCTP_STATE(SCTP_STATE_CLOSED));
3302 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3303 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3304 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3306 /* ...and remove all record of the association. */
3307 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3308 return SCTP_DISPOSITION_DELETE_TCB;
3311 sctp_ulpevent_free(ev);
3313 return SCTP_DISPOSITION_NOMEM;
3317 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3319 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3320 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3321 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3322 * packet must fill in the Verification Tag field of the outbound
3323 * packet with the Verification Tag received in the SHUTDOWN ACK and
3324 * set the T-bit in the Chunk Flags to indicate that the Verification
3327 * 8) The receiver should respond to the sender of the OOTB packet with
3328 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3329 * MUST fill in the Verification Tag field of the outbound packet
3330 * with the value found in the Verification Tag field of the OOTB
3331 * packet and set the T-bit in the Chunk Flags to indicate that the
3332 * Verification Tag is reflected. After sending this ABORT, the
3333 * receiver of the OOTB packet shall discard the OOTB packet and take
3334 * no further action.
3336 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3337 const struct sctp_association *asoc,
3338 const sctp_subtype_t type,
3340 sctp_cmd_seq_t *commands)
3342 struct sctp_chunk *chunk = arg;
3343 struct sk_buff *skb = chunk->skb;
3344 sctp_chunkhdr_t *ch;
3347 int ootb_shut_ack = 0;
3348 int ootb_cookie_ack = 0;
3350 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3352 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3354 /* Report violation if the chunk is less then minimal */
3355 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3356 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3359 /* Now that we know we at least have a chunk header,
3360 * do things that are type appropriate.
3362 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3365 /* RFC 2960, Section 3.3.7
3366 * Moreover, under any circumstances, an endpoint that
3367 * receives an ABORT MUST NOT respond to that ABORT by
3368 * sending an ABORT of its own.
3370 if (SCTP_CID_ABORT == ch->type)
3371 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3373 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3374 * or a COOKIE ACK the SCTP Packet should be silently
3378 if (SCTP_CID_COOKIE_ACK == ch->type)
3379 ootb_cookie_ack = 1;
3381 if (SCTP_CID_ERROR == ch->type) {
3382 sctp_walk_errors(err, ch) {
3383 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3384 ootb_cookie_ack = 1;
3390 /* Report violation if chunk len overflows */
3391 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3392 if (ch_end > skb_tail_pointer(skb))
3393 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3396 ch = (sctp_chunkhdr_t *) ch_end;
3397 } while (ch_end < skb_tail_pointer(skb));
3400 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
3401 else if (ootb_cookie_ack)
3402 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3404 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
3408 * Handle an "Out of the blue" SHUTDOWN ACK.
3410 * Section: 8.4 5, sctpimpguide 2.41.
3412 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3413 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3414 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3415 * packet must fill in the Verification Tag field of the outbound
3416 * packet with the Verification Tag received in the SHUTDOWN ACK and
3417 * set the T-bit in the Chunk Flags to indicate that the Verification
3421 * (endpoint, asoc, type, arg, commands)
3424 * (sctp_disposition_t)
3426 * The return value is the disposition of the chunk.
3428 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3429 const struct sctp_association *asoc,
3430 const sctp_subtype_t type,
3432 sctp_cmd_seq_t *commands)
3434 struct sctp_packet *packet = NULL;
3435 struct sctp_chunk *chunk = arg;
3436 struct sctp_chunk *shut;
3439 net = sock_net(ep->base.sk);
3440 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3443 /* Make an SHUTDOWN_COMPLETE.
3444 * The T bit will be set if the asoc is NULL.
3446 shut = sctp_make_shutdown_complete(asoc, chunk);
3448 sctp_ootb_pkt_free(packet);
3449 return SCTP_DISPOSITION_NOMEM;
3452 /* Reflect vtag if T-Bit is set */
3453 if (sctp_test_T_bit(shut))
3454 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3456 /* Set the skb to the belonging sock for accounting. */
3457 shut->skb->sk = ep->base.sk;
3459 sctp_packet_append_chunk(packet, shut);
3461 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3462 SCTP_PACKET(packet));
3464 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3466 /* If the chunk length is invalid, we don't want to process
3467 * the reset of the packet.
3469 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3470 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3472 /* We need to discard the rest of the packet to prevent
3473 * potential bomming attacks from additional bundled chunks.
3474 * This is documented in SCTP Threats ID.
3476 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3479 return SCTP_DISPOSITION_NOMEM;
3483 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3485 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3486 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3487 * procedures in section 8.4 SHOULD be followed, in other words it
3488 * should be treated as an Out Of The Blue packet.
3489 * [This means that we do NOT check the Verification Tag on these
3493 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3494 const struct sctp_association *asoc,
3495 const sctp_subtype_t type,
3497 sctp_cmd_seq_t *commands)
3499 struct sctp_chunk *chunk = arg;
3501 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3502 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3503 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3506 /* Although we do have an association in this case, it corresponds
3507 * to a restarted association. So the packet is treated as an OOTB
3508 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3509 * called with a NULL association.
3511 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3513 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3516 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3517 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3518 const struct sctp_association *asoc,
3519 const sctp_subtype_t type, void *arg,
3520 sctp_cmd_seq_t *commands)
3522 struct sctp_chunk *chunk = arg;
3523 struct sctp_chunk *asconf_ack = NULL;
3524 struct sctp_paramhdr *err_param = NULL;
3525 sctp_addiphdr_t *hdr;
3526 union sctp_addr_param *addr_param;
3530 if (!sctp_vtag_verify(chunk, asoc)) {
3531 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3533 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3536 /* ADD-IP: Section 4.1.1
3537 * This chunk MUST be sent in an authenticated way by using
3538 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3539 * is received unauthenticated it MUST be silently discarded as
3540 * described in [I-D.ietf-tsvwg-sctp-auth].
3542 if (!sctp_addip_noauth && !chunk->auth)
3543 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3545 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3546 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3547 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3550 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3551 serial = ntohl(hdr->serial);
3553 addr_param = (union sctp_addr_param *)hdr->params;
3554 length = ntohs(addr_param->p.length);
3555 if (length < sizeof(sctp_paramhdr_t))
3556 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3557 (void *)addr_param, commands);
3559 /* Verify the ASCONF chunk before processing it. */
3560 if (!sctp_verify_asconf(asoc,
3561 (sctp_paramhdr_t *)((void *)addr_param + length),
3562 (void *)chunk->chunk_end,
3564 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3565 (void *)err_param, commands);
3567 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3568 * the endpoint stored in a new association variable
3569 * 'Peer-Serial-Number'.
3571 if (serial == asoc->peer.addip_serial + 1) {
3572 /* If this is the first instance of ASCONF in the packet,
3573 * we can clean our old ASCONF-ACKs.
3575 if (!chunk->has_asconf)
3576 sctp_assoc_clean_asconf_ack_cache(asoc);
3578 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3579 * expected, process the ASCONF as described below and after
3580 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3581 * the response packet and cache a copy of it (in the event it
3582 * later needs to be retransmitted).
3584 * Essentially, do V1-V5.
3586 asconf_ack = sctp_process_asconf((struct sctp_association *)
3589 return SCTP_DISPOSITION_NOMEM;
3590 } else if (serial < asoc->peer.addip_serial + 1) {
3592 * If the value found in the Sequence Number is less than the
3593 * ('Peer- Sequence-Number' + 1), simply skip to the next
3594 * ASCONF, and include in the outbound response packet
3595 * any previously cached ASCONF-ACK response that was
3596 * sent and saved that matches the Sequence Number of the
3597 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3598 * Chunk exists. This will occur when an older ASCONF
3599 * arrives out of order. In such a case, the receiver
3600 * should skip the ASCONF Chunk and not include ASCONF-ACK
3601 * Chunk for that chunk.
3603 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3605 return SCTP_DISPOSITION_DISCARD;
3607 /* Reset the transport so that we select the correct one
3608 * this time around. This is to make sure that we don't
3609 * accidentally use a stale transport that's been removed.
3611 asconf_ack->transport = NULL;
3613 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3614 * it must be either a stale packet or from an attacker.
3616 return SCTP_DISPOSITION_DISCARD;
3619 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3620 * containing the ASCONF-ACK Chunks MUST be the source address of
3621 * the SCTP packet that held the ASCONF Chunks.
3623 * To do this properly, we'll set the destination address of the chunk
3624 * and at the transmit time, will try look up the transport to use.
3625 * Since ASCONFs may be bundled, the correct transport may not be
3626 * created until we process the entire packet, thus this workaround.
3628 asconf_ack->dest = chunk->source;
3629 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3630 if (asoc->new_transport) {
3631 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3633 ((struct sctp_association *)asoc)->new_transport = NULL;
3636 return SCTP_DISPOSITION_CONSUME;
3640 * ADDIP Section 4.3 General rules for address manipulation
3641 * When building TLV parameters for the ASCONF Chunk that will add or
3642 * delete IP addresses the D0 to D13 rules should be applied:
3644 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3645 const struct sctp_association *asoc,
3646 const sctp_subtype_t type, void *arg,
3647 sctp_cmd_seq_t *commands)
3649 struct sctp_chunk *asconf_ack = arg;
3650 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3651 struct sctp_chunk *abort;
3652 struct sctp_paramhdr *err_param = NULL;
3653 sctp_addiphdr_t *addip_hdr;
3654 __u32 sent_serial, rcvd_serial;
3656 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3657 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3659 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3662 /* ADD-IP, Section 4.1.2:
3663 * This chunk MUST be sent in an authenticated way by using
3664 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3665 * is received unauthenticated it MUST be silently discarded as
3666 * described in [I-D.ietf-tsvwg-sctp-auth].
3668 if (!sctp_addip_noauth && !asconf_ack->auth)
3669 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3671 /* Make sure that the ADDIP chunk has a valid length. */
3672 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3673 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3676 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3677 rcvd_serial = ntohl(addip_hdr->serial);
3679 /* Verify the ASCONF-ACK chunk before processing it. */
3680 if (!sctp_verify_asconf(asoc,
3681 (sctp_paramhdr_t *)addip_hdr->params,
3682 (void *)asconf_ack->chunk_end,
3684 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3685 (void *)err_param, commands);
3688 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3689 sent_serial = ntohl(addip_hdr->serial);
3691 sent_serial = asoc->addip_serial - 1;
3694 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3695 * equal to the next serial number to be used but no ASCONF chunk is
3696 * outstanding the endpoint MUST ABORT the association. Note that a
3697 * sequence number is greater than if it is no more than 2^^31-1
3698 * larger than the current sequence number (using serial arithmetic).
3700 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3701 !(asoc->addip_last_asconf)) {
3702 abort = sctp_make_abort(asoc, asconf_ack,
3703 sizeof(sctp_errhdr_t));
3705 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3706 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3709 /* We are going to ABORT, so we might as well stop
3710 * processing the rest of the chunks in the packet.
3712 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3713 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3714 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3715 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3716 SCTP_ERROR(ECONNABORTED));
3717 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3718 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3719 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3720 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3721 return SCTP_DISPOSITION_ABORT;
3724 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3725 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3726 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3728 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3730 /* Successfully processed ASCONF_ACK. We can
3731 * release the next asconf if we have one.
3733 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3735 return SCTP_DISPOSITION_CONSUME;
3738 abort = sctp_make_abort(asoc, asconf_ack,
3739 sizeof(sctp_errhdr_t));
3741 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3742 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3745 /* We are going to ABORT, so we might as well stop
3746 * processing the rest of the chunks in the packet.
3748 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3749 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3750 SCTP_ERROR(ECONNABORTED));
3751 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3752 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3753 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3754 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3755 return SCTP_DISPOSITION_ABORT;
3758 return SCTP_DISPOSITION_DISCARD;
3762 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3764 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3765 * its cumulative TSN point to the value carried in the FORWARD TSN
3766 * chunk, and then MUST further advance its cumulative TSN point locally
3768 * After the above processing, the data receiver MUST stop reporting any
3769 * missing TSNs earlier than or equal to the new cumulative TSN point.
3771 * Verification Tag: 8.5 Verification Tag [Normal verification]
3773 * The return value is the disposition of the chunk.
3775 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3776 const struct sctp_association *asoc,
3777 const sctp_subtype_t type,
3779 sctp_cmd_seq_t *commands)
3781 struct sctp_chunk *chunk = arg;
3782 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3783 struct sctp_fwdtsn_skip *skip;
3787 if (!sctp_vtag_verify(chunk, asoc)) {
3788 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3790 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3793 /* Make sure that the FORWARD_TSN chunk has valid length. */
3794 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3795 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3798 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3799 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3800 len = ntohs(chunk->chunk_hdr->length);
3801 len -= sizeof(struct sctp_chunkhdr);
3802 skb_pull(chunk->skb, len);
3804 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3805 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3807 /* The TSN is too high--silently discard the chunk and count on it
3808 * getting retransmitted later.
3810 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3811 goto discard_noforce;
3813 /* Silently discard the chunk if stream-id is not valid */
3814 sctp_walk_fwdtsn(skip, chunk) {
3815 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3816 goto discard_noforce;
3819 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3820 if (len > sizeof(struct sctp_fwdtsn_hdr))
3821 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3824 /* Count this as receiving DATA. */
3825 if (asoc->autoclose) {
3826 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3827 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3830 /* FIXME: For now send a SACK, but DATA processing may
3833 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3835 return SCTP_DISPOSITION_CONSUME;
3838 return SCTP_DISPOSITION_DISCARD;
3841 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3842 const struct sctp_endpoint *ep,
3843 const struct sctp_association *asoc,
3844 const sctp_subtype_t type,
3846 sctp_cmd_seq_t *commands)
3848 struct sctp_chunk *chunk = arg;
3849 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3850 struct sctp_fwdtsn_skip *skip;
3854 if (!sctp_vtag_verify(chunk, asoc)) {
3855 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3857 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3860 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3861 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3862 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3865 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3866 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3867 len = ntohs(chunk->chunk_hdr->length);
3868 len -= sizeof(struct sctp_chunkhdr);
3869 skb_pull(chunk->skb, len);
3871 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3872 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3874 /* The TSN is too high--silently discard the chunk and count on it
3875 * getting retransmitted later.
3877 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3880 /* Silently discard the chunk if stream-id is not valid */
3881 sctp_walk_fwdtsn(skip, chunk) {
3882 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3886 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3887 if (len > sizeof(struct sctp_fwdtsn_hdr))
3888 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3891 /* Go a head and force a SACK, since we are shutting down. */
3893 /* Implementor's Guide.
3895 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3896 * respond to each received packet containing one or more DATA chunk(s)
3897 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3899 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3900 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3901 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3902 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3904 return SCTP_DISPOSITION_CONSUME;
3908 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3910 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3911 * Identifier field. If this algorithm was not specified by the
3912 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3913 * during association setup, the AUTH chunk and all chunks after it MUST
3914 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3915 * defined in Section 4.1.
3917 * If an endpoint with no shared key receives a Shared Key Identifier
3918 * other than 0, it MUST silently discard all authenticated chunks. If
3919 * the endpoint has at least one endpoint pair shared key for the peer,
3920 * it MUST use the key specified by the Shared Key Identifier if a
3921 * key has been configured for that Shared Key Identifier. If no
3922 * endpoint pair shared key has been configured for that Shared Key
3923 * Identifier, all authenticated chunks MUST be silently discarded.
3925 * Verification Tag: 8.5 Verification Tag [Normal verification]
3927 * The return value is the disposition of the chunk.
3929 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3930 const struct sctp_association *asoc,
3931 const sctp_subtype_t type,
3932 struct sctp_chunk *chunk)
3934 struct sctp_authhdr *auth_hdr;
3935 struct sctp_hmac *hmac;
3936 unsigned int sig_len;
3941 /* Pull in the auth header, so we can do some more verification */
3942 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3943 chunk->subh.auth_hdr = auth_hdr;
3944 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3946 /* Make sure that we suport the HMAC algorithm from the auth
3949 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3950 return SCTP_IERROR_AUTH_BAD_HMAC;
3952 /* Make sure that the provided shared key identifier has been
3955 key_id = ntohs(auth_hdr->shkey_id);
3956 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3957 return SCTP_IERROR_AUTH_BAD_KEYID;
3960 /* Make sure that the length of the signature matches what
3963 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3964 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3965 if (sig_len != hmac->hmac_len)
3966 return SCTP_IERROR_PROTO_VIOLATION;
3968 /* Now that we've done validation checks, we can compute and
3969 * verify the hmac. The steps involved are:
3970 * 1. Save the digest from the chunk.
3971 * 2. Zero out the digest in the chunk.
3972 * 3. Compute the new digest
3973 * 4. Compare saved and new digests.
3975 digest = auth_hdr->hmac;
3976 skb_pull(chunk->skb, sig_len);
3978 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3982 memset(digest, 0, sig_len);
3984 sctp_auth_calculate_hmac(asoc, chunk->skb,
3985 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3988 /* Discard the packet if the digests do not match */
3989 if (memcmp(save_digest, digest, sig_len)) {
3991 return SCTP_IERROR_BAD_SIG;
3997 return SCTP_IERROR_NO_ERROR;
3999 return SCTP_IERROR_NOMEM;
4002 sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
4003 const struct sctp_association *asoc,
4004 const sctp_subtype_t type,
4006 sctp_cmd_seq_t *commands)
4008 struct sctp_authhdr *auth_hdr;
4009 struct sctp_chunk *chunk = arg;
4010 struct sctp_chunk *err_chunk;
4011 sctp_ierror_t error;
4013 /* Make sure that the peer has AUTH capable */
4014 if (!asoc->peer.auth_capable)
4015 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
4017 if (!sctp_vtag_verify(chunk, asoc)) {
4018 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4020 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4023 /* Make sure that the AUTH chunk has valid length. */
4024 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4025 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4028 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4029 error = sctp_sf_authenticate(ep, asoc, type, chunk);
4031 case SCTP_IERROR_AUTH_BAD_HMAC:
4032 /* Generate the ERROR chunk and discard the rest
4035 err_chunk = sctp_make_op_error(asoc, chunk,
4036 SCTP_ERROR_UNSUP_HMAC,
4040 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4041 SCTP_CHUNK(err_chunk));
4044 case SCTP_IERROR_AUTH_BAD_KEYID:
4045 case SCTP_IERROR_BAD_SIG:
4046 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4048 case SCTP_IERROR_PROTO_VIOLATION:
4049 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4052 case SCTP_IERROR_NOMEM:
4053 return SCTP_DISPOSITION_NOMEM;
4055 default: /* Prevent gcc warnings */
4059 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4060 struct sctp_ulpevent *ev;
4062 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4063 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4068 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4072 return SCTP_DISPOSITION_CONSUME;
4076 * Process an unknown chunk.
4078 * Section: 3.2. Also, 2.1 in the implementor's guide.
4080 * Chunk Types are encoded such that the highest-order two bits specify
4081 * the action that must be taken if the processing endpoint does not
4082 * recognize the Chunk Type.
4084 * 00 - Stop processing this SCTP packet and discard it, do not process
4085 * any further chunks within it.
4087 * 01 - Stop processing this SCTP packet and discard it, do not process
4088 * any further chunks within it, and report the unrecognized
4089 * chunk in an 'Unrecognized Chunk Type'.
4091 * 10 - Skip this chunk and continue processing.
4093 * 11 - Skip this chunk and continue processing, but report in an ERROR
4094 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4096 * The return value is the disposition of the chunk.
4098 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4099 const struct sctp_association *asoc,
4100 const sctp_subtype_t type,
4102 sctp_cmd_seq_t *commands)
4104 struct sctp_chunk *unk_chunk = arg;
4105 struct sctp_chunk *err_chunk;
4106 sctp_chunkhdr_t *hdr;
4108 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4110 if (!sctp_vtag_verify(unk_chunk, asoc))
4111 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4113 /* Make sure that the chunk has a valid length.
4114 * Since we don't know the chunk type, we use a general
4115 * chunkhdr structure to make a comparison.
4117 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4118 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4121 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4122 case SCTP_CID_ACTION_DISCARD:
4123 /* Discard the packet. */
4124 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4126 case SCTP_CID_ACTION_DISCARD_ERR:
4127 /* Generate an ERROR chunk as response. */
4128 hdr = unk_chunk->chunk_hdr;
4129 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4130 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4131 WORD_ROUND(ntohs(hdr->length)),
4134 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4135 SCTP_CHUNK(err_chunk));
4138 /* Discard the packet. */
4139 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4140 return SCTP_DISPOSITION_CONSUME;
4142 case SCTP_CID_ACTION_SKIP:
4143 /* Skip the chunk. */
4144 return SCTP_DISPOSITION_DISCARD;
4146 case SCTP_CID_ACTION_SKIP_ERR:
4147 /* Generate an ERROR chunk as response. */
4148 hdr = unk_chunk->chunk_hdr;
4149 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4150 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4151 WORD_ROUND(ntohs(hdr->length)),
4154 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4155 SCTP_CHUNK(err_chunk));
4157 /* Skip the chunk. */
4158 return SCTP_DISPOSITION_CONSUME;
4164 return SCTP_DISPOSITION_DISCARD;
4168 * Discard the chunk.
4170 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4171 * [Too numerous to mention...]
4172 * Verification Tag: No verification needed.
4174 * (endpoint, asoc, chunk)
4177 * (asoc, reply_msg, msg_up, timers, counters)
4179 * The return value is the disposition of the chunk.
4181 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4182 const struct sctp_association *asoc,
4183 const sctp_subtype_t type,
4185 sctp_cmd_seq_t *commands)
4187 struct sctp_chunk *chunk = arg;
4189 /* Make sure that the chunk has a valid length.
4190 * Since we don't know the chunk type, we use a general
4191 * chunkhdr structure to make a comparison.
4193 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4194 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4197 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4198 return SCTP_DISPOSITION_DISCARD;
4202 * Discard the whole packet.
4206 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4207 * silently discard the OOTB packet and take no further action.
4209 * Verification Tag: No verification necessary
4212 * (endpoint, asoc, chunk)
4215 * (asoc, reply_msg, msg_up, timers, counters)
4217 * The return value is the disposition of the chunk.
4219 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4220 const struct sctp_association *asoc,
4221 const sctp_subtype_t type,
4223 sctp_cmd_seq_t *commands)
4225 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
4226 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4228 return SCTP_DISPOSITION_CONSUME;
4233 * The other end is violating protocol.
4235 * Section: Not specified
4236 * Verification Tag: Not specified
4238 * (endpoint, asoc, chunk)
4241 * (asoc, reply_msg, msg_up, timers, counters)
4243 * We simply tag the chunk as a violation. The state machine will log
4244 * the violation and continue.
4246 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4247 const struct sctp_association *asoc,
4248 const sctp_subtype_t type,
4250 sctp_cmd_seq_t *commands)
4252 struct sctp_chunk *chunk = arg;
4254 /* Make sure that the chunk has a valid length. */
4255 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4256 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4259 return SCTP_DISPOSITION_VIOLATION;
4263 * Common function to handle a protocol violation.
4265 static sctp_disposition_t sctp_sf_abort_violation(
4266 const struct sctp_endpoint *ep,
4267 const struct sctp_association *asoc,
4269 sctp_cmd_seq_t *commands,
4270 const __u8 *payload,
4271 const size_t paylen)
4273 struct sctp_packet *packet = NULL;
4274 struct sctp_chunk *chunk = arg;
4275 struct sctp_chunk *abort = NULL;
4278 /* SCTP-AUTH, Section 6.3:
4279 * It should be noted that if the receiver wants to tear
4280 * down an association in an authenticated way only, the
4281 * handling of malformed packets should not result in
4282 * tearing down the association.
4284 * This means that if we only want to abort associations
4285 * in an authenticated way (i.e AUTH+ABORT), then we
4286 * can't destroy this association just because the packet
4289 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4292 /* Make the abort chunk. */
4293 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4297 net = sock_net(ep->base.sk);
4299 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4300 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4301 !asoc->peer.i.init_tag) {
4302 sctp_initack_chunk_t *initack;
4304 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4305 if (!sctp_chunk_length_valid(chunk,
4306 sizeof(sctp_initack_chunk_t)))
4307 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4309 unsigned int inittag;
4311 inittag = ntohl(initack->init_hdr.init_tag);
4312 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4317 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4318 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4320 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4321 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4322 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4323 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4324 SCTP_ERROR(ECONNREFUSED));
4325 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4326 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4328 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4329 SCTP_ERROR(ECONNABORTED));
4330 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4331 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4332 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4335 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4340 if (sctp_test_T_bit(abort))
4341 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4343 abort->skb->sk = ep->base.sk;
4345 sctp_packet_append_chunk(packet, abort);
4347 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4348 SCTP_PACKET(packet));
4350 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4353 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4356 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4357 return SCTP_DISPOSITION_ABORT;
4360 sctp_chunk_free(abort);
4362 return SCTP_DISPOSITION_NOMEM;
4366 * Handle a protocol violation when the chunk length is invalid.
4367 * "Invalid" length is identified as smaller than the minimal length a
4368 * given chunk can be. For example, a SACK chunk has invalid length
4369 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4371 * We inform the other end by sending an ABORT with a Protocol Violation
4374 * Section: Not specified
4375 * Verification Tag: Nothing to do
4377 * (endpoint, asoc, chunk)
4380 * (reply_msg, msg_up, counters)
4382 * Generate an ABORT chunk and terminate the association.
4384 static sctp_disposition_t sctp_sf_violation_chunklen(
4385 const struct sctp_endpoint *ep,
4386 const struct sctp_association *asoc,
4387 const sctp_subtype_t type,
4389 sctp_cmd_seq_t *commands)
4391 static const char err_str[]="The following chunk had invalid length:";
4393 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4398 * Handle a protocol violation when the parameter length is invalid.
4399 * If the length is smaller than the minimum length of a given parameter,
4400 * or accumulated length in multi parameters exceeds the end of the chunk,
4401 * the length is considered as invalid.
4403 static sctp_disposition_t sctp_sf_violation_paramlen(
4404 const struct sctp_endpoint *ep,
4405 const struct sctp_association *asoc,
4406 const sctp_subtype_t type,
4407 void *arg, void *ext,
4408 sctp_cmd_seq_t *commands)
4410 struct sctp_chunk *chunk = arg;
4411 struct sctp_paramhdr *param = ext;
4412 struct sctp_chunk *abort = NULL;
4414 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4417 /* Make the abort chunk. */
4418 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4422 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4423 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4425 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4426 SCTP_ERROR(ECONNABORTED));
4427 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4428 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4429 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4430 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4433 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4434 return SCTP_DISPOSITION_ABORT;
4436 return SCTP_DISPOSITION_NOMEM;
4439 /* Handle a protocol violation when the peer trying to advance the
4440 * cumulative tsn ack to a point beyond the max tsn currently sent.
4442 * We inform the other end by sending an ABORT with a Protocol Violation
4445 static sctp_disposition_t sctp_sf_violation_ctsn(
4446 const struct sctp_endpoint *ep,
4447 const struct sctp_association *asoc,
4448 const sctp_subtype_t type,
4450 sctp_cmd_seq_t *commands)
4452 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4454 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4458 /* Handle protocol violation of an invalid chunk bundling. For example,
4459 * when we have an association and we receive bundled INIT-ACK, or
4460 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4461 * statement from the specs. Additionally, there might be an attacker
4462 * on the path and we may not want to continue this communication.
4464 static sctp_disposition_t sctp_sf_violation_chunk(
4465 const struct sctp_endpoint *ep,
4466 const struct sctp_association *asoc,
4467 const sctp_subtype_t type,
4469 sctp_cmd_seq_t *commands)
4471 static const char err_str[]="The following chunk violates protocol:";
4474 return sctp_sf_violation(ep, asoc, type, arg, commands);
4476 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4479 /***************************************************************************
4480 * These are the state functions for handling primitive (Section 10) events.
4481 ***************************************************************************/
4483 * sctp_sf_do_prm_asoc
4485 * Section: 10.1 ULP-to-SCTP
4488 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4489 * outbound stream count)
4490 * -> association id [,destination transport addr list] [,outbound stream
4493 * This primitive allows the upper layer to initiate an association to a
4494 * specific peer endpoint.
4496 * The peer endpoint shall be specified by one of the transport addresses
4497 * which defines the endpoint (see Section 1.4). If the local SCTP
4498 * instance has not been initialized, the ASSOCIATE is considered an
4500 * [This is not relevant for the kernel implementation since we do all
4501 * initialization at boot time. It we hadn't initialized we wouldn't
4502 * get anywhere near this code.]
4504 * An association id, which is a local handle to the SCTP association,
4505 * will be returned on successful establishment of the association. If
4506 * SCTP is not able to open an SCTP association with the peer endpoint,
4507 * an error is returned.
4508 * [In the kernel implementation, the struct sctp_association needs to
4509 * be created BEFORE causing this primitive to run.]
4511 * Other association parameters may be returned, including the
4512 * complete destination transport addresses of the peer as well as the
4513 * outbound stream count of the local endpoint. One of the transport
4514 * address from the returned destination addresses will be selected by
4515 * the local endpoint as default primary path for sending SCTP packets
4516 * to this peer. The returned "destination transport addr list" can
4517 * be used by the ULP to change the default primary path or to force
4518 * sending a packet to a specific transport address. [All of this
4519 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4522 * Mandatory attributes:
4524 * o local SCTP instance name - obtained from the INITIALIZE operation.
4525 * [This is the argument asoc.]
4526 * o destination transport addr - specified as one of the transport
4527 * addresses of the peer endpoint with which the association is to be
4529 * [This is asoc->peer.active_path.]
4530 * o outbound stream count - the number of outbound streams the ULP
4531 * would like to open towards this peer endpoint.
4532 * [BUG: This is not currently implemented.]
4533 * Optional attributes:
4537 * The return value is a disposition.
4539 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4540 const struct sctp_association *asoc,
4541 const sctp_subtype_t type,
4543 sctp_cmd_seq_t *commands)
4545 struct sctp_chunk *repl;
4546 struct sctp_association* my_asoc;
4548 /* The comment below says that we enter COOKIE-WAIT AFTER
4549 * sending the INIT, but that doesn't actually work in our
4552 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4553 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4555 /* RFC 2960 5.1 Normal Establishment of an Association
4557 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4558 * must provide its Verification Tag (Tag_A) in the Initiate
4559 * Tag field. Tag_A SHOULD be a random number in the range of
4560 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4563 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4567 /* Cast away the const modifier, as we want to just
4568 * rerun it through as a sideffect.
4570 my_asoc = (struct sctp_association *)asoc;
4571 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4573 /* Choose transport for INIT. */
4574 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4577 /* After sending the INIT, "A" starts the T1-init timer and
4578 * enters the COOKIE-WAIT state.
4580 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4581 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4582 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4583 return SCTP_DISPOSITION_CONSUME;
4586 return SCTP_DISPOSITION_NOMEM;
4590 * Process the SEND primitive.
4592 * Section: 10.1 ULP-to-SCTP
4595 * Format: SEND(association id, buffer address, byte count [,context]
4596 * [,stream id] [,life time] [,destination transport address]
4597 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4600 * This is the main method to send user data via SCTP.
4602 * Mandatory attributes:
4604 * o association id - local handle to the SCTP association
4606 * o buffer address - the location where the user message to be
4607 * transmitted is stored;
4609 * o byte count - The size of the user data in number of bytes;
4611 * Optional attributes:
4613 * o context - an optional 32 bit integer that will be carried in the
4614 * sending failure notification to the ULP if the transportation of
4615 * this User Message fails.
4617 * o stream id - to indicate which stream to send the data on. If not
4618 * specified, stream 0 will be used.
4620 * o life time - specifies the life time of the user data. The user data
4621 * will not be sent by SCTP after the life time expires. This
4622 * parameter can be used to avoid efforts to transmit stale
4623 * user messages. SCTP notifies the ULP if the data cannot be
4624 * initiated to transport (i.e. sent to the destination via SCTP's
4625 * send primitive) within the life time variable. However, the
4626 * user data will be transmitted if SCTP has attempted to transmit a
4627 * chunk before the life time expired.
4629 * o destination transport address - specified as one of the destination
4630 * transport addresses of the peer endpoint to which this packet
4631 * should be sent. Whenever possible, SCTP should use this destination
4632 * transport address for sending the packets, instead of the current
4635 * o unorder flag - this flag, if present, indicates that the user
4636 * would like the data delivered in an unordered fashion to the peer
4637 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4640 * o no-bundle flag - instructs SCTP not to bundle this user data with
4641 * other outbound DATA chunks. SCTP MAY still bundle even when
4642 * this flag is present, when faced with network congestion.
4644 * o payload protocol-id - A 32 bit unsigned integer that is to be
4645 * passed to the peer indicating the type of payload protocol data
4646 * being transmitted. This value is passed as opaque data by SCTP.
4648 * The return value is the disposition.
4650 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4651 const struct sctp_association *asoc,
4652 const sctp_subtype_t type,
4654 sctp_cmd_seq_t *commands)
4656 struct sctp_datamsg *msg = arg;
4658 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4659 return SCTP_DISPOSITION_CONSUME;
4663 * Process the SHUTDOWN primitive.
4668 * Format: SHUTDOWN(association id)
4671 * Gracefully closes an association. Any locally queued user data
4672 * will be delivered to the peer. The association will be terminated only
4673 * after the peer acknowledges all the SCTP packets sent. A success code
4674 * will be returned on successful termination of the association. If
4675 * attempting to terminate the association results in a failure, an error
4676 * code shall be returned.
4678 * Mandatory attributes:
4680 * o association id - local handle to the SCTP association
4682 * Optional attributes:
4686 * The return value is the disposition.
4688 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4689 const struct sctp_endpoint *ep,
4690 const struct sctp_association *asoc,
4691 const sctp_subtype_t type,
4693 sctp_cmd_seq_t *commands)
4697 /* From 9.2 Shutdown of an Association
4698 * Upon receipt of the SHUTDOWN primitive from its upper
4699 * layer, the endpoint enters SHUTDOWN-PENDING state and
4700 * remains there until all outstanding data has been
4701 * acknowledged by its peer. The endpoint accepts no new data
4702 * from its upper layer, but retransmits data to the far end
4703 * if necessary to fill gaps.
4705 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4706 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4708 disposition = SCTP_DISPOSITION_CONSUME;
4709 if (sctp_outq_is_empty(&asoc->outqueue)) {
4710 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4717 * Process the ABORT primitive.
4722 * Format: Abort(association id [, cause code])
4725 * Ungracefully closes an association. Any locally queued user data
4726 * will be discarded and an ABORT chunk is sent to the peer. A success code
4727 * will be returned on successful abortion of the association. If
4728 * attempting to abort the association results in a failure, an error
4729 * code shall be returned.
4731 * Mandatory attributes:
4733 * o association id - local handle to the SCTP association
4735 * Optional attributes:
4737 * o cause code - reason of the abort to be passed to the peer
4741 * The return value is the disposition.
4743 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4744 const struct sctp_endpoint *ep,
4745 const struct sctp_association *asoc,
4746 const sctp_subtype_t type,
4748 sctp_cmd_seq_t *commands)
4750 /* From 9.1 Abort of an Association
4751 * Upon receipt of the ABORT primitive from its upper
4752 * layer, the endpoint enters CLOSED state and
4753 * discard all outstanding data has been
4754 * acknowledged by its peer. The endpoint accepts no new data
4755 * from its upper layer, but retransmits data to the far end
4756 * if necessary to fill gaps.
4758 struct sctp_chunk *abort = arg;
4759 sctp_disposition_t retval;
4761 retval = SCTP_DISPOSITION_CONSUME;
4763 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4765 /* Even if we can't send the ABORT due to low memory delete the
4766 * TCB. This is a departure from our typical NOMEM handling.
4769 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4770 SCTP_ERROR(ECONNABORTED));
4771 /* Delete the established association. */
4772 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4773 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4775 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4776 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4781 /* We tried an illegal operation on an association which is closed. */
4782 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4783 const struct sctp_association *asoc,
4784 const sctp_subtype_t type,
4786 sctp_cmd_seq_t *commands)
4788 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4789 return SCTP_DISPOSITION_CONSUME;
4792 /* We tried an illegal operation on an association which is shutting
4795 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4796 const struct sctp_association *asoc,
4797 const sctp_subtype_t type,
4799 sctp_cmd_seq_t *commands)
4801 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4802 SCTP_ERROR(-ESHUTDOWN));
4803 return SCTP_DISPOSITION_CONSUME;
4807 * sctp_cookie_wait_prm_shutdown
4809 * Section: 4 Note: 2
4814 * The RFC does not explicitly address this issue, but is the route through the
4815 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4820 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4821 const struct sctp_endpoint *ep,
4822 const struct sctp_association *asoc,
4823 const sctp_subtype_t type,
4825 sctp_cmd_seq_t *commands)
4827 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4828 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4830 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4831 SCTP_STATE(SCTP_STATE_CLOSED));
4833 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4835 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4837 return SCTP_DISPOSITION_DELETE_TCB;
4841 * sctp_cookie_echoed_prm_shutdown
4843 * Section: 4 Note: 2
4848 * The RFC does not explcitly address this issue, but is the route through the
4849 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4854 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4855 const struct sctp_endpoint *ep,
4856 const struct sctp_association *asoc,
4857 const sctp_subtype_t type,
4858 void *arg, sctp_cmd_seq_t *commands)
4860 /* There is a single T1 timer, so we should be able to use
4861 * common function with the COOKIE-WAIT state.
4863 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4867 * sctp_sf_cookie_wait_prm_abort
4869 * Section: 4 Note: 2
4874 * The RFC does not explicitly address this issue, but is the route through the
4875 * state table when someone issues an abort while in COOKIE_WAIT state.
4880 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4881 const struct sctp_endpoint *ep,
4882 const struct sctp_association *asoc,
4883 const sctp_subtype_t type,
4885 sctp_cmd_seq_t *commands)
4887 struct sctp_chunk *abort = arg;
4888 sctp_disposition_t retval;
4890 /* Stop T1-init timer */
4891 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4892 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4893 retval = SCTP_DISPOSITION_CONSUME;
4895 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4897 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4898 SCTP_STATE(SCTP_STATE_CLOSED));
4900 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4902 /* Even if we can't send the ABORT due to low memory delete the
4903 * TCB. This is a departure from our typical NOMEM handling.
4906 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4907 SCTP_ERROR(ECONNREFUSED));
4908 /* Delete the established association. */
4909 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4910 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4916 * sctp_sf_cookie_echoed_prm_abort
4918 * Section: 4 Note: 3
4923 * The RFC does not explcitly address this issue, but is the route through the
4924 * state table when someone issues an abort while in COOKIE_ECHOED state.
4929 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4930 const struct sctp_endpoint *ep,
4931 const struct sctp_association *asoc,
4932 const sctp_subtype_t type,
4934 sctp_cmd_seq_t *commands)
4936 /* There is a single T1 timer, so we should be able to use
4937 * common function with the COOKIE-WAIT state.
4939 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4943 * sctp_sf_shutdown_pending_prm_abort
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 SHUTDOWN-PENDING state.
4954 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4955 const struct sctp_endpoint *ep,
4956 const struct sctp_association *asoc,
4957 const sctp_subtype_t type,
4959 sctp_cmd_seq_t *commands)
4961 /* Stop the T5-shutdown guard timer. */
4962 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4963 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4965 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4969 * sctp_sf_shutdown_sent_prm_abort
4974 * The RFC does not explicitly address this issue, but is the route through the
4975 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4980 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4981 const struct sctp_endpoint *ep,
4982 const struct sctp_association *asoc,
4983 const sctp_subtype_t type,
4985 sctp_cmd_seq_t *commands)
4987 /* Stop the T2-shutdown timer. */
4988 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4989 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4991 /* Stop the T5-shutdown guard timer. */
4992 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4993 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4995 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4999 * sctp_sf_cookie_echoed_prm_abort
5004 * The RFC does not explcitly address this issue, but is the route through the
5005 * state table when someone issues an abort while in COOKIE_ECHOED state.
5010 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5011 const struct sctp_endpoint *ep,
5012 const struct sctp_association *asoc,
5013 const sctp_subtype_t type,
5015 sctp_cmd_seq_t *commands)
5017 /* The same T2 timer, so we should be able to use
5018 * common function with the SHUTDOWN-SENT state.
5020 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
5024 * Process the REQUESTHEARTBEAT primitive
5027 * J) Request Heartbeat
5029 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5033 * Instructs the local endpoint to perform a HeartBeat on the specified
5034 * destination transport address of the given association. The returned
5035 * result should indicate whether the transmission of the HEARTBEAT
5036 * chunk to the destination address is successful.
5038 * Mandatory attributes:
5040 * o association id - local handle to the SCTP association
5042 * o destination transport address - the transport address of the
5043 * association on which a heartbeat should be issued.
5045 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5046 const struct sctp_endpoint *ep,
5047 const struct sctp_association *asoc,
5048 const sctp_subtype_t type,
5050 sctp_cmd_seq_t *commands)
5052 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5053 (struct sctp_transport *)arg, commands))
5054 return SCTP_DISPOSITION_NOMEM;
5057 * RFC 2960 (bis), section 8.3
5059 * D) Request an on-demand HEARTBEAT on a specific destination
5060 * transport address of a given association.
5062 * The endpoint should increment the respective error counter of
5063 * the destination transport address each time a HEARTBEAT is sent
5064 * to that address and not acknowledged within one RTO.
5067 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5068 SCTP_TRANSPORT(arg));
5069 return SCTP_DISPOSITION_CONSUME;
5073 * ADDIP Section 4.1 ASCONF Chunk Procedures
5074 * When an endpoint has an ASCONF signaled change to be sent to the
5075 * remote endpoint it should do A1 to A9
5077 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5078 const struct sctp_association *asoc,
5079 const sctp_subtype_t type,
5081 sctp_cmd_seq_t *commands)
5083 struct sctp_chunk *chunk = arg;
5085 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5086 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5087 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5088 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5089 return SCTP_DISPOSITION_CONSUME;
5093 * Ignore the primitive event
5095 * The return value is the disposition of the primitive.
5097 sctp_disposition_t sctp_sf_ignore_primitive(
5098 const struct sctp_endpoint *ep,
5099 const struct sctp_association *asoc,
5100 const sctp_subtype_t type,
5102 sctp_cmd_seq_t *commands)
5104 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5105 return SCTP_DISPOSITION_DISCARD;
5108 /***************************************************************************
5109 * These are the state functions for the OTHER events.
5110 ***************************************************************************/
5113 * When the SCTP stack has no more user data to send or retransmit, this
5114 * notification is given to the user. Also, at the time when a user app
5115 * subscribes to this event, if there is no data to be sent or
5116 * retransmit, the stack will immediately send up this notification.
5118 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5119 const struct sctp_endpoint *ep,
5120 const struct sctp_association *asoc,
5121 const sctp_subtype_t type,
5123 sctp_cmd_seq_t *commands)
5125 struct sctp_ulpevent *event;
5127 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5129 return SCTP_DISPOSITION_NOMEM;
5131 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5133 return SCTP_DISPOSITION_CONSUME;
5137 * Start the shutdown negotiation.
5140 * Once all its outstanding data has been acknowledged, the endpoint
5141 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5142 * TSN Ack field the last sequential TSN it has received from the peer.
5143 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5144 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5145 * with the updated last sequential TSN received from its peer.
5147 * The return value is the disposition.
5149 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5150 const struct sctp_endpoint *ep,
5151 const struct sctp_association *asoc,
5152 const sctp_subtype_t type,
5154 sctp_cmd_seq_t *commands)
5156 struct sctp_chunk *reply;
5158 /* Once all its outstanding data has been acknowledged, the
5159 * endpoint shall send a SHUTDOWN chunk to its peer including
5160 * in the Cumulative TSN Ack field the last sequential TSN it
5161 * has received from the peer.
5163 reply = sctp_make_shutdown(asoc, NULL);
5167 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5168 * T2-shutdown timer.
5170 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5172 /* It shall then start the T2-shutdown timer */
5173 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5174 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5176 /* RFC 4960 Section 9.2
5177 * The sender of the SHUTDOWN MAY also start an overall guard timer
5178 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5180 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5181 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5183 if (asoc->autoclose)
5184 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5185 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5187 /* and enter the SHUTDOWN-SENT state. */
5188 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5189 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5191 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5193 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5196 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5198 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5200 return SCTP_DISPOSITION_CONSUME;
5203 return SCTP_DISPOSITION_NOMEM;
5207 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5211 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5212 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5213 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5214 * endpoint must re-send the SHUTDOWN ACK.
5216 * The return value is the disposition.
5218 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5219 const struct sctp_endpoint *ep,
5220 const struct sctp_association *asoc,
5221 const sctp_subtype_t type,
5223 sctp_cmd_seq_t *commands)
5225 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5226 struct sctp_chunk *reply;
5228 /* There are 2 ways of getting here:
5229 * 1) called in response to a SHUTDOWN chunk
5230 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5232 * For the case (2), the arg parameter is set to NULL. We need
5233 * to check that we have a chunk before accessing it's fields.
5236 if (!sctp_vtag_verify(chunk, asoc))
5237 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5239 /* Make sure that the SHUTDOWN chunk has a valid length. */
5240 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5241 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5245 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5246 * shall send a SHUTDOWN ACK ...
5248 reply = sctp_make_shutdown_ack(asoc, chunk);
5252 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5253 * the T2-shutdown timer.
5255 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5257 /* and start/restart a T2-shutdown timer of its own, */
5258 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5259 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5261 if (asoc->autoclose)
5262 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5263 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5265 /* Enter the SHUTDOWN-ACK-SENT state. */
5266 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5267 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5269 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5271 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5274 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5276 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5278 return SCTP_DISPOSITION_CONSUME;
5281 return SCTP_DISPOSITION_NOMEM;
5285 * Ignore the event defined as other
5287 * The return value is the disposition of the event.
5289 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5290 const struct sctp_association *asoc,
5291 const sctp_subtype_t type,
5293 sctp_cmd_seq_t *commands)
5295 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5296 return SCTP_DISPOSITION_DISCARD;
5299 /************************************************************
5300 * These are the state functions for handling timeout events.
5301 ************************************************************/
5306 * Section: 6.3.3 Handle T3-rtx Expiration
5308 * Whenever the retransmission timer T3-rtx expires for a destination
5309 * address, do the following:
5312 * The return value is the disposition of the chunk.
5314 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5315 const struct sctp_association *asoc,
5316 const sctp_subtype_t type,
5318 sctp_cmd_seq_t *commands)
5320 struct sctp_transport *transport = arg;
5322 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5324 if (asoc->overall_error_count >= asoc->max_retrans) {
5325 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5327 * We are here likely because the receiver had its rwnd
5328 * closed for a while and we have not been able to
5329 * transmit the locally queued data within the maximum
5330 * retransmission attempts limit. Start the T5
5331 * shutdown guard timer to give the receiver one last
5332 * chance and some additional time to recover before
5335 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5336 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5338 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5339 SCTP_ERROR(ETIMEDOUT));
5340 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5341 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5342 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5343 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5344 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5345 return SCTP_DISPOSITION_DELETE_TCB;
5349 /* E1) For the destination address for which the timer
5350 * expires, adjust its ssthresh with rules defined in Section
5351 * 7.2.3 and set the cwnd <- MTU.
5354 /* E2) For the destination address for which the timer
5355 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5356 * maximum value discussed in rule C7 above (RTO.max) may be
5357 * used to provide an upper bound to this doubling operation.
5360 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5361 * outstanding DATA chunks for the address for which the
5362 * T3-rtx has expired will fit into a single packet, subject
5363 * to the MTU constraint for the path corresponding to the
5364 * destination transport address to which the retransmission
5365 * is being sent (this may be different from the address for
5366 * which the timer expires [see Section 6.4]). Call this
5367 * value K. Bundle and retransmit those K DATA chunks in a
5368 * single packet to the destination endpoint.
5370 * Note: Any DATA chunks that were sent to the address for
5371 * which the T3-rtx timer expired but did not fit in one MTU
5372 * (rule E3 above), should be marked for retransmission and
5373 * sent as soon as cwnd allows (normally when a SACK arrives).
5376 /* Do some failure management (Section 8.2). */
5377 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5379 /* NB: Rules E4 and F1 are implicit in R1. */
5380 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5382 return SCTP_DISPOSITION_CONSUME;
5386 * Generate delayed SACK on timeout
5388 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5390 * The guidelines on delayed acknowledgement algorithm specified in
5391 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5392 * acknowledgement SHOULD be generated for at least every second packet
5393 * (not every second DATA chunk) received, and SHOULD be generated
5394 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5395 * some situations it may be beneficial for an SCTP transmitter to be
5396 * more conservative than the algorithms detailed in this document
5397 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5398 * the following algorithms allow.
5400 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5401 const struct sctp_association *asoc,
5402 const sctp_subtype_t type,
5404 sctp_cmd_seq_t *commands)
5406 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
5407 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5408 return SCTP_DISPOSITION_CONSUME;
5412 * sctp_sf_t1_init_timer_expire
5414 * Section: 4 Note: 2
5419 * RFC 2960 Section 4 Notes
5420 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5421 * and re-start the T1-init timer without changing state. This MUST
5422 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5423 * endpoint MUST abort the initialization process and report the
5424 * error to SCTP user.
5430 sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5431 const struct sctp_association *asoc,
5432 const sctp_subtype_t type,
5434 sctp_cmd_seq_t *commands)
5436 struct sctp_chunk *repl = NULL;
5437 struct sctp_bind_addr *bp;
5438 int attempts = asoc->init_err_counter + 1;
5440 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
5441 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
5443 if (attempts <= asoc->max_init_attempts) {
5444 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5445 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5447 return SCTP_DISPOSITION_NOMEM;
5449 /* Choose transport for INIT. */
5450 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5453 /* Issue a sideeffect to do the needed accounting. */
5454 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5455 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5457 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5459 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5460 " max_init_attempts: %d\n",
5461 attempts, asoc->max_init_attempts);
5462 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5463 SCTP_ERROR(ETIMEDOUT));
5464 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5465 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5466 return SCTP_DISPOSITION_DELETE_TCB;
5469 return SCTP_DISPOSITION_CONSUME;
5473 * sctp_sf_t1_cookie_timer_expire
5475 * Section: 4 Note: 2
5480 * RFC 2960 Section 4 Notes
5481 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5482 * COOKIE ECHO and re-start the T1-cookie timer without changing
5483 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5484 * After that, the endpoint MUST abort the initialization process and
5485 * report the error to SCTP user.
5491 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
5492 const struct sctp_association *asoc,
5493 const sctp_subtype_t type,
5495 sctp_cmd_seq_t *commands)
5497 struct sctp_chunk *repl = NULL;
5498 int attempts = asoc->init_err_counter + 1;
5500 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
5501 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
5503 if (attempts <= asoc->max_init_attempts) {
5504 repl = sctp_make_cookie_echo(asoc, NULL);
5506 return SCTP_DISPOSITION_NOMEM;
5508 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5510 /* Issue a sideeffect to do the needed accounting. */
5511 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5512 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5514 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5516 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5517 SCTP_ERROR(ETIMEDOUT));
5518 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5519 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5520 return SCTP_DISPOSITION_DELETE_TCB;
5523 return SCTP_DISPOSITION_CONSUME;
5526 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5527 * with the updated last sequential TSN received from its peer.
5529 * An endpoint should limit the number of retransmissions of the
5530 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5531 * If this threshold is exceeded the endpoint should destroy the TCB and
5532 * MUST report the peer endpoint unreachable to the upper layer (and
5533 * thus the association enters the CLOSED state). The reception of any
5534 * packet from its peer (i.e. as the peer sends all of its queued DATA
5535 * chunks) should clear the endpoint's retransmission count and restart
5536 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5537 * all of its queued DATA chunks that have not yet been sent.
5539 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5540 const struct sctp_association *asoc,
5541 const sctp_subtype_t type,
5543 sctp_cmd_seq_t *commands)
5545 struct sctp_chunk *reply = NULL;
5547 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
5548 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5550 ((struct sctp_association *)asoc)->shutdown_retries++;
5552 if (asoc->overall_error_count >= asoc->max_retrans) {
5553 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5554 SCTP_ERROR(ETIMEDOUT));
5555 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5556 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5557 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5558 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5559 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5560 return SCTP_DISPOSITION_DELETE_TCB;
5563 switch (asoc->state) {
5564 case SCTP_STATE_SHUTDOWN_SENT:
5565 reply = sctp_make_shutdown(asoc, NULL);
5568 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5569 reply = sctp_make_shutdown_ack(asoc, NULL);
5580 /* Do some failure management (Section 8.2).
5581 * If we remove the transport an SHUTDOWN was last sent to, don't
5582 * do failure management.
5584 if (asoc->shutdown_last_sent_to)
5585 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5586 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5588 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5589 * the T2-shutdown timer.
5591 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5593 /* Restart the T2-shutdown timer. */
5594 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5595 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5596 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5597 return SCTP_DISPOSITION_CONSUME;
5600 return SCTP_DISPOSITION_NOMEM;
5604 * ADDIP Section 4.1 ASCONF CHunk Procedures
5605 * If the T4 RTO timer expires the endpoint should do B1 to B5
5607 sctp_disposition_t sctp_sf_t4_timer_expire(
5608 const struct sctp_endpoint *ep,
5609 const struct sctp_association *asoc,
5610 const sctp_subtype_t type,
5612 sctp_cmd_seq_t *commands)
5614 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5615 struct sctp_transport *transport = chunk->transport;
5617 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5619 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5620 * detection on the appropriate destination address as defined in
5621 * RFC2960 [5] section 8.1 and 8.2.
5624 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5625 SCTP_TRANSPORT(transport));
5627 /* Reconfig T4 timer and transport. */
5628 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5630 /* ADDIP 4.1 B2) Increment the association error counters and perform
5631 * endpoint failure detection on the association as defined in
5632 * RFC2960 [5] section 8.1 and 8.2.
5633 * association error counter is incremented in SCTP_CMD_STRIKE.
5635 if (asoc->overall_error_count >= asoc->max_retrans) {
5636 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5637 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5638 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5639 SCTP_ERROR(ETIMEDOUT));
5640 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5641 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5642 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5643 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5644 return SCTP_DISPOSITION_ABORT;
5647 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5648 * the ASCONF chunk was sent by doubling the RTO timer value.
5649 * This is done in SCTP_CMD_STRIKE.
5652 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5653 * choose an alternate destination address (please refer to RFC2960
5654 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5655 * chunk, it MUST be the same (including its serial number) as the last
5658 sctp_chunk_hold(asoc->addip_last_asconf);
5659 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5660 SCTP_CHUNK(asoc->addip_last_asconf));
5662 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5663 * destination is selected, then the RTO used will be that of the new
5664 * destination address.
5666 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5667 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5669 return SCTP_DISPOSITION_CONSUME;
5672 /* sctpimpguide-05 Section 2.12.2
5673 * The sender of the SHUTDOWN MAY also start an overall guard timer
5674 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5675 * At the expiration of this timer the sender SHOULD abort the association
5676 * by sending an ABORT chunk.
5678 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5679 const struct sctp_association *asoc,
5680 const sctp_subtype_t type,
5682 sctp_cmd_seq_t *commands)
5684 struct sctp_chunk *reply = NULL;
5686 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
5687 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5689 reply = sctp_make_abort(asoc, NULL, 0);
5693 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5694 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5695 SCTP_ERROR(ETIMEDOUT));
5696 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5697 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5699 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5700 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5702 return SCTP_DISPOSITION_DELETE_TCB;
5704 return SCTP_DISPOSITION_NOMEM;
5707 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5708 * the association is automatically closed by starting the shutdown process.
5709 * The work that needs to be done is same as when SHUTDOWN is initiated by
5710 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5712 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5713 const struct sctp_endpoint *ep,
5714 const struct sctp_association *asoc,
5715 const sctp_subtype_t type,
5717 sctp_cmd_seq_t *commands)
5721 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5723 /* From 9.2 Shutdown of an Association
5724 * Upon receipt of the SHUTDOWN primitive from its upper
5725 * layer, the endpoint enters SHUTDOWN-PENDING state and
5726 * remains there until all outstanding data has been
5727 * acknowledged by its peer. The endpoint accepts no new data
5728 * from its upper layer, but retransmits data to the far end
5729 * if necessary to fill gaps.
5731 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5732 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5734 disposition = SCTP_DISPOSITION_CONSUME;
5735 if (sctp_outq_is_empty(&asoc->outqueue)) {
5736 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5742 /*****************************************************************************
5743 * These are sa state functions which could apply to all types of events.
5744 ****************************************************************************/
5747 * This table entry is not implemented.
5750 * (endpoint, asoc, chunk)
5752 * The return value is the disposition of the chunk.
5754 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5755 const struct sctp_association *asoc,
5756 const sctp_subtype_t type,
5758 sctp_cmd_seq_t *commands)
5760 return SCTP_DISPOSITION_NOT_IMPL;
5764 * This table entry represents a bug.
5767 * (endpoint, asoc, chunk)
5769 * The return value is the disposition of the chunk.
5771 sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5772 const struct sctp_association *asoc,
5773 const sctp_subtype_t type,
5775 sctp_cmd_seq_t *commands)
5777 return SCTP_DISPOSITION_BUG;
5781 * This table entry represents the firing of a timer in the wrong state.
5782 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5783 * when the association is in the wrong state. This event should
5784 * be ignored, so as to prevent any rearming of the timer.
5787 * (endpoint, asoc, chunk)
5789 * The return value is the disposition of the chunk.
5791 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5792 const struct sctp_association *asoc,
5793 const sctp_subtype_t type,
5795 sctp_cmd_seq_t *commands)
5797 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5798 return SCTP_DISPOSITION_CONSUME;
5801 /********************************************************************
5802 * 2nd Level Abstractions
5803 ********************************************************************/
5805 /* Pull the SACK chunk based on the SACK header. */
5806 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5808 struct sctp_sackhdr *sack;
5813 /* Protect ourselves from reading too far into
5814 * the skb from a bogus sender.
5816 sack = (struct sctp_sackhdr *) chunk->skb->data;
5818 num_blocks = ntohs(sack->num_gap_ack_blocks);
5819 num_dup_tsns = ntohs(sack->num_dup_tsns);
5820 len = sizeof(struct sctp_sackhdr);
5821 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5822 if (len > chunk->skb->len)
5825 skb_pull(chunk->skb, len);
5830 /* Create an ABORT packet to be sent as a response, with the specified
5833 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5834 const struct sctp_association *asoc,
5835 struct sctp_chunk *chunk,
5836 const void *payload,
5839 struct sctp_packet *packet;
5840 struct sctp_chunk *abort;
5843 net = sock_net(ep->base.sk);
5844 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5848 * The T bit will be set if the asoc is NULL.
5850 abort = sctp_make_abort(asoc, chunk, paylen);
5852 sctp_ootb_pkt_free(packet);
5856 /* Reflect vtag if T-Bit is set */
5857 if (sctp_test_T_bit(abort))
5858 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5860 /* Add specified error causes, i.e., payload, to the
5863 sctp_addto_chunk(abort, paylen, payload);
5865 /* Set the skb to the belonging sock for accounting. */
5866 abort->skb->sk = ep->base.sk;
5868 sctp_packet_append_chunk(packet, abort);
5875 /* Allocate a packet for responding in the OOTB conditions. */
5876 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5877 const struct sctp_association *asoc,
5878 const struct sctp_chunk *chunk)
5880 struct sctp_packet *packet;
5881 struct sctp_transport *transport;
5886 /* Get the source and destination port from the inbound packet. */
5887 sport = ntohs(chunk->sctp_hdr->dest);
5888 dport = ntohs(chunk->sctp_hdr->source);
5890 /* The V-tag is going to be the same as the inbound packet if no
5891 * association exists, otherwise, use the peer's vtag.
5894 /* Special case the INIT-ACK as there is no peer's vtag
5897 switch(chunk->chunk_hdr->type) {
5898 case SCTP_CID_INIT_ACK:
5900 sctp_initack_chunk_t *initack;
5902 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5903 vtag = ntohl(initack->init_hdr.init_tag);
5907 vtag = asoc->peer.i.init_tag;
5911 /* Special case the INIT and stale COOKIE_ECHO as there is no
5914 switch(chunk->chunk_hdr->type) {
5917 sctp_init_chunk_t *init;
5919 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5920 vtag = ntohl(init->init_hdr.init_tag);
5924 vtag = ntohl(chunk->sctp_hdr->vtag);
5929 /* Make a transport for the bucket, Eliza... */
5930 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
5934 /* Cache a route for the transport with the chunk's destination as
5935 * the source address.
5937 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
5938 sctp_sk(net->sctp.ctl_sock));
5940 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5941 packet = sctp_packet_config(packet, vtag, 0);
5949 /* Free the packet allocated earlier for responding in the OOTB condition. */
5950 void sctp_ootb_pkt_free(struct sctp_packet *packet)
5952 sctp_transport_free(packet->transport);
5955 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5956 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5957 const struct sctp_association *asoc,
5958 const struct sctp_chunk *chunk,
5959 sctp_cmd_seq_t *commands,
5960 struct sctp_chunk *err_chunk)
5962 struct sctp_packet *packet;
5965 struct net *net = sock_net(ep->base.sk);
5966 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5968 struct sctp_signed_cookie *cookie;
5970 /* Override the OOTB vtag from the cookie. */
5971 cookie = chunk->subh.cookie_hdr;
5972 packet->vtag = cookie->c.peer_vtag;
5974 /* Set the skb to the belonging sock for accounting. */
5975 err_chunk->skb->sk = ep->base.sk;
5976 sctp_packet_append_chunk(packet, err_chunk);
5977 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5978 SCTP_PACKET(packet));
5979 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5981 sctp_chunk_free (err_chunk);
5986 /* Process a data chunk */
5987 static int sctp_eat_data(const struct sctp_association *asoc,
5988 struct sctp_chunk *chunk,
5989 sctp_cmd_seq_t *commands)
5991 sctp_datahdr_t *data_hdr;
5992 struct sctp_chunk *err;
5994 sctp_verb_t deliver;
5997 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
5998 struct sock *sk = asoc->base.sk;
6003 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6004 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6006 tsn = ntohl(data_hdr->tsn);
6007 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
6009 /* ASSERT: Now skb->data is really the user data. */
6011 /* Process ECN based congestion.
6013 * Since the chunk structure is reused for all chunks within
6014 * a packet, we use ecn_ce_done to track if we've already
6015 * done CE processing for this packet.
6017 * We need to do ECN processing even if we plan to discard the
6021 if (!chunk->ecn_ce_done) {
6023 chunk->ecn_ce_done = 1;
6025 af = sctp_get_af_specific(
6026 ipver2af(ip_hdr(chunk->skb)->version));
6028 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6029 /* Do real work as sideffect. */
6030 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6035 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6037 /* The TSN is too high--silently discard the chunk and
6038 * count on it getting retransmitted later.
6040 return SCTP_IERROR_HIGH_TSN;
6041 } else if (tmp > 0) {
6042 /* This is a duplicate. Record it. */
6043 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6044 return SCTP_IERROR_DUP_TSN;
6047 /* This is a new TSN. */
6049 /* Discard if there is no room in the receive window.
6050 * Actually, allow a little bit of overflow (up to a MTU).
6052 datalen = ntohs(chunk->chunk_hdr->length);
6053 datalen -= sizeof(sctp_data_chunk_t);
6055 deliver = SCTP_CMD_CHUNK_ULP;
6057 /* Think about partial delivery. */
6058 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6060 /* Even if we don't accept this chunk there is
6063 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6066 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6067 * seems a bit troublesome in that frag_point varies based on
6068 * PMTU. In cases, such as loopback, this might be a rather
6071 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6072 (datalen > asoc->rwnd + asoc->frag_point))) {
6074 /* If this is the next TSN, consider reneging to make
6075 * room. Note: Playing nice with a confused sender. A
6076 * malicious sender can still eat up all our buffer
6077 * space and in the future we may want to detect and
6078 * do more drastic reneging.
6080 if (sctp_tsnmap_has_gap(map) &&
6081 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6082 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6083 deliver = SCTP_CMD_RENEGE;
6085 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6086 "rwnd: %d\n", tsn, datalen,
6088 return SCTP_IERROR_IGNORE_TSN;
6093 * Also try to renege to limit our memory usage in the event that
6094 * we are under memory pressure
6095 * If we can't renege, don't worry about it, the sk_rmem_schedule
6096 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6097 * memory usage too much
6099 if (*sk->sk_prot_creator->memory_pressure) {
6100 if (sctp_tsnmap_has_gap(map) &&
6101 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6102 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6103 deliver = SCTP_CMD_RENEGE;
6108 * Section 3.3.10.9 No User Data (9)
6112 * No User Data: This error cause is returned to the originator of a
6113 * DATA chunk if a received DATA chunk has no user data.
6115 if (unlikely(0 == datalen)) {
6116 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6118 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6121 /* We are going to ABORT, so we might as well stop
6122 * processing the rest of the chunks in the packet.
6124 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6125 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6126 SCTP_ERROR(ECONNABORTED));
6127 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6128 SCTP_PERR(SCTP_ERROR_NO_DATA));
6129 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6130 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6131 return SCTP_IERROR_NO_DATA;
6134 chunk->data_accepted = 1;
6136 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6137 * if we renege and the chunk arrives again.
6139 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6140 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6142 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6146 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6148 * If an endpoint receive a DATA chunk with an invalid stream
6149 * identifier, it shall acknowledge the reception of the DATA chunk
6150 * following the normal procedure, immediately send an ERROR chunk
6151 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6152 * and discard the DATA chunk.
6154 sid = ntohs(data_hdr->stream);
6155 if (sid >= asoc->c.sinit_max_instreams) {
6156 /* Mark tsn as received even though we drop it */
6157 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6159 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6161 sizeof(data_hdr->stream),
6164 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6166 return SCTP_IERROR_BAD_STREAM;
6169 /* Check to see if the SSN is possible for this TSN.
6170 * The biggest gap we can record is 4K wide. Since SSNs wrap
6171 * at an unsigned short, there is no way that an SSN can
6172 * wrap and for a valid TSN. We can simply check if the current
6173 * SSN is smaller then the next expected one. If it is, it wrapped
6176 ssn = ntohs(data_hdr->ssn);
6177 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6178 return SCTP_IERROR_PROTO_VIOLATION;
6181 /* Send the data up to the user. Note: Schedule the
6182 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6183 * chunk needs the updated rwnd.
6185 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6187 return SCTP_IERROR_NO_ERROR;