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(struct net *net,
70 const struct sctp_endpoint *ep,
71 const struct sctp_association *asoc,
72 struct sctp_chunk *chunk,
75 static int sctp_eat_data(const struct sctp_association *asoc,
76 struct sctp_chunk *chunk,
77 sctp_cmd_seq_t *commands);
78 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
79 const struct sctp_association *asoc,
80 const struct sctp_chunk *chunk);
81 static void sctp_send_stale_cookie_err(struct net *net,
82 const struct sctp_endpoint *ep,
83 const struct sctp_association *asoc,
84 const struct sctp_chunk *chunk,
85 sctp_cmd_seq_t *commands,
86 struct sctp_chunk *err_chunk);
87 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
88 const struct sctp_endpoint *ep,
89 const struct sctp_association *asoc,
90 const sctp_subtype_t type,
92 sctp_cmd_seq_t *commands);
93 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
94 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 sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
100 const struct sctp_endpoint *ep,
101 const struct sctp_association *asoc,
102 const sctp_subtype_t type,
104 sctp_cmd_seq_t *commands);
105 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
107 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
108 sctp_cmd_seq_t *commands,
109 __be16 error, int sk_err,
110 const struct sctp_association *asoc,
111 struct sctp_transport *transport);
113 static sctp_disposition_t sctp_sf_abort_violation(
115 const struct sctp_endpoint *ep,
116 const struct sctp_association *asoc,
118 sctp_cmd_seq_t *commands,
120 const size_t paylen);
122 static sctp_disposition_t sctp_sf_violation_chunklen(
124 const struct sctp_endpoint *ep,
125 const struct sctp_association *asoc,
126 const sctp_subtype_t type,
128 sctp_cmd_seq_t *commands);
130 static sctp_disposition_t sctp_sf_violation_paramlen(
132 const struct sctp_endpoint *ep,
133 const struct sctp_association *asoc,
134 const sctp_subtype_t type,
135 void *arg, void *ext,
136 sctp_cmd_seq_t *commands);
138 static sctp_disposition_t sctp_sf_violation_ctsn(
140 const struct sctp_endpoint *ep,
141 const struct sctp_association *asoc,
142 const sctp_subtype_t type,
144 sctp_cmd_seq_t *commands);
146 static sctp_disposition_t sctp_sf_violation_chunk(
148 const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
152 sctp_cmd_seq_t *commands);
154 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
155 const struct sctp_endpoint *ep,
156 const struct sctp_association *asoc,
157 const sctp_subtype_t type,
158 struct sctp_chunk *chunk);
160 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
161 const struct sctp_endpoint *ep,
162 const struct sctp_association *asoc,
163 const sctp_subtype_t type,
165 sctp_cmd_seq_t *commands);
167 /* Small helper function that checks if the chunk length
168 * is of the appropriate length. The 'required_length' argument
169 * is set to be the size of a specific chunk we are testing.
170 * Return Values: 1 = Valid length
175 sctp_chunk_length_valid(struct sctp_chunk *chunk,
176 __u16 required_length)
178 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
180 if (unlikely(chunk_length < required_length))
186 /**********************************************************
187 * These are the state functions for handling chunk events.
188 **********************************************************/
191 * Process the final SHUTDOWN COMPLETE.
193 * Section: 4 (C) (diagram), 9.2
194 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
195 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
196 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
197 * should stop the T2-shutdown timer and remove all knowledge of the
198 * association (and thus the association enters the CLOSED state).
200 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
201 * C) Rules for packet carrying SHUTDOWN COMPLETE:
203 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
204 * if the Verification Tag field of the packet matches its own tag and
205 * the T bit is not set
207 * it is set to its peer's tag and the T bit is set in the Chunk
209 * Otherwise, the receiver MUST silently discard the packet
210 * and take no further action. An endpoint MUST ignore the
211 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
214 * (endpoint, asoc, chunk)
217 * (asoc, reply_msg, msg_up, timers, counters)
219 * The return value is the disposition of the chunk.
221 sctp_disposition_t sctp_sf_do_4_C(struct net *net,
222 const struct sctp_endpoint *ep,
223 const struct sctp_association *asoc,
224 const sctp_subtype_t type,
226 sctp_cmd_seq_t *commands)
228 struct sctp_chunk *chunk = arg;
229 struct sctp_ulpevent *ev;
231 if (!sctp_vtag_verify_either(chunk, asoc))
232 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
234 /* RFC 2960 6.10 Bundling
236 * An endpoint MUST NOT bundle INIT, INIT ACK or
237 * SHUTDOWN COMPLETE with any other chunks.
239 if (!chunk->singleton)
240 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
242 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
243 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
244 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
247 /* RFC 2960 10.2 SCTP-to-ULP
249 * H) SHUTDOWN COMPLETE notification
251 * When SCTP completes the shutdown procedures (section 9.2) this
252 * notification is passed to the upper layer.
254 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
255 0, 0, 0, NULL, GFP_ATOMIC);
257 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
260 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
261 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
262 * not the chunk should be discarded. If the endpoint is in
263 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
264 * T2-shutdown timer and remove all knowledge of the
265 * association (and thus the association enters the CLOSED
268 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
269 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
272 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
274 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
275 SCTP_STATE(SCTP_STATE_CLOSED));
277 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
278 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
280 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
282 return SCTP_DISPOSITION_DELETE_TCB;
286 * Respond to a normal INIT chunk.
287 * We are the side that is being asked for an association.
289 * Section: 5.1 Normal Establishment of an Association, B
290 * B) "Z" shall respond immediately with an INIT ACK chunk. The
291 * destination IP address of the INIT ACK MUST be set to the source
292 * IP address of the INIT to which this INIT ACK is responding. In
293 * the response, besides filling in other parameters, "Z" must set the
294 * Verification Tag field to Tag_A, and also provide its own
295 * Verification Tag (Tag_Z) in the Initiate Tag field.
297 * Verification Tag: Must be 0.
300 * (endpoint, asoc, chunk)
303 * (asoc, reply_msg, msg_up, timers, counters)
305 * The return value is the disposition of the chunk.
307 sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
308 const struct sctp_endpoint *ep,
309 const struct sctp_association *asoc,
310 const sctp_subtype_t type,
312 sctp_cmd_seq_t *commands)
314 struct sctp_chunk *chunk = arg;
315 struct sctp_chunk *repl;
316 struct sctp_association *new_asoc;
317 struct sctp_chunk *err_chunk;
318 struct sctp_packet *packet;
319 sctp_unrecognized_param_t *unk_param;
323 * An endpoint MUST NOT bundle INIT, INIT ACK or
324 * SHUTDOWN COMPLETE with any other chunks.
327 * Furthermore, we require that the receiver of an INIT chunk MUST
328 * enforce these rules by silently discarding an arriving packet
329 * with an INIT chunk that is bundled with other chunks.
331 if (!chunk->singleton)
332 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
334 /* If the packet is an OOTB packet which is temporarily on the
335 * control endpoint, respond with an ABORT.
337 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
338 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
339 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
342 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
345 if (chunk->sctp_hdr->vtag != 0)
346 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
348 /* Make sure that the INIT chunk has a valid length.
349 * Normally, this would cause an ABORT with a Protocol Violation
350 * error, but since we don't have an association, we'll
351 * just discard the packet.
353 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
354 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
356 /* If the INIT is coming toward a closing socket, we'll send back
357 * and ABORT. Essentially, this catches the race of INIT being
358 * backloged to the socket at the same time as the user isses close().
359 * Since the socket and all its associations are going away, we
360 * can treat this OOTB
362 if (sctp_sstate(ep->base.sk, CLOSING))
363 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
365 /* Verify the INIT chunk before processing it. */
367 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
368 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
370 /* This chunk contains fatal error. It is to be discarded.
371 * Send an ABORT, with causes if there is any.
374 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
375 (__u8 *)(err_chunk->chunk_hdr) +
376 sizeof(sctp_chunkhdr_t),
377 ntohs(err_chunk->chunk_hdr->length) -
378 sizeof(sctp_chunkhdr_t));
380 sctp_chunk_free(err_chunk);
383 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
384 SCTP_PACKET(packet));
385 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
386 return SCTP_DISPOSITION_CONSUME;
388 return SCTP_DISPOSITION_NOMEM;
391 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
396 /* Grab the INIT header. */
397 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
399 /* Tag the variable length parameters. */
400 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
402 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
406 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
407 sctp_scope(sctp_source(chunk)),
411 /* The call, sctp_process_init(), can fail on memory allocation. */
412 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
413 (sctp_init_chunk_t *)chunk->chunk_hdr,
417 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
419 /* If there are errors need to be reported for unknown parameters,
420 * make sure to reserve enough room in the INIT ACK for them.
424 len = ntohs(err_chunk->chunk_hdr->length) -
425 sizeof(sctp_chunkhdr_t);
427 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
431 /* If there are errors need to be reported for unknown parameters,
432 * include them in the outgoing INIT ACK as "Unrecognized parameter"
436 /* Get the "Unrecognized parameter" parameter(s) out of the
437 * ERROR chunk generated by sctp_verify_init(). Since the
438 * error cause code for "unknown parameter" and the
439 * "Unrecognized parameter" type is the same, we can
440 * construct the parameters in INIT ACK by copying the
443 unk_param = (sctp_unrecognized_param_t *)
444 ((__u8 *)(err_chunk->chunk_hdr) +
445 sizeof(sctp_chunkhdr_t));
446 /* Replace the cause code with the "Unrecognized parameter"
449 sctp_addto_chunk(repl, len, unk_param);
450 sctp_chunk_free(err_chunk);
453 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
455 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
458 * Note: After sending out INIT ACK with the State Cookie parameter,
459 * "Z" MUST NOT allocate any resources, nor keep any states for the
460 * new association. Otherwise, "Z" will be vulnerable to resource
463 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
465 return SCTP_DISPOSITION_DELETE_TCB;
468 sctp_association_free(new_asoc);
471 sctp_chunk_free(err_chunk);
472 return SCTP_DISPOSITION_NOMEM;
476 * Respond to a normal INIT ACK chunk.
477 * We are the side that is initiating the association.
479 * Section: 5.1 Normal Establishment of an Association, C
480 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
481 * timer and leave COOKIE-WAIT state. "A" shall then send the State
482 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
483 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
485 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
486 * DATA chunks, but it MUST be the first chunk in the packet and
487 * until the COOKIE ACK is returned the sender MUST NOT send any
488 * other packets to the peer.
490 * Verification Tag: 3.3.3
491 * If the value of the Initiate Tag in a received INIT ACK chunk is
492 * found to be 0, the receiver MUST treat it as an error and close the
493 * association by transmitting an ABORT.
496 * (endpoint, asoc, chunk)
499 * (asoc, reply_msg, msg_up, timers, counters)
501 * The return value is the disposition of the chunk.
503 sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
504 const struct sctp_endpoint *ep,
505 const struct sctp_association *asoc,
506 const sctp_subtype_t type,
508 sctp_cmd_seq_t *commands)
510 struct sctp_chunk *chunk = arg;
511 sctp_init_chunk_t *initchunk;
512 struct sctp_chunk *err_chunk;
513 struct sctp_packet *packet;
515 if (!sctp_vtag_verify(chunk, asoc))
516 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
519 * An endpoint MUST NOT bundle INIT, INIT ACK or
520 * SHUTDOWN COMPLETE with any other chunks.
522 if (!chunk->singleton)
523 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
525 /* Make sure that the INIT-ACK chunk has a valid length */
526 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
527 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
529 /* Grab the INIT header. */
530 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
532 /* Verify the INIT chunk before processing it. */
534 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
535 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
538 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
540 /* This chunk contains fatal error. It is to be discarded.
541 * Send an ABORT, with causes. If there are no causes,
542 * then there wasn't enough memory. Just terminate
546 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
547 (__u8 *)(err_chunk->chunk_hdr) +
548 sizeof(sctp_chunkhdr_t),
549 ntohs(err_chunk->chunk_hdr->length) -
550 sizeof(sctp_chunkhdr_t));
552 sctp_chunk_free(err_chunk);
555 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
556 SCTP_PACKET(packet));
557 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
558 error = SCTP_ERROR_INV_PARAM;
562 /* SCTP-AUTH, Section 6.3:
563 * It should be noted that if the receiver wants to tear
564 * down an association in an authenticated way only, the
565 * handling of malformed packets should not result in
566 * tearing down the association.
568 * This means that if we only want to abort associations
569 * in an authenticated way (i.e AUTH+ABORT), then we
570 * can't destroy this association just because the packet
573 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
574 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
576 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
577 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
578 asoc, chunk->transport);
581 /* Tag the variable length parameters. Note that we never
582 * convert the parameters in an INIT chunk.
584 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
586 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
588 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
589 SCTP_PEER_INIT(initchunk));
591 /* Reset init error count upon receipt of INIT-ACK. */
592 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
594 /* 5.1 C) "A" shall stop the T1-init timer and leave
595 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
596 * timer, and enter the COOKIE-ECHOED state.
598 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
599 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
600 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
601 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
602 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
603 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
605 /* SCTP-AUTH: genereate the assocition shared keys so that
606 * we can potentially signe the COOKIE-ECHO.
608 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
610 /* 5.1 C) "A" shall then send the State Cookie received in the
611 * INIT ACK chunk in a COOKIE ECHO chunk, ...
613 /* If there is any errors to report, send the ERROR chunk generated
614 * for unknown parameters as well.
616 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
617 SCTP_CHUNK(err_chunk));
619 return SCTP_DISPOSITION_CONSUME;
623 * Respond to a normal COOKIE ECHO chunk.
624 * We are the side that is being asked for an association.
626 * Section: 5.1 Normal Establishment of an Association, D
627 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
628 * with a COOKIE ACK chunk after building a TCB and moving to
629 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
630 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
631 * chunk MUST be the first chunk in the packet.
633 * IMPLEMENTATION NOTE: An implementation may choose to send the
634 * Communication Up notification to the SCTP user upon reception
635 * of a valid COOKIE ECHO chunk.
637 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
638 * D) Rules for packet carrying a COOKIE ECHO
640 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
641 * Initial Tag received in the INIT ACK.
643 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
646 * (endpoint, asoc, chunk)
649 * (asoc, reply_msg, msg_up, timers, counters)
651 * The return value is the disposition of the chunk.
653 sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
654 const struct sctp_endpoint *ep,
655 const struct sctp_association *asoc,
656 const sctp_subtype_t type, void *arg,
657 sctp_cmd_seq_t *commands)
659 struct sctp_chunk *chunk = arg;
660 struct sctp_association *new_asoc;
661 sctp_init_chunk_t *peer_init;
662 struct sctp_chunk *repl;
663 struct sctp_ulpevent *ev, *ai_ev = NULL;
665 struct sctp_chunk *err_chk_p;
668 /* If the packet is an OOTB packet which is temporarily on the
669 * control endpoint, respond with an ABORT.
671 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
672 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
673 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
676 /* Make sure that the COOKIE_ECHO chunk has a valid length.
677 * In this case, we check that we have enough for at least a
678 * chunk header. More detailed verification is done
679 * in sctp_unpack_cookie().
681 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
682 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
684 /* If the endpoint is not listening or if the number of associations
685 * on the TCP-style socket exceed the max backlog, respond with an
689 if (!sctp_sstate(sk, LISTENING) ||
690 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
691 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
693 /* "Decode" the chunk. We have no optional parameters so we
696 chunk->subh.cookie_hdr =
697 (struct sctp_signed_cookie *)chunk->skb->data;
698 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
699 sizeof(sctp_chunkhdr_t)))
702 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
703 * "Z" will reply with a COOKIE ACK chunk after building a TCB
704 * and moving to the ESTABLISHED state.
706 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
710 * If the re-build failed, what is the proper error path
713 * [We should abort the association. --piggy]
716 /* FIXME: Several errors are possible. A bad cookie should
717 * be silently discarded, but think about logging it too.
720 case -SCTP_IERROR_NOMEM:
723 case -SCTP_IERROR_STALE_COOKIE:
724 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
726 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
728 case -SCTP_IERROR_BAD_SIG:
730 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
735 /* Delay state machine commands until later.
737 * Re-build the bind address for the association is done in
738 * the sctp_unpack_cookie() already.
740 /* This is a brand-new association, so these are not yet side
741 * effects--it is safe to run them here.
743 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
745 if (!sctp_process_init(new_asoc, chunk,
746 &chunk->subh.cookie_hdr->c.peer_addr,
747 peer_init, GFP_ATOMIC))
750 /* SCTP-AUTH: Now that we've populate required fields in
751 * sctp_process_init, set up the assocaition shared keys as
752 * necessary so that we can potentially authenticate the ACK
754 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
758 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
759 * is supposed to be authenticated and we have to do delayed
760 * authentication. We've just recreated the association using
761 * the information in the cookie and now it's much easier to
762 * do the authentication.
764 if (chunk->auth_chunk) {
765 struct sctp_chunk auth;
768 /* Make sure that we and the peer are AUTH capable */
769 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
770 sctp_association_free(new_asoc);
771 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
774 /* set-up our fake chunk so that we can process it */
775 auth.skb = chunk->auth_chunk;
776 auth.asoc = chunk->asoc;
777 auth.sctp_hdr = chunk->sctp_hdr;
778 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
779 sizeof(sctp_chunkhdr_t));
780 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
781 auth.transport = chunk->transport;
783 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
784 if (ret != SCTP_IERROR_NO_ERROR) {
785 sctp_association_free(new_asoc);
786 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
790 repl = sctp_make_cookie_ack(new_asoc, chunk);
794 /* RFC 2960 5.1 Normal Establishment of an Association
796 * D) IMPLEMENTATION NOTE: An implementation may choose to
797 * send the Communication Up notification to the SCTP user
798 * upon reception of a valid COOKIE ECHO chunk.
800 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
801 new_asoc->c.sinit_num_ostreams,
802 new_asoc->c.sinit_max_instreams,
807 /* Sockets API Draft Section 5.3.1.6
808 * When a peer sends a Adaptation Layer Indication parameter , SCTP
809 * delivers this notification to inform the application that of the
810 * peers requested adaptation layer.
812 if (new_asoc->peer.adaptation_ind) {
813 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
819 /* Add all the state machine commands now since we've created
820 * everything. This way we don't introduce memory corruptions
821 * during side-effect processing and correclty count established
824 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
825 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
826 SCTP_STATE(SCTP_STATE_ESTABLISHED));
827 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
828 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
829 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
831 if (new_asoc->autoclose)
832 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
833 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
835 /* This will send the COOKIE ACK */
836 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
838 /* Queue the ASSOC_CHANGE event */
839 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
841 /* Send up the Adaptation Layer Indication event */
843 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
844 SCTP_ULPEVENT(ai_ev));
846 return SCTP_DISPOSITION_CONSUME;
849 sctp_ulpevent_free(ev);
851 sctp_chunk_free(repl);
853 sctp_association_free(new_asoc);
855 return SCTP_DISPOSITION_NOMEM;
859 * Respond to a normal COOKIE ACK chunk.
860 * We are the side that is being asked for an association.
862 * RFC 2960 5.1 Normal Establishment of an Association
864 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
865 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
866 * timer. It may also notify its ULP about the successful
867 * establishment of the association with a Communication Up
868 * notification (see Section 10).
872 * (endpoint, asoc, chunk)
875 * (asoc, reply_msg, msg_up, timers, counters)
877 * The return value is the disposition of the chunk.
879 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
880 const struct sctp_endpoint *ep,
881 const struct sctp_association *asoc,
882 const sctp_subtype_t type, void *arg,
883 sctp_cmd_seq_t *commands)
885 struct sctp_chunk *chunk = arg;
886 struct sctp_ulpevent *ev;
888 if (!sctp_vtag_verify(chunk, asoc))
889 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
891 /* Verify that the chunk length for the COOKIE-ACK is OK.
892 * If we don't do this, any bundled chunks may be junked.
894 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
895 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
898 /* Reset init error count upon receipt of COOKIE-ACK,
899 * to avoid problems with the managemement of this
900 * counter in stale cookie situations when a transition back
901 * from the COOKIE-ECHOED state to the COOKIE-WAIT
902 * state is performed.
904 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
906 /* RFC 2960 5.1 Normal Establishment of an Association
908 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
909 * from the COOKIE-ECHOED state to the ESTABLISHED state,
910 * stopping the T1-cookie timer.
912 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
913 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
914 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
915 SCTP_STATE(SCTP_STATE_ESTABLISHED));
916 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
917 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
918 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
920 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
921 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
923 /* It may also notify its ULP about the successful
924 * establishment of the association with a Communication Up
925 * notification (see Section 10).
927 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
928 0, asoc->c.sinit_num_ostreams,
929 asoc->c.sinit_max_instreams,
935 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
937 /* Sockets API Draft Section 5.3.1.6
938 * When a peer sends a Adaptation Layer Indication parameter , SCTP
939 * delivers this notification to inform the application that of the
940 * peers requested adaptation layer.
942 if (asoc->peer.adaptation_ind) {
943 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
947 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
951 return SCTP_DISPOSITION_CONSUME;
953 return SCTP_DISPOSITION_NOMEM;
956 /* Generate and sendout a heartbeat packet. */
957 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
958 const struct sctp_association *asoc,
959 const sctp_subtype_t type,
961 sctp_cmd_seq_t *commands)
963 struct sctp_transport *transport = (struct sctp_transport *) arg;
964 struct sctp_chunk *reply;
966 /* Send a heartbeat to our peer. */
967 reply = sctp_make_heartbeat(asoc, transport);
969 return SCTP_DISPOSITION_NOMEM;
971 /* Set rto_pending indicating that an RTT measurement
972 * is started with this heartbeat chunk.
974 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
975 SCTP_TRANSPORT(transport));
977 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
978 return SCTP_DISPOSITION_CONSUME;
981 /* Generate a HEARTBEAT packet on the given transport. */
982 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
983 const struct sctp_endpoint *ep,
984 const struct sctp_association *asoc,
985 const sctp_subtype_t type,
987 sctp_cmd_seq_t *commands)
989 struct sctp_transport *transport = (struct sctp_transport *) arg;
991 if (asoc->overall_error_count >= asoc->max_retrans) {
992 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
993 SCTP_ERROR(ETIMEDOUT));
994 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
995 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
996 SCTP_PERR(SCTP_ERROR_NO_ERROR));
997 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
998 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
999 return SCTP_DISPOSITION_DELETE_TCB;
1003 * The Sender-specific Heartbeat Info field should normally include
1004 * information about the sender's current time when this HEARTBEAT
1005 * chunk is sent and the destination transport address to which this
1006 * HEARTBEAT is sent (see Section 8.3).
1009 if (transport->param_flags & SPP_HB_ENABLE) {
1010 if (SCTP_DISPOSITION_NOMEM ==
1011 sctp_sf_heartbeat(ep, asoc, type, arg,
1013 return SCTP_DISPOSITION_NOMEM;
1015 /* Set transport error counter and association error counter
1016 * when sending heartbeat.
1018 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1019 SCTP_TRANSPORT(transport));
1021 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1022 SCTP_TRANSPORT(transport));
1023 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1024 SCTP_TRANSPORT(transport));
1026 return SCTP_DISPOSITION_CONSUME;
1030 * Process an heartbeat request.
1032 * Section: 8.3 Path Heartbeat
1033 * The receiver of the HEARTBEAT should immediately respond with a
1034 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1035 * from the received HEARTBEAT chunk.
1037 * Verification Tag: 8.5 Verification Tag [Normal verification]
1038 * When receiving an SCTP packet, the endpoint MUST ensure that the
1039 * value in the Verification Tag field of the received SCTP packet
1040 * matches its own Tag. If the received Verification Tag value does not
1041 * match the receiver's own tag value, the receiver shall silently
1042 * discard the packet and shall not process it any further except for
1043 * those cases listed in Section 8.5.1 below.
1046 * (endpoint, asoc, chunk)
1049 * (asoc, reply_msg, msg_up, timers, counters)
1051 * The return value is the disposition of the chunk.
1053 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1054 const struct sctp_endpoint *ep,
1055 const struct sctp_association *asoc,
1056 const sctp_subtype_t type,
1058 sctp_cmd_seq_t *commands)
1060 sctp_paramhdr_t *param_hdr;
1061 struct sctp_chunk *chunk = arg;
1062 struct sctp_chunk *reply;
1065 if (!sctp_vtag_verify(chunk, asoc))
1066 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1068 /* Make sure that the HEARTBEAT chunk has a valid length. */
1069 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1070 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1073 /* 8.3 The receiver of the HEARTBEAT should immediately
1074 * respond with a HEARTBEAT ACK that contains the Heartbeat
1075 * Information field copied from the received HEARTBEAT chunk.
1077 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1078 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
1079 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1081 if (ntohs(param_hdr->length) > paylen)
1082 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1083 param_hdr, commands);
1085 if (!pskb_pull(chunk->skb, paylen))
1088 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1092 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1093 return SCTP_DISPOSITION_CONSUME;
1096 return SCTP_DISPOSITION_NOMEM;
1100 * Process the returning HEARTBEAT ACK.
1102 * Section: 8.3 Path Heartbeat
1103 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1104 * should clear the error counter of the destination transport
1105 * address to which the HEARTBEAT was sent, and mark the destination
1106 * transport address as active if it is not so marked. The endpoint may
1107 * optionally report to the upper layer when an inactive destination
1108 * address is marked as active due to the reception of the latest
1109 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1110 * clear the association overall error count as well (as defined
1113 * The receiver of the HEARTBEAT ACK should also perform an RTT
1114 * measurement for that destination transport address using the time
1115 * value carried in the HEARTBEAT ACK chunk.
1117 * Verification Tag: 8.5 Verification Tag [Normal verification]
1120 * (endpoint, asoc, chunk)
1123 * (asoc, reply_msg, msg_up, timers, counters)
1125 * The return value is the disposition of the chunk.
1127 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1128 const struct sctp_endpoint *ep,
1129 const struct sctp_association *asoc,
1130 const sctp_subtype_t type,
1132 sctp_cmd_seq_t *commands)
1134 struct sctp_chunk *chunk = arg;
1135 union sctp_addr from_addr;
1136 struct sctp_transport *link;
1137 sctp_sender_hb_info_t *hbinfo;
1138 unsigned long max_interval;
1140 if (!sctp_vtag_verify(chunk, asoc))
1141 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1143 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1144 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1145 sizeof(sctp_sender_hb_info_t)))
1146 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1149 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1150 /* Make sure that the length of the parameter is what we expect */
1151 if (ntohs(hbinfo->param_hdr.length) !=
1152 sizeof(sctp_sender_hb_info_t)) {
1153 return SCTP_DISPOSITION_DISCARD;
1156 from_addr = hbinfo->daddr;
1157 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1159 /* This should never happen, but lets log it if so. */
1160 if (unlikely(!link)) {
1161 if (from_addr.sa.sa_family == AF_INET6) {
1162 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1165 &from_addr.v6.sin6_addr);
1167 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1170 &from_addr.v4.sin_addr.s_addr);
1172 return SCTP_DISPOSITION_DISCARD;
1175 /* Validate the 64-bit random nonce. */
1176 if (hbinfo->hb_nonce != link->hb_nonce)
1177 return SCTP_DISPOSITION_DISCARD;
1179 max_interval = link->hbinterval + link->rto;
1181 /* Check if the timestamp looks valid. */
1182 if (time_after(hbinfo->sent_at, jiffies) ||
1183 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1184 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1185 "received for transport: %p\n",
1187 return SCTP_DISPOSITION_DISCARD;
1190 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1191 * the HEARTBEAT should clear the error counter of the
1192 * destination transport address to which the HEARTBEAT was
1193 * sent and mark the destination transport address as active if
1194 * it is not so marked.
1196 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1198 return SCTP_DISPOSITION_CONSUME;
1201 /* Helper function to send out an abort for the restart
1204 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1205 struct sctp_chunk *init,
1206 sctp_cmd_seq_t *commands)
1209 struct sctp_packet *pkt;
1210 union sctp_addr_param *addrparm;
1211 struct sctp_errhdr *errhdr;
1212 struct sctp_endpoint *ep;
1213 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1214 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1216 /* Build the error on the stack. We are way to malloc crazy
1217 * throughout the code today.
1219 errhdr = (struct sctp_errhdr *)buffer;
1220 addrparm = (union sctp_addr_param *)errhdr->variable;
1222 /* Copy into a parm format. */
1223 len = af->to_addr_param(ssa, addrparm);
1224 len += sizeof(sctp_errhdr_t);
1226 errhdr->cause = SCTP_ERROR_RESTART;
1227 errhdr->length = htons(len);
1229 /* Assign to the control socket. */
1230 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1232 /* Association is NULL since this may be a restart attack and we
1233 * want to send back the attacker's vtag.
1235 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1239 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1241 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1243 /* Discard the rest of the inbound packet. */
1244 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1247 /* Even if there is no memory, treat as a failure so
1248 * the packet will get dropped.
1253 static bool list_has_sctp_addr(const struct list_head *list,
1254 union sctp_addr *ipaddr)
1256 struct sctp_transport *addr;
1258 list_for_each_entry(addr, list, transports) {
1259 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1265 /* A restart is occurring, check to make sure no new addresses
1266 * are being added as we may be under a takeover attack.
1268 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1269 const struct sctp_association *asoc,
1270 struct sctp_chunk *init,
1271 sctp_cmd_seq_t *commands)
1273 struct net *net = sock_net(new_asoc->base.sk);
1274 struct sctp_transport *new_addr;
1277 /* Implementor's Guide - Section 5.2.2
1279 * Before responding the endpoint MUST check to see if the
1280 * unexpected INIT adds new addresses to the association. If new
1281 * addresses are added to the association, the endpoint MUST respond
1285 /* Search through all current addresses and make sure
1286 * we aren't adding any new ones.
1288 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1290 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1291 &new_addr->ipaddr)) {
1292 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1299 /* Return success if all addresses were found. */
1303 /* Populate the verification/tie tags based on overlapping INIT
1306 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1308 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1309 const struct sctp_association *asoc)
1311 switch (asoc->state) {
1313 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1315 case SCTP_STATE_COOKIE_WAIT:
1316 new_asoc->c.my_vtag = asoc->c.my_vtag;
1317 new_asoc->c.my_ttag = asoc->c.my_vtag;
1318 new_asoc->c.peer_ttag = 0;
1321 case SCTP_STATE_COOKIE_ECHOED:
1322 new_asoc->c.my_vtag = asoc->c.my_vtag;
1323 new_asoc->c.my_ttag = asoc->c.my_vtag;
1324 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1327 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1328 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1331 new_asoc->c.my_ttag = asoc->c.my_vtag;
1332 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1336 /* Other parameters for the endpoint SHOULD be copied from the
1337 * existing parameters of the association (e.g. number of
1338 * outbound streams) into the INIT ACK and cookie.
1340 new_asoc->rwnd = asoc->rwnd;
1341 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1342 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1343 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1347 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1350 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1352 * Returns value representing action to be taken. These action values
1353 * correspond to Action/Description values in RFC 2960, Table 2.
1355 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1356 const struct sctp_association *asoc)
1358 /* In this case, the peer may have restarted. */
1359 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1360 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1361 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1362 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1365 /* Collision case B. */
1366 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1367 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1368 (0 == asoc->c.peer_vtag))) {
1372 /* Collision case D. */
1373 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1374 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1377 /* Collision case C. */
1378 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1379 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1380 (0 == new_asoc->c.my_ttag) &&
1381 (0 == new_asoc->c.peer_ttag))
1384 /* No match to any of the special cases; discard this packet. */
1388 /* Common helper routine for both duplicate and simulataneous INIT
1391 static sctp_disposition_t sctp_sf_do_unexpected_init(
1393 const struct sctp_endpoint *ep,
1394 const struct sctp_association *asoc,
1395 const sctp_subtype_t type,
1396 void *arg, sctp_cmd_seq_t *commands)
1398 sctp_disposition_t retval;
1399 struct sctp_chunk *chunk = arg;
1400 struct sctp_chunk *repl;
1401 struct sctp_association *new_asoc;
1402 struct sctp_chunk *err_chunk;
1403 struct sctp_packet *packet;
1404 sctp_unrecognized_param_t *unk_param;
1408 * An endpoint MUST NOT bundle INIT, INIT ACK or
1409 * SHUTDOWN COMPLETE with any other chunks.
1412 * Furthermore, we require that the receiver of an INIT chunk MUST
1413 * enforce these rules by silently discarding an arriving packet
1414 * with an INIT chunk that is bundled with other chunks.
1416 if (!chunk->singleton)
1417 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1419 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1422 if (chunk->sctp_hdr->vtag != 0)
1423 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1425 /* Make sure that the INIT chunk has a valid length.
1426 * In this case, we generate a protocol violation since we have
1427 * an association established.
1429 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1430 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1432 /* Grab the INIT header. */
1433 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1435 /* Tag the variable length parameters. */
1436 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1438 /* Verify the INIT chunk before processing it. */
1440 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1441 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1443 /* This chunk contains fatal error. It is to be discarded.
1444 * Send an ABORT, with causes if there is any.
1447 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1448 (__u8 *)(err_chunk->chunk_hdr) +
1449 sizeof(sctp_chunkhdr_t),
1450 ntohs(err_chunk->chunk_hdr->length) -
1451 sizeof(sctp_chunkhdr_t));
1454 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1455 SCTP_PACKET(packet));
1456 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1457 retval = SCTP_DISPOSITION_CONSUME;
1459 retval = SCTP_DISPOSITION_NOMEM;
1463 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1469 * Other parameters for the endpoint SHOULD be copied from the
1470 * existing parameters of the association (e.g. number of
1471 * outbound streams) into the INIT ACK and cookie.
1472 * FIXME: We are copying parameters from the endpoint not the
1475 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1479 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1480 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1483 /* In the outbound INIT ACK the endpoint MUST copy its current
1484 * Verification Tag and Peers Verification tag into a reserved
1485 * place (local tie-tag and per tie-tag) within the state cookie.
1487 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1488 (sctp_init_chunk_t *)chunk->chunk_hdr,
1492 /* Make sure no new addresses are being added during the
1493 * restart. Do not do this check for COOKIE-WAIT state,
1494 * since there are no peer addresses to check against.
1495 * Upon return an ABORT will have been sent if needed.
1497 if (!sctp_state(asoc, COOKIE_WAIT)) {
1498 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1500 retval = SCTP_DISPOSITION_CONSUME;
1505 sctp_tietags_populate(new_asoc, asoc);
1507 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1509 /* If there are errors need to be reported for unknown parameters,
1510 * make sure to reserve enough room in the INIT ACK for them.
1514 len = ntohs(err_chunk->chunk_hdr->length) -
1515 sizeof(sctp_chunkhdr_t);
1518 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1522 /* If there are errors need to be reported for unknown parameters,
1523 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1527 /* Get the "Unrecognized parameter" parameter(s) out of the
1528 * ERROR chunk generated by sctp_verify_init(). Since the
1529 * error cause code for "unknown parameter" and the
1530 * "Unrecognized parameter" type is the same, we can
1531 * construct the parameters in INIT ACK by copying the
1532 * ERROR causes over.
1534 unk_param = (sctp_unrecognized_param_t *)
1535 ((__u8 *)(err_chunk->chunk_hdr) +
1536 sizeof(sctp_chunkhdr_t));
1537 /* Replace the cause code with the "Unrecognized parameter"
1540 sctp_addto_chunk(repl, len, unk_param);
1543 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1544 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1547 * Note: After sending out INIT ACK with the State Cookie parameter,
1548 * "Z" MUST NOT allocate any resources for this new association.
1549 * Otherwise, "Z" will be vulnerable to resource attacks.
1551 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1552 retval = SCTP_DISPOSITION_CONSUME;
1557 retval = SCTP_DISPOSITION_NOMEM;
1560 sctp_association_free(new_asoc);
1563 sctp_chunk_free(err_chunk);
1568 * Handle simultaneous INIT.
1569 * This means we started an INIT and then we got an INIT request from
1572 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1573 * This usually indicates an initialization collision, i.e., each
1574 * endpoint is attempting, at about the same time, to establish an
1575 * association with the other endpoint.
1577 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1578 * endpoint MUST respond with an INIT ACK using the same parameters it
1579 * sent in its original INIT chunk (including its Verification Tag,
1580 * unchanged). These original parameters are combined with those from the
1581 * newly received INIT chunk. The endpoint shall also generate a State
1582 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1583 * INIT to calculate the State Cookie.
1585 * After that, the endpoint MUST NOT change its state, the T1-init
1586 * timer shall be left running and the corresponding TCB MUST NOT be
1587 * destroyed. The normal procedures for handling State Cookies when
1588 * a TCB exists will resolve the duplicate INITs to a single association.
1590 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1591 * its Tie-Tags with the Tag information of itself and its peer (see
1592 * section 5.2.2 for a description of the Tie-Tags).
1594 * Verification Tag: Not explicit, but an INIT can not have a valid
1595 * verification tag, so we skip the check.
1598 * (endpoint, asoc, chunk)
1601 * (asoc, reply_msg, msg_up, timers, counters)
1603 * The return value is the disposition of the chunk.
1605 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1606 const struct sctp_endpoint *ep,
1607 const struct sctp_association *asoc,
1608 const sctp_subtype_t type,
1610 sctp_cmd_seq_t *commands)
1612 /* Call helper to do the real work for both simulataneous and
1613 * duplicate INIT chunk handling.
1615 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1619 * Handle duplicated INIT messages. These are usually delayed
1622 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1623 * COOKIE-ECHOED and COOKIE-WAIT
1625 * Unless otherwise stated, upon reception of an unexpected INIT for
1626 * this association, the endpoint shall generate an INIT ACK with a
1627 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1628 * current Verification Tag and peer's Verification Tag into a reserved
1629 * place within the state cookie. We shall refer to these locations as
1630 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1631 * containing this INIT ACK MUST carry a Verification Tag value equal to
1632 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1633 * MUST contain a new Initiation Tag (randomly generated see Section
1634 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1635 * existing parameters of the association (e.g. number of outbound
1636 * streams) into the INIT ACK and cookie.
1638 * After sending out the INIT ACK, the endpoint shall take no further
1639 * actions, i.e., the existing association, including its current state,
1640 * and the corresponding TCB MUST NOT be changed.
1642 * Note: Only when a TCB exists and the association is not in a COOKIE-
1643 * WAIT state are the Tie-Tags populated. For a normal association INIT
1644 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1645 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1646 * State Cookie are populated as specified in section 5.2.1.
1648 * Verification Tag: Not specified, but an INIT has no way of knowing
1649 * what the verification tag could be, so we ignore it.
1652 * (endpoint, asoc, chunk)
1655 * (asoc, reply_msg, msg_up, timers, counters)
1657 * The return value is the disposition of the chunk.
1659 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1660 const struct sctp_endpoint *ep,
1661 const struct sctp_association *asoc,
1662 const sctp_subtype_t type,
1664 sctp_cmd_seq_t *commands)
1666 /* Call helper to do the real work for both simulataneous and
1667 * duplicate INIT chunk handling.
1669 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1674 * Unexpected INIT-ACK handler.
1677 * If an INIT ACK received by an endpoint in any state other than the
1678 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1679 * An unexpected INIT ACK usually indicates the processing of an old or
1680 * duplicated INIT chunk.
1682 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1683 const struct sctp_endpoint *ep,
1684 const struct sctp_association *asoc,
1685 const sctp_subtype_t type,
1686 void *arg, sctp_cmd_seq_t *commands)
1688 /* Per the above section, we'll discard the chunk if we have an
1689 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1691 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1692 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1694 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1697 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1700 * A) In this case, the peer may have restarted.
1702 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1703 const struct sctp_endpoint *ep,
1704 const struct sctp_association *asoc,
1705 struct sctp_chunk *chunk,
1706 sctp_cmd_seq_t *commands,
1707 struct sctp_association *new_asoc)
1709 sctp_init_chunk_t *peer_init;
1710 struct sctp_ulpevent *ev;
1711 struct sctp_chunk *repl;
1712 struct sctp_chunk *err;
1713 sctp_disposition_t disposition;
1715 /* new_asoc is a brand-new association, so these are not yet
1716 * side effects--it is safe to run them here.
1718 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1720 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1724 /* Make sure no new addresses are being added during the
1725 * restart. Though this is a pretty complicated attack
1726 * since you'd have to get inside the cookie.
1728 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1729 return SCTP_DISPOSITION_CONSUME;
1732 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1733 * the peer has restarted (Action A), it MUST NOT setup a new
1734 * association but instead resend the SHUTDOWN ACK and send an ERROR
1735 * chunk with a "Cookie Received while Shutting Down" error cause to
1738 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1739 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1740 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1742 if (SCTP_DISPOSITION_NOMEM == disposition)
1745 err = sctp_make_op_error(asoc, chunk,
1746 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1749 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1752 return SCTP_DISPOSITION_CONSUME;
1755 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1756 * data. Consider the optional choice of resending of this data.
1758 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1759 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1760 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1761 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1763 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1764 * and ASCONF-ACK cache.
1766 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1767 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1768 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1770 repl = sctp_make_cookie_ack(new_asoc, chunk);
1774 /* Report association restart to upper layer. */
1775 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1776 new_asoc->c.sinit_num_ostreams,
1777 new_asoc->c.sinit_max_instreams,
1782 /* Update the content of current association. */
1783 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1784 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1785 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1786 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1787 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1788 return SCTP_DISPOSITION_CONSUME;
1791 sctp_chunk_free(repl);
1793 return SCTP_DISPOSITION_NOMEM;
1796 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1799 * B) In this case, both sides may be attempting to start an association
1800 * at about the same time but the peer endpoint started its INIT
1801 * after responding to the local endpoint's INIT
1803 /* This case represents an initialization collision. */
1804 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1805 const struct sctp_endpoint *ep,
1806 const struct sctp_association *asoc,
1807 struct sctp_chunk *chunk,
1808 sctp_cmd_seq_t *commands,
1809 struct sctp_association *new_asoc)
1811 sctp_init_chunk_t *peer_init;
1812 struct sctp_chunk *repl;
1814 /* new_asoc is a brand-new association, so these are not yet
1815 * side effects--it is safe to run them here.
1817 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1818 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1822 /* Update the content of current association. */
1823 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1824 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1825 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1826 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1827 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1829 repl = sctp_make_cookie_ack(new_asoc, chunk);
1833 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1835 /* RFC 2960 5.1 Normal Establishment of an Association
1837 * D) IMPLEMENTATION NOTE: An implementation may choose to
1838 * send the Communication Up notification to the SCTP user
1839 * upon reception of a valid COOKIE ECHO chunk.
1841 * Sadly, this needs to be implemented as a side-effect, because
1842 * we are not guaranteed to have set the association id of the real
1843 * association and so these notifications need to be delayed until
1844 * the association id is allocated.
1847 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1849 /* Sockets API Draft Section 5.3.1.6
1850 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1851 * delivers this notification to inform the application that of the
1852 * peers requested adaptation layer.
1854 * This also needs to be done as a side effect for the same reason as
1857 if (asoc->peer.adaptation_ind)
1858 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1860 return SCTP_DISPOSITION_CONSUME;
1863 return SCTP_DISPOSITION_NOMEM;
1866 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1869 * C) In this case, the local endpoint's cookie has arrived late.
1870 * Before it arrived, the local endpoint sent an INIT and received an
1871 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1872 * but a new tag of its own.
1874 /* This case represents an initialization collision. */
1875 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1876 const struct sctp_endpoint *ep,
1877 const struct sctp_association *asoc,
1878 struct sctp_chunk *chunk,
1879 sctp_cmd_seq_t *commands,
1880 struct sctp_association *new_asoc)
1882 /* The cookie should be silently discarded.
1883 * The endpoint SHOULD NOT change states and should leave
1884 * any timers running.
1886 return SCTP_DISPOSITION_DISCARD;
1889 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1893 * D) When both local and remote tags match the endpoint should always
1894 * enter the ESTABLISHED state, if it has not already done so.
1896 /* This case represents an initialization collision. */
1897 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1898 const struct sctp_endpoint *ep,
1899 const struct sctp_association *asoc,
1900 struct sctp_chunk *chunk,
1901 sctp_cmd_seq_t *commands,
1902 struct sctp_association *new_asoc)
1904 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1905 struct sctp_chunk *repl;
1907 /* Clarification from Implementor's Guide:
1908 * D) When both local and remote tags match the endpoint should
1909 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1910 * It should stop any cookie timer that may be running and send
1914 /* Don't accidentally move back into established state. */
1915 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1916 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1917 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1918 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1919 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1920 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1921 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1924 /* RFC 2960 5.1 Normal Establishment of an Association
1926 * D) IMPLEMENTATION NOTE: An implementation may choose
1927 * to send the Communication Up notification to the
1928 * SCTP user upon reception of a valid COOKIE
1931 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1933 asoc->c.sinit_num_ostreams,
1934 asoc->c.sinit_max_instreams,
1939 /* Sockets API Draft Section 5.3.1.6
1940 * When a peer sends a Adaptation Layer Indication parameter,
1941 * SCTP delivers this notification to inform the application
1942 * that of the peers requested adaptation layer.
1944 if (asoc->peer.adaptation_ind) {
1945 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1953 repl = sctp_make_cookie_ack(new_asoc, chunk);
1957 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1960 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1963 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1964 SCTP_ULPEVENT(ai_ev));
1966 return SCTP_DISPOSITION_CONSUME;
1970 sctp_ulpevent_free(ai_ev);
1972 sctp_ulpevent_free(ev);
1973 return SCTP_DISPOSITION_NOMEM;
1977 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1978 * chunk was retransmitted and then delayed in the network.
1980 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1982 * Verification Tag: None. Do cookie validation.
1985 * (endpoint, asoc, chunk)
1988 * (asoc, reply_msg, msg_up, timers, counters)
1990 * The return value is the disposition of the chunk.
1992 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1993 const struct sctp_endpoint *ep,
1994 const struct sctp_association *asoc,
1995 const sctp_subtype_t type,
1997 sctp_cmd_seq_t *commands)
1999 sctp_disposition_t retval;
2000 struct sctp_chunk *chunk = arg;
2001 struct sctp_association *new_asoc;
2004 struct sctp_chunk *err_chk_p;
2006 /* Make sure that the chunk has a valid length from the protocol
2007 * perspective. In this case check to make sure we have at least
2008 * enough for the chunk header. Cookie length verification is
2011 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2012 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2015 /* "Decode" the chunk. We have no optional parameters so we
2016 * are in good shape.
2018 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2019 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2020 sizeof(sctp_chunkhdr_t)))
2023 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2024 * of a duplicate COOKIE ECHO match the Verification Tags of the
2025 * current association, consider the State Cookie valid even if
2026 * the lifespan is exceeded.
2028 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2032 * If the re-build failed, what is the proper error path
2035 * [We should abort the association. --piggy]
2038 /* FIXME: Several errors are possible. A bad cookie should
2039 * be silently discarded, but think about logging it too.
2042 case -SCTP_IERROR_NOMEM:
2045 case -SCTP_IERROR_STALE_COOKIE:
2046 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2048 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2049 case -SCTP_IERROR_BAD_SIG:
2051 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2055 /* Compare the tie_tag in cookie with the verification tag of
2056 * current association.
2058 action = sctp_tietags_compare(new_asoc, asoc);
2061 case 'A': /* Association restart. */
2062 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2066 case 'B': /* Collision case B. */
2067 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2071 case 'C': /* Collision case C. */
2072 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2076 case 'D': /* Collision case D. */
2077 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2081 default: /* Discard packet for all others. */
2082 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2086 /* Delete the tempory new association. */
2087 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2088 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2090 /* Restore association pointer to provide SCTP command interpeter
2091 * with a valid context in case it needs to manipulate
2093 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2094 SCTP_ASOC((struct sctp_association *)asoc));
2099 return SCTP_DISPOSITION_NOMEM;
2103 * Process an ABORT. (SHUTDOWN-PENDING state)
2105 * See sctp_sf_do_9_1_abort().
2107 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2109 const struct sctp_endpoint *ep,
2110 const struct sctp_association *asoc,
2111 const sctp_subtype_t type,
2113 sctp_cmd_seq_t *commands)
2115 struct sctp_chunk *chunk = arg;
2117 if (!sctp_vtag_verify_either(chunk, asoc))
2118 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2120 /* Make sure that the ABORT chunk has a valid length.
2121 * Since this is an ABORT chunk, we have to discard it
2122 * because of the following text:
2123 * RFC 2960, Section 3.3.7
2124 * If an endpoint receives an ABORT with a format error or for an
2125 * association that doesn't exist, it MUST silently discard it.
2126 * Because the length is "invalid", we can't really discard just
2127 * as we do not know its true length. So, to be safe, discard the
2130 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2131 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2133 /* ADD-IP: Special case for ABORT chunks
2134 * F4) One special consideration is that ABORT Chunks arriving
2135 * destined to the IP address being deleted MUST be
2136 * ignored (see Section 5.3.1 for further details).
2138 if (SCTP_ADDR_DEL ==
2139 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2140 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2142 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2146 * Process an ABORT. (SHUTDOWN-SENT state)
2148 * See sctp_sf_do_9_1_abort().
2150 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2151 const struct sctp_endpoint *ep,
2152 const struct sctp_association *asoc,
2153 const sctp_subtype_t type,
2155 sctp_cmd_seq_t *commands)
2157 struct sctp_chunk *chunk = arg;
2159 if (!sctp_vtag_verify_either(chunk, asoc))
2160 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2162 /* Make sure that the ABORT chunk has a valid length.
2163 * Since this is an ABORT chunk, we have to discard it
2164 * because of the following text:
2165 * RFC 2960, Section 3.3.7
2166 * If an endpoint receives an ABORT with a format error or for an
2167 * association that doesn't exist, it MUST silently discard it.
2168 * Because the length is "invalid", we can't really discard just
2169 * as we do not know its true length. So, to be safe, discard the
2172 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2173 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2175 /* ADD-IP: Special case for ABORT chunks
2176 * F4) One special consideration is that ABORT Chunks arriving
2177 * destined to the IP address being deleted MUST be
2178 * ignored (see Section 5.3.1 for further details).
2180 if (SCTP_ADDR_DEL ==
2181 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2182 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2184 /* Stop the T2-shutdown timer. */
2185 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2186 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2188 /* Stop the T5-shutdown guard timer. */
2189 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2190 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2192 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2196 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2198 * See sctp_sf_do_9_1_abort().
2200 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2202 const struct sctp_endpoint *ep,
2203 const struct sctp_association *asoc,
2204 const sctp_subtype_t type,
2206 sctp_cmd_seq_t *commands)
2208 /* The same T2 timer, so we should be able to use
2209 * common function with the SHUTDOWN-SENT state.
2211 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2215 * Handle an Error received in COOKIE_ECHOED state.
2217 * Only handle the error type of stale COOKIE Error, the other errors will
2221 * (endpoint, asoc, chunk)
2224 * (asoc, reply_msg, msg_up, timers, counters)
2226 * The return value is the disposition of the chunk.
2228 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2229 const struct sctp_endpoint *ep,
2230 const struct sctp_association *asoc,
2231 const sctp_subtype_t type,
2233 sctp_cmd_seq_t *commands)
2235 struct sctp_chunk *chunk = arg;
2238 if (!sctp_vtag_verify(chunk, asoc))
2239 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2241 /* Make sure that the ERROR chunk has a valid length.
2242 * The parameter walking depends on this as well.
2244 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2245 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2248 /* Process the error here */
2249 /* FUTURE FIXME: When PR-SCTP related and other optional
2250 * parms are emitted, this will have to change to handle multiple
2253 sctp_walk_errors(err, chunk->chunk_hdr) {
2254 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2255 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2259 /* It is possible to have malformed error causes, and that
2260 * will cause us to end the walk early. However, since
2261 * we are discarding the packet, there should be no adverse
2264 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2268 * Handle a Stale COOKIE Error
2270 * Section: 5.2.6 Handle Stale COOKIE Error
2271 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2272 * one of the following three alternatives.
2274 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2275 * Preservative parameter requesting an extension to the lifetime of
2276 * the State Cookie. When calculating the time extension, an
2277 * implementation SHOULD use the RTT information measured based on the
2278 * previous COOKIE ECHO / ERROR exchange, and should add no more
2279 * than 1 second beyond the measured RTT, due to long State Cookie
2280 * lifetimes making the endpoint more subject to a replay attack.
2282 * Verification Tag: Not explicit, but safe to ignore.
2285 * (endpoint, asoc, chunk)
2288 * (asoc, reply_msg, msg_up, timers, counters)
2290 * The return value is the disposition of the chunk.
2292 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2293 const struct sctp_endpoint *ep,
2294 const struct sctp_association *asoc,
2295 const sctp_subtype_t type,
2297 sctp_cmd_seq_t *commands)
2299 struct sctp_chunk *chunk = arg;
2301 sctp_cookie_preserve_param_t bht;
2303 struct sctp_chunk *reply;
2304 struct sctp_bind_addr *bp;
2305 int attempts = asoc->init_err_counter + 1;
2307 if (attempts > asoc->max_init_attempts) {
2308 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2309 SCTP_ERROR(ETIMEDOUT));
2310 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2311 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2312 return SCTP_DISPOSITION_DELETE_TCB;
2315 err = (sctp_errhdr_t *)(chunk->skb->data);
2317 /* When calculating the time extension, an implementation
2318 * SHOULD use the RTT information measured based on the
2319 * previous COOKIE ECHO / ERROR exchange, and should add no
2320 * more than 1 second beyond the measured RTT, due to long
2321 * State Cookie lifetimes making the endpoint more subject to
2323 * Measure of Staleness's unit is usec. (1/1000000 sec)
2324 * Suggested Cookie Life-span Increment's unit is msec.
2326 * In general, if you use the suggested cookie life, the value
2327 * found in the field of measure of staleness should be doubled
2328 * to give ample time to retransmit the new cookie and thus
2329 * yield a higher probability of success on the reattempt.
2331 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2332 stale = (stale * 2) / 1000;
2334 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2335 bht.param_hdr.length = htons(sizeof(bht));
2336 bht.lifespan_increment = htonl(stale);
2338 /* Build that new INIT chunk. */
2339 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2340 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2344 sctp_addto_chunk(reply, sizeof(bht), &bht);
2346 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2347 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2349 /* Stop pending T3-rtx and heartbeat timers */
2350 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2351 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2353 /* Delete non-primary peer ip addresses since we are transitioning
2354 * back to the COOKIE-WAIT state
2356 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2358 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2361 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2362 SCTP_TRANSPORT(asoc->peer.primary_path));
2364 /* Cast away the const modifier, as we want to just
2365 * rerun it through as a sideffect.
2367 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2369 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2370 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2371 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2372 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2373 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2374 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2376 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2378 return SCTP_DISPOSITION_CONSUME;
2381 return SCTP_DISPOSITION_NOMEM;
2388 * After checking the Verification Tag, the receiving endpoint shall
2389 * remove the association from its record, and shall report the
2390 * termination to its upper layer.
2392 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2393 * B) Rules for packet carrying ABORT:
2395 * - The endpoint shall always fill in the Verification Tag field of the
2396 * outbound packet with the destination endpoint's tag value if it
2399 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2400 * MUST follow the procedure described in Section 8.4.
2402 * - The receiver MUST accept the packet if the Verification Tag
2403 * matches either its own tag, OR the tag of its peer. Otherwise, the
2404 * receiver MUST silently discard the packet and take no further
2408 * (endpoint, asoc, chunk)
2411 * (asoc, reply_msg, msg_up, timers, counters)
2413 * The return value is the disposition of the chunk.
2415 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2416 const struct sctp_endpoint *ep,
2417 const struct sctp_association *asoc,
2418 const sctp_subtype_t type,
2420 sctp_cmd_seq_t *commands)
2422 struct sctp_chunk *chunk = arg;
2424 if (!sctp_vtag_verify_either(chunk, asoc))
2425 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2427 /* Make sure that the ABORT chunk has a valid length.
2428 * Since this is an ABORT chunk, we have to discard it
2429 * because of the following text:
2430 * RFC 2960, Section 3.3.7
2431 * If an endpoint receives an ABORT with a format error or for an
2432 * association that doesn't exist, it MUST silently discard it.
2433 * Because the length is "invalid", we can't really discard just
2434 * as we do not know its true length. So, to be safe, discard the
2437 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2438 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2440 /* ADD-IP: Special case for ABORT chunks
2441 * F4) One special consideration is that ABORT Chunks arriving
2442 * destined to the IP address being deleted MUST be
2443 * ignored (see Section 5.3.1 for further details).
2445 if (SCTP_ADDR_DEL ==
2446 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2447 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2449 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2452 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2453 const struct sctp_endpoint *ep,
2454 const struct sctp_association *asoc,
2455 const sctp_subtype_t type,
2457 sctp_cmd_seq_t *commands)
2459 struct sctp_chunk *chunk = arg;
2461 __be16 error = SCTP_ERROR_NO_ERROR;
2463 /* See if we have an error cause code in the chunk. */
2464 len = ntohs(chunk->chunk_hdr->length);
2465 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2468 sctp_walk_errors(err, chunk->chunk_hdr);
2469 if ((void *)err != (void *)chunk->chunk_end)
2470 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2472 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2475 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2476 /* ASSOC_FAILED will DELETE_TCB. */
2477 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2478 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2479 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2481 return SCTP_DISPOSITION_ABORT;
2485 * Process an ABORT. (COOKIE-WAIT state)
2487 * See sctp_sf_do_9_1_abort() above.
2489 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2490 const struct sctp_endpoint *ep,
2491 const struct sctp_association *asoc,
2492 const sctp_subtype_t type,
2494 sctp_cmd_seq_t *commands)
2496 struct sctp_chunk *chunk = arg;
2498 __be16 error = SCTP_ERROR_NO_ERROR;
2500 if (!sctp_vtag_verify_either(chunk, asoc))
2501 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2503 /* Make sure that the ABORT chunk has a valid length.
2504 * Since this is an ABORT chunk, we have to discard it
2505 * because of the following text:
2506 * RFC 2960, Section 3.3.7
2507 * If an endpoint receives an ABORT with a format error or for an
2508 * association that doesn't exist, it MUST silently discard it.
2509 * Because the length is "invalid", we can't really discard just
2510 * as we do not know its true length. So, to be safe, discard the
2513 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2514 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2516 /* See if we have an error cause code in the chunk. */
2517 len = ntohs(chunk->chunk_hdr->length);
2518 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2519 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2521 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2526 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2528 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2529 const struct sctp_endpoint *ep,
2530 const struct sctp_association *asoc,
2531 const sctp_subtype_t type,
2533 sctp_cmd_seq_t *commands)
2535 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2537 (struct sctp_transport *)arg);
2541 * Process an ABORT. (COOKIE-ECHOED state)
2543 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2544 const struct sctp_endpoint *ep,
2545 const struct sctp_association *asoc,
2546 const sctp_subtype_t type,
2548 sctp_cmd_seq_t *commands)
2550 /* There is a single T1 timer, so we should be able to use
2551 * common function with the COOKIE-WAIT state.
2553 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2557 * Stop T1 timer and abort association with "INIT failed".
2559 * This is common code called by several sctp_sf_*_abort() functions above.
2561 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2562 sctp_cmd_seq_t *commands,
2563 __be16 error, int sk_err,
2564 const struct sctp_association *asoc,
2565 struct sctp_transport *transport)
2567 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2568 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2569 SCTP_STATE(SCTP_STATE_CLOSED));
2570 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2571 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2572 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2573 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2574 /* CMD_INIT_FAILED will DELETE_TCB. */
2575 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2577 return SCTP_DISPOSITION_ABORT;
2581 * sctp_sf_do_9_2_shut
2584 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2585 * - enter the SHUTDOWN-RECEIVED state,
2587 * - stop accepting new data from its SCTP user
2589 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2590 * that all its outstanding DATA chunks have been received by the
2593 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2594 * send a SHUTDOWN in response to a ULP request. And should discard
2595 * subsequent SHUTDOWN chunks.
2597 * If there are still outstanding DATA chunks left, the SHUTDOWN
2598 * receiver shall continue to follow normal data transmission
2599 * procedures defined in Section 6 until all outstanding DATA chunks
2600 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2601 * new data from its SCTP user.
2603 * Verification Tag: 8.5 Verification Tag [Normal verification]
2606 * (endpoint, asoc, chunk)
2609 * (asoc, reply_msg, msg_up, timers, counters)
2611 * The return value is the disposition of the chunk.
2613 sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2614 const struct sctp_endpoint *ep,
2615 const struct sctp_association *asoc,
2616 const sctp_subtype_t type,
2618 sctp_cmd_seq_t *commands)
2620 struct sctp_chunk *chunk = arg;
2621 sctp_shutdownhdr_t *sdh;
2622 sctp_disposition_t disposition;
2623 struct sctp_ulpevent *ev;
2626 if (!sctp_vtag_verify(chunk, asoc))
2627 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2629 /* Make sure that the SHUTDOWN chunk has a valid length. */
2630 if (!sctp_chunk_length_valid(chunk,
2631 sizeof(struct sctp_shutdown_chunk_t)))
2632 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2635 /* Convert the elaborate header. */
2636 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2637 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2638 chunk->subh.shutdown_hdr = sdh;
2639 ctsn = ntohl(sdh->cum_tsn_ack);
2641 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2642 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2643 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2644 return SCTP_DISPOSITION_DISCARD;
2647 /* If Cumulative TSN Ack beyond the max tsn currently
2648 * send, terminating the association and respond to the
2649 * sender with an ABORT.
2651 if (!TSN_lt(ctsn, asoc->next_tsn))
2652 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2654 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2655 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2656 * inform the application that it should cease sending data.
2658 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2660 disposition = SCTP_DISPOSITION_NOMEM;
2663 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2665 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2666 * - enter the SHUTDOWN-RECEIVED state,
2667 * - stop accepting new data from its SCTP user
2669 * [This is implicit in the new state.]
2671 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2672 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2673 disposition = SCTP_DISPOSITION_CONSUME;
2675 if (sctp_outq_is_empty(&asoc->outqueue)) {
2676 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2680 if (SCTP_DISPOSITION_NOMEM == disposition)
2683 /* - verify, by checking the Cumulative TSN Ack field of the
2684 * chunk, that all its outstanding DATA chunks have been
2685 * received by the SHUTDOWN sender.
2687 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2688 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2695 * sctp_sf_do_9_2_shut_ctsn
2697 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2698 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2699 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2700 * MUST be processed.
2702 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2703 const struct sctp_endpoint *ep,
2704 const struct sctp_association *asoc,
2705 const sctp_subtype_t type,
2707 sctp_cmd_seq_t *commands)
2709 struct sctp_chunk *chunk = arg;
2710 sctp_shutdownhdr_t *sdh;
2713 if (!sctp_vtag_verify(chunk, asoc))
2714 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2716 /* Make sure that the SHUTDOWN chunk has a valid length. */
2717 if (!sctp_chunk_length_valid(chunk,
2718 sizeof(struct sctp_shutdown_chunk_t)))
2719 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2722 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2723 ctsn = ntohl(sdh->cum_tsn_ack);
2725 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2726 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2727 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2728 return SCTP_DISPOSITION_DISCARD;
2731 /* If Cumulative TSN Ack beyond the max tsn currently
2732 * send, terminating the association and respond to the
2733 * sender with an ABORT.
2735 if (!TSN_lt(ctsn, asoc->next_tsn))
2736 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2738 /* verify, by checking the Cumulative TSN Ack field of the
2739 * chunk, that all its outstanding DATA chunks have been
2740 * received by the SHUTDOWN sender.
2742 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2743 SCTP_BE32(sdh->cum_tsn_ack));
2745 return SCTP_DISPOSITION_CONSUME;
2749 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2750 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2751 * transport addresses (either in the IP addresses or in the INIT chunk)
2752 * that belong to this association, it should discard the INIT chunk and
2753 * retransmit the SHUTDOWN ACK chunk.
2755 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2756 const struct sctp_endpoint *ep,
2757 const struct sctp_association *asoc,
2758 const sctp_subtype_t type,
2760 sctp_cmd_seq_t *commands)
2762 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2763 struct sctp_chunk *reply;
2765 /* Make sure that the chunk has a valid length */
2766 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2767 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2770 /* Since we are not going to really process this INIT, there
2771 * is no point in verifying chunk boundries. Just generate
2774 reply = sctp_make_shutdown_ack(asoc, chunk);
2778 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2779 * the T2-SHUTDOWN timer.
2781 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2783 /* and restart the T2-shutdown timer. */
2784 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2785 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2787 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2789 return SCTP_DISPOSITION_CONSUME;
2791 return SCTP_DISPOSITION_NOMEM;
2795 * sctp_sf_do_ecn_cwr
2797 * Section: Appendix A: Explicit Congestion Notification
2801 * RFC 2481 details a specific bit for a sender to send in the header of
2802 * its next outbound TCP segment to indicate to its peer that it has
2803 * reduced its congestion window. This is termed the CWR bit. For
2804 * SCTP the same indication is made by including the CWR chunk.
2805 * This chunk contains one data element, i.e. the TSN number that
2806 * was sent in the ECNE chunk. This element represents the lowest
2807 * TSN number in the datagram that was originally marked with the
2810 * Verification Tag: 8.5 Verification Tag [Normal verification]
2812 * (endpoint, asoc, chunk)
2815 * (asoc, reply_msg, msg_up, timers, counters)
2817 * The return value is the disposition of the chunk.
2819 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2820 const struct sctp_endpoint *ep,
2821 const struct sctp_association *asoc,
2822 const sctp_subtype_t type,
2824 sctp_cmd_seq_t *commands)
2827 struct sctp_chunk *chunk = arg;
2830 if (!sctp_vtag_verify(chunk, asoc))
2831 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2833 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2834 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2837 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2838 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2840 lowest_tsn = ntohl(cwr->lowest_tsn);
2842 /* Does this CWR ack the last sent congestion notification? */
2843 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2844 /* Stop sending ECNE. */
2845 sctp_add_cmd_sf(commands,
2847 SCTP_U32(lowest_tsn));
2849 return SCTP_DISPOSITION_CONSUME;
2855 * Section: Appendix A: Explicit Congestion Notification
2859 * RFC 2481 details a specific bit for a receiver to send back in its
2860 * TCP acknowledgements to notify the sender of the Congestion
2861 * Experienced (CE) bit having arrived from the network. For SCTP this
2862 * same indication is made by including the ECNE chunk. This chunk
2863 * contains one data element, i.e. the lowest TSN associated with the IP
2864 * datagram marked with the CE bit.....
2866 * Verification Tag: 8.5 Verification Tag [Normal verification]
2868 * (endpoint, asoc, chunk)
2871 * (asoc, reply_msg, msg_up, timers, counters)
2873 * The return value is the disposition of the chunk.
2875 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2876 const struct sctp_endpoint *ep,
2877 const struct sctp_association *asoc,
2878 const sctp_subtype_t type,
2880 sctp_cmd_seq_t *commands)
2882 sctp_ecnehdr_t *ecne;
2883 struct sctp_chunk *chunk = arg;
2885 if (!sctp_vtag_verify(chunk, asoc))
2886 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2888 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2889 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2892 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2893 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2895 /* If this is a newer ECNE than the last CWR packet we sent out */
2896 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2897 SCTP_U32(ntohl(ecne->lowest_tsn)));
2899 return SCTP_DISPOSITION_CONSUME;
2903 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2905 * The SCTP endpoint MUST always acknowledge the reception of each valid
2908 * The guidelines on delayed acknowledgement algorithm specified in
2909 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2910 * acknowledgement SHOULD be generated for at least every second packet
2911 * (not every second DATA chunk) received, and SHOULD be generated within
2912 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2913 * situations it may be beneficial for an SCTP transmitter to be more
2914 * conservative than the algorithms detailed in this document allow.
2915 * However, an SCTP transmitter MUST NOT be more aggressive than the
2916 * following algorithms allow.
2918 * A SCTP receiver MUST NOT generate more than one SACK for every
2919 * incoming packet, other than to update the offered window as the
2920 * receiving application consumes new data.
2922 * Verification Tag: 8.5 Verification Tag [Normal verification]
2925 * (endpoint, asoc, chunk)
2928 * (asoc, reply_msg, msg_up, timers, counters)
2930 * The return value is the disposition of the chunk.
2932 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2933 const struct sctp_endpoint *ep,
2934 const struct sctp_association *asoc,
2935 const sctp_subtype_t type,
2937 sctp_cmd_seq_t *commands)
2939 struct sctp_chunk *chunk = arg;
2940 sctp_arg_t force = SCTP_NOFORCE();
2943 if (!sctp_vtag_verify(chunk, asoc)) {
2944 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2946 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2949 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2950 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2953 error = sctp_eat_data(asoc, chunk, commands );
2955 case SCTP_IERROR_NO_ERROR:
2957 case SCTP_IERROR_HIGH_TSN:
2958 case SCTP_IERROR_BAD_STREAM:
2959 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2960 goto discard_noforce;
2961 case SCTP_IERROR_DUP_TSN:
2962 case SCTP_IERROR_IGNORE_TSN:
2963 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2965 case SCTP_IERROR_NO_DATA:
2967 case SCTP_IERROR_PROTO_VIOLATION:
2968 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
2969 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2974 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2975 force = SCTP_FORCE();
2977 if (asoc->autoclose) {
2978 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2979 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2982 /* If this is the last chunk in a packet, we need to count it
2983 * toward sack generation. Note that we need to SACK every
2984 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2985 * THEM. We elect to NOT generate SACK's if the chunk fails
2986 * the verification tag test.
2988 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2990 * The SCTP endpoint MUST always acknowledge the reception of
2991 * each valid DATA chunk.
2993 * The guidelines on delayed acknowledgement algorithm
2994 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2995 * Specifically, an acknowledgement SHOULD be generated for at
2996 * least every second packet (not every second DATA chunk)
2997 * received, and SHOULD be generated within 200 ms of the
2998 * arrival of any unacknowledged DATA chunk. In some
2999 * situations it may be beneficial for an SCTP transmitter to
3000 * be more conservative than the algorithms detailed in this
3001 * document allow. However, an SCTP transmitter MUST NOT be
3002 * more aggressive than the following algorithms allow.
3004 if (chunk->end_of_packet)
3005 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3007 return SCTP_DISPOSITION_CONSUME;
3010 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3012 * When a packet arrives with duplicate DATA chunk(s) and with
3013 * no new DATA chunk(s), the endpoint MUST immediately send a
3014 * SACK with no delay. If a packet arrives with duplicate
3015 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3016 * MAY immediately send a SACK. Normally receipt of duplicate
3017 * DATA chunks will occur when the original SACK chunk was lost
3018 * and the peer's RTO has expired. The duplicate TSN number(s)
3019 * SHOULD be reported in the SACK as duplicate.
3021 /* In our case, we split the MAY SACK advice up whether or not
3022 * the last chunk is a duplicate.'
3024 if (chunk->end_of_packet)
3025 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3026 return SCTP_DISPOSITION_DISCARD;
3029 if (chunk->end_of_packet)
3030 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3032 return SCTP_DISPOSITION_DISCARD;
3034 return SCTP_DISPOSITION_CONSUME;
3039 * sctp_sf_eat_data_fast_4_4
3042 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3043 * DATA chunks without delay.
3045 * Verification Tag: 8.5 Verification Tag [Normal verification]
3047 * (endpoint, asoc, chunk)
3050 * (asoc, reply_msg, msg_up, timers, counters)
3052 * The return value is the disposition of the chunk.
3054 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3055 const struct sctp_endpoint *ep,
3056 const struct sctp_association *asoc,
3057 const sctp_subtype_t type,
3059 sctp_cmd_seq_t *commands)
3061 struct sctp_chunk *chunk = arg;
3064 if (!sctp_vtag_verify(chunk, asoc)) {
3065 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3067 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3070 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3071 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3074 error = sctp_eat_data(asoc, chunk, commands );
3076 case SCTP_IERROR_NO_ERROR:
3077 case SCTP_IERROR_HIGH_TSN:
3078 case SCTP_IERROR_DUP_TSN:
3079 case SCTP_IERROR_IGNORE_TSN:
3080 case SCTP_IERROR_BAD_STREAM:
3082 case SCTP_IERROR_NO_DATA:
3084 case SCTP_IERROR_PROTO_VIOLATION:
3085 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3086 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3091 /* Go a head and force a SACK, since we are shutting down. */
3093 /* Implementor's Guide.
3095 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3096 * respond to each received packet containing one or more DATA chunk(s)
3097 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3099 if (chunk->end_of_packet) {
3100 /* We must delay the chunk creation since the cumulative
3101 * TSN has not been updated yet.
3103 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3104 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3105 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3106 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3110 return SCTP_DISPOSITION_CONSUME;
3114 * Section: 6.2 Processing a Received SACK
3115 * D) Any time a SACK arrives, the endpoint performs the following:
3117 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3118 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3119 * increasing, a SACK whose Cumulative TSN Ack is less than the
3120 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3122 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3123 * of bytes still outstanding after processing the Cumulative TSN Ack
3124 * and the Gap Ack Blocks.
3126 * iii) If the SACK is missing a TSN that was previously
3127 * acknowledged via a Gap Ack Block (e.g., the data receiver
3128 * reneged on the data), then mark the corresponding DATA chunk
3129 * as available for retransmit: Mark it as missing for fast
3130 * retransmit as described in Section 7.2.4 and if no retransmit
3131 * timer is running for the destination address to which the DATA
3132 * chunk was originally transmitted, then T3-rtx is started for
3133 * that destination address.
3135 * Verification Tag: 8.5 Verification Tag [Normal verification]
3138 * (endpoint, asoc, chunk)
3141 * (asoc, reply_msg, msg_up, timers, counters)
3143 * The return value is the disposition of the chunk.
3145 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3146 const struct sctp_endpoint *ep,
3147 const struct sctp_association *asoc,
3148 const sctp_subtype_t type,
3150 sctp_cmd_seq_t *commands)
3152 struct sctp_chunk *chunk = arg;
3153 sctp_sackhdr_t *sackh;
3156 if (!sctp_vtag_verify(chunk, asoc))
3157 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3159 /* Make sure that the SACK chunk has a valid length. */
3160 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3161 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3164 /* Pull the SACK chunk from the data buffer */
3165 sackh = sctp_sm_pull_sack(chunk);
3166 /* Was this a bogus SACK? */
3168 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3169 chunk->subh.sack_hdr = sackh;
3170 ctsn = ntohl(sackh->cum_tsn_ack);
3172 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3173 * Ack Point, then drop the SACK. Since Cumulative TSN
3174 * Ack is monotonically increasing, a SACK whose
3175 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3176 * Point indicates an out-of-order SACK.
3178 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3179 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
3180 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
3181 return SCTP_DISPOSITION_DISCARD;
3184 /* If Cumulative TSN Ack beyond the max tsn currently
3185 * send, terminating the association and respond to the
3186 * sender with an ABORT.
3188 if (!TSN_lt(ctsn, asoc->next_tsn))
3189 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3191 /* Return this SACK for further processing. */
3192 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3194 /* Note: We do the rest of the work on the PROCESS_SACK
3197 return SCTP_DISPOSITION_CONSUME;
3201 * Generate an ABORT in response to a packet.
3203 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3205 * 8) The receiver should respond to the sender of the OOTB packet with
3206 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3207 * MUST fill in the Verification Tag field of the outbound packet
3208 * with the value found in the Verification Tag field of the OOTB
3209 * packet and set the T-bit in the Chunk Flags to indicate that the
3210 * Verification Tag is reflected. After sending this ABORT, the
3211 * receiver of the OOTB packet shall discard the OOTB packet and take
3212 * no further action.
3216 * The return value is the disposition of the chunk.
3218 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3219 const struct sctp_endpoint *ep,
3220 const struct sctp_association *asoc,
3221 const sctp_subtype_t type,
3223 sctp_cmd_seq_t *commands)
3225 struct sctp_packet *packet = NULL;
3226 struct sctp_chunk *chunk = arg;
3227 struct sctp_chunk *abort;
3229 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3232 /* Make an ABORT. The T bit will be set if the asoc
3235 abort = sctp_make_abort(asoc, chunk, 0);
3237 sctp_ootb_pkt_free(packet);
3238 return SCTP_DISPOSITION_NOMEM;
3241 /* Reflect vtag if T-Bit is set */
3242 if (sctp_test_T_bit(abort))
3243 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3245 /* Set the skb to the belonging sock for accounting. */
3246 abort->skb->sk = ep->base.sk;
3248 sctp_packet_append_chunk(packet, abort);
3250 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3251 SCTP_PACKET(packet));
3253 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3255 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3256 return SCTP_DISPOSITION_CONSUME;
3259 return SCTP_DISPOSITION_NOMEM;
3263 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3264 * event as ULP notification for each cause included in the chunk.
3266 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3268 * The return value is the disposition of the chunk.
3270 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3271 const struct sctp_endpoint *ep,
3272 const struct sctp_association *asoc,
3273 const sctp_subtype_t type,
3275 sctp_cmd_seq_t *commands)
3277 struct sctp_chunk *chunk = arg;
3280 if (!sctp_vtag_verify(chunk, asoc))
3281 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3283 /* Make sure that the ERROR chunk has a valid length. */
3284 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3285 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3287 sctp_walk_errors(err, chunk->chunk_hdr);
3288 if ((void *)err != (void *)chunk->chunk_end)
3289 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3290 (void *)err, commands);
3292 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3295 return SCTP_DISPOSITION_CONSUME;
3299 * Process an inbound SHUTDOWN ACK.
3302 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3303 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3304 * peer, and remove all record of the association.
3306 * The return value is the disposition.
3308 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3309 const struct sctp_endpoint *ep,
3310 const struct sctp_association *asoc,
3311 const sctp_subtype_t type,
3313 sctp_cmd_seq_t *commands)
3315 struct sctp_chunk *chunk = arg;
3316 struct sctp_chunk *reply;
3317 struct sctp_ulpevent *ev;
3319 if (!sctp_vtag_verify(chunk, asoc))
3320 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3322 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3323 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3324 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3326 /* 10.2 H) SHUTDOWN COMPLETE notification
3328 * When SCTP completes the shutdown procedures (section 9.2) this
3329 * notification is passed to the upper layer.
3331 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3332 0, 0, 0, NULL, GFP_ATOMIC);
3336 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3337 reply = sctp_make_shutdown_complete(asoc, chunk);
3341 /* Do all the commands now (after allocation), so that we
3342 * have consistent state if memory allocation failes
3344 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3346 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3347 * stop the T2-shutdown timer,
3349 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3350 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3352 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3353 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3355 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3356 SCTP_STATE(SCTP_STATE_CLOSED));
3357 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3358 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3359 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3361 /* ...and remove all record of the association. */
3362 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3363 return SCTP_DISPOSITION_DELETE_TCB;
3366 sctp_ulpevent_free(ev);
3368 return SCTP_DISPOSITION_NOMEM;
3372 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3374 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3375 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3376 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3377 * packet must fill in the Verification Tag field of the outbound
3378 * packet with the Verification Tag received in the SHUTDOWN ACK and
3379 * set the T-bit in the Chunk Flags to indicate that the Verification
3382 * 8) The receiver should respond to the sender of the OOTB packet with
3383 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3384 * MUST fill in the Verification Tag field of the outbound packet
3385 * with the value found in the Verification Tag field of the OOTB
3386 * packet and set the T-bit in the Chunk Flags to indicate that the
3387 * Verification Tag is reflected. After sending this ABORT, the
3388 * receiver of the OOTB packet shall discard the OOTB packet and take
3389 * no further action.
3391 sctp_disposition_t sctp_sf_ootb(struct net *net,
3392 const struct sctp_endpoint *ep,
3393 const struct sctp_association *asoc,
3394 const sctp_subtype_t type,
3396 sctp_cmd_seq_t *commands)
3398 struct sctp_chunk *chunk = arg;
3399 struct sk_buff *skb = chunk->skb;
3400 sctp_chunkhdr_t *ch;
3403 int ootb_shut_ack = 0;
3404 int ootb_cookie_ack = 0;
3406 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3408 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3410 /* Report violation if the chunk is less then minimal */
3411 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3412 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3415 /* Now that we know we at least have a chunk header,
3416 * do things that are type appropriate.
3418 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3421 /* RFC 2960, Section 3.3.7
3422 * Moreover, under any circumstances, an endpoint that
3423 * receives an ABORT MUST NOT respond to that ABORT by
3424 * sending an ABORT of its own.
3426 if (SCTP_CID_ABORT == ch->type)
3427 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3429 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3430 * or a COOKIE ACK the SCTP Packet should be silently
3434 if (SCTP_CID_COOKIE_ACK == ch->type)
3435 ootb_cookie_ack = 1;
3437 if (SCTP_CID_ERROR == ch->type) {
3438 sctp_walk_errors(err, ch) {
3439 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3440 ootb_cookie_ack = 1;
3446 /* Report violation if chunk len overflows */
3447 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3448 if (ch_end > skb_tail_pointer(skb))
3449 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3452 ch = (sctp_chunkhdr_t *) ch_end;
3453 } while (ch_end < skb_tail_pointer(skb));
3456 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3457 else if (ootb_cookie_ack)
3458 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3460 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3464 * Handle an "Out of the blue" SHUTDOWN ACK.
3466 * Section: 8.4 5, sctpimpguide 2.41.
3468 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3469 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3470 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3471 * packet must fill in the Verification Tag field of the outbound
3472 * packet with the Verification Tag received in the SHUTDOWN ACK and
3473 * set the T-bit in the Chunk Flags to indicate that the Verification
3477 * (endpoint, asoc, type, arg, commands)
3480 * (sctp_disposition_t)
3482 * The return value is the disposition of the chunk.
3484 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3485 const struct sctp_endpoint *ep,
3486 const struct sctp_association *asoc,
3487 const sctp_subtype_t type,
3489 sctp_cmd_seq_t *commands)
3491 struct sctp_packet *packet = NULL;
3492 struct sctp_chunk *chunk = arg;
3493 struct sctp_chunk *shut;
3495 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3498 /* Make an SHUTDOWN_COMPLETE.
3499 * The T bit will be set if the asoc is NULL.
3501 shut = sctp_make_shutdown_complete(asoc, chunk);
3503 sctp_ootb_pkt_free(packet);
3504 return SCTP_DISPOSITION_NOMEM;
3507 /* Reflect vtag if T-Bit is set */
3508 if (sctp_test_T_bit(shut))
3509 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3511 /* Set the skb to the belonging sock for accounting. */
3512 shut->skb->sk = ep->base.sk;
3514 sctp_packet_append_chunk(packet, shut);
3516 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3517 SCTP_PACKET(packet));
3519 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3521 /* If the chunk length is invalid, we don't want to process
3522 * the reset of the packet.
3524 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3525 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3527 /* We need to discard the rest of the packet to prevent
3528 * potential bomming attacks from additional bundled chunks.
3529 * This is documented in SCTP Threats ID.
3531 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3534 return SCTP_DISPOSITION_NOMEM;
3538 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3540 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3541 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3542 * procedures in section 8.4 SHOULD be followed, in other words it
3543 * should be treated as an Out Of The Blue packet.
3544 * [This means that we do NOT check the Verification Tag on these
3548 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3549 const struct sctp_endpoint *ep,
3550 const struct sctp_association *asoc,
3551 const sctp_subtype_t type,
3553 sctp_cmd_seq_t *commands)
3555 struct sctp_chunk *chunk = arg;
3557 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3558 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3559 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3562 /* Although we do have an association in this case, it corresponds
3563 * to a restarted association. So the packet is treated as an OOTB
3564 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3565 * called with a NULL association.
3567 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3569 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3572 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3573 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3574 const struct sctp_endpoint *ep,
3575 const struct sctp_association *asoc,
3576 const sctp_subtype_t type, void *arg,
3577 sctp_cmd_seq_t *commands)
3579 struct sctp_chunk *chunk = arg;
3580 struct sctp_chunk *asconf_ack = NULL;
3581 struct sctp_paramhdr *err_param = NULL;
3582 sctp_addiphdr_t *hdr;
3583 union sctp_addr_param *addr_param;
3587 if (!sctp_vtag_verify(chunk, asoc)) {
3588 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3590 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3593 /* ADD-IP: Section 4.1.1
3594 * This chunk MUST be sent in an authenticated way by using
3595 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3596 * is received unauthenticated it MUST be silently discarded as
3597 * described in [I-D.ietf-tsvwg-sctp-auth].
3599 if (!net->sctp.addip_noauth && !chunk->auth)
3600 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3602 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3603 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3604 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3607 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3608 serial = ntohl(hdr->serial);
3610 addr_param = (union sctp_addr_param *)hdr->params;
3611 length = ntohs(addr_param->p.length);
3612 if (length < sizeof(sctp_paramhdr_t))
3613 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3614 (void *)addr_param, commands);
3616 /* Verify the ASCONF chunk before processing it. */
3617 if (!sctp_verify_asconf(asoc,
3618 (sctp_paramhdr_t *)((void *)addr_param + length),
3619 (void *)chunk->chunk_end,
3621 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3622 (void *)err_param, commands);
3624 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3625 * the endpoint stored in a new association variable
3626 * 'Peer-Serial-Number'.
3628 if (serial == asoc->peer.addip_serial + 1) {
3629 /* If this is the first instance of ASCONF in the packet,
3630 * we can clean our old ASCONF-ACKs.
3632 if (!chunk->has_asconf)
3633 sctp_assoc_clean_asconf_ack_cache(asoc);
3635 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3636 * expected, process the ASCONF as described below and after
3637 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3638 * the response packet and cache a copy of it (in the event it
3639 * later needs to be retransmitted).
3641 * Essentially, do V1-V5.
3643 asconf_ack = sctp_process_asconf((struct sctp_association *)
3646 return SCTP_DISPOSITION_NOMEM;
3647 } else if (serial < asoc->peer.addip_serial + 1) {
3649 * If the value found in the Sequence Number is less than the
3650 * ('Peer- Sequence-Number' + 1), simply skip to the next
3651 * ASCONF, and include in the outbound response packet
3652 * any previously cached ASCONF-ACK response that was
3653 * sent and saved that matches the Sequence Number of the
3654 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3655 * Chunk exists. This will occur when an older ASCONF
3656 * arrives out of order. In such a case, the receiver
3657 * should skip the ASCONF Chunk and not include ASCONF-ACK
3658 * Chunk for that chunk.
3660 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3662 return SCTP_DISPOSITION_DISCARD;
3664 /* Reset the transport so that we select the correct one
3665 * this time around. This is to make sure that we don't
3666 * accidentally use a stale transport that's been removed.
3668 asconf_ack->transport = NULL;
3670 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3671 * it must be either a stale packet or from an attacker.
3673 return SCTP_DISPOSITION_DISCARD;
3676 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3677 * containing the ASCONF-ACK Chunks MUST be the source address of
3678 * the SCTP packet that held the ASCONF Chunks.
3680 * To do this properly, we'll set the destination address of the chunk
3681 * and at the transmit time, will try look up the transport to use.
3682 * Since ASCONFs may be bundled, the correct transport may not be
3683 * created until we process the entire packet, thus this workaround.
3685 asconf_ack->dest = chunk->source;
3686 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3687 if (asoc->new_transport) {
3688 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport,
3690 ((struct sctp_association *)asoc)->new_transport = NULL;
3693 return SCTP_DISPOSITION_CONSUME;
3697 * ADDIP Section 4.3 General rules for address manipulation
3698 * When building TLV parameters for the ASCONF Chunk that will add or
3699 * delete IP addresses the D0 to D13 rules should be applied:
3701 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3702 const struct sctp_endpoint *ep,
3703 const struct sctp_association *asoc,
3704 const sctp_subtype_t type, void *arg,
3705 sctp_cmd_seq_t *commands)
3707 struct sctp_chunk *asconf_ack = arg;
3708 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3709 struct sctp_chunk *abort;
3710 struct sctp_paramhdr *err_param = NULL;
3711 sctp_addiphdr_t *addip_hdr;
3712 __u32 sent_serial, rcvd_serial;
3714 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3715 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3717 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3720 /* ADD-IP, Section 4.1.2:
3721 * This chunk MUST be sent in an authenticated way by using
3722 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3723 * is received unauthenticated it MUST be silently discarded as
3724 * described in [I-D.ietf-tsvwg-sctp-auth].
3726 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3727 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3729 /* Make sure that the ADDIP chunk has a valid length. */
3730 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3731 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3734 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3735 rcvd_serial = ntohl(addip_hdr->serial);
3737 /* Verify the ASCONF-ACK chunk before processing it. */
3738 if (!sctp_verify_asconf(asoc,
3739 (sctp_paramhdr_t *)addip_hdr->params,
3740 (void *)asconf_ack->chunk_end,
3742 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3743 (void *)err_param, commands);
3746 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3747 sent_serial = ntohl(addip_hdr->serial);
3749 sent_serial = asoc->addip_serial - 1;
3752 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3753 * equal to the next serial number to be used but no ASCONF chunk is
3754 * outstanding the endpoint MUST ABORT the association. Note that a
3755 * sequence number is greater than if it is no more than 2^^31-1
3756 * larger than the current sequence number (using serial arithmetic).
3758 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3759 !(asoc->addip_last_asconf)) {
3760 abort = sctp_make_abort(asoc, asconf_ack,
3761 sizeof(sctp_errhdr_t));
3763 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3764 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3767 /* We are going to ABORT, so we might as well stop
3768 * processing the rest of the chunks in the packet.
3770 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3771 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3772 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3773 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3774 SCTP_ERROR(ECONNABORTED));
3775 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3776 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3777 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3778 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3779 return SCTP_DISPOSITION_ABORT;
3782 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3783 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3784 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3786 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3788 /* Successfully processed ASCONF_ACK. We can
3789 * release the next asconf if we have one.
3791 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3793 return SCTP_DISPOSITION_CONSUME;
3796 abort = sctp_make_abort(asoc, asconf_ack,
3797 sizeof(sctp_errhdr_t));
3799 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3800 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3803 /* We are going to ABORT, so we might as well stop
3804 * processing the rest of the chunks in the packet.
3806 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3807 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3808 SCTP_ERROR(ECONNABORTED));
3809 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3810 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3811 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3812 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3813 return SCTP_DISPOSITION_ABORT;
3816 return SCTP_DISPOSITION_DISCARD;
3820 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3822 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3823 * its cumulative TSN point to the value carried in the FORWARD TSN
3824 * chunk, and then MUST further advance its cumulative TSN point locally
3826 * After the above processing, the data receiver MUST stop reporting any
3827 * missing TSNs earlier than or equal to the new cumulative TSN point.
3829 * Verification Tag: 8.5 Verification Tag [Normal verification]
3831 * The return value is the disposition of the chunk.
3833 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3834 const struct sctp_endpoint *ep,
3835 const struct sctp_association *asoc,
3836 const sctp_subtype_t type,
3838 sctp_cmd_seq_t *commands)
3840 struct sctp_chunk *chunk = arg;
3841 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3842 struct sctp_fwdtsn_skip *skip;
3846 if (!sctp_vtag_verify(chunk, asoc)) {
3847 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3849 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3852 /* Make sure that the FORWARD_TSN chunk has valid length. */
3853 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3854 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3857 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3858 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3859 len = ntohs(chunk->chunk_hdr->length);
3860 len -= sizeof(struct sctp_chunkhdr);
3861 skb_pull(chunk->skb, len);
3863 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3864 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3866 /* The TSN is too high--silently discard the chunk and count on it
3867 * getting retransmitted later.
3869 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3870 goto discard_noforce;
3872 /* Silently discard the chunk if stream-id is not valid */
3873 sctp_walk_fwdtsn(skip, chunk) {
3874 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3875 goto discard_noforce;
3878 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3879 if (len > sizeof(struct sctp_fwdtsn_hdr))
3880 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3883 /* Count this as receiving DATA. */
3884 if (asoc->autoclose) {
3885 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3886 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3889 /* FIXME: For now send a SACK, but DATA processing may
3892 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3894 return SCTP_DISPOSITION_CONSUME;
3897 return SCTP_DISPOSITION_DISCARD;
3900 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3902 const struct sctp_endpoint *ep,
3903 const struct sctp_association *asoc,
3904 const sctp_subtype_t type,
3906 sctp_cmd_seq_t *commands)
3908 struct sctp_chunk *chunk = arg;
3909 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3910 struct sctp_fwdtsn_skip *skip;
3914 if (!sctp_vtag_verify(chunk, asoc)) {
3915 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3917 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3920 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3921 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3922 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3925 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3926 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3927 len = ntohs(chunk->chunk_hdr->length);
3928 len -= sizeof(struct sctp_chunkhdr);
3929 skb_pull(chunk->skb, len);
3931 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3932 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __func__, tsn);
3934 /* The TSN is too high--silently discard the chunk and count on it
3935 * getting retransmitted later.
3937 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3940 /* Silently discard the chunk if stream-id is not valid */
3941 sctp_walk_fwdtsn(skip, chunk) {
3942 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3946 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3947 if (len > sizeof(struct sctp_fwdtsn_hdr))
3948 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3951 /* Go a head and force a SACK, since we are shutting down. */
3953 /* Implementor's Guide.
3955 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3956 * respond to each received packet containing one or more DATA chunk(s)
3957 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3959 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3960 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3961 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3962 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3964 return SCTP_DISPOSITION_CONSUME;
3968 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3970 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3971 * Identifier field. If this algorithm was not specified by the
3972 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3973 * during association setup, the AUTH chunk and all chunks after it MUST
3974 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3975 * defined in Section 4.1.
3977 * If an endpoint with no shared key receives a Shared Key Identifier
3978 * other than 0, it MUST silently discard all authenticated chunks. If
3979 * the endpoint has at least one endpoint pair shared key for the peer,
3980 * it MUST use the key specified by the Shared Key Identifier if a
3981 * key has been configured for that Shared Key Identifier. If no
3982 * endpoint pair shared key has been configured for that Shared Key
3983 * Identifier, all authenticated chunks MUST be silently discarded.
3985 * Verification Tag: 8.5 Verification Tag [Normal verification]
3987 * The return value is the disposition of the chunk.
3989 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
3990 const struct sctp_endpoint *ep,
3991 const struct sctp_association *asoc,
3992 const sctp_subtype_t type,
3993 struct sctp_chunk *chunk)
3995 struct sctp_authhdr *auth_hdr;
3996 struct sctp_hmac *hmac;
3997 unsigned int sig_len;
4002 /* Pull in the auth header, so we can do some more verification */
4003 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4004 chunk->subh.auth_hdr = auth_hdr;
4005 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4007 /* Make sure that we support the HMAC algorithm from the auth
4010 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4011 return SCTP_IERROR_AUTH_BAD_HMAC;
4013 /* Make sure that the provided shared key identifier has been
4016 key_id = ntohs(auth_hdr->shkey_id);
4017 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4018 return SCTP_IERROR_AUTH_BAD_KEYID;
4021 /* Make sure that the length of the signature matches what
4024 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4025 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4026 if (sig_len != hmac->hmac_len)
4027 return SCTP_IERROR_PROTO_VIOLATION;
4029 /* Now that we've done validation checks, we can compute and
4030 * verify the hmac. The steps involved are:
4031 * 1. Save the digest from the chunk.
4032 * 2. Zero out the digest in the chunk.
4033 * 3. Compute the new digest
4034 * 4. Compare saved and new digests.
4036 digest = auth_hdr->hmac;
4037 skb_pull(chunk->skb, sig_len);
4039 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4043 memset(digest, 0, sig_len);
4045 sctp_auth_calculate_hmac(asoc, chunk->skb,
4046 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4049 /* Discard the packet if the digests do not match */
4050 if (memcmp(save_digest, digest, sig_len)) {
4052 return SCTP_IERROR_BAD_SIG;
4058 return SCTP_IERROR_NO_ERROR;
4060 return SCTP_IERROR_NOMEM;
4063 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4064 const struct sctp_endpoint *ep,
4065 const struct sctp_association *asoc,
4066 const sctp_subtype_t type,
4068 sctp_cmd_seq_t *commands)
4070 struct sctp_authhdr *auth_hdr;
4071 struct sctp_chunk *chunk = arg;
4072 struct sctp_chunk *err_chunk;
4073 sctp_ierror_t error;
4075 /* Make sure that the peer has AUTH capable */
4076 if (!asoc->peer.auth_capable)
4077 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4079 if (!sctp_vtag_verify(chunk, asoc)) {
4080 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4082 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4085 /* Make sure that the AUTH chunk has valid length. */
4086 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4087 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4090 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4091 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
4093 case SCTP_IERROR_AUTH_BAD_HMAC:
4094 /* Generate the ERROR chunk and discard the rest
4097 err_chunk = sctp_make_op_error(asoc, chunk,
4098 SCTP_ERROR_UNSUP_HMAC,
4102 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4103 SCTP_CHUNK(err_chunk));
4106 case SCTP_IERROR_AUTH_BAD_KEYID:
4107 case SCTP_IERROR_BAD_SIG:
4108 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4110 case SCTP_IERROR_PROTO_VIOLATION:
4111 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4114 case SCTP_IERROR_NOMEM:
4115 return SCTP_DISPOSITION_NOMEM;
4117 default: /* Prevent gcc warnings */
4121 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4122 struct sctp_ulpevent *ev;
4124 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4125 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4130 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4134 return SCTP_DISPOSITION_CONSUME;
4138 * Process an unknown chunk.
4140 * Section: 3.2. Also, 2.1 in the implementor's guide.
4142 * Chunk Types are encoded such that the highest-order two bits specify
4143 * the action that must be taken if the processing endpoint does not
4144 * recognize the Chunk Type.
4146 * 00 - Stop processing this SCTP packet and discard it, do not process
4147 * any further chunks within it.
4149 * 01 - Stop processing this SCTP packet and discard it, do not process
4150 * any further chunks within it, and report the unrecognized
4151 * chunk in an 'Unrecognized Chunk Type'.
4153 * 10 - Skip this chunk and continue processing.
4155 * 11 - Skip this chunk and continue processing, but report in an ERROR
4156 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4158 * The return value is the disposition of the chunk.
4160 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4161 const struct sctp_endpoint *ep,
4162 const struct sctp_association *asoc,
4163 const sctp_subtype_t type,
4165 sctp_cmd_seq_t *commands)
4167 struct sctp_chunk *unk_chunk = arg;
4168 struct sctp_chunk *err_chunk;
4169 sctp_chunkhdr_t *hdr;
4171 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
4173 if (!sctp_vtag_verify(unk_chunk, asoc))
4174 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4176 /* Make sure that the chunk has a valid length.
4177 * Since we don't know the chunk type, we use a general
4178 * chunkhdr structure to make a comparison.
4180 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4181 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4184 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4185 case SCTP_CID_ACTION_DISCARD:
4186 /* Discard the packet. */
4187 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4189 case SCTP_CID_ACTION_DISCARD_ERR:
4190 /* Generate an ERROR chunk as response. */
4191 hdr = unk_chunk->chunk_hdr;
4192 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4193 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4194 WORD_ROUND(ntohs(hdr->length)),
4197 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4198 SCTP_CHUNK(err_chunk));
4201 /* Discard the packet. */
4202 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4203 return SCTP_DISPOSITION_CONSUME;
4205 case SCTP_CID_ACTION_SKIP:
4206 /* Skip the chunk. */
4207 return SCTP_DISPOSITION_DISCARD;
4209 case SCTP_CID_ACTION_SKIP_ERR:
4210 /* Generate an ERROR chunk as response. */
4211 hdr = unk_chunk->chunk_hdr;
4212 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4213 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4214 WORD_ROUND(ntohs(hdr->length)),
4217 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4218 SCTP_CHUNK(err_chunk));
4220 /* Skip the chunk. */
4221 return SCTP_DISPOSITION_CONSUME;
4227 return SCTP_DISPOSITION_DISCARD;
4231 * Discard the chunk.
4233 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4234 * [Too numerous to mention...]
4235 * Verification Tag: No verification needed.
4237 * (endpoint, asoc, chunk)
4240 * (asoc, reply_msg, msg_up, timers, counters)
4242 * The return value is the disposition of the chunk.
4244 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4245 const struct sctp_endpoint *ep,
4246 const struct sctp_association *asoc,
4247 const sctp_subtype_t type,
4249 sctp_cmd_seq_t *commands)
4251 struct sctp_chunk *chunk = arg;
4253 /* Make sure that the chunk has a valid length.
4254 * Since we don't know the chunk type, we use a general
4255 * chunkhdr structure to make a comparison.
4257 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4258 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4261 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
4262 return SCTP_DISPOSITION_DISCARD;
4266 * Discard the whole packet.
4270 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4271 * silently discard the OOTB packet and take no further action.
4273 * Verification Tag: No verification necessary
4276 * (endpoint, asoc, chunk)
4279 * (asoc, reply_msg, msg_up, timers, counters)
4281 * The return value is the disposition of the chunk.
4283 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4284 const struct sctp_endpoint *ep,
4285 const struct sctp_association *asoc,
4286 const sctp_subtype_t type,
4288 sctp_cmd_seq_t *commands)
4290 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4291 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4293 return SCTP_DISPOSITION_CONSUME;
4298 * The other end is violating protocol.
4300 * Section: Not specified
4301 * Verification Tag: Not specified
4303 * (endpoint, asoc, chunk)
4306 * (asoc, reply_msg, msg_up, timers, counters)
4308 * We simply tag the chunk as a violation. The state machine will log
4309 * the violation and continue.
4311 sctp_disposition_t sctp_sf_violation(struct net *net,
4312 const struct sctp_endpoint *ep,
4313 const struct sctp_association *asoc,
4314 const sctp_subtype_t type,
4316 sctp_cmd_seq_t *commands)
4318 struct sctp_chunk *chunk = arg;
4320 /* Make sure that the chunk has a valid length. */
4321 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4322 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4325 return SCTP_DISPOSITION_VIOLATION;
4329 * Common function to handle a protocol violation.
4331 static sctp_disposition_t sctp_sf_abort_violation(
4333 const struct sctp_endpoint *ep,
4334 const struct sctp_association *asoc,
4336 sctp_cmd_seq_t *commands,
4337 const __u8 *payload,
4338 const size_t paylen)
4340 struct sctp_packet *packet = NULL;
4341 struct sctp_chunk *chunk = arg;
4342 struct sctp_chunk *abort = NULL;
4344 /* SCTP-AUTH, Section 6.3:
4345 * It should be noted that if the receiver wants to tear
4346 * down an association in an authenticated way only, the
4347 * handling of malformed packets should not result in
4348 * tearing down the association.
4350 * This means that if we only want to abort associations
4351 * in an authenticated way (i.e AUTH+ABORT), then we
4352 * can't destroy this association just because the packet
4355 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4358 /* Make the abort chunk. */
4359 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4364 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4365 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4366 !asoc->peer.i.init_tag) {
4367 sctp_initack_chunk_t *initack;
4369 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4370 if (!sctp_chunk_length_valid(chunk,
4371 sizeof(sctp_initack_chunk_t)))
4372 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4374 unsigned int inittag;
4376 inittag = ntohl(initack->init_hdr.init_tag);
4377 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4382 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4383 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4385 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4386 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4387 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4388 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4389 SCTP_ERROR(ECONNREFUSED));
4390 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4391 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4393 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4394 SCTP_ERROR(ECONNABORTED));
4395 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4396 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4397 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4400 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4405 if (sctp_test_T_bit(abort))
4406 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4408 abort->skb->sk = ep->base.sk;
4410 sctp_packet_append_chunk(packet, abort);
4412 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4413 SCTP_PACKET(packet));
4415 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4418 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4421 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4422 return SCTP_DISPOSITION_ABORT;
4425 sctp_chunk_free(abort);
4427 return SCTP_DISPOSITION_NOMEM;
4431 * Handle a protocol violation when the chunk length is invalid.
4432 * "Invalid" length is identified as smaller than the minimal length a
4433 * given chunk can be. For example, a SACK chunk has invalid length
4434 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4436 * We inform the other end by sending an ABORT with a Protocol Violation
4439 * Section: Not specified
4440 * Verification Tag: Nothing to do
4442 * (endpoint, asoc, chunk)
4445 * (reply_msg, msg_up, counters)
4447 * Generate an ABORT chunk and terminate the association.
4449 static sctp_disposition_t sctp_sf_violation_chunklen(
4451 const struct sctp_endpoint *ep,
4452 const struct sctp_association *asoc,
4453 const sctp_subtype_t type,
4455 sctp_cmd_seq_t *commands)
4457 static const char err_str[]="The following chunk had invalid length:";
4459 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4464 * Handle a protocol violation when the parameter length is invalid.
4465 * If the length is smaller than the minimum length of a given parameter,
4466 * or accumulated length in multi parameters exceeds the end of the chunk,
4467 * the length is considered as invalid.
4469 static sctp_disposition_t sctp_sf_violation_paramlen(
4471 const struct sctp_endpoint *ep,
4472 const struct sctp_association *asoc,
4473 const sctp_subtype_t type,
4474 void *arg, void *ext,
4475 sctp_cmd_seq_t *commands)
4477 struct sctp_chunk *chunk = arg;
4478 struct sctp_paramhdr *param = ext;
4479 struct sctp_chunk *abort = NULL;
4481 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4484 /* Make the abort chunk. */
4485 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4489 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4490 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4492 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4493 SCTP_ERROR(ECONNABORTED));
4494 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4495 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4496 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4497 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4500 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4501 return SCTP_DISPOSITION_ABORT;
4503 return SCTP_DISPOSITION_NOMEM;
4506 /* Handle a protocol violation when the peer trying to advance the
4507 * cumulative tsn ack to a point beyond the max tsn currently sent.
4509 * We inform the other end by sending an ABORT with a Protocol Violation
4512 static sctp_disposition_t sctp_sf_violation_ctsn(
4514 const struct sctp_endpoint *ep,
4515 const struct sctp_association *asoc,
4516 const sctp_subtype_t type,
4518 sctp_cmd_seq_t *commands)
4520 static const char err_str[]="The cumulative tsn ack beyond the max tsn currently sent:";
4522 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4526 /* Handle protocol violation of an invalid chunk bundling. For example,
4527 * when we have an association and we receive bundled INIT-ACK, or
4528 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4529 * statement from the specs. Additionally, there might be an attacker
4530 * on the path and we may not want to continue this communication.
4532 static sctp_disposition_t sctp_sf_violation_chunk(
4534 const struct sctp_endpoint *ep,
4535 const struct sctp_association *asoc,
4536 const sctp_subtype_t type,
4538 sctp_cmd_seq_t *commands)
4540 static const char err_str[]="The following chunk violates protocol:";
4543 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4545 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4548 /***************************************************************************
4549 * These are the state functions for handling primitive (Section 10) events.
4550 ***************************************************************************/
4552 * sctp_sf_do_prm_asoc
4554 * Section: 10.1 ULP-to-SCTP
4557 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4558 * outbound stream count)
4559 * -> association id [,destination transport addr list] [,outbound stream
4562 * This primitive allows the upper layer to initiate an association to a
4563 * specific peer endpoint.
4565 * The peer endpoint shall be specified by one of the transport addresses
4566 * which defines the endpoint (see Section 1.4). If the local SCTP
4567 * instance has not been initialized, the ASSOCIATE is considered an
4569 * [This is not relevant for the kernel implementation since we do all
4570 * initialization at boot time. It we hadn't initialized we wouldn't
4571 * get anywhere near this code.]
4573 * An association id, which is a local handle to the SCTP association,
4574 * will be returned on successful establishment of the association. If
4575 * SCTP is not able to open an SCTP association with the peer endpoint,
4576 * an error is returned.
4577 * [In the kernel implementation, the struct sctp_association needs to
4578 * be created BEFORE causing this primitive to run.]
4580 * Other association parameters may be returned, including the
4581 * complete destination transport addresses of the peer as well as the
4582 * outbound stream count of the local endpoint. One of the transport
4583 * address from the returned destination addresses will be selected by
4584 * the local endpoint as default primary path for sending SCTP packets
4585 * to this peer. The returned "destination transport addr list" can
4586 * be used by the ULP to change the default primary path or to force
4587 * sending a packet to a specific transport address. [All of this
4588 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4591 * Mandatory attributes:
4593 * o local SCTP instance name - obtained from the INITIALIZE operation.
4594 * [This is the argument asoc.]
4595 * o destination transport addr - specified as one of the transport
4596 * addresses of the peer endpoint with which the association is to be
4598 * [This is asoc->peer.active_path.]
4599 * o outbound stream count - the number of outbound streams the ULP
4600 * would like to open towards this peer endpoint.
4601 * [BUG: This is not currently implemented.]
4602 * Optional attributes:
4606 * The return value is a disposition.
4608 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4609 const struct sctp_endpoint *ep,
4610 const struct sctp_association *asoc,
4611 const sctp_subtype_t type,
4613 sctp_cmd_seq_t *commands)
4615 struct sctp_chunk *repl;
4616 struct sctp_association* my_asoc;
4618 /* The comment below says that we enter COOKIE-WAIT AFTER
4619 * sending the INIT, but that doesn't actually work in our
4622 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4623 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4625 /* RFC 2960 5.1 Normal Establishment of an Association
4627 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4628 * must provide its Verification Tag (Tag_A) in the Initiate
4629 * Tag field. Tag_A SHOULD be a random number in the range of
4630 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4633 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4637 /* Cast away the const modifier, as we want to just
4638 * rerun it through as a sideffect.
4640 my_asoc = (struct sctp_association *)asoc;
4641 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4643 /* Choose transport for INIT. */
4644 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4647 /* After sending the INIT, "A" starts the T1-init timer and
4648 * enters the COOKIE-WAIT state.
4650 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4651 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4652 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4653 return SCTP_DISPOSITION_CONSUME;
4656 return SCTP_DISPOSITION_NOMEM;
4660 * Process the SEND primitive.
4662 * Section: 10.1 ULP-to-SCTP
4665 * Format: SEND(association id, buffer address, byte count [,context]
4666 * [,stream id] [,life time] [,destination transport address]
4667 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4670 * This is the main method to send user data via SCTP.
4672 * Mandatory attributes:
4674 * o association id - local handle to the SCTP association
4676 * o buffer address - the location where the user message to be
4677 * transmitted is stored;
4679 * o byte count - The size of the user data in number of bytes;
4681 * Optional attributes:
4683 * o context - an optional 32 bit integer that will be carried in the
4684 * sending failure notification to the ULP if the transportation of
4685 * this User Message fails.
4687 * o stream id - to indicate which stream to send the data on. If not
4688 * specified, stream 0 will be used.
4690 * o life time - specifies the life time of the user data. The user data
4691 * will not be sent by SCTP after the life time expires. This
4692 * parameter can be used to avoid efforts to transmit stale
4693 * user messages. SCTP notifies the ULP if the data cannot be
4694 * initiated to transport (i.e. sent to the destination via SCTP's
4695 * send primitive) within the life time variable. However, the
4696 * user data will be transmitted if SCTP has attempted to transmit a
4697 * chunk before the life time expired.
4699 * o destination transport address - specified as one of the destination
4700 * transport addresses of the peer endpoint to which this packet
4701 * should be sent. Whenever possible, SCTP should use this destination
4702 * transport address for sending the packets, instead of the current
4705 * o unorder flag - this flag, if present, indicates that the user
4706 * would like the data delivered in an unordered fashion to the peer
4707 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4710 * o no-bundle flag - instructs SCTP not to bundle this user data with
4711 * other outbound DATA chunks. SCTP MAY still bundle even when
4712 * this flag is present, when faced with network congestion.
4714 * o payload protocol-id - A 32 bit unsigned integer that is to be
4715 * passed to the peer indicating the type of payload protocol data
4716 * being transmitted. This value is passed as opaque data by SCTP.
4718 * The return value is the disposition.
4720 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4721 const struct sctp_endpoint *ep,
4722 const struct sctp_association *asoc,
4723 const sctp_subtype_t type,
4725 sctp_cmd_seq_t *commands)
4727 struct sctp_datamsg *msg = arg;
4729 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4730 return SCTP_DISPOSITION_CONSUME;
4734 * Process the SHUTDOWN primitive.
4739 * Format: SHUTDOWN(association id)
4742 * Gracefully closes an association. Any locally queued user data
4743 * will be delivered to the peer. The association will be terminated only
4744 * after the peer acknowledges all the SCTP packets sent. A success code
4745 * will be returned on successful termination of the association. If
4746 * attempting to terminate the association results in a failure, an error
4747 * code shall be returned.
4749 * Mandatory attributes:
4751 * o association id - local handle to the SCTP association
4753 * Optional attributes:
4757 * The return value is the disposition.
4759 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4761 const struct sctp_endpoint *ep,
4762 const struct sctp_association *asoc,
4763 const sctp_subtype_t type,
4765 sctp_cmd_seq_t *commands)
4769 /* From 9.2 Shutdown of an Association
4770 * Upon receipt of the SHUTDOWN primitive from its upper
4771 * layer, the endpoint enters SHUTDOWN-PENDING state and
4772 * remains there until all outstanding data has been
4773 * acknowledged by its peer. The endpoint accepts no new data
4774 * from its upper layer, but retransmits data to the far end
4775 * if necessary to fill gaps.
4777 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4778 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4780 disposition = SCTP_DISPOSITION_CONSUME;
4781 if (sctp_outq_is_empty(&asoc->outqueue)) {
4782 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4789 * Process the ABORT primitive.
4794 * Format: Abort(association id [, cause code])
4797 * Ungracefully closes an association. Any locally queued user data
4798 * will be discarded and an ABORT chunk is sent to the peer. A success code
4799 * will be returned on successful abortion of the association. If
4800 * attempting to abort the association results in a failure, an error
4801 * code shall be returned.
4803 * Mandatory attributes:
4805 * o association id - local handle to the SCTP association
4807 * Optional attributes:
4809 * o cause code - reason of the abort to be passed to the peer
4813 * The return value is the disposition.
4815 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4817 const struct sctp_endpoint *ep,
4818 const struct sctp_association *asoc,
4819 const sctp_subtype_t type,
4821 sctp_cmd_seq_t *commands)
4823 /* From 9.1 Abort of an Association
4824 * Upon receipt of the ABORT primitive from its upper
4825 * layer, the endpoint enters CLOSED state and
4826 * discard all outstanding data has been
4827 * acknowledged by its peer. The endpoint accepts no new data
4828 * from its upper layer, but retransmits data to the far end
4829 * if necessary to fill gaps.
4831 struct sctp_chunk *abort = arg;
4832 sctp_disposition_t retval;
4834 retval = SCTP_DISPOSITION_CONSUME;
4836 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4838 /* Even if we can't send the ABORT due to low memory delete the
4839 * TCB. This is a departure from our typical NOMEM handling.
4842 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4843 SCTP_ERROR(ECONNABORTED));
4844 /* Delete the established association. */
4845 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4846 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4848 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4849 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4854 /* We tried an illegal operation on an association which is closed. */
4855 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4856 const struct sctp_endpoint *ep,
4857 const struct sctp_association *asoc,
4858 const sctp_subtype_t type,
4860 sctp_cmd_seq_t *commands)
4862 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4863 return SCTP_DISPOSITION_CONSUME;
4866 /* We tried an illegal operation on an association which is shutting
4869 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4870 const struct sctp_endpoint *ep,
4871 const struct sctp_association *asoc,
4872 const sctp_subtype_t type,
4874 sctp_cmd_seq_t *commands)
4876 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4877 SCTP_ERROR(-ESHUTDOWN));
4878 return SCTP_DISPOSITION_CONSUME;
4882 * sctp_cookie_wait_prm_shutdown
4884 * Section: 4 Note: 2
4889 * The RFC does not explicitly address this issue, but is the route through the
4890 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4895 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4897 const struct sctp_endpoint *ep,
4898 const struct sctp_association *asoc,
4899 const sctp_subtype_t type,
4901 sctp_cmd_seq_t *commands)
4903 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4904 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4906 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4907 SCTP_STATE(SCTP_STATE_CLOSED));
4909 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4911 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4913 return SCTP_DISPOSITION_DELETE_TCB;
4917 * sctp_cookie_echoed_prm_shutdown
4919 * Section: 4 Note: 2
4924 * The RFC does not explcitly address this issue, but is the route through the
4925 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4930 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4932 const struct sctp_endpoint *ep,
4933 const struct sctp_association *asoc,
4934 const sctp_subtype_t type,
4935 void *arg, sctp_cmd_seq_t *commands)
4937 /* There is a single T1 timer, so we should be able to use
4938 * common function with the COOKIE-WAIT state.
4940 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4944 * sctp_sf_cookie_wait_prm_abort
4946 * Section: 4 Note: 2
4951 * The RFC does not explicitly address this issue, but is the route through the
4952 * state table when someone issues an abort while in COOKIE_WAIT state.
4957 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4959 const struct sctp_endpoint *ep,
4960 const struct sctp_association *asoc,
4961 const sctp_subtype_t type,
4963 sctp_cmd_seq_t *commands)
4965 struct sctp_chunk *abort = arg;
4966 sctp_disposition_t retval;
4968 /* Stop T1-init timer */
4969 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4970 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4971 retval = SCTP_DISPOSITION_CONSUME;
4973 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4975 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4976 SCTP_STATE(SCTP_STATE_CLOSED));
4978 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4980 /* Even if we can't send the ABORT due to low memory delete the
4981 * TCB. This is a departure from our typical NOMEM handling.
4984 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4985 SCTP_ERROR(ECONNREFUSED));
4986 /* Delete the established association. */
4987 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4988 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4994 * sctp_sf_cookie_echoed_prm_abort
4996 * Section: 4 Note: 3
5001 * The RFC does not explcitly address this issue, but is the route through the
5002 * state table when someone issues an abort while in COOKIE_ECHOED state.
5007 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5009 const struct sctp_endpoint *ep,
5010 const struct sctp_association *asoc,
5011 const sctp_subtype_t type,
5013 sctp_cmd_seq_t *commands)
5015 /* There is a single T1 timer, so we should be able to use
5016 * common function with the COOKIE-WAIT state.
5018 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5022 * sctp_sf_shutdown_pending_prm_abort
5027 * The RFC does not explicitly address this issue, but is the route through the
5028 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5033 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5035 const struct sctp_endpoint *ep,
5036 const struct sctp_association *asoc,
5037 const sctp_subtype_t type,
5039 sctp_cmd_seq_t *commands)
5041 /* Stop the T5-shutdown guard timer. */
5042 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5043 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5045 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5049 * sctp_sf_shutdown_sent_prm_abort
5054 * The RFC does not explicitly address this issue, but is the route through the
5055 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5060 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5062 const struct sctp_endpoint *ep,
5063 const struct sctp_association *asoc,
5064 const sctp_subtype_t type,
5066 sctp_cmd_seq_t *commands)
5068 /* Stop the T2-shutdown timer. */
5069 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5070 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5072 /* Stop the T5-shutdown guard timer. */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5074 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5076 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5080 * sctp_sf_cookie_echoed_prm_abort
5085 * The RFC does not explcitly address this issue, but is the route through the
5086 * state table when someone issues an abort while in COOKIE_ECHOED state.
5091 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5093 const struct sctp_endpoint *ep,
5094 const struct sctp_association *asoc,
5095 const sctp_subtype_t type,
5097 sctp_cmd_seq_t *commands)
5099 /* The same T2 timer, so we should be able to use
5100 * common function with the SHUTDOWN-SENT state.
5102 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5106 * Process the REQUESTHEARTBEAT primitive
5109 * J) Request Heartbeat
5111 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5115 * Instructs the local endpoint to perform a HeartBeat on the specified
5116 * destination transport address of the given association. The returned
5117 * result should indicate whether the transmission of the HEARTBEAT
5118 * chunk to the destination address is successful.
5120 * Mandatory attributes:
5122 * o association id - local handle to the SCTP association
5124 * o destination transport address - the transport address of the
5125 * association on which a heartbeat should be issued.
5127 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5129 const struct sctp_endpoint *ep,
5130 const struct sctp_association *asoc,
5131 const sctp_subtype_t type,
5133 sctp_cmd_seq_t *commands)
5135 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5136 (struct sctp_transport *)arg, commands))
5137 return SCTP_DISPOSITION_NOMEM;
5140 * RFC 2960 (bis), section 8.3
5142 * D) Request an on-demand HEARTBEAT on a specific destination
5143 * transport address of a given association.
5145 * The endpoint should increment the respective error counter of
5146 * the destination transport address each time a HEARTBEAT is sent
5147 * to that address and not acknowledged within one RTO.
5150 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5151 SCTP_TRANSPORT(arg));
5152 return SCTP_DISPOSITION_CONSUME;
5156 * ADDIP Section 4.1 ASCONF Chunk Procedures
5157 * When an endpoint has an ASCONF signaled change to be sent to the
5158 * remote endpoint it should do A1 to A9
5160 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5161 const struct sctp_endpoint *ep,
5162 const struct sctp_association *asoc,
5163 const sctp_subtype_t type,
5165 sctp_cmd_seq_t *commands)
5167 struct sctp_chunk *chunk = arg;
5169 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5170 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5171 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5172 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5173 return SCTP_DISPOSITION_CONSUME;
5177 * Ignore the primitive event
5179 * The return value is the disposition of the primitive.
5181 sctp_disposition_t sctp_sf_ignore_primitive(
5183 const struct sctp_endpoint *ep,
5184 const struct sctp_association *asoc,
5185 const sctp_subtype_t type,
5187 sctp_cmd_seq_t *commands)
5189 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
5190 return SCTP_DISPOSITION_DISCARD;
5193 /***************************************************************************
5194 * These are the state functions for the OTHER events.
5195 ***************************************************************************/
5198 * When the SCTP stack has no more user data to send or retransmit, this
5199 * notification is given to the user. Also, at the time when a user app
5200 * subscribes to this event, if there is no data to be sent or
5201 * retransmit, the stack will immediately send up this notification.
5203 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5205 const struct sctp_endpoint *ep,
5206 const struct sctp_association *asoc,
5207 const sctp_subtype_t type,
5209 sctp_cmd_seq_t *commands)
5211 struct sctp_ulpevent *event;
5213 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5215 return SCTP_DISPOSITION_NOMEM;
5217 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5219 return SCTP_DISPOSITION_CONSUME;
5223 * Start the shutdown negotiation.
5226 * Once all its outstanding data has been acknowledged, the endpoint
5227 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5228 * TSN Ack field the last sequential TSN it has received from the peer.
5229 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5230 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5231 * with the updated last sequential TSN received from its peer.
5233 * The return value is the disposition.
5235 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5237 const struct sctp_endpoint *ep,
5238 const struct sctp_association *asoc,
5239 const sctp_subtype_t type,
5241 sctp_cmd_seq_t *commands)
5243 struct sctp_chunk *reply;
5245 /* Once all its outstanding data has been acknowledged, the
5246 * endpoint shall send a SHUTDOWN chunk to its peer including
5247 * in the Cumulative TSN Ack field the last sequential TSN it
5248 * has received from the peer.
5250 reply = sctp_make_shutdown(asoc, NULL);
5254 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5255 * T2-shutdown timer.
5257 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5259 /* It shall then start the T2-shutdown timer */
5260 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5261 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5263 /* RFC 4960 Section 9.2
5264 * The sender of the SHUTDOWN MAY also start an overall guard timer
5265 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5270 if (asoc->autoclose)
5271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5272 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5274 /* and enter the SHUTDOWN-SENT state. */
5275 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5276 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5278 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5280 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5283 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5285 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5287 return SCTP_DISPOSITION_CONSUME;
5290 return SCTP_DISPOSITION_NOMEM;
5294 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5298 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5299 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5300 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5301 * endpoint must re-send the SHUTDOWN ACK.
5303 * The return value is the disposition.
5305 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5307 const struct sctp_endpoint *ep,
5308 const struct sctp_association *asoc,
5309 const sctp_subtype_t type,
5311 sctp_cmd_seq_t *commands)
5313 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5314 struct sctp_chunk *reply;
5316 /* There are 2 ways of getting here:
5317 * 1) called in response to a SHUTDOWN chunk
5318 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5320 * For the case (2), the arg parameter is set to NULL. We need
5321 * to check that we have a chunk before accessing it's fields.
5324 if (!sctp_vtag_verify(chunk, asoc))
5325 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5327 /* Make sure that the SHUTDOWN chunk has a valid length. */
5328 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5329 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5333 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5334 * shall send a SHUTDOWN ACK ...
5336 reply = sctp_make_shutdown_ack(asoc, chunk);
5340 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5341 * the T2-shutdown timer.
5343 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5345 /* and start/restart a T2-shutdown timer of its own, */
5346 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5347 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5349 if (asoc->autoclose)
5350 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5351 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5353 /* Enter the SHUTDOWN-ACK-SENT state. */
5354 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5355 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5357 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5359 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5362 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5364 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5366 return SCTP_DISPOSITION_CONSUME;
5369 return SCTP_DISPOSITION_NOMEM;
5373 * Ignore the event defined as other
5375 * The return value is the disposition of the event.
5377 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5378 const struct sctp_endpoint *ep,
5379 const struct sctp_association *asoc,
5380 const sctp_subtype_t type,
5382 sctp_cmd_seq_t *commands)
5384 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
5385 return SCTP_DISPOSITION_DISCARD;
5388 /************************************************************
5389 * These are the state functions for handling timeout events.
5390 ************************************************************/
5395 * Section: 6.3.3 Handle T3-rtx Expiration
5397 * Whenever the retransmission timer T3-rtx expires for a destination
5398 * address, do the following:
5401 * The return value is the disposition of the chunk.
5403 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5404 const struct sctp_endpoint *ep,
5405 const struct sctp_association *asoc,
5406 const sctp_subtype_t type,
5408 sctp_cmd_seq_t *commands)
5410 struct sctp_transport *transport = arg;
5412 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5414 if (asoc->overall_error_count >= asoc->max_retrans) {
5415 if (asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5417 * We are here likely because the receiver had its rwnd
5418 * closed for a while and we have not been able to
5419 * transmit the locally queued data within the maximum
5420 * retransmission attempts limit. Start the T5
5421 * shutdown guard timer to give the receiver one last
5422 * chance and some additional time to recover before
5425 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5426 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5428 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5429 SCTP_ERROR(ETIMEDOUT));
5430 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5431 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5432 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5433 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5434 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5435 return SCTP_DISPOSITION_DELETE_TCB;
5439 /* E1) For the destination address for which the timer
5440 * expires, adjust its ssthresh with rules defined in Section
5441 * 7.2.3 and set the cwnd <- MTU.
5444 /* E2) For the destination address for which the timer
5445 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5446 * maximum value discussed in rule C7 above (RTO.max) may be
5447 * used to provide an upper bound to this doubling operation.
5450 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5451 * outstanding DATA chunks for the address for which the
5452 * T3-rtx has expired will fit into a single packet, subject
5453 * to the MTU constraint for the path corresponding to the
5454 * destination transport address to which the retransmission
5455 * is being sent (this may be different from the address for
5456 * which the timer expires [see Section 6.4]). Call this
5457 * value K. Bundle and retransmit those K DATA chunks in a
5458 * single packet to the destination endpoint.
5460 * Note: Any DATA chunks that were sent to the address for
5461 * which the T3-rtx timer expired but did not fit in one MTU
5462 * (rule E3 above), should be marked for retransmission and
5463 * sent as soon as cwnd allows (normally when a SACK arrives).
5466 /* Do some failure management (Section 8.2). */
5467 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5469 /* NB: Rules E4 and F1 are implicit in R1. */
5470 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5472 return SCTP_DISPOSITION_CONSUME;
5476 * Generate delayed SACK on timeout
5478 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5480 * The guidelines on delayed acknowledgement algorithm specified in
5481 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5482 * acknowledgement SHOULD be generated for at least every second packet
5483 * (not every second DATA chunk) received, and SHOULD be generated
5484 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5485 * some situations it may be beneficial for an SCTP transmitter to be
5486 * more conservative than the algorithms detailed in this document
5487 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5488 * the following algorithms allow.
5490 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5491 const struct sctp_endpoint *ep,
5492 const struct sctp_association *asoc,
5493 const sctp_subtype_t type,
5495 sctp_cmd_seq_t *commands)
5497 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5498 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5499 return SCTP_DISPOSITION_CONSUME;
5503 * sctp_sf_t1_init_timer_expire
5505 * Section: 4 Note: 2
5510 * RFC 2960 Section 4 Notes
5511 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5512 * and re-start the T1-init timer without changing state. This MUST
5513 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5514 * endpoint MUST abort the initialization process and report the
5515 * error to SCTP user.
5521 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5522 const struct sctp_endpoint *ep,
5523 const struct sctp_association *asoc,
5524 const sctp_subtype_t type,
5526 sctp_cmd_seq_t *commands)
5528 struct sctp_chunk *repl = NULL;
5529 struct sctp_bind_addr *bp;
5530 int attempts = asoc->init_err_counter + 1;
5532 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
5533 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5535 if (attempts <= asoc->max_init_attempts) {
5536 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5537 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5539 return SCTP_DISPOSITION_NOMEM;
5541 /* Choose transport for INIT. */
5542 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5545 /* Issue a sideeffect to do the needed accounting. */
5546 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5547 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5549 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5551 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
5552 " max_init_attempts: %d\n",
5553 attempts, asoc->max_init_attempts);
5554 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5555 SCTP_ERROR(ETIMEDOUT));
5556 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5557 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5558 return SCTP_DISPOSITION_DELETE_TCB;
5561 return SCTP_DISPOSITION_CONSUME;
5565 * sctp_sf_t1_cookie_timer_expire
5567 * Section: 4 Note: 2
5572 * RFC 2960 Section 4 Notes
5573 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5574 * COOKIE ECHO and re-start the T1-cookie timer without changing
5575 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5576 * After that, the endpoint MUST abort the initialization process and
5577 * report the error to SCTP user.
5583 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5584 const struct sctp_endpoint *ep,
5585 const struct sctp_association *asoc,
5586 const sctp_subtype_t type,
5588 sctp_cmd_seq_t *commands)
5590 struct sctp_chunk *repl = NULL;
5591 int attempts = asoc->init_err_counter + 1;
5593 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
5594 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5596 if (attempts <= asoc->max_init_attempts) {
5597 repl = sctp_make_cookie_echo(asoc, NULL);
5599 return SCTP_DISPOSITION_NOMEM;
5601 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5603 /* Issue a sideeffect to do the needed accounting. */
5604 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5607 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5609 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5610 SCTP_ERROR(ETIMEDOUT));
5611 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5612 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5613 return SCTP_DISPOSITION_DELETE_TCB;
5616 return SCTP_DISPOSITION_CONSUME;
5619 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5620 * with the updated last sequential TSN received from its peer.
5622 * An endpoint should limit the number of retransmissions of the
5623 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5624 * If this threshold is exceeded the endpoint should destroy the TCB and
5625 * MUST report the peer endpoint unreachable to the upper layer (and
5626 * thus the association enters the CLOSED state). The reception of any
5627 * packet from its peer (i.e. as the peer sends all of its queued DATA
5628 * chunks) should clear the endpoint's retransmission count and restart
5629 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5630 * all of its queued DATA chunks that have not yet been sent.
5632 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5633 const struct sctp_endpoint *ep,
5634 const struct sctp_association *asoc,
5635 const sctp_subtype_t type,
5637 sctp_cmd_seq_t *commands)
5639 struct sctp_chunk *reply = NULL;
5641 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
5642 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5644 ((struct sctp_association *)asoc)->shutdown_retries++;
5646 if (asoc->overall_error_count >= asoc->max_retrans) {
5647 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5648 SCTP_ERROR(ETIMEDOUT));
5649 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5650 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5651 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5652 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5653 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5654 return SCTP_DISPOSITION_DELETE_TCB;
5657 switch (asoc->state) {
5658 case SCTP_STATE_SHUTDOWN_SENT:
5659 reply = sctp_make_shutdown(asoc, NULL);
5662 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5663 reply = sctp_make_shutdown_ack(asoc, NULL);
5674 /* Do some failure management (Section 8.2).
5675 * If we remove the transport an SHUTDOWN was last sent to, don't
5676 * do failure management.
5678 if (asoc->shutdown_last_sent_to)
5679 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5680 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5682 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5683 * the T2-shutdown timer.
5685 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5687 /* Restart the T2-shutdown timer. */
5688 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5689 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5690 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5691 return SCTP_DISPOSITION_CONSUME;
5694 return SCTP_DISPOSITION_NOMEM;
5698 * ADDIP Section 4.1 ASCONF CHunk Procedures
5699 * If the T4 RTO timer expires the endpoint should do B1 to B5
5701 sctp_disposition_t sctp_sf_t4_timer_expire(
5703 const struct sctp_endpoint *ep,
5704 const struct sctp_association *asoc,
5705 const sctp_subtype_t type,
5707 sctp_cmd_seq_t *commands)
5709 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5710 struct sctp_transport *transport = chunk->transport;
5712 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5714 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5715 * detection on the appropriate destination address as defined in
5716 * RFC2960 [5] section 8.1 and 8.2.
5719 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5720 SCTP_TRANSPORT(transport));
5722 /* Reconfig T4 timer and transport. */
5723 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5725 /* ADDIP 4.1 B2) Increment the association error counters and perform
5726 * endpoint failure detection on the association as defined in
5727 * RFC2960 [5] section 8.1 and 8.2.
5728 * association error counter is incremented in SCTP_CMD_STRIKE.
5730 if (asoc->overall_error_count >= asoc->max_retrans) {
5731 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5732 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5733 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5734 SCTP_ERROR(ETIMEDOUT));
5735 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5736 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5737 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5738 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5739 return SCTP_DISPOSITION_ABORT;
5742 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5743 * the ASCONF chunk was sent by doubling the RTO timer value.
5744 * This is done in SCTP_CMD_STRIKE.
5747 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5748 * choose an alternate destination address (please refer to RFC2960
5749 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5750 * chunk, it MUST be the same (including its serial number) as the last
5753 sctp_chunk_hold(asoc->addip_last_asconf);
5754 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5755 SCTP_CHUNK(asoc->addip_last_asconf));
5757 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5758 * destination is selected, then the RTO used will be that of the new
5759 * destination address.
5761 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5762 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5764 return SCTP_DISPOSITION_CONSUME;
5767 /* sctpimpguide-05 Section 2.12.2
5768 * The sender of the SHUTDOWN MAY also start an overall guard timer
5769 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5770 * At the expiration of this timer the sender SHOULD abort the association
5771 * by sending an ABORT chunk.
5773 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5774 const struct sctp_endpoint *ep,
5775 const struct sctp_association *asoc,
5776 const sctp_subtype_t type,
5778 sctp_cmd_seq_t *commands)
5780 struct sctp_chunk *reply = NULL;
5782 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
5783 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5785 reply = sctp_make_abort(asoc, NULL, 0);
5789 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5790 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5791 SCTP_ERROR(ETIMEDOUT));
5792 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5793 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5795 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5796 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5798 return SCTP_DISPOSITION_DELETE_TCB;
5800 return SCTP_DISPOSITION_NOMEM;
5803 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5804 * the association is automatically closed by starting the shutdown process.
5805 * The work that needs to be done is same as when SHUTDOWN is initiated by
5806 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5808 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5810 const struct sctp_endpoint *ep,
5811 const struct sctp_association *asoc,
5812 const sctp_subtype_t type,
5814 sctp_cmd_seq_t *commands)
5818 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5820 /* From 9.2 Shutdown of an Association
5821 * Upon receipt of the SHUTDOWN primitive from its upper
5822 * layer, the endpoint enters SHUTDOWN-PENDING state and
5823 * remains there until all outstanding data has been
5824 * acknowledged by its peer. The endpoint accepts no new data
5825 * from its upper layer, but retransmits data to the far end
5826 * if necessary to fill gaps.
5828 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5829 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5831 disposition = SCTP_DISPOSITION_CONSUME;
5832 if (sctp_outq_is_empty(&asoc->outqueue)) {
5833 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5839 /*****************************************************************************
5840 * These are sa state functions which could apply to all types of events.
5841 ****************************************************************************/
5844 * This table entry is not implemented.
5847 * (endpoint, asoc, chunk)
5849 * The return value is the disposition of the chunk.
5851 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5852 const struct sctp_endpoint *ep,
5853 const struct sctp_association *asoc,
5854 const sctp_subtype_t type,
5856 sctp_cmd_seq_t *commands)
5858 return SCTP_DISPOSITION_NOT_IMPL;
5862 * This table entry represents a bug.
5865 * (endpoint, asoc, chunk)
5867 * The return value is the disposition of the chunk.
5869 sctp_disposition_t sctp_sf_bug(struct net *net,
5870 const struct sctp_endpoint *ep,
5871 const struct sctp_association *asoc,
5872 const sctp_subtype_t type,
5874 sctp_cmd_seq_t *commands)
5876 return SCTP_DISPOSITION_BUG;
5880 * This table entry represents the firing of a timer in the wrong state.
5881 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5882 * when the association is in the wrong state. This event should
5883 * be ignored, so as to prevent any rearming of the timer.
5886 * (endpoint, asoc, chunk)
5888 * The return value is the disposition of the chunk.
5890 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5891 const struct sctp_endpoint *ep,
5892 const struct sctp_association *asoc,
5893 const sctp_subtype_t type,
5895 sctp_cmd_seq_t *commands)
5897 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5898 return SCTP_DISPOSITION_CONSUME;
5901 /********************************************************************
5902 * 2nd Level Abstractions
5903 ********************************************************************/
5905 /* Pull the SACK chunk based on the SACK header. */
5906 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5908 struct sctp_sackhdr *sack;
5913 /* Protect ourselves from reading too far into
5914 * the skb from a bogus sender.
5916 sack = (struct sctp_sackhdr *) chunk->skb->data;
5918 num_blocks = ntohs(sack->num_gap_ack_blocks);
5919 num_dup_tsns = ntohs(sack->num_dup_tsns);
5920 len = sizeof(struct sctp_sackhdr);
5921 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5922 if (len > chunk->skb->len)
5925 skb_pull(chunk->skb, len);
5930 /* Create an ABORT packet to be sent as a response, with the specified
5933 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5934 const struct sctp_endpoint *ep,
5935 const struct sctp_association *asoc,
5936 struct sctp_chunk *chunk,
5937 const void *payload,
5940 struct sctp_packet *packet;
5941 struct sctp_chunk *abort;
5943 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5947 * The T bit will be set if the asoc is NULL.
5949 abort = sctp_make_abort(asoc, chunk, paylen);
5951 sctp_ootb_pkt_free(packet);
5955 /* Reflect vtag if T-Bit is set */
5956 if (sctp_test_T_bit(abort))
5957 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5959 /* Add specified error causes, i.e., payload, to the
5962 sctp_addto_chunk(abort, paylen, payload);
5964 /* Set the skb to the belonging sock for accounting. */
5965 abort->skb->sk = ep->base.sk;
5967 sctp_packet_append_chunk(packet, abort);
5974 /* Allocate a packet for responding in the OOTB conditions. */
5975 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
5976 const struct sctp_association *asoc,
5977 const struct sctp_chunk *chunk)
5979 struct sctp_packet *packet;
5980 struct sctp_transport *transport;
5985 /* Get the source and destination port from the inbound packet. */
5986 sport = ntohs(chunk->sctp_hdr->dest);
5987 dport = ntohs(chunk->sctp_hdr->source);
5989 /* The V-tag is going to be the same as the inbound packet if no
5990 * association exists, otherwise, use the peer's vtag.
5993 /* Special case the INIT-ACK as there is no peer's vtag
5996 switch(chunk->chunk_hdr->type) {
5997 case SCTP_CID_INIT_ACK:
5999 sctp_initack_chunk_t *initack;
6001 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6002 vtag = ntohl(initack->init_hdr.init_tag);
6006 vtag = asoc->peer.i.init_tag;
6010 /* Special case the INIT and stale COOKIE_ECHO as there is no
6013 switch(chunk->chunk_hdr->type) {
6016 sctp_init_chunk_t *init;
6018 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6019 vtag = ntohl(init->init_hdr.init_tag);
6023 vtag = ntohl(chunk->sctp_hdr->vtag);
6028 /* Make a transport for the bucket, Eliza... */
6029 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6033 /* Cache a route for the transport with the chunk's destination as
6034 * the source address.
6036 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6037 sctp_sk(net->sctp.ctl_sock));
6039 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6040 packet = sctp_packet_config(packet, vtag, 0);
6048 /* Free the packet allocated earlier for responding in the OOTB condition. */
6049 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6051 sctp_transport_free(packet->transport);
6054 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6055 static void sctp_send_stale_cookie_err(struct net *net,
6056 const struct sctp_endpoint *ep,
6057 const struct sctp_association *asoc,
6058 const struct sctp_chunk *chunk,
6059 sctp_cmd_seq_t *commands,
6060 struct sctp_chunk *err_chunk)
6062 struct sctp_packet *packet;
6065 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6067 struct sctp_signed_cookie *cookie;
6069 /* Override the OOTB vtag from the cookie. */
6070 cookie = chunk->subh.cookie_hdr;
6071 packet->vtag = cookie->c.peer_vtag;
6073 /* Set the skb to the belonging sock for accounting. */
6074 err_chunk->skb->sk = ep->base.sk;
6075 sctp_packet_append_chunk(packet, err_chunk);
6076 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6077 SCTP_PACKET(packet));
6078 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6080 sctp_chunk_free (err_chunk);
6085 /* Process a data chunk */
6086 static int sctp_eat_data(const struct sctp_association *asoc,
6087 struct sctp_chunk *chunk,
6088 sctp_cmd_seq_t *commands)
6090 sctp_datahdr_t *data_hdr;
6091 struct sctp_chunk *err;
6093 sctp_verb_t deliver;
6096 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6097 struct sock *sk = asoc->base.sk;
6098 struct net *net = sock_net(sk);
6103 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6104 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6106 tsn = ntohl(data_hdr->tsn);
6107 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
6109 /* ASSERT: Now skb->data is really the user data. */
6111 /* Process ECN based congestion.
6113 * Since the chunk structure is reused for all chunks within
6114 * a packet, we use ecn_ce_done to track if we've already
6115 * done CE processing for this packet.
6117 * We need to do ECN processing even if we plan to discard the
6121 if (!chunk->ecn_ce_done) {
6123 chunk->ecn_ce_done = 1;
6125 af = sctp_get_af_specific(
6126 ipver2af(ip_hdr(chunk->skb)->version));
6128 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6129 /* Do real work as sideffect. */
6130 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6135 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6137 /* The TSN is too high--silently discard the chunk and
6138 * count on it getting retransmitted later.
6141 chunk->asoc->stats.outofseqtsns++;
6142 return SCTP_IERROR_HIGH_TSN;
6143 } else if (tmp > 0) {
6144 /* This is a duplicate. Record it. */
6145 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6146 return SCTP_IERROR_DUP_TSN;
6149 /* This is a new TSN. */
6151 /* Discard if there is no room in the receive window.
6152 * Actually, allow a little bit of overflow (up to a MTU).
6154 datalen = ntohs(chunk->chunk_hdr->length);
6155 datalen -= sizeof(sctp_data_chunk_t);
6157 deliver = SCTP_CMD_CHUNK_ULP;
6159 /* Think about partial delivery. */
6160 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6162 /* Even if we don't accept this chunk there is
6165 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6168 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6169 * seems a bit troublesome in that frag_point varies based on
6170 * PMTU. In cases, such as loopback, this might be a rather
6173 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6174 (datalen > asoc->rwnd + asoc->frag_point))) {
6176 /* If this is the next TSN, consider reneging to make
6177 * room. Note: Playing nice with a confused sender. A
6178 * malicious sender can still eat up all our buffer
6179 * space and in the future we may want to detect and
6180 * do more drastic reneging.
6182 if (sctp_tsnmap_has_gap(map) &&
6183 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6184 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
6185 deliver = SCTP_CMD_RENEGE;
6187 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
6188 "rwnd: %d\n", tsn, datalen,
6190 return SCTP_IERROR_IGNORE_TSN;
6195 * Also try to renege to limit our memory usage in the event that
6196 * we are under memory pressure
6197 * If we can't renege, don't worry about it, the sk_rmem_schedule
6198 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6199 * memory usage too much
6201 if (*sk->sk_prot_creator->memory_pressure) {
6202 if (sctp_tsnmap_has_gap(map) &&
6203 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6204 SCTP_DEBUG_PRINTK("Under Pressure! Reneging for tsn:%u\n", tsn);
6205 deliver = SCTP_CMD_RENEGE;
6210 * Section 3.3.10.9 No User Data (9)
6214 * No User Data: This error cause is returned to the originator of a
6215 * DATA chunk if a received DATA chunk has no user data.
6217 if (unlikely(0 == datalen)) {
6218 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6220 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6223 /* We are going to ABORT, so we might as well stop
6224 * processing the rest of the chunks in the packet.
6226 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
6227 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6228 SCTP_ERROR(ECONNABORTED));
6229 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6230 SCTP_PERR(SCTP_ERROR_NO_DATA));
6231 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6232 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6233 return SCTP_IERROR_NO_DATA;
6236 chunk->data_accepted = 1;
6238 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6239 * if we renege and the chunk arrives again.
6241 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6242 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6244 chunk->asoc->stats.iuodchunks++;
6246 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6248 chunk->asoc->stats.iodchunks++;
6252 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6254 * If an endpoint receive a DATA chunk with an invalid stream
6255 * identifier, it shall acknowledge the reception of the DATA chunk
6256 * following the normal procedure, immediately send an ERROR chunk
6257 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6258 * and discard the DATA chunk.
6260 sid = ntohs(data_hdr->stream);
6261 if (sid >= asoc->c.sinit_max_instreams) {
6262 /* Mark tsn as received even though we drop it */
6263 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6265 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6267 sizeof(data_hdr->stream),
6270 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6272 return SCTP_IERROR_BAD_STREAM;
6275 /* Check to see if the SSN is possible for this TSN.
6276 * The biggest gap we can record is 4K wide. Since SSNs wrap
6277 * at an unsigned short, there is no way that an SSN can
6278 * wrap and for a valid TSN. We can simply check if the current
6279 * SSN is smaller then the next expected one. If it is, it wrapped
6282 ssn = ntohs(data_hdr->ssn);
6283 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6284 return SCTP_IERROR_PROTO_VIOLATION;
6287 /* Send the data up to the user. Note: Schedule the
6288 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6289 * chunk needs the updated rwnd.
6291 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6293 return SCTP_IERROR_NO_ERROR;