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(const struct sctp_association *asoc,
78 const struct sctp_chunk *chunk);
79 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
80 const struct sctp_association *asoc,
81 const struct sctp_chunk *chunk,
82 sctp_cmd_seq_t *commands,
83 struct sctp_chunk *err_chunk);
84 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
85 const struct sctp_association *asoc,
86 const sctp_subtype_t type,
88 sctp_cmd_seq_t *commands);
89 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
90 const struct sctp_association *asoc,
91 const sctp_subtype_t type,
93 sctp_cmd_seq_t *commands);
94 static sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
95 const struct sctp_association *asoc,
96 const sctp_subtype_t type,
98 sctp_cmd_seq_t *commands);
99 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
101 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
102 __be16 error, int sk_err,
103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
106 static sctp_disposition_t sctp_sf_abort_violation(
107 const struct sctp_endpoint *ep,
108 const struct sctp_association *asoc,
110 sctp_cmd_seq_t *commands,
112 const size_t paylen);
114 static sctp_disposition_t sctp_sf_violation_chunklen(
115 const struct sctp_endpoint *ep,
116 const struct sctp_association *asoc,
117 const sctp_subtype_t type,
119 sctp_cmd_seq_t *commands);
121 static sctp_disposition_t sctp_sf_violation_paramlen(
122 const struct sctp_endpoint *ep,
123 const struct sctp_association *asoc,
124 const sctp_subtype_t type,
125 void *arg, void *ext,
126 sctp_cmd_seq_t *commands);
128 static sctp_disposition_t sctp_sf_violation_ctsn(
129 const struct sctp_endpoint *ep,
130 const struct sctp_association *asoc,
131 const sctp_subtype_t type,
133 sctp_cmd_seq_t *commands);
135 static sctp_disposition_t sctp_sf_violation_chunk(
136 const struct sctp_endpoint *ep,
137 const struct sctp_association *asoc,
138 const sctp_subtype_t type,
140 sctp_cmd_seq_t *commands);
142 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
143 const struct sctp_association *asoc,
144 const sctp_subtype_t type,
145 struct sctp_chunk *chunk);
147 static sctp_disposition_t __sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
148 const struct sctp_association *asoc,
149 const sctp_subtype_t type,
151 sctp_cmd_seq_t *commands);
153 /* Small helper function that checks if the chunk length
154 * is of the appropriate length. The 'required_length' argument
155 * is set to be the size of a specific chunk we are testing.
156 * Return Values: 1 = Valid length
161 sctp_chunk_length_valid(struct sctp_chunk *chunk,
162 __u16 required_length)
164 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
166 /* Previously already marked? */
167 if (unlikely(chunk->pdiscard))
169 if (unlikely(chunk_length < required_length))
175 /**********************************************************
176 * These are the state functions for handling chunk events.
177 **********************************************************/
180 * Process the final SHUTDOWN COMPLETE.
182 * Section: 4 (C) (diagram), 9.2
183 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
184 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
185 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
186 * should stop the T2-shutdown timer and remove all knowledge of the
187 * association (and thus the association enters the CLOSED state).
189 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
190 * C) Rules for packet carrying SHUTDOWN COMPLETE:
192 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
193 * if the Verification Tag field of the packet matches its own tag and
194 * the T bit is not set
196 * it is set to its peer's tag and the T bit is set in the Chunk
198 * Otherwise, the receiver MUST silently discard the packet
199 * and take no further action. An endpoint MUST ignore the
200 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
203 * (endpoint, asoc, chunk)
206 * (asoc, reply_msg, msg_up, timers, counters)
208 * The return value is the disposition of the chunk.
210 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
211 const struct sctp_association *asoc,
212 const sctp_subtype_t type,
214 sctp_cmd_seq_t *commands)
216 struct sctp_chunk *chunk = arg;
217 struct sctp_ulpevent *ev;
219 if (!sctp_vtag_verify_either(chunk, asoc))
220 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
222 /* RFC 2960 6.10 Bundling
224 * An endpoint MUST NOT bundle INIT, INIT ACK or
225 * SHUTDOWN COMPLETE with any other chunks.
227 if (!chunk->singleton)
228 return sctp_sf_violation_chunk(ep, asoc, type, arg, commands);
230 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
231 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
232 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
235 /* RFC 2960 10.2 SCTP-to-ULP
237 * H) SHUTDOWN COMPLETE notification
239 * When SCTP completes the shutdown procedures (section 9.2) this
240 * notification is passed to the upper layer.
242 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
243 0, 0, 0, NULL, GFP_ATOMIC);
245 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
248 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
249 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
250 * not the chunk should be discarded. If the endpoint is in
251 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
252 * T2-shutdown timer and remove all knowledge of the
253 * association (and thus the association enters the CLOSED
256 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
257 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
259 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
260 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
262 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
263 SCTP_STATE(SCTP_STATE_CLOSED));
265 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
266 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
268 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
270 return SCTP_DISPOSITION_DELETE_TCB;
274 * Respond to a normal INIT chunk.
275 * We are the side that is being asked for an association.
277 * Section: 5.1 Normal Establishment of an Association, B
278 * B) "Z" shall respond immediately with an INIT ACK chunk. The
279 * destination IP address of the INIT ACK MUST be set to the source
280 * IP address of the INIT to which this INIT ACK is responding. In
281 * the response, besides filling in other parameters, "Z" must set the
282 * Verification Tag field to Tag_A, and also provide its own
283 * Verification Tag (Tag_Z) in the Initiate Tag field.
285 * Verification Tag: Must be 0.
288 * (endpoint, asoc, chunk)
291 * (asoc, reply_msg, msg_up, timers, counters)
293 * The return value is the disposition of the chunk.
295 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
296 const struct sctp_association *asoc,
297 const sctp_subtype_t type,
299 sctp_cmd_seq_t *commands)
301 struct sctp_chunk *chunk = arg;
302 struct sctp_chunk *repl;
303 struct sctp_association *new_asoc;
304 struct sctp_chunk *err_chunk;
305 struct sctp_packet *packet;
306 sctp_unrecognized_param_t *unk_param;
310 * An endpoint MUST NOT bundle INIT, INIT ACK or
311 * SHUTDOWN COMPLETE with any other chunks.
314 * Furthermore, we require that the receiver of an INIT chunk MUST
315 * enforce these rules by silently discarding an arriving packet
316 * with an INIT chunk that is bundled with other chunks.
318 if (!chunk->singleton)
319 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
321 /* If the packet is an OOTB packet which is temporarily on the
322 * control endpoint, respond with an ABORT.
324 if (ep == sctp_sk((sctp_get_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;
653 /* If the packet is an OOTB packet which is temporarily on the
654 * control endpoint, respond with an ABORT.
656 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep) {
657 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
658 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
661 /* Make sure that the COOKIE_ECHO chunk has a valid length.
662 * In this case, we check that we have enough for at least a
663 * chunk header. More detailed verification is done
664 * in sctp_unpack_cookie().
666 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
667 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
669 /* If the endpoint is not listening or if the number of associations
670 * on the TCP-style socket exceed the max backlog, respond with an
674 if (!sctp_sstate(sk, LISTENING) ||
675 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
676 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
678 /* "Decode" the chunk. We have no optional parameters so we
681 chunk->subh.cookie_hdr =
682 (struct sctp_signed_cookie *)chunk->skb->data;
683 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
684 sizeof(sctp_chunkhdr_t)))
687 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
688 * "Z" will reply with a COOKIE ACK chunk after building a TCB
689 * and moving to the ESTABLISHED state.
691 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
695 * If the re-build failed, what is the proper error path
698 * [We should abort the association. --piggy]
701 /* FIXME: Several errors are possible. A bad cookie should
702 * be silently discarded, but think about logging it too.
705 case -SCTP_IERROR_NOMEM:
708 case -SCTP_IERROR_STALE_COOKIE:
709 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
711 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
713 case -SCTP_IERROR_BAD_SIG:
715 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
720 /* Delay state machine commands until later.
722 * Re-build the bind address for the association is done in
723 * the sctp_unpack_cookie() already.
725 /* This is a brand-new association, so these are not yet side
726 * effects--it is safe to run them here.
728 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
730 if (!sctp_process_init(new_asoc, chunk,
731 &chunk->subh.cookie_hdr->c.peer_addr,
732 peer_init, GFP_ATOMIC))
735 /* SCTP-AUTH: Now that we've populate required fields in
736 * sctp_process_init, set up the assocaition shared keys as
737 * necessary so that we can potentially authenticate the ACK
739 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
743 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
744 * is supposed to be authenticated and we have to do delayed
745 * authentication. We've just recreated the association using
746 * the information in the cookie and now it's much easier to
747 * do the authentication.
749 if (chunk->auth_chunk) {
750 struct sctp_chunk auth;
753 /* Make sure that we and the peer are AUTH capable */
754 if (!sctp_auth_enable || !new_asoc->peer.auth_capable) {
755 sctp_association_free(new_asoc);
756 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
759 /* set-up our fake chunk so that we can process it */
760 auth.skb = chunk->auth_chunk;
761 auth.asoc = chunk->asoc;
762 auth.sctp_hdr = chunk->sctp_hdr;
763 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
764 sizeof(sctp_chunkhdr_t));
765 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
766 auth.transport = chunk->transport;
768 ret = sctp_sf_authenticate(ep, new_asoc, type, &auth);
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 if (net_ratelimit())
1138 pr_warn("%s association %p could not find address %pI6\n",
1141 &from_addr.v6.sin6_addr);
1143 if (net_ratelimit())
1144 pr_warn("%s association %p could not find address %pI4\n",
1147 &from_addr.v4.sin_addr.s_addr);
1149 return SCTP_DISPOSITION_DISCARD;
1152 /* Validate the 64-bit random nonce. */
1153 if (hbinfo->hb_nonce != link->hb_nonce)
1154 return SCTP_DISPOSITION_DISCARD;
1156 max_interval = link->hbinterval + link->rto;
1158 /* Check if the timestamp looks valid. */
1159 if (time_after(hbinfo->sent_at, jiffies) ||
1160 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1161 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1162 "received for transport: %p\n",
1164 return SCTP_DISPOSITION_DISCARD;
1167 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1168 * the HEARTBEAT should clear the error counter of the
1169 * destination transport address to which the HEARTBEAT was
1170 * sent and mark the destination transport address as active if
1171 * it is not so marked.
1173 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1175 return SCTP_DISPOSITION_CONSUME;
1178 /* Helper function to send out an abort for the restart
1181 static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1182 struct sctp_chunk *init,
1183 sctp_cmd_seq_t *commands)
1186 struct sctp_packet *pkt;
1187 union sctp_addr_param *addrparm;
1188 struct sctp_errhdr *errhdr;
1189 struct sctp_endpoint *ep;
1190 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1191 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1193 /* Build the error on the stack. We are way to malloc crazy
1194 * throughout the code today.
1196 errhdr = (struct sctp_errhdr *)buffer;
1197 addrparm = (union sctp_addr_param *)errhdr->variable;
1199 /* Copy into a parm format. */
1200 len = af->to_addr_param(ssa, addrparm);
1201 len += sizeof(sctp_errhdr_t);
1203 errhdr->cause = SCTP_ERROR_RESTART;
1204 errhdr->length = htons(len);
1206 /* Assign to the control socket. */
1207 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1209 /* Association is NULL since this may be a restart attack and we
1210 * want to send back the attacker's vtag.
1212 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1216 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1218 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1220 /* Discard the rest of the inbound packet. */
1221 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1224 /* Even if there is no memory, treat as a failure so
1225 * the packet will get dropped.
1230 static bool list_has_sctp_addr(const struct list_head *list,
1231 union sctp_addr *ipaddr)
1233 struct sctp_transport *addr;
1235 list_for_each_entry(addr, list, transports) {
1236 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1242 /* A restart is occurring, check to make sure no new addresses
1243 * are being added as we may be under a takeover attack.
1245 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1246 const struct sctp_association *asoc,
1247 struct sctp_chunk *init,
1248 sctp_cmd_seq_t *commands)
1250 struct sctp_transport *new_addr;
1253 /* Implementor's Guide - Section 5.2.2
1255 * Before responding the endpoint MUST check to see if the
1256 * unexpected INIT adds new addresses to the association. If new
1257 * addresses are added to the association, the endpoint MUST respond
1261 /* Search through all current addresses and make sure
1262 * we aren't adding any new ones.
1264 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1266 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1267 &new_addr->ipaddr)) {
1268 sctp_sf_send_restart_abort(&new_addr->ipaddr, init,
1275 /* Return success if all addresses were found. */
1279 /* Populate the verification/tie tags based on overlapping INIT
1282 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1284 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1285 const struct sctp_association *asoc)
1287 switch (asoc->state) {
1289 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1291 case SCTP_STATE_COOKIE_WAIT:
1292 new_asoc->c.my_vtag = asoc->c.my_vtag;
1293 new_asoc->c.my_ttag = asoc->c.my_vtag;
1294 new_asoc->c.peer_ttag = 0;
1297 case SCTP_STATE_COOKIE_ECHOED:
1298 new_asoc->c.my_vtag = asoc->c.my_vtag;
1299 new_asoc->c.my_ttag = asoc->c.my_vtag;
1300 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1303 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1304 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1307 new_asoc->c.my_ttag = asoc->c.my_vtag;
1308 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1312 /* Other parameters for the endpoint SHOULD be copied from the
1313 * existing parameters of the association (e.g. number of
1314 * outbound streams) into the INIT ACK and cookie.
1316 new_asoc->rwnd = asoc->rwnd;
1317 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1318 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1319 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1323 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1326 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1328 * Returns value representing action to be taken. These action values
1329 * correspond to Action/Description values in RFC 2960, Table 2.
1331 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1332 const struct sctp_association *asoc)
1334 /* In this case, the peer may have restarted. */
1335 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1336 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1337 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1338 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1341 /* Collision case B. */
1342 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1343 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1344 (0 == asoc->c.peer_vtag))) {
1348 /* Collision case D. */
1349 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1350 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1353 /* Collision case C. */
1354 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1355 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1356 (0 == new_asoc->c.my_ttag) &&
1357 (0 == new_asoc->c.peer_ttag))
1360 /* No match to any of the special cases; discard this packet. */
1364 /* Common helper routine for both duplicate and simulataneous INIT
1367 static sctp_disposition_t sctp_sf_do_unexpected_init(
1368 const struct sctp_endpoint *ep,
1369 const struct sctp_association *asoc,
1370 const sctp_subtype_t type,
1371 void *arg, sctp_cmd_seq_t *commands)
1373 sctp_disposition_t retval;
1374 struct sctp_chunk *chunk = arg;
1375 struct sctp_chunk *repl;
1376 struct sctp_association *new_asoc;
1377 struct sctp_chunk *err_chunk;
1378 struct sctp_packet *packet;
1379 sctp_unrecognized_param_t *unk_param;
1383 * An endpoint MUST NOT bundle INIT, INIT ACK or
1384 * SHUTDOWN COMPLETE with any other chunks.
1387 * Furthermore, we require that the receiver of an INIT chunk MUST
1388 * enforce these rules by silently discarding an arriving packet
1389 * with an INIT chunk that is bundled with other chunks.
1391 if (!chunk->singleton)
1392 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1394 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1397 if (chunk->sctp_hdr->vtag != 0)
1398 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1400 /* Make sure that the INIT chunk has a valid length.
1401 * In this case, we generate a protocol violation since we have
1402 * an association established.
1404 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1405 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1407 /* Grab the INIT header. */
1408 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1410 /* Tag the variable length parameters. */
1411 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1413 /* Verify the INIT chunk before processing it. */
1415 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1416 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1418 /* This chunk contains fatal error. It is to be discarded.
1419 * Send an ABORT, with causes if there is any.
1422 packet = sctp_abort_pkt_new(ep, asoc, arg,
1423 (__u8 *)(err_chunk->chunk_hdr) +
1424 sizeof(sctp_chunkhdr_t),
1425 ntohs(err_chunk->chunk_hdr->length) -
1426 sizeof(sctp_chunkhdr_t));
1429 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1430 SCTP_PACKET(packet));
1431 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1432 retval = SCTP_DISPOSITION_CONSUME;
1434 retval = SCTP_DISPOSITION_NOMEM;
1438 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1444 * Other parameters for the endpoint SHOULD be copied from the
1445 * existing parameters of the association (e.g. number of
1446 * outbound streams) into the INIT ACK and cookie.
1447 * FIXME: We are copying parameters from the endpoint not the
1450 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1454 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1455 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1458 /* In the outbound INIT ACK the endpoint MUST copy its current
1459 * Verification Tag and Peers Verification tag into a reserved
1460 * place (local tie-tag and per tie-tag) within the state cookie.
1462 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1463 (sctp_init_chunk_t *)chunk->chunk_hdr,
1467 /* Make sure no new addresses are being added during the
1468 * restart. Do not do this check for COOKIE-WAIT state,
1469 * since there are no peer addresses to check against.
1470 * Upon return an ABORT will have been sent if needed.
1472 if (!sctp_state(asoc, COOKIE_WAIT)) {
1473 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1475 retval = SCTP_DISPOSITION_CONSUME;
1480 sctp_tietags_populate(new_asoc, asoc);
1482 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1484 /* If there are errors need to be reported for unknown parameters,
1485 * make sure to reserve enough room in the INIT ACK for them.
1489 len = ntohs(err_chunk->chunk_hdr->length) -
1490 sizeof(sctp_chunkhdr_t);
1493 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1497 /* If there are errors need to be reported for unknown parameters,
1498 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1502 /* Get the "Unrecognized parameter" parameter(s) out of the
1503 * ERROR chunk generated by sctp_verify_init(). Since the
1504 * error cause code for "unknown parameter" and the
1505 * "Unrecognized parameter" type is the same, we can
1506 * construct the parameters in INIT ACK by copying the
1507 * ERROR causes over.
1509 unk_param = (sctp_unrecognized_param_t *)
1510 ((__u8 *)(err_chunk->chunk_hdr) +
1511 sizeof(sctp_chunkhdr_t));
1512 /* Replace the cause code with the "Unrecognized parameter"
1515 sctp_addto_chunk(repl, len, unk_param);
1518 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1519 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1522 * Note: After sending out INIT ACK with the State Cookie parameter,
1523 * "Z" MUST NOT allocate any resources for this new association.
1524 * Otherwise, "Z" will be vulnerable to resource attacks.
1526 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1527 retval = SCTP_DISPOSITION_CONSUME;
1532 retval = SCTP_DISPOSITION_NOMEM;
1535 sctp_association_free(new_asoc);
1538 sctp_chunk_free(err_chunk);
1543 * Handle simultaneous INIT.
1544 * This means we started an INIT and then we got an INIT request from
1547 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1548 * This usually indicates an initialization collision, i.e., each
1549 * endpoint is attempting, at about the same time, to establish an
1550 * association with the other endpoint.
1552 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1553 * endpoint MUST respond with an INIT ACK using the same parameters it
1554 * sent in its original INIT chunk (including its Verification Tag,
1555 * unchanged). These original parameters are combined with those from the
1556 * newly received INIT chunk. The endpoint shall also generate a State
1557 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1558 * INIT to calculate the State Cookie.
1560 * After that, the endpoint MUST NOT change its state, the T1-init
1561 * timer shall be left running and the corresponding TCB MUST NOT be
1562 * destroyed. The normal procedures for handling State Cookies when
1563 * a TCB exists will resolve the duplicate INITs to a single association.
1565 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1566 * its Tie-Tags with the Tag information of itself and its peer (see
1567 * section 5.2.2 for a description of the Tie-Tags).
1569 * Verification Tag: Not explicit, but an INIT can not have a valid
1570 * verification tag, so we skip the check.
1573 * (endpoint, asoc, chunk)
1576 * (asoc, reply_msg, msg_up, timers, counters)
1578 * The return value is the disposition of the chunk.
1580 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1581 const struct sctp_association *asoc,
1582 const sctp_subtype_t type,
1584 sctp_cmd_seq_t *commands)
1586 /* Call helper to do the real work for both simulataneous and
1587 * duplicate INIT chunk handling.
1589 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1593 * Handle duplicated INIT messages. These are usually delayed
1596 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1597 * COOKIE-ECHOED and COOKIE-WAIT
1599 * Unless otherwise stated, upon reception of an unexpected INIT for
1600 * this association, the endpoint shall generate an INIT ACK with a
1601 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1602 * current Verification Tag and peer's Verification Tag into a reserved
1603 * place within the state cookie. We shall refer to these locations as
1604 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1605 * containing this INIT ACK MUST carry a Verification Tag value equal to
1606 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1607 * MUST contain a new Initiation Tag (randomly generated see Section
1608 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1609 * existing parameters of the association (e.g. number of outbound
1610 * streams) into the INIT ACK and cookie.
1612 * After sending out the INIT ACK, the endpoint shall take no further
1613 * actions, i.e., the existing association, including its current state,
1614 * and the corresponding TCB MUST NOT be changed.
1616 * Note: Only when a TCB exists and the association is not in a COOKIE-
1617 * WAIT state are the Tie-Tags populated. For a normal association INIT
1618 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1619 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1620 * State Cookie are populated as specified in section 5.2.1.
1622 * Verification Tag: Not specified, but an INIT has no way of knowing
1623 * what the verification tag could be, so we ignore it.
1626 * (endpoint, asoc, chunk)
1629 * (asoc, reply_msg, msg_up, timers, counters)
1631 * The return value is the disposition of the chunk.
1633 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1634 const struct sctp_association *asoc,
1635 const sctp_subtype_t type,
1637 sctp_cmd_seq_t *commands)
1639 /* Call helper to do the real work for both simulataneous and
1640 * duplicate INIT chunk handling.
1642 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1647 * Unexpected INIT-ACK handler.
1650 * If an INIT ACK received by an endpoint in any state other than the
1651 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1652 * An unexpected INIT ACK usually indicates the processing of an old or
1653 * duplicated INIT chunk.
1655 sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1656 const struct sctp_association *asoc,
1657 const sctp_subtype_t type,
1658 void *arg, sctp_cmd_seq_t *commands)
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((sctp_get_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_SET_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;
3174 packet = sctp_ootb_pkt_new(asoc, chunk);
3177 /* Make an ABORT. The T bit will be set if the asoc
3180 abort = sctp_make_abort(asoc, chunk, 0);
3182 sctp_ootb_pkt_free(packet);
3183 return SCTP_DISPOSITION_NOMEM;
3186 /* Reflect vtag if T-Bit is set */
3187 if (sctp_test_T_bit(abort))
3188 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3190 /* Set the skb to the belonging sock for accounting. */
3191 abort->skb->sk = ep->base.sk;
3193 sctp_packet_append_chunk(packet, abort);
3195 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3196 SCTP_PACKET(packet));
3198 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3200 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3201 return SCTP_DISPOSITION_CONSUME;
3204 return SCTP_DISPOSITION_NOMEM;
3208 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3209 * event as ULP notification for each cause included in the chunk.
3211 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3213 * The return value is the disposition of the chunk.
3215 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
3216 const struct sctp_association *asoc,
3217 const sctp_subtype_t type,
3219 sctp_cmd_seq_t *commands)
3221 struct sctp_chunk *chunk = arg;
3224 if (!sctp_vtag_verify(chunk, asoc))
3225 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3227 /* Make sure that the ERROR chunk has a valid length. */
3228 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3229 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3231 sctp_walk_errors(err, chunk->chunk_hdr);
3232 if ((void *)err != (void *)chunk->chunk_end)
3233 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3234 (void *)err, commands);
3236 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3239 return SCTP_DISPOSITION_CONSUME;
3243 * Process an inbound SHUTDOWN ACK.
3246 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3247 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3248 * peer, and remove all record of the association.
3250 * The return value is the disposition.
3252 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3253 const struct sctp_association *asoc,
3254 const sctp_subtype_t type,
3256 sctp_cmd_seq_t *commands)
3258 struct sctp_chunk *chunk = arg;
3259 struct sctp_chunk *reply;
3260 struct sctp_ulpevent *ev;
3262 if (!sctp_vtag_verify(chunk, asoc))
3263 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3265 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3266 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3267 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3269 /* 10.2 H) SHUTDOWN COMPLETE notification
3271 * When SCTP completes the shutdown procedures (section 9.2) this
3272 * notification is passed to the upper layer.
3274 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3275 0, 0, 0, NULL, GFP_ATOMIC);
3279 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3280 reply = sctp_make_shutdown_complete(asoc, chunk);
3284 /* Do all the commands now (after allocation), so that we
3285 * have consistent state if memory allocation failes
3287 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3289 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3290 * stop the T2-shutdown timer,
3292 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3293 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3295 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3296 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3298 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3299 SCTP_STATE(SCTP_STATE_CLOSED));
3300 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3301 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3302 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3304 /* ...and remove all record of the association. */
3305 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3306 return SCTP_DISPOSITION_DELETE_TCB;
3309 sctp_ulpevent_free(ev);
3311 return SCTP_DISPOSITION_NOMEM;
3315 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3317 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3318 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3319 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3320 * packet must fill in the Verification Tag field of the outbound
3321 * packet with the Verification Tag received in the SHUTDOWN ACK and
3322 * set the T-bit in the Chunk Flags to indicate that the Verification
3325 * 8) The receiver should respond to the sender of the OOTB packet with
3326 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3327 * MUST fill in the Verification Tag field of the outbound packet
3328 * with the value found in the Verification Tag field of the OOTB
3329 * packet and set the T-bit in the Chunk Flags to indicate that the
3330 * Verification Tag is reflected. After sending this ABORT, the
3331 * receiver of the OOTB packet shall discard the OOTB packet and take
3332 * no further action.
3334 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3335 const struct sctp_association *asoc,
3336 const sctp_subtype_t type,
3338 sctp_cmd_seq_t *commands)
3340 struct sctp_chunk *chunk = arg;
3341 struct sk_buff *skb = chunk->skb;
3342 sctp_chunkhdr_t *ch;
3345 int ootb_shut_ack = 0;
3346 int ootb_cookie_ack = 0;
3348 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3350 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3352 /* Report violation if the chunk is less then minimal */
3353 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3354 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3357 /* Report violation if chunk len overflows */
3358 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3359 if (ch_end > skb_tail_pointer(skb))
3360 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3363 /* Now that we know we at least have a chunk header,
3364 * do things that are type appropriate.
3366 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3369 /* RFC 2960, Section 3.3.7
3370 * Moreover, under any circumstances, an endpoint that
3371 * receives an ABORT MUST NOT respond to that ABORT by
3372 * sending an ABORT of its own.
3374 if (SCTP_CID_ABORT == ch->type)
3375 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3377 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3378 * or a COOKIE ACK the SCTP Packet should be silently
3382 if (SCTP_CID_COOKIE_ACK == ch->type)
3383 ootb_cookie_ack = 1;
3385 if (SCTP_CID_ERROR == ch->type) {
3386 sctp_walk_errors(err, ch) {
3387 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3388 ootb_cookie_ack = 1;
3394 ch = (sctp_chunkhdr_t *) ch_end;
3395 } while (ch_end < skb_tail_pointer(skb));
3398 return sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
3399 else if (ootb_cookie_ack)
3400 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3402 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
3406 * Handle an "Out of the blue" SHUTDOWN ACK.
3408 * Section: 8.4 5, sctpimpguide 2.41.
3410 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3411 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3412 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3413 * packet must fill in the Verification Tag field of the outbound
3414 * packet with the Verification Tag received in the SHUTDOWN ACK and
3415 * set the T-bit in the Chunk Flags to indicate that the Verification
3419 * (endpoint, asoc, type, arg, commands)
3422 * (sctp_disposition_t)
3424 * The return value is the disposition of the chunk.
3426 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3427 const struct sctp_association *asoc,
3428 const sctp_subtype_t type,
3430 sctp_cmd_seq_t *commands)
3432 struct sctp_packet *packet = NULL;
3433 struct sctp_chunk *chunk = arg;
3434 struct sctp_chunk *shut;
3436 packet = sctp_ootb_pkt_new(asoc, chunk);
3439 /* Make an SHUTDOWN_COMPLETE.
3440 * The T bit will be set if the asoc is NULL.
3442 shut = sctp_make_shutdown_complete(asoc, chunk);
3444 sctp_ootb_pkt_free(packet);
3445 return SCTP_DISPOSITION_NOMEM;
3448 /* Reflect vtag if T-Bit is set */
3449 if (sctp_test_T_bit(shut))
3450 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3452 /* Set the skb to the belonging sock for accounting. */
3453 shut->skb->sk = ep->base.sk;
3455 sctp_packet_append_chunk(packet, shut);
3457 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3458 SCTP_PACKET(packet));
3460 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3462 /* If the chunk length is invalid, we don't want to process
3463 * the reset of the packet.
3465 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3466 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3468 /* We need to discard the rest of the packet to prevent
3469 * potential bomming attacks from additional bundled chunks.
3470 * This is documented in SCTP Threats ID.
3472 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3475 return SCTP_DISPOSITION_NOMEM;
3479 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3481 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3482 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3483 * procedures in section 8.4 SHOULD be followed, in other words it
3484 * should be treated as an Out Of The Blue packet.
3485 * [This means that we do NOT check the Verification Tag on these
3489 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3490 const struct sctp_association *asoc,
3491 const sctp_subtype_t type,
3493 sctp_cmd_seq_t *commands)
3495 struct sctp_chunk *chunk = arg;
3497 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3498 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3499 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3502 /* Although we do have an association in this case, it corresponds
3503 * to a restarted association. So the packet is treated as an OOTB
3504 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3505 * called with a NULL association.
3507 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3509 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3512 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3513 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3514 const struct sctp_association *asoc,
3515 const sctp_subtype_t type, void *arg,
3516 sctp_cmd_seq_t *commands)
3518 struct sctp_chunk *chunk = arg;
3519 struct sctp_chunk *asconf_ack = NULL;
3520 struct sctp_paramhdr *err_param = NULL;
3521 sctp_addiphdr_t *hdr;
3524 if (!sctp_vtag_verify(chunk, asoc)) {
3525 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3527 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3530 /* ADD-IP: Section 4.1.1
3531 * This chunk MUST be sent in an authenticated way by using
3532 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3533 * is received unauthenticated it MUST be silently discarded as
3534 * described in [I-D.ietf-tsvwg-sctp-auth].
3536 if (!sctp_addip_noauth && !chunk->auth)
3537 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3539 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3540 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3541 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3544 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3545 serial = ntohl(hdr->serial);
3547 /* Verify the ASCONF chunk before processing it. */
3548 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3549 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3550 (void *)err_param, commands);
3552 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3553 * the endpoint stored in a new association variable
3554 * 'Peer-Serial-Number'.
3556 if (serial == asoc->peer.addip_serial + 1) {
3557 /* If this is the first instance of ASCONF in the packet,
3558 * we can clean our old ASCONF-ACKs.
3560 if (!chunk->has_asconf)
3561 sctp_assoc_clean_asconf_ack_cache(asoc);
3563 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3564 * expected, process the ASCONF as described below and after
3565 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3566 * the response packet and cache a copy of it (in the event it
3567 * later needs to be retransmitted).
3569 * Essentially, do V1-V5.
3571 asconf_ack = sctp_process_asconf((struct sctp_association *)
3574 return SCTP_DISPOSITION_NOMEM;
3575 } else if (serial < asoc->peer.addip_serial + 1) {
3577 * If the value found in the Sequence Number is less than the
3578 * ('Peer- Sequence-Number' + 1), simply skip to the next
3579 * ASCONF, and include in the outbound response packet
3580 * any previously cached ASCONF-ACK response that was
3581 * sent and saved that matches the Sequence Number of the
3582 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3583 * Chunk exists. This will occur when an older ASCONF
3584 * arrives out of order. In such a case, the receiver
3585 * should skip the ASCONF Chunk and not include ASCONF-ACK
3586 * Chunk for that chunk.
3588 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3590 return SCTP_DISPOSITION_DISCARD;
3592 /* Reset the transport so that we select the correct one
3593 * this time around. This is to make sure that we don't
3594 * accidentally use a stale transport that's been removed.
3596 asconf_ack->transport = NULL;
3598 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3599 * it must be either a stale packet or from an attacker.
3601 return SCTP_DISPOSITION_DISCARD;
3604 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3605 * containing the ASCONF-ACK Chunks MUST be the source address of
3606 * the SCTP packet that held the ASCONF Chunks.
3608 * To do this properly, we'll set the destination address of the chunk
3609 * and at the transmit time, will try look up the transport to use.
3610 * Since ASCONFs may be bundled, the correct transport may not be
3611 * created until we process the entire packet, thus this workaround.
3613 asconf_ack->dest = chunk->source;
3614 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3615 if (asoc->new_transport) {
3616 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3618 ((struct sctp_association *)asoc)->new_transport = NULL;
3621 return SCTP_DISPOSITION_CONSUME;
3625 * ADDIP Section 4.3 General rules for address manipulation
3626 * When building TLV parameters for the ASCONF Chunk that will add or
3627 * delete IP addresses the D0 to D13 rules should be applied:
3629 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3630 const struct sctp_association *asoc,
3631 const sctp_subtype_t type, void *arg,
3632 sctp_cmd_seq_t *commands)
3634 struct sctp_chunk *asconf_ack = arg;
3635 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3636 struct sctp_chunk *abort;
3637 struct sctp_paramhdr *err_param = NULL;
3638 sctp_addiphdr_t *addip_hdr;
3639 __u32 sent_serial, rcvd_serial;
3641 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3642 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3644 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3647 /* ADD-IP, Section 4.1.2:
3648 * This chunk MUST be sent in an authenticated way by using
3649 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3650 * is received unauthenticated it MUST be silently discarded as
3651 * described in [I-D.ietf-tsvwg-sctp-auth].
3653 if (!sctp_addip_noauth && !asconf_ack->auth)
3654 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
3656 /* Make sure that the ADDIP chunk has a valid length. */
3657 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3658 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3661 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3662 rcvd_serial = ntohl(addip_hdr->serial);
3664 /* Verify the ASCONF-ACK chunk before processing it. */
3665 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3666 return sctp_sf_violation_paramlen(ep, asoc, type, arg,
3667 (void *)err_param, commands);
3670 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3671 sent_serial = ntohl(addip_hdr->serial);
3673 sent_serial = asoc->addip_serial - 1;
3676 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3677 * equal to the next serial number to be used but no ASCONF chunk is
3678 * outstanding the endpoint MUST ABORT the association. Note that a
3679 * sequence number is greater than if it is no more than 2^^31-1
3680 * larger than the current sequence number (using serial arithmetic).
3682 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3683 !(asoc->addip_last_asconf)) {
3684 abort = sctp_make_abort(asoc, asconf_ack,
3685 sizeof(sctp_errhdr_t));
3687 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3688 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3691 /* We are going to ABORT, so we might as well stop
3692 * processing the rest of the chunks in the packet.
3694 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3695 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3696 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3697 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3698 SCTP_ERROR(ECONNABORTED));
3699 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3700 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3701 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3702 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3703 return SCTP_DISPOSITION_ABORT;
3706 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3707 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3708 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3710 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3712 /* Successfully processed ASCONF_ACK. We can
3713 * release the next asconf if we have one.
3715 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3717 return SCTP_DISPOSITION_CONSUME;
3720 abort = sctp_make_abort(asoc, asconf_ack,
3721 sizeof(sctp_errhdr_t));
3723 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3724 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3727 /* We are going to ABORT, so we might as well stop
3728 * processing the rest of the chunks in the packet.
3730 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3731 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3732 SCTP_ERROR(ECONNABORTED));
3733 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3734 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3735 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3736 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3737 return SCTP_DISPOSITION_ABORT;
3740 return SCTP_DISPOSITION_DISCARD;
3744 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3746 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3747 * its cumulative TSN point to the value carried in the FORWARD TSN
3748 * chunk, and then MUST further advance its cumulative TSN point locally
3750 * After the above processing, the data receiver MUST stop reporting any
3751 * missing TSNs earlier than or equal to the new cumulative TSN point.
3753 * Verification Tag: 8.5 Verification Tag [Normal verification]
3755 * The return value is the disposition of the chunk.
3757 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3758 const struct sctp_association *asoc,
3759 const sctp_subtype_t type,
3761 sctp_cmd_seq_t *commands)
3763 struct sctp_chunk *chunk = arg;
3764 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3765 struct sctp_fwdtsn_skip *skip;
3769 if (!sctp_vtag_verify(chunk, asoc)) {
3770 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3772 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3775 /* Make sure that the FORWARD_TSN chunk has valid length. */
3776 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3777 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3780 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3781 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3782 len = ntohs(chunk->chunk_hdr->length);
3783 len -= sizeof(struct sctp_chunkhdr);
3784 skb_pull(chunk->skb, len);
3786 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3787 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3789 /* The TSN is too high--silently discard the chunk and count on it
3790 * getting retransmitted later.
3792 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3793 goto discard_noforce;
3795 /* Silently discard the chunk if stream-id is not valid */
3796 sctp_walk_fwdtsn(skip, chunk) {
3797 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3798 goto discard_noforce;
3801 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3802 if (len > sizeof(struct sctp_fwdtsn_hdr))
3803 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3806 /* Count this as receiving DATA. */
3807 if (asoc->autoclose) {
3808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3809 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3812 /* FIXME: For now send a SACK, but DATA processing may
3815 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3817 return SCTP_DISPOSITION_CONSUME;
3820 return SCTP_DISPOSITION_DISCARD;
3823 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3824 const struct sctp_endpoint *ep,
3825 const struct sctp_association *asoc,
3826 const sctp_subtype_t type,
3828 sctp_cmd_seq_t *commands)
3830 struct sctp_chunk *chunk = arg;
3831 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3832 struct sctp_fwdtsn_skip *skip;
3836 if (!sctp_vtag_verify(chunk, asoc)) {
3837 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3839 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3842 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3843 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3844 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3847 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3848 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3849 len = ntohs(chunk->chunk_hdr->length);
3850 len -= sizeof(struct sctp_chunkhdr);
3851 skb_pull(chunk->skb, len);
3853 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3854 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3856 /* The TSN is too high--silently discard the chunk and count on it
3857 * getting retransmitted later.
3859 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3862 /* Silently discard the chunk if stream-id is not valid */
3863 sctp_walk_fwdtsn(skip, chunk) {
3864 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3868 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3869 if (len > sizeof(struct sctp_fwdtsn_hdr))
3870 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3873 /* Go a head and force a SACK, since we are shutting down. */
3875 /* Implementor's Guide.
3877 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3878 * respond to each received packet containing one or more DATA chunk(s)
3879 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3881 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3882 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3883 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3884 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3886 return SCTP_DISPOSITION_CONSUME;
3890 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3892 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3893 * Identifier field. If this algorithm was not specified by the
3894 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3895 * during association setup, the AUTH chunk and all chunks after it MUST
3896 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3897 * defined in Section 4.1.
3899 * If an endpoint with no shared key receives a Shared Key Identifier
3900 * other than 0, it MUST silently discard all authenticated chunks. If
3901 * the endpoint has at least one endpoint pair shared key for the peer,
3902 * it MUST use the key specified by the Shared Key Identifier if a
3903 * key has been configured for that Shared Key Identifier. If no
3904 * endpoint pair shared key has been configured for that Shared Key
3905 * Identifier, all authenticated chunks MUST be silently discarded.
3907 * Verification Tag: 8.5 Verification Tag [Normal verification]
3909 * The return value is the disposition of the chunk.
3911 static sctp_ierror_t sctp_sf_authenticate(const struct sctp_endpoint *ep,
3912 const struct sctp_association *asoc,
3913 const sctp_subtype_t type,
3914 struct sctp_chunk *chunk)
3916 struct sctp_authhdr *auth_hdr;
3917 struct sctp_hmac *hmac;
3918 unsigned int sig_len;
3923 /* Pull in the auth header, so we can do some more verification */
3924 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
3925 chunk->subh.auth_hdr = auth_hdr;
3926 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
3928 /* Make sure that we suport the HMAC algorithm from the auth
3931 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
3932 return SCTP_IERROR_AUTH_BAD_HMAC;
3934 /* Make sure that the provided shared key identifier has been
3937 key_id = ntohs(auth_hdr->shkey_id);
3938 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
3939 return SCTP_IERROR_AUTH_BAD_KEYID;
3942 /* Make sure that the length of the signature matches what
3945 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
3946 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
3947 if (sig_len != hmac->hmac_len)
3948 return SCTP_IERROR_PROTO_VIOLATION;
3950 /* Now that we've done validation checks, we can compute and
3951 * verify the hmac. The steps involved are:
3952 * 1. Save the digest from the chunk.
3953 * 2. Zero out the digest in the chunk.
3954 * 3. Compute the new digest
3955 * 4. Compare saved and new digests.
3957 digest = auth_hdr->hmac;
3958 skb_pull(chunk->skb, sig_len);
3960 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
3964 memset(digest, 0, sig_len);
3966 sctp_auth_calculate_hmac(asoc, chunk->skb,
3967 (struct sctp_auth_chunk *)chunk->chunk_hdr,
3970 /* Discard the packet if the digests do not match */
3971 if (memcmp(save_digest, digest, sig_len)) {
3973 return SCTP_IERROR_BAD_SIG;
3979 return SCTP_IERROR_NO_ERROR;
3981 return SCTP_IERROR_NOMEM;
3984 sctp_disposition_t sctp_sf_eat_auth(const struct sctp_endpoint *ep,
3985 const struct sctp_association *asoc,
3986 const sctp_subtype_t type,
3988 sctp_cmd_seq_t *commands)
3990 struct sctp_authhdr *auth_hdr;
3991 struct sctp_chunk *chunk = arg;
3992 struct sctp_chunk *err_chunk;
3993 sctp_ierror_t error;
3995 /* Make sure that the peer has AUTH capable */
3996 if (!asoc->peer.auth_capable)
3997 return sctp_sf_unk_chunk(ep, asoc, type, arg, commands);
3999 if (!sctp_vtag_verify(chunk, asoc)) {
4000 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4002 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4005 /* Make sure that the AUTH chunk has valid length. */
4006 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4007 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4010 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4011 error = sctp_sf_authenticate(ep, asoc, type, chunk);
4013 case SCTP_IERROR_AUTH_BAD_HMAC:
4014 /* Generate the ERROR chunk and discard the rest
4017 err_chunk = sctp_make_op_error(asoc, chunk,
4018 SCTP_ERROR_UNSUP_HMAC,
4022 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4023 SCTP_CHUNK(err_chunk));
4026 case SCTP_IERROR_AUTH_BAD_KEYID:
4027 case SCTP_IERROR_BAD_SIG:
4028 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4030 case SCTP_IERROR_PROTO_VIOLATION:
4031 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4034 case SCTP_IERROR_NOMEM:
4035 return SCTP_DISPOSITION_NOMEM;
4037 default: /* Prevent gcc warnings */
4041 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4042 struct sctp_ulpevent *ev;
4044 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4045 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4050 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4054 return SCTP_DISPOSITION_CONSUME;
4058 * Process an unknown chunk.
4060 * Section: 3.2. Also, 2.1 in the implementor's guide.
4062 * Chunk Types are encoded such that the highest-order two bits specify
4063 * the action that must be taken if the processing endpoint does not
4064 * recognize the Chunk Type.
4066 * 00 - Stop processing this SCTP packet and discard it, do not process
4067 * any further chunks within it.
4069 * 01 - Stop processing this SCTP packet and discard it, do not process
4070 * any further chunks within it, and report the unrecognized
4071 * chunk in an 'Unrecognized Chunk Type'.
4073 * 10 - Skip this chunk and continue processing.
4075 * 11 - Skip this chunk and continue processing, but report in an ERROR
4076 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4078 * The return value is the disposition of the chunk.
4080 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
4081 const struct sctp_association *asoc,
4082 const sctp_subtype_t type,
4084 sctp_cmd_seq_t *commands)
4086 struct sctp_chunk *unk_chunk = arg;
4087 struct sctp_chunk *err_chunk;
4088 sctp_chunkhdr_t *hdr;
4090 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4092 if (!sctp_vtag_verify(unk_chunk, asoc))
4093 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4095 /* Make sure that the chunk has a valid length.
4096 * Since we don't know the chunk type, we use a general
4097 * chunkhdr structure to make a comparison.
4099 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4100 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4103 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4104 case SCTP_CID_ACTION_DISCARD:
4105 /* Discard the packet. */
4106 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4108 case SCTP_CID_ACTION_DISCARD_ERR:
4109 /* Generate an ERROR chunk as response. */
4110 hdr = unk_chunk->chunk_hdr;
4111 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4112 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4113 WORD_ROUND(ntohs(hdr->length)),
4116 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4117 SCTP_CHUNK(err_chunk));
4120 /* Discard the packet. */
4121 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4122 return SCTP_DISPOSITION_CONSUME;
4124 case SCTP_CID_ACTION_SKIP:
4125 /* Skip the chunk. */
4126 return SCTP_DISPOSITION_DISCARD;
4128 case SCTP_CID_ACTION_SKIP_ERR:
4129 /* Generate an ERROR chunk as response. */
4130 hdr = unk_chunk->chunk_hdr;
4131 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4132 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4133 WORD_ROUND(ntohs(hdr->length)),
4136 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4137 SCTP_CHUNK(err_chunk));
4139 /* Skip the chunk. */
4140 return SCTP_DISPOSITION_CONSUME;
4146 return SCTP_DISPOSITION_DISCARD;
4150 * Discard the chunk.
4152 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4153 * [Too numerous to mention...]
4154 * Verification Tag: No verification needed.
4156 * (endpoint, asoc, chunk)
4159 * (asoc, reply_msg, msg_up, timers, counters)
4161 * The return value is the disposition of the chunk.
4163 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
4164 const struct sctp_association *asoc,
4165 const sctp_subtype_t type,
4167 sctp_cmd_seq_t *commands)
4169 struct sctp_chunk *chunk = arg;
4171 /* Make sure that the chunk has a valid length.
4172 * Since we don't know the chunk type, we use a general
4173 * chunkhdr structure to make a comparison.
4175 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4176 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4179 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4180 return SCTP_DISPOSITION_DISCARD;
4184 * Discard the whole packet.
4188 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4189 * silently discard the OOTB packet and take no further action.
4191 * Verification Tag: No verification necessary
4194 * (endpoint, asoc, chunk)
4197 * (asoc, reply_msg, msg_up, timers, counters)
4199 * The return value is the disposition of the chunk.
4201 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
4202 const struct sctp_association *asoc,
4203 const sctp_subtype_t type,
4205 sctp_cmd_seq_t *commands)
4207 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
4208 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4210 return SCTP_DISPOSITION_CONSUME;
4215 * The other end is violating protocol.
4217 * Section: Not specified
4218 * Verification Tag: Not specified
4220 * (endpoint, asoc, chunk)
4223 * (asoc, reply_msg, msg_up, timers, counters)
4225 * We simply tag the chunk as a violation. The state machine will log
4226 * the violation and continue.
4228 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
4229 const struct sctp_association *asoc,
4230 const sctp_subtype_t type,
4232 sctp_cmd_seq_t *commands)
4234 struct sctp_chunk *chunk = arg;
4236 /* Make sure that the chunk has a valid length. */
4237 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4238 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4241 return SCTP_DISPOSITION_VIOLATION;
4245 * Common function to handle a protocol violation.
4247 static sctp_disposition_t sctp_sf_abort_violation(
4248 const struct sctp_endpoint *ep,
4249 const struct sctp_association *asoc,
4251 sctp_cmd_seq_t *commands,
4252 const __u8 *payload,
4253 const size_t paylen)
4255 struct sctp_packet *packet = NULL;
4256 struct sctp_chunk *chunk = arg;
4257 struct sctp_chunk *abort = NULL;
4259 /* SCTP-AUTH, Section 6.3:
4260 * It should be noted that if the receiver wants to tear
4261 * down an association in an authenticated way only, the
4262 * handling of malformed packets should not result in
4263 * tearing down the association.
4265 * This means that if we only want to abort associations
4266 * in an authenticated way (i.e AUTH+ABORT), then we
4267 * can't destroy this association just because the packet
4270 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4273 /* Make the abort chunk. */
4274 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4279 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4280 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4281 !asoc->peer.i.init_tag) {
4282 sctp_initack_chunk_t *initack;
4284 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4285 if (!sctp_chunk_length_valid(chunk,
4286 sizeof(sctp_initack_chunk_t)))
4287 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4289 unsigned int inittag;
4291 inittag = ntohl(initack->init_hdr.init_tag);
4292 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4297 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4298 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4300 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4301 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4302 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4303 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4304 SCTP_ERROR(ECONNREFUSED));
4305 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4306 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4308 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4309 SCTP_ERROR(ECONNABORTED));
4310 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4311 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4312 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4315 packet = sctp_ootb_pkt_new(asoc, chunk);
4320 if (sctp_test_T_bit(abort))
4321 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4323 abort->skb->sk = ep->base.sk;
4325 sctp_packet_append_chunk(packet, abort);
4327 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4328 SCTP_PACKET(packet));
4330 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4333 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4336 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4337 return SCTP_DISPOSITION_ABORT;
4340 sctp_chunk_free(abort);
4342 return SCTP_DISPOSITION_NOMEM;
4346 * Handle a protocol violation when the chunk length is invalid.
4347 * "Invalid" length is identified as smaller than the minimal length a
4348 * given chunk can be. For example, a SACK chunk has invalid length
4349 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4351 * We inform the other end by sending an ABORT with a Protocol Violation
4354 * Section: Not specified
4355 * Verification Tag: Nothing to do
4357 * (endpoint, asoc, chunk)
4360 * (reply_msg, msg_up, counters)
4362 * Generate an ABORT chunk and terminate the association.
4364 static sctp_disposition_t sctp_sf_violation_chunklen(
4365 const struct sctp_endpoint *ep,
4366 const struct sctp_association *asoc,
4367 const sctp_subtype_t type,
4369 sctp_cmd_seq_t *commands)
4371 static const char err_str[]="The following chunk had invalid length:";
4373 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4378 * Handle a protocol violation when the parameter length is invalid.
4379 * If the length is smaller than the minimum length of a given parameter,
4380 * or accumulated length in multi parameters exceeds the end of the chunk,
4381 * the length is considered as invalid.
4383 static sctp_disposition_t sctp_sf_violation_paramlen(
4384 const struct sctp_endpoint *ep,
4385 const struct sctp_association *asoc,
4386 const sctp_subtype_t type,
4387 void *arg, void *ext,
4388 sctp_cmd_seq_t *commands)
4390 struct sctp_chunk *chunk = arg;
4391 struct sctp_paramhdr *param = ext;
4392 struct sctp_chunk *abort = NULL;
4394 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4397 /* Make the abort chunk. */
4398 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4402 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4403 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
4405 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4406 SCTP_ERROR(ECONNABORTED));
4407 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4408 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4409 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4410 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4413 sctp_sf_pdiscard(ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4414 return SCTP_DISPOSITION_ABORT;
4416 return SCTP_DISPOSITION_NOMEM;
4419 /* Handle a protocol violation when the peer trying to advance the
4420 * cumulative tsn ack to a point beyond the max tsn currently sent.
4422 * We inform the other end by sending an ABORT with a Protocol Violation
4425 static sctp_disposition_t sctp_sf_violation_ctsn(
4426 const struct sctp_endpoint *ep,
4427 const struct sctp_association *asoc,
4428 const sctp_subtype_t type,
4430 sctp_cmd_seq_t *commands)
4432 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4434 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4438 /* Handle protocol violation of an invalid chunk bundling. For example,
4439 * when we have an association and we receive bundled INIT-ACK, or
4440 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4441 * statement from the specs. Additionally, there might be an attacker
4442 * on the path and we may not want to continue this communication.
4444 static sctp_disposition_t sctp_sf_violation_chunk(
4445 const struct sctp_endpoint *ep,
4446 const struct sctp_association *asoc,
4447 const sctp_subtype_t type,
4449 sctp_cmd_seq_t *commands)
4451 static const char err_str[]="The following chunk violates protocol:";
4454 return sctp_sf_violation(ep, asoc, type, arg, commands);
4456 return sctp_sf_abort_violation(ep, asoc, arg, commands, err_str,
4459 /***************************************************************************
4460 * These are the state functions for handling primitive (Section 10) events.
4461 ***************************************************************************/
4463 * sctp_sf_do_prm_asoc
4465 * Section: 10.1 ULP-to-SCTP
4468 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4469 * outbound stream count)
4470 * -> association id [,destination transport addr list] [,outbound stream
4473 * This primitive allows the upper layer to initiate an association to a
4474 * specific peer endpoint.
4476 * The peer endpoint shall be specified by one of the transport addresses
4477 * which defines the endpoint (see Section 1.4). If the local SCTP
4478 * instance has not been initialized, the ASSOCIATE is considered an
4480 * [This is not relevant for the kernel implementation since we do all
4481 * initialization at boot time. It we hadn't initialized we wouldn't
4482 * get anywhere near this code.]
4484 * An association id, which is a local handle to the SCTP association,
4485 * will be returned on successful establishment of the association. If
4486 * SCTP is not able to open an SCTP association with the peer endpoint,
4487 * an error is returned.
4488 * [In the kernel implementation, the struct sctp_association needs to
4489 * be created BEFORE causing this primitive to run.]
4491 * Other association parameters may be returned, including the
4492 * complete destination transport addresses of the peer as well as the
4493 * outbound stream count of the local endpoint. One of the transport
4494 * address from the returned destination addresses will be selected by
4495 * the local endpoint as default primary path for sending SCTP packets
4496 * to this peer. The returned "destination transport addr list" can
4497 * be used by the ULP to change the default primary path or to force
4498 * sending a packet to a specific transport address. [All of this
4499 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4502 * Mandatory attributes:
4504 * o local SCTP instance name - obtained from the INITIALIZE operation.
4505 * [This is the argument asoc.]
4506 * o destination transport addr - specified as one of the transport
4507 * addresses of the peer endpoint with which the association is to be
4509 * [This is asoc->peer.active_path.]
4510 * o outbound stream count - the number of outbound streams the ULP
4511 * would like to open towards this peer endpoint.
4512 * [BUG: This is not currently implemented.]
4513 * Optional attributes:
4517 * The return value is a disposition.
4519 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
4520 const struct sctp_association *asoc,
4521 const sctp_subtype_t type,
4523 sctp_cmd_seq_t *commands)
4525 struct sctp_chunk *repl;
4526 struct sctp_association* my_asoc;
4528 /* The comment below says that we enter COOKIE-WAIT AFTER
4529 * sending the INIT, but that doesn't actually work in our
4532 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4533 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4535 /* RFC 2960 5.1 Normal Establishment of an Association
4537 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4538 * must provide its Verification Tag (Tag_A) in the Initiate
4539 * Tag field. Tag_A SHOULD be a random number in the range of
4540 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4543 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4547 /* Cast away the const modifier, as we want to just
4548 * rerun it through as a sideffect.
4550 my_asoc = (struct sctp_association *)asoc;
4551 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4553 /* Choose transport for INIT. */
4554 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4557 /* After sending the INIT, "A" starts the T1-init timer and
4558 * enters the COOKIE-WAIT state.
4560 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4561 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4562 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4563 return SCTP_DISPOSITION_CONSUME;
4566 return SCTP_DISPOSITION_NOMEM;
4570 * Process the SEND primitive.
4572 * Section: 10.1 ULP-to-SCTP
4575 * Format: SEND(association id, buffer address, byte count [,context]
4576 * [,stream id] [,life time] [,destination transport address]
4577 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4580 * This is the main method to send user data via SCTP.
4582 * Mandatory attributes:
4584 * o association id - local handle to the SCTP association
4586 * o buffer address - the location where the user message to be
4587 * transmitted is stored;
4589 * o byte count - The size of the user data in number of bytes;
4591 * Optional attributes:
4593 * o context - an optional 32 bit integer that will be carried in the
4594 * sending failure notification to the ULP if the transportation of
4595 * this User Message fails.
4597 * o stream id - to indicate which stream to send the data on. If not
4598 * specified, stream 0 will be used.
4600 * o life time - specifies the life time of the user data. The user data
4601 * will not be sent by SCTP after the life time expires. This
4602 * parameter can be used to avoid efforts to transmit stale
4603 * user messages. SCTP notifies the ULP if the data cannot be
4604 * initiated to transport (i.e. sent to the destination via SCTP's
4605 * send primitive) within the life time variable. However, the
4606 * user data will be transmitted if SCTP has attempted to transmit a
4607 * chunk before the life time expired.
4609 * o destination transport address - specified as one of the destination
4610 * transport addresses of the peer endpoint to which this packet
4611 * should be sent. Whenever possible, SCTP should use this destination
4612 * transport address for sending the packets, instead of the current
4615 * o unorder flag - this flag, if present, indicates that the user
4616 * would like the data delivered in an unordered fashion to the peer
4617 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4620 * o no-bundle flag - instructs SCTP not to bundle this user data with
4621 * other outbound DATA chunks. SCTP MAY still bundle even when
4622 * this flag is present, when faced with network congestion.
4624 * o payload protocol-id - A 32 bit unsigned integer that is to be
4625 * passed to the peer indicating the type of payload protocol data
4626 * being transmitted. This value is passed as opaque data by SCTP.
4628 * The return value is the disposition.
4630 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
4631 const struct sctp_association *asoc,
4632 const sctp_subtype_t type,
4634 sctp_cmd_seq_t *commands)
4636 struct sctp_datamsg *msg = arg;
4638 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4639 return SCTP_DISPOSITION_CONSUME;
4643 * Process the SHUTDOWN primitive.
4648 * Format: SHUTDOWN(association id)
4651 * Gracefully closes an association. Any locally queued user data
4652 * will be delivered to the peer. The association will be terminated only
4653 * after the peer acknowledges all the SCTP packets sent. A success code
4654 * will be returned on successful termination of the association. If
4655 * attempting to terminate the association results in a failure, an error
4656 * code shall be returned.
4658 * Mandatory attributes:
4660 * o association id - local handle to the SCTP association
4662 * Optional attributes:
4666 * The return value is the disposition.
4668 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4669 const struct sctp_endpoint *ep,
4670 const struct sctp_association *asoc,
4671 const sctp_subtype_t type,
4673 sctp_cmd_seq_t *commands)
4677 /* From 9.2 Shutdown of an Association
4678 * Upon receipt of the SHUTDOWN primitive from its upper
4679 * layer, the endpoint enters SHUTDOWN-PENDING state and
4680 * remains there until all outstanding data has been
4681 * acknowledged by its peer. The endpoint accepts no new data
4682 * from its upper layer, but retransmits data to the far end
4683 * if necessary to fill gaps.
4685 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4686 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4688 disposition = SCTP_DISPOSITION_CONSUME;
4689 if (sctp_outq_is_empty(&asoc->outqueue)) {
4690 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4697 * Process the ABORT primitive.
4702 * Format: Abort(association id [, cause code])
4705 * Ungracefully closes an association. Any locally queued user data
4706 * will be discarded and an ABORT chunk is sent to the peer. A success code
4707 * will be returned on successful abortion of the association. If
4708 * attempting to abort the association results in a failure, an error
4709 * code shall be returned.
4711 * Mandatory attributes:
4713 * o association id - local handle to the SCTP association
4715 * Optional attributes:
4717 * o cause code - reason of the abort to be passed to the peer
4721 * The return value is the disposition.
4723 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4724 const struct sctp_endpoint *ep,
4725 const struct sctp_association *asoc,
4726 const sctp_subtype_t type,
4728 sctp_cmd_seq_t *commands)
4730 /* From 9.1 Abort of an Association
4731 * Upon receipt of the ABORT primitive from its upper
4732 * layer, the endpoint enters CLOSED state and
4733 * discard all outstanding data has been
4734 * acknowledged by its peer. The endpoint accepts no new data
4735 * from its upper layer, but retransmits data to the far end
4736 * if necessary to fill gaps.
4738 struct sctp_chunk *abort = arg;
4739 sctp_disposition_t retval;
4741 retval = SCTP_DISPOSITION_CONSUME;
4744 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4746 /* Even if we can't send the ABORT due to low memory delete the
4747 * TCB. This is a departure from our typical NOMEM handling.
4750 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4751 SCTP_ERROR(ECONNABORTED));
4752 /* Delete the established association. */
4753 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4754 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4756 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4757 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4762 /* We tried an illegal operation on an association which is closed. */
4763 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4764 const struct sctp_association *asoc,
4765 const sctp_subtype_t type,
4767 sctp_cmd_seq_t *commands)
4769 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4770 return SCTP_DISPOSITION_CONSUME;
4773 /* We tried an illegal operation on an association which is shutting
4776 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4777 const struct sctp_association *asoc,
4778 const sctp_subtype_t type,
4780 sctp_cmd_seq_t *commands)
4782 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4783 SCTP_ERROR(-ESHUTDOWN));
4784 return SCTP_DISPOSITION_CONSUME;
4788 * sctp_cookie_wait_prm_shutdown
4790 * Section: 4 Note: 2
4795 * The RFC does not explicitly address this issue, but is the route through the
4796 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4801 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4802 const struct sctp_endpoint *ep,
4803 const struct sctp_association *asoc,
4804 const sctp_subtype_t type,
4806 sctp_cmd_seq_t *commands)
4808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4809 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4811 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4812 SCTP_STATE(SCTP_STATE_CLOSED));
4814 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4816 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4818 return SCTP_DISPOSITION_DELETE_TCB;
4822 * sctp_cookie_echoed_prm_shutdown
4824 * Section: 4 Note: 2
4829 * The RFC does not explcitly address this issue, but is the route through the
4830 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4835 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4836 const struct sctp_endpoint *ep,
4837 const struct sctp_association *asoc,
4838 const sctp_subtype_t type,
4839 void *arg, sctp_cmd_seq_t *commands)
4841 /* There is a single T1 timer, so we should be able to use
4842 * common function with the COOKIE-WAIT state.
4844 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4848 * sctp_sf_cookie_wait_prm_abort
4850 * Section: 4 Note: 2
4855 * The RFC does not explicitly address this issue, but is the route through the
4856 * state table when someone issues an abort while in COOKIE_WAIT state.
4861 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4862 const struct sctp_endpoint *ep,
4863 const struct sctp_association *asoc,
4864 const sctp_subtype_t type,
4866 sctp_cmd_seq_t *commands)
4868 struct sctp_chunk *abort = arg;
4869 sctp_disposition_t retval;
4871 /* Stop T1-init timer */
4872 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4873 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4874 retval = SCTP_DISPOSITION_CONSUME;
4877 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4879 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4880 SCTP_STATE(SCTP_STATE_CLOSED));
4882 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4884 /* Even if we can't send the ABORT due to low memory delete the
4885 * TCB. This is a departure from our typical NOMEM handling.
4888 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4889 SCTP_ERROR(ECONNREFUSED));
4890 /* Delete the established association. */
4891 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4892 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4898 * sctp_sf_cookie_echoed_prm_abort
4900 * Section: 4 Note: 3
4905 * The RFC does not explcitly address this issue, but is the route through the
4906 * state table when someone issues an abort while in COOKIE_ECHOED state.
4911 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4912 const struct sctp_endpoint *ep,
4913 const struct sctp_association *asoc,
4914 const sctp_subtype_t type,
4916 sctp_cmd_seq_t *commands)
4918 /* There is a single T1 timer, so we should be able to use
4919 * common function with the COOKIE-WAIT state.
4921 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4925 * sctp_sf_shutdown_pending_prm_abort
4930 * The RFC does not explicitly address this issue, but is the route through the
4931 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4936 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4937 const struct sctp_endpoint *ep,
4938 const struct sctp_association *asoc,
4939 const sctp_subtype_t type,
4941 sctp_cmd_seq_t *commands)
4943 /* Stop the T5-shutdown guard timer. */
4944 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4945 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4947 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4951 * sctp_sf_shutdown_sent_prm_abort
4956 * The RFC does not explicitly address this issue, but is the route through the
4957 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4962 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4963 const struct sctp_endpoint *ep,
4964 const struct sctp_association *asoc,
4965 const sctp_subtype_t type,
4967 sctp_cmd_seq_t *commands)
4969 /* Stop the T2-shutdown timer. */
4970 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4971 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4973 /* Stop the T5-shutdown guard timer. */
4974 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4975 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4977 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4981 * sctp_sf_cookie_echoed_prm_abort
4986 * The RFC does not explcitly address this issue, but is the route through the
4987 * state table when someone issues an abort while in COOKIE_ECHOED state.
4992 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4993 const struct sctp_endpoint *ep,
4994 const struct sctp_association *asoc,
4995 const sctp_subtype_t type,
4997 sctp_cmd_seq_t *commands)
4999 /* The same T2 timer, so we should be able to use
5000 * common function with the SHUTDOWN-SENT state.
5002 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
5006 * Process the REQUESTHEARTBEAT primitive
5009 * J) Request Heartbeat
5011 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5015 * Instructs the local endpoint to perform a HeartBeat on the specified
5016 * destination transport address of the given association. The returned
5017 * result should indicate whether the transmission of the HEARTBEAT
5018 * chunk to the destination address is successful.
5020 * Mandatory attributes:
5022 * o association id - local handle to the SCTP association
5024 * o destination transport address - the transport address of the
5025 * association on which a heartbeat should be issued.
5027 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5028 const struct sctp_endpoint *ep,
5029 const struct sctp_association *asoc,
5030 const sctp_subtype_t type,
5032 sctp_cmd_seq_t *commands)
5034 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5035 (struct sctp_transport *)arg, commands))
5036 return SCTP_DISPOSITION_NOMEM;
5039 * RFC 2960 (bis), section 8.3
5041 * D) Request an on-demand HEARTBEAT on a specific destination
5042 * transport address of a given association.
5044 * The endpoint should increment the respective error counter of
5045 * the destination transport address each time a HEARTBEAT is sent
5046 * to that address and not acknowledged within one RTO.
5049 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5050 SCTP_TRANSPORT(arg));
5051 return SCTP_DISPOSITION_CONSUME;
5055 * ADDIP Section 4.1 ASCONF Chunk Procedures
5056 * When an endpoint has an ASCONF signaled change to be sent to the
5057 * remote endpoint it should do A1 to A9
5059 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
5060 const struct sctp_association *asoc,
5061 const sctp_subtype_t type,
5063 sctp_cmd_seq_t *commands)
5065 struct sctp_chunk *chunk = arg;
5067 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5068 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5069 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5070 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5071 return SCTP_DISPOSITION_CONSUME;
5075 * Ignore the primitive event
5077 * The return value is the disposition of the primitive.
5079 sctp_disposition_t sctp_sf_ignore_primitive(
5080 const struct sctp_endpoint *ep,
5081 const struct sctp_association *asoc,
5082 const sctp_subtype_t type,
5084 sctp_cmd_seq_t *commands)
5086 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5087 return SCTP_DISPOSITION_DISCARD;
5090 /***************************************************************************
5091 * These are the state functions for the OTHER events.
5092 ***************************************************************************/
5095 * When the SCTP stack has no more user data to send or retransmit, this
5096 * notification is given to the user. Also, at the time when a user app
5097 * subscribes to this event, if there is no data to be sent or
5098 * retransmit, the stack will immediately send up this notification.
5100 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5101 const struct sctp_endpoint *ep,
5102 const struct sctp_association *asoc,
5103 const sctp_subtype_t type,
5105 sctp_cmd_seq_t *commands)
5107 struct sctp_ulpevent *event;
5109 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5111 return SCTP_DISPOSITION_NOMEM;
5113 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5115 return SCTP_DISPOSITION_CONSUME;
5119 * Start the shutdown negotiation.
5122 * Once all its outstanding data has been acknowledged, the endpoint
5123 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5124 * TSN Ack field the last sequential TSN it has received from the peer.
5125 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5126 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5127 * with the updated last sequential TSN received from its peer.
5129 * The return value is the disposition.
5131 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5132 const struct sctp_endpoint *ep,
5133 const struct sctp_association *asoc,
5134 const sctp_subtype_t type,
5136 sctp_cmd_seq_t *commands)
5138 struct sctp_chunk *reply;
5140 /* Once all its outstanding data has been acknowledged, the
5141 * endpoint shall send a SHUTDOWN chunk to its peer including
5142 * in the Cumulative TSN Ack field the last sequential TSN it
5143 * has received from the peer.
5145 reply = sctp_make_shutdown(asoc, NULL);
5149 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5150 * T2-shutdown timer.
5152 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5154 /* It shall then start the T2-shutdown timer */
5155 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5156 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5158 /* RFC 4960 Section 9.2
5159 * The sender of the SHUTDOWN MAY also start an overall guard timer
5160 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5162 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5163 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5165 if (asoc->autoclose)
5166 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5167 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5169 /* and enter the SHUTDOWN-SENT state. */
5170 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5171 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5173 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5175 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5178 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5180 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5182 return SCTP_DISPOSITION_CONSUME;
5185 return SCTP_DISPOSITION_NOMEM;
5189 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5193 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5194 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5195 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5196 * endpoint must re-send the SHUTDOWN ACK.
5198 * The return value is the disposition.
5200 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5201 const struct sctp_endpoint *ep,
5202 const struct sctp_association *asoc,
5203 const sctp_subtype_t type,
5205 sctp_cmd_seq_t *commands)
5207 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5208 struct sctp_chunk *reply;
5210 /* There are 2 ways of getting here:
5211 * 1) called in response to a SHUTDOWN chunk
5212 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5214 * For the case (2), the arg parameter is set to NULL. We need
5215 * to check that we have a chunk before accessing it's fields.
5218 if (!sctp_vtag_verify(chunk, asoc))
5219 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
5221 /* Make sure that the SHUTDOWN chunk has a valid length. */
5222 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5223 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
5227 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5228 * shall send a SHUTDOWN ACK ...
5230 reply = sctp_make_shutdown_ack(asoc, chunk);
5234 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5235 * the T2-shutdown timer.
5237 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5239 /* and start/restart a T2-shutdown timer of its own, */
5240 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5241 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5243 if (asoc->autoclose)
5244 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5245 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5247 /* Enter the SHUTDOWN-ACK-SENT state. */
5248 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5249 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5251 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5253 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5256 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5258 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5260 return SCTP_DISPOSITION_CONSUME;
5263 return SCTP_DISPOSITION_NOMEM;
5267 * Ignore the event defined as other
5269 * The return value is the disposition of the event.
5271 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
5272 const struct sctp_association *asoc,
5273 const sctp_subtype_t type,
5275 sctp_cmd_seq_t *commands)
5277 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5278 return SCTP_DISPOSITION_DISCARD;
5281 /************************************************************
5282 * These are the state functions for handling timeout events.
5283 ************************************************************/
5288 * Section: 6.3.3 Handle T3-rtx Expiration
5290 * Whenever the retransmission timer T3-rtx expires for a destination
5291 * address, do the following:
5294 * The return value is the disposition of the chunk.
5296 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
5297 const struct sctp_association *asoc,
5298 const sctp_subtype_t type,
5300 sctp_cmd_seq_t *commands)
5302 struct sctp_transport *transport = arg;
5304 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
5306 if (asoc->overall_error_count >= asoc->max_retrans) {
5307 if (asoc->peer.zero_window_announced &&
5308 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5310 * We are here likely because the receiver had its rwnd
5311 * closed for a while and we have not been able to
5312 * transmit the locally queued data within the maximum
5313 * retransmission attempts limit. Start the T5
5314 * shutdown guard timer to give the receiver one last
5315 * chance and some additional time to recover before
5318 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5319 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5321 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5322 SCTP_ERROR(ETIMEDOUT));
5323 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5324 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5325 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5326 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5327 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5328 return SCTP_DISPOSITION_DELETE_TCB;
5332 /* E1) For the destination address for which the timer
5333 * expires, adjust its ssthresh with rules defined in Section
5334 * 7.2.3 and set the cwnd <- MTU.
5337 /* E2) For the destination address for which the timer
5338 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5339 * maximum value discussed in rule C7 above (RTO.max) may be
5340 * used to provide an upper bound to this doubling operation.
5343 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5344 * outstanding DATA chunks for the address for which the
5345 * T3-rtx has expired will fit into a single packet, subject
5346 * to the MTU constraint for the path corresponding to the
5347 * destination transport address to which the retransmission
5348 * is being sent (this may be different from the address for
5349 * which the timer expires [see Section 6.4]). Call this
5350 * value K. Bundle and retransmit those K DATA chunks in a
5351 * single packet to the destination endpoint.
5353 * Note: Any DATA chunks that were sent to the address for
5354 * which the T3-rtx timer expired but did not fit in one MTU
5355 * (rule E3 above), should be marked for retransmission and
5356 * sent as soon as cwnd allows (normally when a SACK arrives).
5359 /* Do some failure management (Section 8.2). */
5360 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5362 /* NB: Rules E4 and F1 are implicit in R1. */
5363 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5365 return SCTP_DISPOSITION_CONSUME;
5369 * Generate delayed SACK on timeout
5371 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5373 * The guidelines on delayed acknowledgement algorithm specified in
5374 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5375 * acknowledgement SHOULD be generated for at least every second packet
5376 * (not every second DATA chunk) received, and SHOULD be generated
5377 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5378 * some situations it may be beneficial for an SCTP transmitter to be
5379 * more conservative than the algorithms detailed in this document
5380 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5381 * the following algorithms allow.
5383 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
5384 const struct sctp_association *asoc,
5385 const sctp_subtype_t type,
5387 sctp_cmd_seq_t *commands)
5389 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
5390 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5391 return SCTP_DISPOSITION_CONSUME;
5395 * sctp_sf_t1_init_timer_expire
5397 * Section: 4 Note: 2
5402 * RFC 2960 Section 4 Notes
5403 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5404 * and re-start the T1-init timer without changing state. This MUST
5405 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5406 * endpoint MUST abort the initialization process and report the
5407 * error to SCTP user.
5413 sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
5414 const struct sctp_association *asoc,
5415 const sctp_subtype_t type,
5417 sctp_cmd_seq_t *commands)
5419 struct sctp_chunk *repl = NULL;
5420 struct sctp_bind_addr *bp;
5421 int attempts = asoc->init_err_counter + 1;
5423 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
5424 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
5426 if (attempts <= asoc->max_init_attempts) {
5427 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5428 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5430 return SCTP_DISPOSITION_NOMEM;
5432 /* Choose transport for INIT. */
5433 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5436 /* Issue a sideeffect to do the needed accounting. */
5437 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5438 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5440 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5442 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5443 " max_init_attempts: %d\n",
5444 attempts, asoc->max_init_attempts);
5445 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5446 SCTP_ERROR(ETIMEDOUT));
5447 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5448 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5449 return SCTP_DISPOSITION_DELETE_TCB;
5452 return SCTP_DISPOSITION_CONSUME;
5456 * sctp_sf_t1_cookie_timer_expire
5458 * Section: 4 Note: 2
5463 * RFC 2960 Section 4 Notes
5464 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5465 * COOKIE ECHO and re-start the T1-cookie timer without changing
5466 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5467 * After that, the endpoint MUST abort the initialization process and
5468 * report the error to SCTP user.
5474 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
5475 const struct sctp_association *asoc,
5476 const sctp_subtype_t type,
5478 sctp_cmd_seq_t *commands)
5480 struct sctp_chunk *repl = NULL;
5481 int attempts = asoc->init_err_counter + 1;
5483 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
5484 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
5486 if (attempts <= asoc->max_init_attempts) {
5487 repl = sctp_make_cookie_echo(asoc, NULL);
5489 return SCTP_DISPOSITION_NOMEM;
5491 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5493 /* Issue a sideeffect to do the needed accounting. */
5494 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5495 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5497 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5499 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5500 SCTP_ERROR(ETIMEDOUT));
5501 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5502 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5503 return SCTP_DISPOSITION_DELETE_TCB;
5506 return SCTP_DISPOSITION_CONSUME;
5509 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5510 * with the updated last sequential TSN received from its peer.
5512 * An endpoint should limit the number of retransmissions of the
5513 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5514 * If this threshold is exceeded the endpoint should destroy the TCB and
5515 * MUST report the peer endpoint unreachable to the upper layer (and
5516 * thus the association enters the CLOSED state). The reception of any
5517 * packet from its peer (i.e. as the peer sends all of its queued DATA
5518 * chunks) should clear the endpoint's retransmission count and restart
5519 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5520 * all of its queued DATA chunks that have not yet been sent.
5522 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
5523 const struct sctp_association *asoc,
5524 const sctp_subtype_t type,
5526 sctp_cmd_seq_t *commands)
5528 struct sctp_chunk *reply = NULL;
5530 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
5531 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5533 ((struct sctp_association *)asoc)->shutdown_retries++;
5535 if (asoc->overall_error_count >= asoc->max_retrans) {
5536 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5537 SCTP_ERROR(ETIMEDOUT));
5538 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5539 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5540 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5541 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5542 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5543 return SCTP_DISPOSITION_DELETE_TCB;
5546 switch (asoc->state) {
5547 case SCTP_STATE_SHUTDOWN_SENT:
5548 reply = sctp_make_shutdown(asoc, NULL);
5551 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5552 reply = sctp_make_shutdown_ack(asoc, NULL);
5563 /* Do some failure management (Section 8.2).
5564 * If we remove the transport an SHUTDOWN was last sent to, don't
5565 * do failure management.
5567 if (asoc->shutdown_last_sent_to)
5568 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5569 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5571 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5572 * the T2-shutdown timer.
5574 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5576 /* Restart the T2-shutdown timer. */
5577 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5578 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5579 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5580 return SCTP_DISPOSITION_CONSUME;
5583 return SCTP_DISPOSITION_NOMEM;
5587 * ADDIP Section 4.1 ASCONF CHunk Procedures
5588 * If the T4 RTO timer expires the endpoint should do B1 to B5
5590 sctp_disposition_t sctp_sf_t4_timer_expire(
5591 const struct sctp_endpoint *ep,
5592 const struct sctp_association *asoc,
5593 const sctp_subtype_t type,
5595 sctp_cmd_seq_t *commands)
5597 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5598 struct sctp_transport *transport = chunk->transport;
5600 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
5602 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5603 * detection on the appropriate destination address as defined in
5604 * RFC2960 [5] section 8.1 and 8.2.
5607 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5608 SCTP_TRANSPORT(transport));
5610 /* Reconfig T4 timer and transport. */
5611 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5613 /* ADDIP 4.1 B2) Increment the association error counters and perform
5614 * endpoint failure detection on the association as defined in
5615 * RFC2960 [5] section 8.1 and 8.2.
5616 * association error counter is incremented in SCTP_CMD_STRIKE.
5618 if (asoc->overall_error_count >= asoc->max_retrans) {
5619 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5620 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5621 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5622 SCTP_ERROR(ETIMEDOUT));
5623 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5624 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5625 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5626 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5627 return SCTP_DISPOSITION_ABORT;
5630 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5631 * the ASCONF chunk was sent by doubling the RTO timer value.
5632 * This is done in SCTP_CMD_STRIKE.
5635 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5636 * choose an alternate destination address (please refer to RFC2960
5637 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5638 * chunk, it MUST be the same (including its serial number) as the last
5641 sctp_chunk_hold(asoc->addip_last_asconf);
5642 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5643 SCTP_CHUNK(asoc->addip_last_asconf));
5645 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5646 * destination is selected, then the RTO used will be that of the new
5647 * destination address.
5649 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5650 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5652 return SCTP_DISPOSITION_CONSUME;
5655 /* sctpimpguide-05 Section 2.12.2
5656 * The sender of the SHUTDOWN MAY also start an overall guard timer
5657 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5658 * At the expiration of this timer the sender SHOULD abort the association
5659 * by sending an ABORT chunk.
5661 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
5662 const struct sctp_association *asoc,
5663 const sctp_subtype_t type,
5665 sctp_cmd_seq_t *commands)
5667 struct sctp_chunk *reply = NULL;
5669 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
5670 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5672 reply = sctp_make_abort(asoc, NULL, 0);
5676 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5677 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5678 SCTP_ERROR(ETIMEDOUT));
5679 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5680 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5682 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5683 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5685 return SCTP_DISPOSITION_DELETE_TCB;
5687 return SCTP_DISPOSITION_NOMEM;
5690 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5691 * the association is automatically closed by starting the shutdown process.
5692 * The work that needs to be done is same as when SHUTDOWN is initiated by
5693 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5695 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5696 const struct sctp_endpoint *ep,
5697 const struct sctp_association *asoc,
5698 const sctp_subtype_t type,
5700 sctp_cmd_seq_t *commands)
5704 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
5706 /* From 9.2 Shutdown of an Association
5707 * Upon receipt of the SHUTDOWN primitive from its upper
5708 * layer, the endpoint enters SHUTDOWN-PENDING state and
5709 * remains there until all outstanding data has been
5710 * acknowledged by its peer. The endpoint accepts no new data
5711 * from its upper layer, but retransmits data to the far end
5712 * if necessary to fill gaps.
5714 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5715 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5717 disposition = SCTP_DISPOSITION_CONSUME;
5718 if (sctp_outq_is_empty(&asoc->outqueue)) {
5719 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
5725 /*****************************************************************************
5726 * These are sa state functions which could apply to all types of events.
5727 ****************************************************************************/
5730 * This table entry is not implemented.
5733 * (endpoint, asoc, chunk)
5735 * The return value is the disposition of the chunk.
5737 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
5738 const struct sctp_association *asoc,
5739 const sctp_subtype_t type,
5741 sctp_cmd_seq_t *commands)
5743 return SCTP_DISPOSITION_NOT_IMPL;
5747 * This table entry represents a bug.
5750 * (endpoint, asoc, chunk)
5752 * The return value is the disposition of the chunk.
5754 sctp_disposition_t sctp_sf_bug(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_BUG;
5764 * This table entry represents the firing of a timer in the wrong state.
5765 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5766 * when the association is in the wrong state. This event should
5767 * be ignored, so as to prevent any rearming of the timer.
5770 * (endpoint, asoc, chunk)
5772 * The return value is the disposition of the chunk.
5774 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5775 const struct sctp_association *asoc,
5776 const sctp_subtype_t type,
5778 sctp_cmd_seq_t *commands)
5780 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5781 return SCTP_DISPOSITION_CONSUME;
5784 /********************************************************************
5785 * 2nd Level Abstractions
5786 ********************************************************************/
5788 /* Pull the SACK chunk based on the SACK header. */
5789 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5791 struct sctp_sackhdr *sack;
5796 /* Protect ourselves from reading too far into
5797 * the skb from a bogus sender.
5799 sack = (struct sctp_sackhdr *) chunk->skb->data;
5801 num_blocks = ntohs(sack->num_gap_ack_blocks);
5802 num_dup_tsns = ntohs(sack->num_dup_tsns);
5803 len = sizeof(struct sctp_sackhdr);
5804 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5805 if (len > chunk->skb->len)
5808 skb_pull(chunk->skb, len);
5813 /* Create an ABORT packet to be sent as a response, with the specified
5816 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5817 const struct sctp_association *asoc,
5818 struct sctp_chunk *chunk,
5819 const void *payload,
5822 struct sctp_packet *packet;
5823 struct sctp_chunk *abort;
5825 packet = sctp_ootb_pkt_new(asoc, chunk);
5829 * The T bit will be set if the asoc is NULL.
5831 abort = sctp_make_abort(asoc, chunk, paylen);
5833 sctp_ootb_pkt_free(packet);
5837 /* Reflect vtag if T-Bit is set */
5838 if (sctp_test_T_bit(abort))
5839 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5841 /* Add specified error causes, i.e., payload, to the
5844 sctp_addto_chunk(abort, paylen, payload);
5846 /* Set the skb to the belonging sock for accounting. */
5847 abort->skb->sk = ep->base.sk;
5849 sctp_packet_append_chunk(packet, abort);
5856 /* Allocate a packet for responding in the OOTB conditions. */
5857 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5858 const struct sctp_chunk *chunk)
5860 struct sctp_packet *packet;
5861 struct sctp_transport *transport;
5866 /* Get the source and destination port from the inbound packet. */
5867 sport = ntohs(chunk->sctp_hdr->dest);
5868 dport = ntohs(chunk->sctp_hdr->source);
5870 /* The V-tag is going to be the same as the inbound packet if no
5871 * association exists, otherwise, use the peer's vtag.
5874 /* Special case the INIT-ACK as there is no peer's vtag
5877 switch(chunk->chunk_hdr->type) {
5878 case SCTP_CID_INIT_ACK:
5880 sctp_initack_chunk_t *initack;
5882 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
5883 vtag = ntohl(initack->init_hdr.init_tag);
5887 vtag = asoc->peer.i.init_tag;
5891 /* Special case the INIT and stale COOKIE_ECHO as there is no
5894 switch(chunk->chunk_hdr->type) {
5897 sctp_init_chunk_t *init;
5899 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5900 vtag = ntohl(init->init_hdr.init_tag);
5904 vtag = ntohl(chunk->sctp_hdr->vtag);
5909 /* Make a transport for the bucket, Eliza... */
5910 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
5914 /* Cache a route for the transport with the chunk's destination as
5915 * the source address.
5917 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
5918 sctp_sk(sctp_get_ctl_sock()));
5920 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5921 packet = sctp_packet_config(packet, vtag, 0);
5929 /* Free the packet allocated earlier for responding in the OOTB condition. */
5930 void sctp_ootb_pkt_free(struct sctp_packet *packet)
5932 sctp_transport_free(packet->transport);
5935 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5936 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5937 const struct sctp_association *asoc,
5938 const struct sctp_chunk *chunk,
5939 sctp_cmd_seq_t *commands,
5940 struct sctp_chunk *err_chunk)
5942 struct sctp_packet *packet;
5945 packet = sctp_ootb_pkt_new(asoc, chunk);
5947 struct sctp_signed_cookie *cookie;
5949 /* Override the OOTB vtag from the cookie. */
5950 cookie = chunk->subh.cookie_hdr;
5951 packet->vtag = cookie->c.peer_vtag;
5953 /* Set the skb to the belonging sock for accounting. */
5954 err_chunk->skb->sk = ep->base.sk;
5955 sctp_packet_append_chunk(packet, err_chunk);
5956 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5957 SCTP_PACKET(packet));
5958 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5960 sctp_chunk_free (err_chunk);
5965 /* Process a data chunk */
5966 static int sctp_eat_data(const struct sctp_association *asoc,
5967 struct sctp_chunk *chunk,
5968 sctp_cmd_seq_t *commands)
5970 sctp_datahdr_t *data_hdr;
5971 struct sctp_chunk *err;
5973 sctp_verb_t deliver;
5976 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
5977 struct sock *sk = asoc->base.sk;
5982 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5983 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5985 tsn = ntohl(data_hdr->tsn);
5986 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5988 /* ASSERT: Now skb->data is really the user data. */
5990 /* Process ECN based congestion.
5992 * Since the chunk structure is reused for all chunks within
5993 * a packet, we use ecn_ce_done to track if we've already
5994 * done CE processing for this packet.
5996 * We need to do ECN processing even if we plan to discard the
6000 if (!chunk->ecn_ce_done) {
6002 chunk->ecn_ce_done = 1;
6004 af = sctp_get_af_specific(
6005 ipver2af(ip_hdr(chunk->skb)->version));
6007 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6008 /* Do real work as sideffect. */
6009 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6014 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6016 /* The TSN is too high--silently discard the chunk and
6017 * count on it getting retransmitted later.
6019 return SCTP_IERROR_HIGH_TSN;
6020 } else if (tmp > 0) {
6021 /* This is a duplicate. Record it. */
6022 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6023 return SCTP_IERROR_DUP_TSN;
6026 /* This is a new TSN. */
6028 /* Discard if there is no room in the receive window.
6029 * Actually, allow a little bit of overflow (up to a MTU).
6031 datalen = ntohs(chunk->chunk_hdr->length);
6032 datalen -= sizeof(sctp_data_chunk_t);
6034 deliver = SCTP_CMD_CHUNK_ULP;
6036 /* Think about partial delivery. */
6037 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6039 /* Even if we don't accept this chunk there is
6042 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6045 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6046 * seems a bit troublesome in that frag_point varies based on
6047 * PMTU. In cases, such as loopback, this might be a rather
6050 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6051 (datalen > asoc->rwnd + asoc->frag_point))) {
6053 /* If this is the next TSN, consider reneging to make
6054 * room. Note: Playing nice with a confused sender. A
6055 * malicious sender can still eat up all our buffer
6056 * space and in the future we may want to detect and
6057 * do more drastic reneging.
6059 if (sctp_tsnmap_has_gap(map) &&
6060 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6061 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6062 deliver = SCTP_CMD_RENEGE;
6064 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6065 "rwnd: %d\n", tsn, datalen,
6067 return SCTP_IERROR_IGNORE_TSN;
6072 * Also try to renege to limit our memory usage in the event that
6073 * we are under memory pressure
6074 * If we can't renege, don't worry about it, the sk_rmem_schedule
6075 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6076 * memory usage too much
6078 if (*sk->sk_prot_creator->memory_pressure) {
6079 if (sctp_tsnmap_has_gap(map) &&
6080 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6081 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6082 deliver = SCTP_CMD_RENEGE;
6087 * Section 3.3.10.9 No User Data (9)
6091 * No User Data: This error cause is returned to the originator of a
6092 * DATA chunk if a received DATA chunk has no user data.
6094 if (unlikely(0 == datalen)) {
6095 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6097 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6100 /* We are going to ABORT, so we might as well stop
6101 * processing the rest of the chunks in the packet.
6103 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6104 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6105 SCTP_ERROR(ECONNABORTED));
6106 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6107 SCTP_PERR(SCTP_ERROR_NO_DATA));
6108 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
6109 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
6110 return SCTP_IERROR_NO_DATA;
6113 chunk->data_accepted = 1;
6115 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6116 * if we renege and the chunk arrives again.
6118 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
6119 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
6121 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
6125 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6127 * If an endpoint receive a DATA chunk with an invalid stream
6128 * identifier, it shall acknowledge the reception of the DATA chunk
6129 * following the normal procedure, immediately send an ERROR chunk
6130 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6131 * and discard the DATA chunk.
6133 sid = ntohs(data_hdr->stream);
6134 if (sid >= asoc->c.sinit_max_instreams) {
6135 /* Mark tsn as received even though we drop it */
6136 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6138 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6140 sizeof(data_hdr->stream),
6143 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6145 return SCTP_IERROR_BAD_STREAM;
6148 /* Check to see if the SSN is possible for this TSN.
6149 * The biggest gap we can record is 4K wide. Since SSNs wrap
6150 * at an unsigned short, there is no way that an SSN can
6151 * wrap and for a valid TSN. We can simply check if the current
6152 * SSN is smaller then the next expected one. If it is, it wrapped
6155 ssn = ntohs(data_hdr->ssn);
6156 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6157 return SCTP_IERROR_PROTO_VIOLATION;
6160 /* Send the data up to the user. Note: Schedule the
6161 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6162 * chunk needs the updated rwnd.
6164 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6166 return SCTP_IERROR_NO_ERROR;