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, 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, 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 /* set-up our fake chunk so that we can process it */
769 auth.skb = chunk->auth_chunk;
770 auth.asoc = chunk->asoc;
771 auth.sctp_hdr = chunk->sctp_hdr;
772 auth.chunk_hdr = (sctp_chunkhdr_t *)skb_push(chunk->auth_chunk,
773 sizeof(sctp_chunkhdr_t));
774 skb_pull(chunk->auth_chunk, sizeof(sctp_chunkhdr_t));
775 auth.transport = chunk->transport;
777 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
779 /* We can now safely free the auth_chunk clone */
780 kfree_skb(chunk->auth_chunk);
782 if (ret != SCTP_IERROR_NO_ERROR) {
783 sctp_association_free(new_asoc);
784 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
788 repl = sctp_make_cookie_ack(new_asoc, chunk);
792 /* RFC 2960 5.1 Normal Establishment of an Association
794 * D) IMPLEMENTATION NOTE: An implementation may choose to
795 * send the Communication Up notification to the SCTP user
796 * upon reception of a valid COOKIE ECHO chunk.
798 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
799 new_asoc->c.sinit_num_ostreams,
800 new_asoc->c.sinit_max_instreams,
805 /* Sockets API Draft Section 5.3.1.6
806 * When a peer sends a Adaptation Layer Indication parameter , SCTP
807 * delivers this notification to inform the application that of the
808 * peers requested adaptation layer.
810 if (new_asoc->peer.adaptation_ind) {
811 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
817 /* Add all the state machine commands now since we've created
818 * everything. This way we don't introduce memory corruptions
819 * during side-effect processing and correclty count established
822 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
823 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
824 SCTP_STATE(SCTP_STATE_ESTABLISHED));
825 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
826 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
827 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
829 if (new_asoc->autoclose)
830 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
831 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
833 /* This will send the COOKIE ACK */
834 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
836 /* Queue the ASSOC_CHANGE event */
837 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
839 /* Send up the Adaptation Layer Indication event */
841 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
842 SCTP_ULPEVENT(ai_ev));
844 return SCTP_DISPOSITION_CONSUME;
847 sctp_ulpevent_free(ev);
849 sctp_chunk_free(repl);
851 sctp_association_free(new_asoc);
853 return SCTP_DISPOSITION_NOMEM;
857 * Respond to a normal COOKIE ACK chunk.
858 * We are the side that is being asked for an association.
860 * RFC 2960 5.1 Normal Establishment of an Association
862 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
863 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
864 * timer. It may also notify its ULP about the successful
865 * establishment of the association with a Communication Up
866 * notification (see Section 10).
870 * (endpoint, asoc, chunk)
873 * (asoc, reply_msg, msg_up, timers, counters)
875 * The return value is the disposition of the chunk.
877 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
878 const struct sctp_endpoint *ep,
879 const struct sctp_association *asoc,
880 const sctp_subtype_t type, void *arg,
881 sctp_cmd_seq_t *commands)
883 struct sctp_chunk *chunk = arg;
884 struct sctp_ulpevent *ev;
886 if (!sctp_vtag_verify(chunk, asoc))
887 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
889 /* Verify that the chunk length for the COOKIE-ACK is OK.
890 * If we don't do this, any bundled chunks may be junked.
892 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
893 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
896 /* Reset init error count upon receipt of COOKIE-ACK,
897 * to avoid problems with the managemement of this
898 * counter in stale cookie situations when a transition back
899 * from the COOKIE-ECHOED state to the COOKIE-WAIT
900 * state is performed.
902 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
904 /* RFC 2960 5.1 Normal Establishment of an Association
906 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
907 * from the COOKIE-ECHOED state to the ESTABLISHED state,
908 * stopping the T1-cookie timer.
910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
911 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
912 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
913 SCTP_STATE(SCTP_STATE_ESTABLISHED));
914 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
915 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
916 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
918 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
919 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
921 /* It may also notify its ULP about the successful
922 * establishment of the association with a Communication Up
923 * notification (see Section 10).
925 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
926 0, asoc->c.sinit_num_ostreams,
927 asoc->c.sinit_max_instreams,
933 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
935 /* Sockets API Draft Section 5.3.1.6
936 * When a peer sends a Adaptation Layer Indication parameter , SCTP
937 * delivers this notification to inform the application that of the
938 * peers requested adaptation layer.
940 if (asoc->peer.adaptation_ind) {
941 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
945 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
949 return SCTP_DISPOSITION_CONSUME;
951 return SCTP_DISPOSITION_NOMEM;
954 /* Generate and sendout a heartbeat packet. */
955 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
956 const struct sctp_association *asoc,
957 const sctp_subtype_t type,
959 sctp_cmd_seq_t *commands)
961 struct sctp_transport *transport = (struct sctp_transport *) arg;
962 struct sctp_chunk *reply;
964 /* Send a heartbeat to our peer. */
965 reply = sctp_make_heartbeat(asoc, transport);
967 return SCTP_DISPOSITION_NOMEM;
969 /* Set rto_pending indicating that an RTT measurement
970 * is started with this heartbeat chunk.
972 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
973 SCTP_TRANSPORT(transport));
975 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
976 return SCTP_DISPOSITION_CONSUME;
979 /* Generate a HEARTBEAT packet on the given transport. */
980 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
981 const struct sctp_endpoint *ep,
982 const struct sctp_association *asoc,
983 const sctp_subtype_t type,
985 sctp_cmd_seq_t *commands)
987 struct sctp_transport *transport = (struct sctp_transport *) arg;
989 if (asoc->overall_error_count >= asoc->max_retrans) {
990 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
991 SCTP_ERROR(ETIMEDOUT));
992 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
993 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
994 SCTP_PERR(SCTP_ERROR_NO_ERROR));
995 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
996 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
997 return SCTP_DISPOSITION_DELETE_TCB;
1001 * The Sender-specific Heartbeat Info field should normally include
1002 * information about the sender's current time when this HEARTBEAT
1003 * chunk is sent and the destination transport address to which this
1004 * HEARTBEAT is sent (see Section 8.3).
1007 if (transport->param_flags & SPP_HB_ENABLE) {
1008 if (SCTP_DISPOSITION_NOMEM ==
1009 sctp_sf_heartbeat(ep, asoc, type, arg,
1011 return SCTP_DISPOSITION_NOMEM;
1013 /* Set transport error counter and association error counter
1014 * when sending heartbeat.
1016 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1017 SCTP_TRANSPORT(transport));
1019 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1020 SCTP_TRANSPORT(transport));
1021 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1022 SCTP_TRANSPORT(transport));
1024 return SCTP_DISPOSITION_CONSUME;
1028 * Process an heartbeat request.
1030 * Section: 8.3 Path Heartbeat
1031 * The receiver of the HEARTBEAT should immediately respond with a
1032 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1033 * from the received HEARTBEAT chunk.
1035 * Verification Tag: 8.5 Verification Tag [Normal verification]
1036 * When receiving an SCTP packet, the endpoint MUST ensure that the
1037 * value in the Verification Tag field of the received SCTP packet
1038 * matches its own Tag. If the received Verification Tag value does not
1039 * match the receiver's own tag value, the receiver shall silently
1040 * discard the packet and shall not process it any further except for
1041 * those cases listed in Section 8.5.1 below.
1044 * (endpoint, asoc, chunk)
1047 * (asoc, reply_msg, msg_up, timers, counters)
1049 * The return value is the disposition of the chunk.
1051 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1052 const struct sctp_endpoint *ep,
1053 const struct sctp_association *asoc,
1054 const sctp_subtype_t type,
1056 sctp_cmd_seq_t *commands)
1058 struct sctp_chunk *chunk = arg;
1059 struct sctp_chunk *reply;
1062 if (!sctp_vtag_verify(chunk, asoc))
1063 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1065 /* Make sure that the HEARTBEAT chunk has a valid length. */
1066 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1067 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1070 /* 8.3 The receiver of the HEARTBEAT should immediately
1071 * respond with a HEARTBEAT ACK that contains the Heartbeat
1072 * Information field copied from the received HEARTBEAT chunk.
1074 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1075 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1076 if (!pskb_pull(chunk->skb, paylen))
1079 reply = sctp_make_heartbeat_ack(asoc, chunk,
1080 chunk->subh.hb_hdr, paylen);
1084 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1085 return SCTP_DISPOSITION_CONSUME;
1088 return SCTP_DISPOSITION_NOMEM;
1092 * Process the returning HEARTBEAT ACK.
1094 * Section: 8.3 Path Heartbeat
1095 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1096 * should clear the error counter of the destination transport
1097 * address to which the HEARTBEAT was sent, and mark the destination
1098 * transport address as active if it is not so marked. The endpoint may
1099 * optionally report to the upper layer when an inactive destination
1100 * address is marked as active due to the reception of the latest
1101 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1102 * clear the association overall error count as well (as defined
1105 * The receiver of the HEARTBEAT ACK should also perform an RTT
1106 * measurement for that destination transport address using the time
1107 * value carried in the HEARTBEAT ACK chunk.
1109 * Verification Tag: 8.5 Verification Tag [Normal verification]
1112 * (endpoint, asoc, chunk)
1115 * (asoc, reply_msg, msg_up, timers, counters)
1117 * The return value is the disposition of the chunk.
1119 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1120 const struct sctp_endpoint *ep,
1121 const struct sctp_association *asoc,
1122 const sctp_subtype_t type,
1124 sctp_cmd_seq_t *commands)
1126 struct sctp_chunk *chunk = arg;
1127 union sctp_addr from_addr;
1128 struct sctp_transport *link;
1129 sctp_sender_hb_info_t *hbinfo;
1130 unsigned long max_interval;
1132 if (!sctp_vtag_verify(chunk, asoc))
1133 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1135 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1136 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1137 sizeof(sctp_sender_hb_info_t)))
1138 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1141 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1142 /* Make sure that the length of the parameter is what we expect */
1143 if (ntohs(hbinfo->param_hdr.length) !=
1144 sizeof(sctp_sender_hb_info_t)) {
1145 return SCTP_DISPOSITION_DISCARD;
1148 from_addr = hbinfo->daddr;
1149 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1151 /* This should never happen, but lets log it if so. */
1152 if (unlikely(!link)) {
1153 if (from_addr.sa.sa_family == AF_INET6) {
1154 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1157 &from_addr.v6.sin6_addr);
1159 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1162 &from_addr.v4.sin_addr.s_addr);
1164 return SCTP_DISPOSITION_DISCARD;
1167 /* Validate the 64-bit random nonce. */
1168 if (hbinfo->hb_nonce != link->hb_nonce)
1169 return SCTP_DISPOSITION_DISCARD;
1171 max_interval = link->hbinterval + link->rto;
1173 /* Check if the timestamp looks valid. */
1174 if (time_after(hbinfo->sent_at, jiffies) ||
1175 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1176 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp "
1177 "received for transport: %p\n",
1179 return SCTP_DISPOSITION_DISCARD;
1182 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1183 * the HEARTBEAT should clear the error counter of the
1184 * destination transport address to which the HEARTBEAT was
1185 * sent and mark the destination transport address as active if
1186 * it is not so marked.
1188 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1190 return SCTP_DISPOSITION_CONSUME;
1193 /* Helper function to send out an abort for the restart
1196 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1197 struct sctp_chunk *init,
1198 sctp_cmd_seq_t *commands)
1201 struct sctp_packet *pkt;
1202 union sctp_addr_param *addrparm;
1203 struct sctp_errhdr *errhdr;
1204 struct sctp_endpoint *ep;
1205 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1206 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1208 /* Build the error on the stack. We are way to malloc crazy
1209 * throughout the code today.
1211 errhdr = (struct sctp_errhdr *)buffer;
1212 addrparm = (union sctp_addr_param *)errhdr->variable;
1214 /* Copy into a parm format. */
1215 len = af->to_addr_param(ssa, addrparm);
1216 len += sizeof(sctp_errhdr_t);
1218 errhdr->cause = SCTP_ERROR_RESTART;
1219 errhdr->length = htons(len);
1221 /* Assign to the control socket. */
1222 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1224 /* Association is NULL since this may be a restart attack and we
1225 * want to send back the attacker's vtag.
1227 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1231 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1233 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1235 /* Discard the rest of the inbound packet. */
1236 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1239 /* Even if there is no memory, treat as a failure so
1240 * the packet will get dropped.
1245 static bool list_has_sctp_addr(const struct list_head *list,
1246 union sctp_addr *ipaddr)
1248 struct sctp_transport *addr;
1250 list_for_each_entry(addr, list, transports) {
1251 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1257 /* A restart is occurring, check to make sure no new addresses
1258 * are being added as we may be under a takeover attack.
1260 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1261 const struct sctp_association *asoc,
1262 struct sctp_chunk *init,
1263 sctp_cmd_seq_t *commands)
1265 struct net *net = sock_net(new_asoc->base.sk);
1266 struct sctp_transport *new_addr;
1269 /* Implementor's Guide - Section 5.2.2
1271 * Before responding the endpoint MUST check to see if the
1272 * unexpected INIT adds new addresses to the association. If new
1273 * addresses are added to the association, the endpoint MUST respond
1277 /* Search through all current addresses and make sure
1278 * we aren't adding any new ones.
1280 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1282 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1283 &new_addr->ipaddr)) {
1284 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1291 /* Return success if all addresses were found. */
1295 /* Populate the verification/tie tags based on overlapping INIT
1298 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1300 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1301 const struct sctp_association *asoc)
1303 switch (asoc->state) {
1305 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1307 case SCTP_STATE_COOKIE_WAIT:
1308 new_asoc->c.my_vtag = asoc->c.my_vtag;
1309 new_asoc->c.my_ttag = asoc->c.my_vtag;
1310 new_asoc->c.peer_ttag = 0;
1313 case SCTP_STATE_COOKIE_ECHOED:
1314 new_asoc->c.my_vtag = asoc->c.my_vtag;
1315 new_asoc->c.my_ttag = asoc->c.my_vtag;
1316 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1319 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1320 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1323 new_asoc->c.my_ttag = asoc->c.my_vtag;
1324 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1328 /* Other parameters for the endpoint SHOULD be copied from the
1329 * existing parameters of the association (e.g. number of
1330 * outbound streams) into the INIT ACK and cookie.
1332 new_asoc->rwnd = asoc->rwnd;
1333 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1334 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1335 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1339 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1342 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1344 * Returns value representing action to be taken. These action values
1345 * correspond to Action/Description values in RFC 2960, Table 2.
1347 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1348 const struct sctp_association *asoc)
1350 /* In this case, the peer may have restarted. */
1351 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1352 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1353 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1354 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1357 /* Collision case B. */
1358 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1359 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1360 (0 == asoc->c.peer_vtag))) {
1364 /* Collision case D. */
1365 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1366 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1369 /* Collision case C. */
1370 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1371 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1372 (0 == new_asoc->c.my_ttag) &&
1373 (0 == new_asoc->c.peer_ttag))
1376 /* No match to any of the special cases; discard this packet. */
1380 /* Common helper routine for both duplicate and simulataneous INIT
1383 static sctp_disposition_t sctp_sf_do_unexpected_init(
1385 const struct sctp_endpoint *ep,
1386 const struct sctp_association *asoc,
1387 const sctp_subtype_t type,
1388 void *arg, sctp_cmd_seq_t *commands)
1390 sctp_disposition_t retval;
1391 struct sctp_chunk *chunk = arg;
1392 struct sctp_chunk *repl;
1393 struct sctp_association *new_asoc;
1394 struct sctp_chunk *err_chunk;
1395 struct sctp_packet *packet;
1396 sctp_unrecognized_param_t *unk_param;
1400 * An endpoint MUST NOT bundle INIT, INIT ACK or
1401 * SHUTDOWN COMPLETE with any other chunks.
1404 * Furthermore, we require that the receiver of an INIT chunk MUST
1405 * enforce these rules by silently discarding an arriving packet
1406 * with an INIT chunk that is bundled with other chunks.
1408 if (!chunk->singleton)
1409 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1411 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1414 if (chunk->sctp_hdr->vtag != 0)
1415 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1417 /* Make sure that the INIT chunk has a valid length.
1418 * In this case, we generate a protocol violation since we have
1419 * an association established.
1421 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1422 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1424 /* Grab the INIT header. */
1425 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1427 /* Tag the variable length parameters. */
1428 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1430 /* Verify the INIT chunk before processing it. */
1432 if (!sctp_verify_init(net, asoc, chunk->chunk_hdr->type,
1433 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1435 /* This chunk contains fatal error. It is to be discarded.
1436 * Send an ABORT, with causes if there is any.
1439 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1440 (__u8 *)(err_chunk->chunk_hdr) +
1441 sizeof(sctp_chunkhdr_t),
1442 ntohs(err_chunk->chunk_hdr->length) -
1443 sizeof(sctp_chunkhdr_t));
1446 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1447 SCTP_PACKET(packet));
1448 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1449 retval = SCTP_DISPOSITION_CONSUME;
1451 retval = SCTP_DISPOSITION_NOMEM;
1455 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1461 * Other parameters for the endpoint SHOULD be copied from the
1462 * existing parameters of the association (e.g. number of
1463 * outbound streams) into the INIT ACK and cookie.
1464 * FIXME: We are copying parameters from the endpoint not the
1467 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1471 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1472 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1475 /* In the outbound INIT ACK the endpoint MUST copy its current
1476 * Verification Tag and Peers Verification tag into a reserved
1477 * place (local tie-tag and per tie-tag) within the state cookie.
1479 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1480 (sctp_init_chunk_t *)chunk->chunk_hdr,
1484 /* Make sure no new addresses are being added during the
1485 * restart. Do not do this check for COOKIE-WAIT state,
1486 * since there are no peer addresses to check against.
1487 * Upon return an ABORT will have been sent if needed.
1489 if (!sctp_state(asoc, COOKIE_WAIT)) {
1490 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1492 retval = SCTP_DISPOSITION_CONSUME;
1497 sctp_tietags_populate(new_asoc, asoc);
1499 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1501 /* If there are errors need to be reported for unknown parameters,
1502 * make sure to reserve enough room in the INIT ACK for them.
1506 len = ntohs(err_chunk->chunk_hdr->length) -
1507 sizeof(sctp_chunkhdr_t);
1510 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1514 /* If there are errors need to be reported for unknown parameters,
1515 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1519 /* Get the "Unrecognized parameter" parameter(s) out of the
1520 * ERROR chunk generated by sctp_verify_init(). Since the
1521 * error cause code for "unknown parameter" and the
1522 * "Unrecognized parameter" type is the same, we can
1523 * construct the parameters in INIT ACK by copying the
1524 * ERROR causes over.
1526 unk_param = (sctp_unrecognized_param_t *)
1527 ((__u8 *)(err_chunk->chunk_hdr) +
1528 sizeof(sctp_chunkhdr_t));
1529 /* Replace the cause code with the "Unrecognized parameter"
1532 sctp_addto_chunk(repl, len, unk_param);
1535 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1536 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1539 * Note: After sending out INIT ACK with the State Cookie parameter,
1540 * "Z" MUST NOT allocate any resources for this new association.
1541 * Otherwise, "Z" will be vulnerable to resource attacks.
1543 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1544 retval = SCTP_DISPOSITION_CONSUME;
1549 retval = SCTP_DISPOSITION_NOMEM;
1552 sctp_association_free(new_asoc);
1555 sctp_chunk_free(err_chunk);
1560 * Handle simultaneous INIT.
1561 * This means we started an INIT and then we got an INIT request from
1564 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1565 * This usually indicates an initialization collision, i.e., each
1566 * endpoint is attempting, at about the same time, to establish an
1567 * association with the other endpoint.
1569 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1570 * endpoint MUST respond with an INIT ACK using the same parameters it
1571 * sent in its original INIT chunk (including its Verification Tag,
1572 * unchanged). These original parameters are combined with those from the
1573 * newly received INIT chunk. The endpoint shall also generate a State
1574 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1575 * INIT to calculate the State Cookie.
1577 * After that, the endpoint MUST NOT change its state, the T1-init
1578 * timer shall be left running and the corresponding TCB MUST NOT be
1579 * destroyed. The normal procedures for handling State Cookies when
1580 * a TCB exists will resolve the duplicate INITs to a single association.
1582 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1583 * its Tie-Tags with the Tag information of itself and its peer (see
1584 * section 5.2.2 for a description of the Tie-Tags).
1586 * Verification Tag: Not explicit, but an INIT can not have a valid
1587 * verification tag, so we skip the check.
1590 * (endpoint, asoc, chunk)
1593 * (asoc, reply_msg, msg_up, timers, counters)
1595 * The return value is the disposition of the chunk.
1597 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1598 const struct sctp_endpoint *ep,
1599 const struct sctp_association *asoc,
1600 const sctp_subtype_t type,
1602 sctp_cmd_seq_t *commands)
1604 /* Call helper to do the real work for both simulataneous and
1605 * duplicate INIT chunk handling.
1607 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1611 * Handle duplicated INIT messages. These are usually delayed
1614 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1615 * COOKIE-ECHOED and COOKIE-WAIT
1617 * Unless otherwise stated, upon reception of an unexpected INIT for
1618 * this association, the endpoint shall generate an INIT ACK with a
1619 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1620 * current Verification Tag and peer's Verification Tag into a reserved
1621 * place within the state cookie. We shall refer to these locations as
1622 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1623 * containing this INIT ACK MUST carry a Verification Tag value equal to
1624 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1625 * MUST contain a new Initiation Tag (randomly generated see Section
1626 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1627 * existing parameters of the association (e.g. number of outbound
1628 * streams) into the INIT ACK and cookie.
1630 * After sending out the INIT ACK, the endpoint shall take no further
1631 * actions, i.e., the existing association, including its current state,
1632 * and the corresponding TCB MUST NOT be changed.
1634 * Note: Only when a TCB exists and the association is not in a COOKIE-
1635 * WAIT state are the Tie-Tags populated. For a normal association INIT
1636 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1637 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1638 * State Cookie are populated as specified in section 5.2.1.
1640 * Verification Tag: Not specified, but an INIT has no way of knowing
1641 * what the verification tag could be, so we ignore it.
1644 * (endpoint, asoc, chunk)
1647 * (asoc, reply_msg, msg_up, timers, counters)
1649 * The return value is the disposition of the chunk.
1651 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1652 const struct sctp_endpoint *ep,
1653 const struct sctp_association *asoc,
1654 const sctp_subtype_t type,
1656 sctp_cmd_seq_t *commands)
1658 /* Call helper to do the real work for both simulataneous and
1659 * duplicate INIT chunk handling.
1661 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1666 * Unexpected INIT-ACK handler.
1669 * If an INIT ACK received by an endpoint in any state other than the
1670 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1671 * An unexpected INIT ACK usually indicates the processing of an old or
1672 * duplicated INIT chunk.
1674 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1675 const struct sctp_endpoint *ep,
1676 const struct sctp_association *asoc,
1677 const sctp_subtype_t type,
1678 void *arg, sctp_cmd_seq_t *commands)
1680 /* Per the above section, we'll discard the chunk if we have an
1681 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1683 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1684 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1686 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1689 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1692 * A) In this case, the peer may have restarted.
1694 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1695 const struct sctp_endpoint *ep,
1696 const struct sctp_association *asoc,
1697 struct sctp_chunk *chunk,
1698 sctp_cmd_seq_t *commands,
1699 struct sctp_association *new_asoc)
1701 sctp_init_chunk_t *peer_init;
1702 struct sctp_ulpevent *ev;
1703 struct sctp_chunk *repl;
1704 struct sctp_chunk *err;
1705 sctp_disposition_t disposition;
1707 /* new_asoc is a brand-new association, so these are not yet
1708 * side effects--it is safe to run them here.
1710 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1712 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1716 /* Make sure no new addresses are being added during the
1717 * restart. Though this is a pretty complicated attack
1718 * since you'd have to get inside the cookie.
1720 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1721 return SCTP_DISPOSITION_CONSUME;
1724 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1725 * the peer has restarted (Action A), it MUST NOT setup a new
1726 * association but instead resend the SHUTDOWN ACK and send an ERROR
1727 * chunk with a "Cookie Received while Shutting Down" error cause to
1730 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1731 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1732 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1734 if (SCTP_DISPOSITION_NOMEM == disposition)
1737 err = sctp_make_op_error(asoc, chunk,
1738 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1741 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1744 return SCTP_DISPOSITION_CONSUME;
1747 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1748 * data. Consider the optional choice of resending of this data.
1750 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1751 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1752 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1753 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1755 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1756 * and ASCONF-ACK cache.
1758 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1759 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1760 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1762 repl = sctp_make_cookie_ack(new_asoc, chunk);
1766 /* Report association restart to upper layer. */
1767 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1768 new_asoc->c.sinit_num_ostreams,
1769 new_asoc->c.sinit_max_instreams,
1774 /* Update the content of current association. */
1775 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1776 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1777 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1778 return SCTP_DISPOSITION_CONSUME;
1781 sctp_chunk_free(repl);
1783 return SCTP_DISPOSITION_NOMEM;
1786 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1789 * B) In this case, both sides may be attempting to start an association
1790 * at about the same time but the peer endpoint started its INIT
1791 * after responding to the local endpoint's INIT
1793 /* This case represents an initialization collision. */
1794 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1795 const struct sctp_endpoint *ep,
1796 const struct sctp_association *asoc,
1797 struct sctp_chunk *chunk,
1798 sctp_cmd_seq_t *commands,
1799 struct sctp_association *new_asoc)
1801 sctp_init_chunk_t *peer_init;
1802 struct sctp_chunk *repl;
1804 /* new_asoc is a brand-new association, so these are not yet
1805 * side effects--it is safe to run them here.
1807 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1808 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1812 /* Update the content of current association. */
1813 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1814 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1815 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1816 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1817 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1819 repl = sctp_make_cookie_ack(new_asoc, chunk);
1823 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1825 /* RFC 2960 5.1 Normal Establishment of an Association
1827 * D) IMPLEMENTATION NOTE: An implementation may choose to
1828 * send the Communication Up notification to the SCTP user
1829 * upon reception of a valid COOKIE ECHO chunk.
1831 * Sadly, this needs to be implemented as a side-effect, because
1832 * we are not guaranteed to have set the association id of the real
1833 * association and so these notifications need to be delayed until
1834 * the association id is allocated.
1837 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1839 /* Sockets API Draft Section 5.3.1.6
1840 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1841 * delivers this notification to inform the application that of the
1842 * peers requested adaptation layer.
1844 * This also needs to be done as a side effect for the same reason as
1847 if (asoc->peer.adaptation_ind)
1848 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1850 return SCTP_DISPOSITION_CONSUME;
1853 return SCTP_DISPOSITION_NOMEM;
1856 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1859 * C) In this case, the local endpoint's cookie has arrived late.
1860 * Before it arrived, the local endpoint sent an INIT and received an
1861 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1862 * but a new tag of its own.
1864 /* This case represents an initialization collision. */
1865 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1866 const struct sctp_endpoint *ep,
1867 const struct sctp_association *asoc,
1868 struct sctp_chunk *chunk,
1869 sctp_cmd_seq_t *commands,
1870 struct sctp_association *new_asoc)
1872 /* The cookie should be silently discarded.
1873 * The endpoint SHOULD NOT change states and should leave
1874 * any timers running.
1876 return SCTP_DISPOSITION_DISCARD;
1879 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1883 * D) When both local and remote tags match the endpoint should always
1884 * enter the ESTABLISHED state, if it has not already done so.
1886 /* This case represents an initialization collision. */
1887 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1888 const struct sctp_endpoint *ep,
1889 const struct sctp_association *asoc,
1890 struct sctp_chunk *chunk,
1891 sctp_cmd_seq_t *commands,
1892 struct sctp_association *new_asoc)
1894 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1895 struct sctp_chunk *repl;
1897 /* Clarification from Implementor's Guide:
1898 * D) When both local and remote tags match the endpoint should
1899 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1900 * It should stop any cookie timer that may be running and send
1904 /* Don't accidentally move back into established state. */
1905 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1907 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1908 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1909 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1910 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1911 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1914 /* RFC 2960 5.1 Normal Establishment of an Association
1916 * D) IMPLEMENTATION NOTE: An implementation may choose
1917 * to send the Communication Up notification to the
1918 * SCTP user upon reception of a valid COOKIE
1921 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1923 asoc->c.sinit_num_ostreams,
1924 asoc->c.sinit_max_instreams,
1929 /* Sockets API Draft Section 5.3.1.6
1930 * When a peer sends a Adaptation Layer Indication parameter,
1931 * SCTP delivers this notification to inform the application
1932 * that of the peers requested adaptation layer.
1934 if (asoc->peer.adaptation_ind) {
1935 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1943 repl = sctp_make_cookie_ack(new_asoc, chunk);
1947 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1950 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1953 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1954 SCTP_ULPEVENT(ai_ev));
1956 return SCTP_DISPOSITION_CONSUME;
1960 sctp_ulpevent_free(ai_ev);
1962 sctp_ulpevent_free(ev);
1963 return SCTP_DISPOSITION_NOMEM;
1967 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1968 * chunk was retransmitted and then delayed in the network.
1970 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1972 * Verification Tag: None. Do cookie validation.
1975 * (endpoint, asoc, chunk)
1978 * (asoc, reply_msg, msg_up, timers, counters)
1980 * The return value is the disposition of the chunk.
1982 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
1983 const struct sctp_endpoint *ep,
1984 const struct sctp_association *asoc,
1985 const sctp_subtype_t type,
1987 sctp_cmd_seq_t *commands)
1989 sctp_disposition_t retval;
1990 struct sctp_chunk *chunk = arg;
1991 struct sctp_association *new_asoc;
1994 struct sctp_chunk *err_chk_p;
1996 /* Make sure that the chunk has a valid length from the protocol
1997 * perspective. In this case check to make sure we have at least
1998 * enough for the chunk header. Cookie length verification is
2001 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2002 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2005 /* "Decode" the chunk. We have no optional parameters so we
2006 * are in good shape.
2008 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2009 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2010 sizeof(sctp_chunkhdr_t)))
2013 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2014 * of a duplicate COOKIE ECHO match the Verification Tags of the
2015 * current association, consider the State Cookie valid even if
2016 * the lifespan is exceeded.
2018 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2022 * If the re-build failed, what is the proper error path
2025 * [We should abort the association. --piggy]
2028 /* FIXME: Several errors are possible. A bad cookie should
2029 * be silently discarded, but think about logging it too.
2032 case -SCTP_IERROR_NOMEM:
2035 case -SCTP_IERROR_STALE_COOKIE:
2036 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2038 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2039 case -SCTP_IERROR_BAD_SIG:
2041 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2045 /* Compare the tie_tag in cookie with the verification tag of
2046 * current association.
2048 action = sctp_tietags_compare(new_asoc, asoc);
2051 case 'A': /* Association restart. */
2052 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2056 case 'B': /* Collision case B. */
2057 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2061 case 'C': /* Collision case C. */
2062 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2066 case 'D': /* Collision case D. */
2067 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2071 default: /* Discard packet for all others. */
2072 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2076 /* Delete the tempory new association. */
2077 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
2078 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2080 /* Restore association pointer to provide SCTP command interpeter
2081 * with a valid context in case it needs to manipulate
2083 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2084 SCTP_ASOC((struct sctp_association *)asoc));
2089 return SCTP_DISPOSITION_NOMEM;
2093 * Process an ABORT. (SHUTDOWN-PENDING state)
2095 * See sctp_sf_do_9_1_abort().
2097 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2099 const struct sctp_endpoint *ep,
2100 const struct sctp_association *asoc,
2101 const sctp_subtype_t type,
2103 sctp_cmd_seq_t *commands)
2105 struct sctp_chunk *chunk = arg;
2107 if (!sctp_vtag_verify_either(chunk, asoc))
2108 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2110 /* Make sure that the ABORT chunk has a valid length.
2111 * Since this is an ABORT chunk, we have to discard it
2112 * because of the following text:
2113 * RFC 2960, Section 3.3.7
2114 * If an endpoint receives an ABORT with a format error or for an
2115 * association that doesn't exist, it MUST silently discard it.
2116 * Because the length is "invalid", we can't really discard just
2117 * as we do not know its true length. So, to be safe, discard the
2120 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2121 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2123 /* ADD-IP: Special case for ABORT chunks
2124 * F4) One special consideration is that ABORT Chunks arriving
2125 * destined to the IP address being deleted MUST be
2126 * ignored (see Section 5.3.1 for further details).
2128 if (SCTP_ADDR_DEL ==
2129 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2130 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2132 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2136 * Process an ABORT. (SHUTDOWN-SENT state)
2138 * See sctp_sf_do_9_1_abort().
2140 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2141 const struct sctp_endpoint *ep,
2142 const struct sctp_association *asoc,
2143 const sctp_subtype_t type,
2145 sctp_cmd_seq_t *commands)
2147 struct sctp_chunk *chunk = arg;
2149 if (!sctp_vtag_verify_either(chunk, asoc))
2150 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2152 /* Make sure that the ABORT chunk has a valid length.
2153 * Since this is an ABORT chunk, we have to discard it
2154 * because of the following text:
2155 * RFC 2960, Section 3.3.7
2156 * If an endpoint receives an ABORT with a format error or for an
2157 * association that doesn't exist, it MUST silently discard it.
2158 * Because the length is "invalid", we can't really discard just
2159 * as we do not know its true length. So, to be safe, discard the
2162 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2163 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2165 /* ADD-IP: Special case for ABORT chunks
2166 * F4) One special consideration is that ABORT Chunks arriving
2167 * destined to the IP address being deleted MUST be
2168 * ignored (see Section 5.3.1 for further details).
2170 if (SCTP_ADDR_DEL ==
2171 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2172 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2174 /* Stop the T2-shutdown timer. */
2175 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2176 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2178 /* Stop the T5-shutdown guard timer. */
2179 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2180 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2182 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2186 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2188 * See sctp_sf_do_9_1_abort().
2190 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2192 const struct sctp_endpoint *ep,
2193 const struct sctp_association *asoc,
2194 const sctp_subtype_t type,
2196 sctp_cmd_seq_t *commands)
2198 /* The same T2 timer, so we should be able to use
2199 * common function with the SHUTDOWN-SENT state.
2201 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2205 * Handle an Error received in COOKIE_ECHOED state.
2207 * Only handle the error type of stale COOKIE Error, the other errors will
2211 * (endpoint, asoc, chunk)
2214 * (asoc, reply_msg, msg_up, timers, counters)
2216 * The return value is the disposition of the chunk.
2218 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2219 const struct sctp_endpoint *ep,
2220 const struct sctp_association *asoc,
2221 const sctp_subtype_t type,
2223 sctp_cmd_seq_t *commands)
2225 struct sctp_chunk *chunk = arg;
2228 if (!sctp_vtag_verify(chunk, asoc))
2229 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2231 /* Make sure that the ERROR chunk has a valid length.
2232 * The parameter walking depends on this as well.
2234 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2235 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2238 /* Process the error here */
2239 /* FUTURE FIXME: When PR-SCTP related and other optional
2240 * parms are emitted, this will have to change to handle multiple
2243 sctp_walk_errors(err, chunk->chunk_hdr) {
2244 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2245 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2249 /* It is possible to have malformed error causes, and that
2250 * will cause us to end the walk early. However, since
2251 * we are discarding the packet, there should be no adverse
2254 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2258 * Handle a Stale COOKIE Error
2260 * Section: 5.2.6 Handle Stale COOKIE Error
2261 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2262 * one of the following three alternatives.
2264 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2265 * Preservative parameter requesting an extension to the lifetime of
2266 * the State Cookie. When calculating the time extension, an
2267 * implementation SHOULD use the RTT information measured based on the
2268 * previous COOKIE ECHO / ERROR exchange, and should add no more
2269 * than 1 second beyond the measured RTT, due to long State Cookie
2270 * lifetimes making the endpoint more subject to a replay attack.
2272 * Verification Tag: Not explicit, but safe to ignore.
2275 * (endpoint, asoc, chunk)
2278 * (asoc, reply_msg, msg_up, timers, counters)
2280 * The return value is the disposition of the chunk.
2282 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2283 const struct sctp_endpoint *ep,
2284 const struct sctp_association *asoc,
2285 const sctp_subtype_t type,
2287 sctp_cmd_seq_t *commands)
2289 struct sctp_chunk *chunk = arg;
2291 sctp_cookie_preserve_param_t bht;
2293 struct sctp_chunk *reply;
2294 struct sctp_bind_addr *bp;
2295 int attempts = asoc->init_err_counter + 1;
2297 if (attempts > asoc->max_init_attempts) {
2298 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2299 SCTP_ERROR(ETIMEDOUT));
2300 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2301 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2302 return SCTP_DISPOSITION_DELETE_TCB;
2305 err = (sctp_errhdr_t *)(chunk->skb->data);
2307 /* When calculating the time extension, an implementation
2308 * SHOULD use the RTT information measured based on the
2309 * previous COOKIE ECHO / ERROR exchange, and should add no
2310 * more than 1 second beyond the measured RTT, due to long
2311 * State Cookie lifetimes making the endpoint more subject to
2313 * Measure of Staleness's unit is usec. (1/1000000 sec)
2314 * Suggested Cookie Life-span Increment's unit is msec.
2316 * In general, if you use the suggested cookie life, the value
2317 * found in the field of measure of staleness should be doubled
2318 * to give ample time to retransmit the new cookie and thus
2319 * yield a higher probability of success on the reattempt.
2321 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2322 stale = (stale * 2) / 1000;
2324 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2325 bht.param_hdr.length = htons(sizeof(bht));
2326 bht.lifespan_increment = htonl(stale);
2328 /* Build that new INIT chunk. */
2329 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2330 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2334 sctp_addto_chunk(reply, sizeof(bht), &bht);
2336 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2337 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2339 /* Stop pending T3-rtx and heartbeat timers */
2340 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2341 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2343 /* Delete non-primary peer ip addresses since we are transitioning
2344 * back to the COOKIE-WAIT state
2346 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2348 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2351 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2352 SCTP_TRANSPORT(asoc->peer.primary_path));
2354 /* Cast away the const modifier, as we want to just
2355 * rerun it through as a sideffect.
2357 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2359 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2360 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2361 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2362 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2363 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2364 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2366 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2368 return SCTP_DISPOSITION_CONSUME;
2371 return SCTP_DISPOSITION_NOMEM;
2378 * After checking the Verification Tag, the receiving endpoint shall
2379 * remove the association from its record, and shall report the
2380 * termination to its upper layer.
2382 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2383 * B) Rules for packet carrying ABORT:
2385 * - The endpoint shall always fill in the Verification Tag field of the
2386 * outbound packet with the destination endpoint's tag value if it
2389 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2390 * MUST follow the procedure described in Section 8.4.
2392 * - The receiver MUST accept the packet if the Verification Tag
2393 * matches either its own tag, OR the tag of its peer. Otherwise, the
2394 * receiver MUST silently discard the packet and take no further
2398 * (endpoint, asoc, chunk)
2401 * (asoc, reply_msg, msg_up, timers, counters)
2403 * The return value is the disposition of the chunk.
2405 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2406 const struct sctp_endpoint *ep,
2407 const struct sctp_association *asoc,
2408 const sctp_subtype_t type,
2410 sctp_cmd_seq_t *commands)
2412 struct sctp_chunk *chunk = arg;
2414 if (!sctp_vtag_verify_either(chunk, asoc))
2415 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2417 /* Make sure that the ABORT chunk has a valid length.
2418 * Since this is an ABORT chunk, we have to discard it
2419 * because of the following text:
2420 * RFC 2960, Section 3.3.7
2421 * If an endpoint receives an ABORT with a format error or for an
2422 * association that doesn't exist, it MUST silently discard it.
2423 * Because the length is "invalid", we can't really discard just
2424 * as we do not know its true length. So, to be safe, discard the
2427 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2428 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2430 /* ADD-IP: Special case for ABORT chunks
2431 * F4) One special consideration is that ABORT Chunks arriving
2432 * destined to the IP address being deleted MUST be
2433 * ignored (see Section 5.3.1 for further details).
2435 if (SCTP_ADDR_DEL ==
2436 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2437 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2439 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2442 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2443 const struct sctp_endpoint *ep,
2444 const struct sctp_association *asoc,
2445 const sctp_subtype_t type,
2447 sctp_cmd_seq_t *commands)
2449 struct sctp_chunk *chunk = arg;
2451 __be16 error = SCTP_ERROR_NO_ERROR;
2453 /* See if we have an error cause code in the chunk. */
2454 len = ntohs(chunk->chunk_hdr->length);
2455 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2458 sctp_walk_errors(err, chunk->chunk_hdr);
2459 if ((void *)err != (void *)chunk->chunk_end)
2460 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2462 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2465 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2466 /* ASSOC_FAILED will DELETE_TCB. */
2467 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2468 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2469 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2471 return SCTP_DISPOSITION_ABORT;
2475 * Process an ABORT. (COOKIE-WAIT state)
2477 * See sctp_sf_do_9_1_abort() above.
2479 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2480 const struct sctp_endpoint *ep,
2481 const struct sctp_association *asoc,
2482 const sctp_subtype_t type,
2484 sctp_cmd_seq_t *commands)
2486 struct sctp_chunk *chunk = arg;
2488 __be16 error = SCTP_ERROR_NO_ERROR;
2490 if (!sctp_vtag_verify_either(chunk, asoc))
2491 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2493 /* Make sure that the ABORT chunk has a valid length.
2494 * Since this is an ABORT chunk, we have to discard it
2495 * because of the following text:
2496 * RFC 2960, Section 3.3.7
2497 * If an endpoint receives an ABORT with a format error or for an
2498 * association that doesn't exist, it MUST silently discard it.
2499 * Because the length is "invalid", we can't really discard just
2500 * as we do not know its true length. So, to be safe, discard the
2503 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2504 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2506 /* See if we have an error cause code in the chunk. */
2507 len = ntohs(chunk->chunk_hdr->length);
2508 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2509 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2511 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2516 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2518 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2519 const struct sctp_endpoint *ep,
2520 const struct sctp_association *asoc,
2521 const sctp_subtype_t type,
2523 sctp_cmd_seq_t *commands)
2525 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2527 (struct sctp_transport *)arg);
2531 * Process an ABORT. (COOKIE-ECHOED state)
2533 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2534 const struct sctp_endpoint *ep,
2535 const struct sctp_association *asoc,
2536 const sctp_subtype_t type,
2538 sctp_cmd_seq_t *commands)
2540 /* There is a single T1 timer, so we should be able to use
2541 * common function with the COOKIE-WAIT state.
2543 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2547 * Stop T1 timer and abort association with "INIT failed".
2549 * This is common code called by several sctp_sf_*_abort() functions above.
2551 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2552 sctp_cmd_seq_t *commands,
2553 __be16 error, int sk_err,
2554 const struct sctp_association *asoc,
2555 struct sctp_transport *transport)
2557 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2558 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2559 SCTP_STATE(SCTP_STATE_CLOSED));
2560 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2561 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2562 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2563 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2564 /* CMD_INIT_FAILED will DELETE_TCB. */
2565 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2567 return SCTP_DISPOSITION_ABORT;
2571 * sctp_sf_do_9_2_shut
2574 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2575 * - enter the SHUTDOWN-RECEIVED state,
2577 * - stop accepting new data from its SCTP user
2579 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2580 * that all its outstanding DATA chunks have been received by the
2583 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2584 * send a SHUTDOWN in response to a ULP request. And should discard
2585 * subsequent SHUTDOWN chunks.
2587 * If there are still outstanding DATA chunks left, the SHUTDOWN
2588 * receiver shall continue to follow normal data transmission
2589 * procedures defined in Section 6 until all outstanding DATA chunks
2590 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2591 * new data from its SCTP user.
2593 * Verification Tag: 8.5 Verification Tag [Normal verification]
2596 * (endpoint, asoc, chunk)
2599 * (asoc, reply_msg, msg_up, timers, counters)
2601 * The return value is the disposition of the chunk.
2603 sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2604 const struct sctp_endpoint *ep,
2605 const struct sctp_association *asoc,
2606 const sctp_subtype_t type,
2608 sctp_cmd_seq_t *commands)
2610 struct sctp_chunk *chunk = arg;
2611 sctp_shutdownhdr_t *sdh;
2612 sctp_disposition_t disposition;
2613 struct sctp_ulpevent *ev;
2616 if (!sctp_vtag_verify(chunk, asoc))
2617 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2619 /* Make sure that the SHUTDOWN chunk has a valid length. */
2620 if (!sctp_chunk_length_valid(chunk,
2621 sizeof(struct sctp_shutdown_chunk_t)))
2622 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2625 /* Convert the elaborate header. */
2626 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2627 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2628 chunk->subh.shutdown_hdr = sdh;
2629 ctsn = ntohl(sdh->cum_tsn_ack);
2631 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2632 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2633 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2634 return SCTP_DISPOSITION_DISCARD;
2637 /* If Cumulative TSN Ack beyond the max tsn currently
2638 * send, terminating the association and respond to the
2639 * sender with an ABORT.
2641 if (!TSN_lt(ctsn, asoc->next_tsn))
2642 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2644 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2645 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2646 * inform the application that it should cease sending data.
2648 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2650 disposition = SCTP_DISPOSITION_NOMEM;
2653 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2655 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2656 * - enter the SHUTDOWN-RECEIVED state,
2657 * - stop accepting new data from its SCTP user
2659 * [This is implicit in the new state.]
2661 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2662 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2663 disposition = SCTP_DISPOSITION_CONSUME;
2665 if (sctp_outq_is_empty(&asoc->outqueue)) {
2666 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2670 if (SCTP_DISPOSITION_NOMEM == disposition)
2673 /* - verify, by checking the Cumulative TSN Ack field of the
2674 * chunk, that all its outstanding DATA chunks have been
2675 * received by the SHUTDOWN sender.
2677 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2678 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2685 * sctp_sf_do_9_2_shut_ctsn
2687 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2688 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2689 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2690 * MUST be processed.
2692 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2693 const struct sctp_endpoint *ep,
2694 const struct sctp_association *asoc,
2695 const sctp_subtype_t type,
2697 sctp_cmd_seq_t *commands)
2699 struct sctp_chunk *chunk = arg;
2700 sctp_shutdownhdr_t *sdh;
2703 if (!sctp_vtag_verify(chunk, asoc))
2704 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2706 /* Make sure that the SHUTDOWN chunk has a valid length. */
2707 if (!sctp_chunk_length_valid(chunk,
2708 sizeof(struct sctp_shutdown_chunk_t)))
2709 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2712 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2713 ctsn = ntohl(sdh->cum_tsn_ack);
2715 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2716 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2717 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2718 return SCTP_DISPOSITION_DISCARD;
2721 /* If Cumulative TSN Ack beyond the max tsn currently
2722 * send, terminating the association and respond to the
2723 * sender with an ABORT.
2725 if (!TSN_lt(ctsn, asoc->next_tsn))
2726 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2728 /* verify, by checking the Cumulative TSN Ack field of the
2729 * chunk, that all its outstanding DATA chunks have been
2730 * received by the SHUTDOWN sender.
2732 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2733 SCTP_BE32(sdh->cum_tsn_ack));
2735 return SCTP_DISPOSITION_CONSUME;
2739 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2740 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2741 * transport addresses (either in the IP addresses or in the INIT chunk)
2742 * that belong to this association, it should discard the INIT chunk and
2743 * retransmit the SHUTDOWN ACK chunk.
2745 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2746 const struct sctp_endpoint *ep,
2747 const struct sctp_association *asoc,
2748 const sctp_subtype_t type,
2750 sctp_cmd_seq_t *commands)
2752 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2753 struct sctp_chunk *reply;
2755 /* Make sure that the chunk has a valid length */
2756 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2757 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2760 /* Since we are not going to really process this INIT, there
2761 * is no point in verifying chunk boundries. Just generate
2764 reply = sctp_make_shutdown_ack(asoc, chunk);
2768 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2769 * the T2-SHUTDOWN timer.
2771 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2773 /* and restart the T2-shutdown timer. */
2774 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2775 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2777 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2779 return SCTP_DISPOSITION_CONSUME;
2781 return SCTP_DISPOSITION_NOMEM;
2785 * sctp_sf_do_ecn_cwr
2787 * Section: Appendix A: Explicit Congestion Notification
2791 * RFC 2481 details a specific bit for a sender to send in the header of
2792 * its next outbound TCP segment to indicate to its peer that it has
2793 * reduced its congestion window. This is termed the CWR bit. For
2794 * SCTP the same indication is made by including the CWR chunk.
2795 * This chunk contains one data element, i.e. the TSN number that
2796 * was sent in the ECNE chunk. This element represents the lowest
2797 * TSN number in the datagram that was originally marked with the
2800 * Verification Tag: 8.5 Verification Tag [Normal verification]
2802 * (endpoint, asoc, chunk)
2805 * (asoc, reply_msg, msg_up, timers, counters)
2807 * The return value is the disposition of the chunk.
2809 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2810 const struct sctp_endpoint *ep,
2811 const struct sctp_association *asoc,
2812 const sctp_subtype_t type,
2814 sctp_cmd_seq_t *commands)
2817 struct sctp_chunk *chunk = arg;
2820 if (!sctp_vtag_verify(chunk, asoc))
2821 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2823 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2824 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2827 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2828 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2830 lowest_tsn = ntohl(cwr->lowest_tsn);
2832 /* Does this CWR ack the last sent congestion notification? */
2833 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2834 /* Stop sending ECNE. */
2835 sctp_add_cmd_sf(commands,
2837 SCTP_U32(lowest_tsn));
2839 return SCTP_DISPOSITION_CONSUME;
2845 * Section: Appendix A: Explicit Congestion Notification
2849 * RFC 2481 details a specific bit for a receiver to send back in its
2850 * TCP acknowledgements to notify the sender of the Congestion
2851 * Experienced (CE) bit having arrived from the network. For SCTP this
2852 * same indication is made by including the ECNE chunk. This chunk
2853 * contains one data element, i.e. the lowest TSN associated with the IP
2854 * datagram marked with the CE bit.....
2856 * Verification Tag: 8.5 Verification Tag [Normal verification]
2858 * (endpoint, asoc, chunk)
2861 * (asoc, reply_msg, msg_up, timers, counters)
2863 * The return value is the disposition of the chunk.
2865 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2866 const struct sctp_endpoint *ep,
2867 const struct sctp_association *asoc,
2868 const sctp_subtype_t type,
2870 sctp_cmd_seq_t *commands)
2872 sctp_ecnehdr_t *ecne;
2873 struct sctp_chunk *chunk = arg;
2875 if (!sctp_vtag_verify(chunk, asoc))
2876 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2878 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2879 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2882 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2883 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2885 /* If this is a newer ECNE than the last CWR packet we sent out */
2886 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2887 SCTP_U32(ntohl(ecne->lowest_tsn)));
2889 return SCTP_DISPOSITION_CONSUME;
2893 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2895 * The SCTP endpoint MUST always acknowledge the reception of each valid
2898 * The guidelines on delayed acknowledgement algorithm specified in
2899 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2900 * acknowledgement SHOULD be generated for at least every second packet
2901 * (not every second DATA chunk) received, and SHOULD be generated within
2902 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2903 * situations it may be beneficial for an SCTP transmitter to be more
2904 * conservative than the algorithms detailed in this document allow.
2905 * However, an SCTP transmitter MUST NOT be more aggressive than the
2906 * following algorithms allow.
2908 * A SCTP receiver MUST NOT generate more than one SACK for every
2909 * incoming packet, other than to update the offered window as the
2910 * receiving application consumes new data.
2912 * Verification Tag: 8.5 Verification Tag [Normal verification]
2915 * (endpoint, asoc, chunk)
2918 * (asoc, reply_msg, msg_up, timers, counters)
2920 * The return value is the disposition of the chunk.
2922 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2923 const struct sctp_endpoint *ep,
2924 const struct sctp_association *asoc,
2925 const sctp_subtype_t type,
2927 sctp_cmd_seq_t *commands)
2929 struct sctp_chunk *chunk = arg;
2930 sctp_arg_t force = SCTP_NOFORCE();
2933 if (!sctp_vtag_verify(chunk, asoc)) {
2934 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2936 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2939 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2940 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2943 error = sctp_eat_data(asoc, chunk, commands );
2945 case SCTP_IERROR_NO_ERROR:
2947 case SCTP_IERROR_HIGH_TSN:
2948 case SCTP_IERROR_BAD_STREAM:
2949 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2950 goto discard_noforce;
2951 case SCTP_IERROR_DUP_TSN:
2952 case SCTP_IERROR_IGNORE_TSN:
2953 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2955 case SCTP_IERROR_NO_DATA:
2957 case SCTP_IERROR_PROTO_VIOLATION:
2958 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
2959 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
2964 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2965 force = SCTP_FORCE();
2967 if (asoc->autoclose) {
2968 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2969 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2972 /* If this is the last chunk in a packet, we need to count it
2973 * toward sack generation. Note that we need to SACK every
2974 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2975 * THEM. We elect to NOT generate SACK's if the chunk fails
2976 * the verification tag test.
2978 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2980 * The SCTP endpoint MUST always acknowledge the reception of
2981 * each valid DATA chunk.
2983 * The guidelines on delayed acknowledgement algorithm
2984 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2985 * Specifically, an acknowledgement SHOULD be generated for at
2986 * least every second packet (not every second DATA chunk)
2987 * received, and SHOULD be generated within 200 ms of the
2988 * arrival of any unacknowledged DATA chunk. In some
2989 * situations it may be beneficial for an SCTP transmitter to
2990 * be more conservative than the algorithms detailed in this
2991 * document allow. However, an SCTP transmitter MUST NOT be
2992 * more aggressive than the following algorithms allow.
2994 if (chunk->end_of_packet)
2995 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
2997 return SCTP_DISPOSITION_CONSUME;
3000 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3002 * When a packet arrives with duplicate DATA chunk(s) and with
3003 * no new DATA chunk(s), the endpoint MUST immediately send a
3004 * SACK with no delay. If a packet arrives with duplicate
3005 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3006 * MAY immediately send a SACK. Normally receipt of duplicate
3007 * DATA chunks will occur when the original SACK chunk was lost
3008 * and the peer's RTO has expired. The duplicate TSN number(s)
3009 * SHOULD be reported in the SACK as duplicate.
3011 /* In our case, we split the MAY SACK advice up whether or not
3012 * the last chunk is a duplicate.'
3014 if (chunk->end_of_packet)
3015 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3016 return SCTP_DISPOSITION_DISCARD;