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