1 /* SCTP kernel reference 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 file is part of the SCTP kernel reference Implementation
10 * This is part of the SCTP Linux Kernel Reference Implementation.
12 * These are the state functions for the state machine.
14 * The SCTP reference implementation is free software;
15 * you can redistribute it and/or modify it under the terms of
16 * the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
20 * The SCTP reference implementation is distributed in the hope that it
21 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
22 * ************************
23 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
24 * See the GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with GNU CC; see the file COPYING. If not, write to
28 * the Free Software Foundation, 59 Temple Place - Suite 330,
29 * Boston, MA 02111-1307, USA.
31 * Please send any bug reports or fixes you make to the
33 * lksctp developers <lksctp-developers@lists.sourceforge.net>
35 * Or submit a bug report through the following website:
36 * http://www.sf.net/projects/lksctp
38 * Written or modified by:
39 * La Monte H.P. Yarroll <piggy@acm.org>
40 * Karl Knutson <karl@athena.chicago.il.us>
41 * Mathew Kotowsky <kotowsky@sctp.org>
42 * Sridhar Samudrala <samudrala@us.ibm.com>
43 * Jon Grimm <jgrimm@us.ibm.com>
44 * Hui Huang <hui.huang@nokia.com>
45 * Dajiang Zhang <dajiang.zhang@nokia.com>
46 * Daisy Chang <daisyc@us.ibm.com>
47 * Ardelle Fan <ardelle.fan@intel.com>
48 * Ryan Layer <rmlayer@us.ibm.com>
49 * Kevin Gao <kevin.gao@intel.com>
51 * Any bugs reported given to us we will try to fix... any fixes shared will
52 * be incorporated into the next SCTP release.
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>
62 #include <net/inet_ecn.h>
63 #include <linux/skbuff.h>
64 #include <net/sctp/sctp.h>
65 #include <net/sctp/sm.h>
66 #include <net/sctp/structs.h>
68 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
69 const struct sctp_association *asoc,
70 struct sctp_chunk *chunk,
73 static int sctp_eat_data(const struct sctp_association *asoc,
74 struct sctp_chunk *chunk,
75 sctp_cmd_seq_t *commands);
76 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk);
78 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
79 const struct sctp_association *asoc,
80 const struct sctp_chunk *chunk,
81 sctp_cmd_seq_t *commands,
82 struct sctp_chunk *err_chunk);
83 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
84 const struct sctp_association *asoc,
85 const sctp_subtype_t type,
87 sctp_cmd_seq_t *commands);
88 static sctp_disposition_t sctp_sf_shut_8_4_5(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 struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
95 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
96 __be16 error, int sk_err,
97 const struct sctp_association *asoc,
98 struct sctp_transport *transport);
100 static sctp_disposition_t sctp_sf_violation_chunklen(
101 const struct sctp_endpoint *ep,
102 const struct sctp_association *asoc,
103 const sctp_subtype_t type,
105 sctp_cmd_seq_t *commands);
107 /* Small helper function that checks if the chunk length
108 * is of the appropriate length. The 'required_length' argument
109 * is set to be the size of a specific chunk we are testing.
110 * Return Values: 1 = Valid length
115 sctp_chunk_length_valid(struct sctp_chunk *chunk,
116 __u16 required_length)
118 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
120 if (unlikely(chunk_length < required_length))
126 /**********************************************************
127 * These are the state functions for handling chunk events.
128 **********************************************************/
131 * Process the final SHUTDOWN COMPLETE.
133 * Section: 4 (C) (diagram), 9.2
134 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
135 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
136 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
137 * should stop the T2-shutdown timer and remove all knowledge of the
138 * association (and thus the association enters the CLOSED state).
140 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
141 * C) Rules for packet carrying SHUTDOWN COMPLETE:
143 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
144 * if the Verification Tag field of the packet matches its own tag and
145 * the T bit is not set
147 * it is set to its peer's tag and the T bit is set in the Chunk
149 * Otherwise, the receiver MUST silently discard the packet
150 * and take no further action. An endpoint MUST ignore the
151 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
154 * (endpoint, asoc, chunk)
157 * (asoc, reply_msg, msg_up, timers, counters)
159 * The return value is the disposition of the chunk.
161 sctp_disposition_t sctp_sf_do_4_C(const struct sctp_endpoint *ep,
162 const struct sctp_association *asoc,
163 const sctp_subtype_t type,
165 sctp_cmd_seq_t *commands)
167 struct sctp_chunk *chunk = arg;
168 struct sctp_ulpevent *ev;
170 /* RFC 2960 6.10 Bundling
172 * An endpoint MUST NOT bundle INIT, INIT ACK or
173 * SHUTDOWN COMPLETE with any other chunks.
175 if (!chunk->singleton)
176 return SCTP_DISPOSITION_VIOLATION;
178 if (!sctp_vtag_verify_either(chunk, asoc))
179 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
181 /* RFC 2960 10.2 SCTP-to-ULP
183 * H) SHUTDOWN COMPLETE notification
185 * When SCTP completes the shutdown procedures (section 9.2) this
186 * notification is passed to the upper layer.
188 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
189 0, 0, 0, GFP_ATOMIC);
191 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
194 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
195 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
196 * not the chunk should be discarded. If the endpoint is in
197 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
198 * T2-shutdown timer and remove all knowledge of the
199 * association (and thus the association enters the CLOSED
202 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
203 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
205 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
206 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
208 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
209 SCTP_STATE(SCTP_STATE_CLOSED));
211 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
212 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
214 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
216 return SCTP_DISPOSITION_DELETE_TCB;
220 * Respond to a normal INIT chunk.
221 * We are the side that is being asked for an association.
223 * Section: 5.1 Normal Establishment of an Association, B
224 * B) "Z" shall respond immediately with an INIT ACK chunk. The
225 * destination IP address of the INIT ACK MUST be set to the source
226 * IP address of the INIT to which this INIT ACK is responding. In
227 * the response, besides filling in other parameters, "Z" must set the
228 * Verification Tag field to Tag_A, and also provide its own
229 * Verification Tag (Tag_Z) in the Initiate Tag field.
231 * Verification Tag: Must be 0.
234 * (endpoint, asoc, chunk)
237 * (asoc, reply_msg, msg_up, timers, counters)
239 * The return value is the disposition of the chunk.
241 sctp_disposition_t sctp_sf_do_5_1B_init(const struct sctp_endpoint *ep,
242 const struct sctp_association *asoc,
243 const sctp_subtype_t type,
245 sctp_cmd_seq_t *commands)
247 struct sctp_chunk *chunk = arg;
248 struct sctp_chunk *repl;
249 struct sctp_association *new_asoc;
250 struct sctp_chunk *err_chunk;
251 struct sctp_packet *packet;
252 sctp_unrecognized_param_t *unk_param;
257 * An endpoint MUST NOT bundle INIT, INIT ACK or
258 * SHUTDOWN COMPLETE with any other chunks.
261 * Furthermore, we require that the receiver of an INIT chunk MUST
262 * enforce these rules by silently discarding an arriving packet
263 * with an INIT chunk that is bundled with other chunks.
265 if (!chunk->singleton)
266 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
268 /* If the packet is an OOTB packet which is temporarily on the
269 * control endpoint, respond with an ABORT.
271 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
272 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
275 /* If the endpoint is not listening or if the number of associations
276 * on the TCP-style socket exceed the max backlog, respond with an
279 if (!sctp_sstate(sk, LISTENING) ||
280 (sctp_style(sk, TCP) &&
281 sk_acceptq_is_full(sk)))
282 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
284 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
287 if (chunk->sctp_hdr->vtag != 0)
288 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
290 /* Make sure that the INIT chunk has a valid length.
291 * Normally, this would cause an ABORT with a Protocol Violation
292 * error, but since we don't have an association, we'll
293 * just discard the packet.
295 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
296 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
298 /* Verify the INIT chunk before processing it. */
300 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
301 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
303 /* This chunk contains fatal error. It is to be discarded.
304 * Send an ABORT, with causes if there is any.
307 packet = sctp_abort_pkt_new(ep, asoc, arg,
308 (__u8 *)(err_chunk->chunk_hdr) +
309 sizeof(sctp_chunkhdr_t),
310 ntohs(err_chunk->chunk_hdr->length) -
311 sizeof(sctp_chunkhdr_t));
313 sctp_chunk_free(err_chunk);
316 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
317 SCTP_PACKET(packet));
318 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
319 return SCTP_DISPOSITION_CONSUME;
321 return SCTP_DISPOSITION_NOMEM;
324 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
329 /* Grab the INIT header. */
330 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
332 /* Tag the variable length parameters. */
333 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
335 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
339 /* The call, sctp_process_init(), can fail on memory allocation. */
340 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
342 (sctp_init_chunk_t *)chunk->chunk_hdr,
346 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
348 /* If there are errors need to be reported for unknown parameters,
349 * make sure to reserve enough room in the INIT ACK for them.
353 len = ntohs(err_chunk->chunk_hdr->length) -
354 sizeof(sctp_chunkhdr_t);
356 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
359 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
363 /* If there are errors need to be reported for unknown parameters,
364 * include them in the outgoing INIT ACK as "Unrecognized parameter"
368 /* Get the "Unrecognized parameter" parameter(s) out of the
369 * ERROR chunk generated by sctp_verify_init(). Since the
370 * error cause code for "unknown parameter" and the
371 * "Unrecognized parameter" type is the same, we can
372 * construct the parameters in INIT ACK by copying the
375 unk_param = (sctp_unrecognized_param_t *)
376 ((__u8 *)(err_chunk->chunk_hdr) +
377 sizeof(sctp_chunkhdr_t));
378 /* Replace the cause code with the "Unrecognized parameter"
381 sctp_addto_chunk(repl, len, unk_param);
382 sctp_chunk_free(err_chunk);
385 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
387 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
390 * Note: After sending out INIT ACK with the State Cookie parameter,
391 * "Z" MUST NOT allocate any resources, nor keep any states for the
392 * new association. Otherwise, "Z" will be vulnerable to resource
395 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
397 return SCTP_DISPOSITION_DELETE_TCB;
400 sctp_association_free(new_asoc);
403 sctp_chunk_free(err_chunk);
404 return SCTP_DISPOSITION_NOMEM;
408 * Respond to a normal INIT ACK chunk.
409 * We are the side that is initiating the association.
411 * Section: 5.1 Normal Establishment of an Association, C
412 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
413 * timer and leave COOKIE-WAIT state. "A" shall then send the State
414 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
415 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
417 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
418 * DATA chunks, but it MUST be the first chunk in the packet and
419 * until the COOKIE ACK is returned the sender MUST NOT send any
420 * other packets to the peer.
422 * Verification Tag: 3.3.3
423 * If the value of the Initiate Tag in a received INIT ACK chunk is
424 * found to be 0, the receiver MUST treat it as an error and close the
425 * association by transmitting an ABORT.
428 * (endpoint, asoc, chunk)
431 * (asoc, reply_msg, msg_up, timers, counters)
433 * The return value is the disposition of the chunk.
435 sctp_disposition_t sctp_sf_do_5_1C_ack(const struct sctp_endpoint *ep,
436 const struct sctp_association *asoc,
437 const sctp_subtype_t type,
439 sctp_cmd_seq_t *commands)
441 struct sctp_chunk *chunk = arg;
442 sctp_init_chunk_t *initchunk;
443 struct sctp_chunk *err_chunk;
444 struct sctp_packet *packet;
447 if (!sctp_vtag_verify(chunk, asoc))
448 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
450 /* Make sure that the INIT-ACK chunk has a valid length */
451 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
452 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
455 * An endpoint MUST NOT bundle INIT, INIT ACK or
456 * SHUTDOWN COMPLETE with any other chunks.
458 if (!chunk->singleton)
459 return SCTP_DISPOSITION_VIOLATION;
461 /* Grab the INIT header. */
462 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
464 /* Verify the INIT chunk before processing it. */
466 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
467 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
470 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
472 /* This chunk contains fatal error. It is to be discarded.
473 * Send an ABORT, with causes if there is any.
476 packet = sctp_abort_pkt_new(ep, asoc, arg,
477 (__u8 *)(err_chunk->chunk_hdr) +
478 sizeof(sctp_chunkhdr_t),
479 ntohs(err_chunk->chunk_hdr->length) -
480 sizeof(sctp_chunkhdr_t));
482 sctp_chunk_free(err_chunk);
485 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
486 SCTP_PACKET(packet));
487 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
488 error = SCTP_ERROR_INV_PARAM;
490 error = SCTP_ERROR_NO_RESOURCE;
493 sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
494 error = SCTP_ERROR_INV_PARAM;
496 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED,
497 asoc, chunk->transport);
500 /* Tag the variable length parameters. Note that we never
501 * convert the parameters in an INIT chunk.
503 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
505 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
507 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
508 SCTP_PEER_INIT(initchunk));
510 /* Reset init error count upon receipt of INIT-ACK. */
511 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
513 /* 5.1 C) "A" shall stop the T1-init timer and leave
514 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
515 * timer, and enter the COOKIE-ECHOED state.
517 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
518 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
519 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
520 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
521 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
522 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
524 /* 5.1 C) "A" shall then send the State Cookie received in the
525 * INIT ACK chunk in a COOKIE ECHO chunk, ...
527 /* If there is any errors to report, send the ERROR chunk generated
528 * for unknown parameters as well.
530 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
531 SCTP_CHUNK(err_chunk));
533 return SCTP_DISPOSITION_CONSUME;
536 return SCTP_DISPOSITION_NOMEM;
540 * Respond to a normal COOKIE ECHO chunk.
541 * We are the side that is being asked for an association.
543 * Section: 5.1 Normal Establishment of an Association, D
544 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
545 * with a COOKIE ACK chunk after building a TCB and moving to
546 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
547 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
548 * chunk MUST be the first chunk in the packet.
550 * IMPLEMENTATION NOTE: An implementation may choose to send the
551 * Communication Up notification to the SCTP user upon reception
552 * of a valid COOKIE ECHO chunk.
554 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
555 * D) Rules for packet carrying a COOKIE ECHO
557 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
558 * Initial Tag received in the INIT ACK.
560 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
563 * (endpoint, asoc, chunk)
566 * (asoc, reply_msg, msg_up, timers, counters)
568 * The return value is the disposition of the chunk.
570 sctp_disposition_t sctp_sf_do_5_1D_ce(const struct sctp_endpoint *ep,
571 const struct sctp_association *asoc,
572 const sctp_subtype_t type, void *arg,
573 sctp_cmd_seq_t *commands)
575 struct sctp_chunk *chunk = arg;
576 struct sctp_association *new_asoc;
577 sctp_init_chunk_t *peer_init;
578 struct sctp_chunk *repl;
579 struct sctp_ulpevent *ev, *ai_ev = NULL;
581 struct sctp_chunk *err_chk_p;
583 /* If the packet is an OOTB packet which is temporarily on the
584 * control endpoint, respond with an ABORT.
586 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
587 return sctp_sf_ootb(ep, asoc, type, arg, commands);
589 /* Make sure that the COOKIE_ECHO chunk has a valid length.
590 * In this case, we check that we have enough for at least a
591 * chunk header. More detailed verification is done
592 * in sctp_unpack_cookie().
594 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
595 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
597 /* "Decode" the chunk. We have no optional parameters so we
600 chunk->subh.cookie_hdr =
601 (struct sctp_signed_cookie *)chunk->skb->data;
602 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
603 sizeof(sctp_chunkhdr_t)))
606 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
607 * "Z" will reply with a COOKIE ACK chunk after building a TCB
608 * and moving to the ESTABLISHED state.
610 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
614 * If the re-build failed, what is the proper error path
617 * [We should abort the association. --piggy]
620 /* FIXME: Several errors are possible. A bad cookie should
621 * be silently discarded, but think about logging it too.
624 case -SCTP_IERROR_NOMEM:
627 case -SCTP_IERROR_STALE_COOKIE:
628 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
630 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
632 case -SCTP_IERROR_BAD_SIG:
634 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
639 /* Delay state machine commands until later.
641 * Re-build the bind address for the association is done in
642 * the sctp_unpack_cookie() already.
644 /* This is a brand-new association, so these are not yet side
645 * effects--it is safe to run them here.
647 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
649 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
650 &chunk->subh.cookie_hdr->c.peer_addr,
651 peer_init, GFP_ATOMIC))
654 repl = sctp_make_cookie_ack(new_asoc, chunk);
658 /* RFC 2960 5.1 Normal Establishment of an Association
660 * D) IMPLEMENTATION NOTE: An implementation may choose to
661 * send the Communication Up notification to the SCTP user
662 * upon reception of a valid COOKIE ECHO chunk.
664 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
665 new_asoc->c.sinit_num_ostreams,
666 new_asoc->c.sinit_max_instreams,
671 /* Sockets API Draft Section 5.3.1.6
672 * When a peer sends a Adaptation Layer Indication parameter , SCTP
673 * delivers this notification to inform the application that of the
674 * peers requested adaptation layer.
676 if (new_asoc->peer.adaptation_ind) {
677 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
683 /* Add all the state machine commands now since we've created
684 * everything. This way we don't introduce memory corruptions
685 * during side-effect processing and correclty count established
688 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
689 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
690 SCTP_STATE(SCTP_STATE_ESTABLISHED));
691 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
692 SCTP_INC_STATS(SCTP_MIB_PASSIVEESTABS);
693 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
695 if (new_asoc->autoclose)
696 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
697 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
699 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
701 /* This will send the COOKIE ACK */
702 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
704 /* Queue the ASSOC_CHANGE event */
705 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
707 /* Send up the Adaptation Layer Indication event */
709 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
710 SCTP_ULPEVENT(ai_ev));
712 return SCTP_DISPOSITION_CONSUME;
715 sctp_ulpevent_free(ev);
717 sctp_chunk_free(repl);
719 sctp_association_free(new_asoc);
721 return SCTP_DISPOSITION_NOMEM;
725 * Respond to a normal COOKIE ACK chunk.
726 * We are the side that is being asked for an association.
728 * RFC 2960 5.1 Normal Establishment of an Association
730 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
731 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
732 * timer. It may also notify its ULP about the successful
733 * establishment of the association with a Communication Up
734 * notification (see Section 10).
738 * (endpoint, asoc, chunk)
741 * (asoc, reply_msg, msg_up, timers, counters)
743 * The return value is the disposition of the chunk.
745 sctp_disposition_t sctp_sf_do_5_1E_ca(const struct sctp_endpoint *ep,
746 const struct sctp_association *asoc,
747 const sctp_subtype_t type, void *arg,
748 sctp_cmd_seq_t *commands)
750 struct sctp_chunk *chunk = arg;
751 struct sctp_ulpevent *ev;
753 if (!sctp_vtag_verify(chunk, asoc))
754 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
756 /* Verify that the chunk length for the COOKIE-ACK is OK.
757 * If we don't do this, any bundled chunks may be junked.
759 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
760 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
763 /* Reset init error count upon receipt of COOKIE-ACK,
764 * to avoid problems with the managemement of this
765 * counter in stale cookie situations when a transition back
766 * from the COOKIE-ECHOED state to the COOKIE-WAIT
767 * state is performed.
769 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
771 /* RFC 2960 5.1 Normal Establishment of an Association
773 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
774 * from the COOKIE-ECHOED state to the ESTABLISHED state,
775 * stopping the T1-cookie timer.
777 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
778 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
779 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
780 SCTP_STATE(SCTP_STATE_ESTABLISHED));
781 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
782 SCTP_INC_STATS(SCTP_MIB_ACTIVEESTABS);
783 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
785 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
786 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
787 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
789 /* It may also notify its ULP about the successful
790 * establishment of the association with a Communication Up
791 * notification (see Section 10).
793 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
794 0, asoc->c.sinit_num_ostreams,
795 asoc->c.sinit_max_instreams,
801 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
803 /* Sockets API Draft Section 5.3.1.6
804 * When a peer sends a Adaptation Layer Indication parameter , SCTP
805 * delivers this notification to inform the application that of the
806 * peers requested adaptation layer.
808 if (asoc->peer.adaptation_ind) {
809 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
813 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
817 return SCTP_DISPOSITION_CONSUME;
819 return SCTP_DISPOSITION_NOMEM;
822 /* Generate and sendout a heartbeat packet. */
823 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
824 const struct sctp_association *asoc,
825 const sctp_subtype_t type,
827 sctp_cmd_seq_t *commands)
829 struct sctp_transport *transport = (struct sctp_transport *) arg;
830 struct sctp_chunk *reply;
831 sctp_sender_hb_info_t hbinfo;
834 hbinfo.param_hdr.type = SCTP_PARAM_HEARTBEAT_INFO;
835 hbinfo.param_hdr.length = htons(sizeof(sctp_sender_hb_info_t));
836 hbinfo.daddr = transport->ipaddr;
837 hbinfo.sent_at = jiffies;
838 hbinfo.hb_nonce = transport->hb_nonce;
840 /* Send a heartbeat to our peer. */
841 paylen = sizeof(sctp_sender_hb_info_t);
842 reply = sctp_make_heartbeat(asoc, transport, &hbinfo, paylen);
844 return SCTP_DISPOSITION_NOMEM;
846 /* Set rto_pending indicating that an RTT measurement
847 * is started with this heartbeat chunk.
849 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
850 SCTP_TRANSPORT(transport));
852 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
853 return SCTP_DISPOSITION_CONSUME;
856 /* Generate a HEARTBEAT packet on the given transport. */
857 sctp_disposition_t sctp_sf_sendbeat_8_3(const struct sctp_endpoint *ep,
858 const struct sctp_association *asoc,
859 const sctp_subtype_t type,
861 sctp_cmd_seq_t *commands)
863 struct sctp_transport *transport = (struct sctp_transport *) arg;
865 if (asoc->overall_error_count >= asoc->max_retrans) {
866 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
867 SCTP_ERROR(ETIMEDOUT));
868 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
869 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
870 SCTP_PERR(SCTP_ERROR_NO_ERROR));
871 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
872 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
873 return SCTP_DISPOSITION_DELETE_TCB;
877 * The Sender-specific Heartbeat Info field should normally include
878 * information about the sender's current time when this HEARTBEAT
879 * chunk is sent and the destination transport address to which this
880 * HEARTBEAT is sent (see Section 8.3).
883 if (transport->param_flags & SPP_HB_ENABLE) {
884 if (SCTP_DISPOSITION_NOMEM ==
885 sctp_sf_heartbeat(ep, asoc, type, arg,
887 return SCTP_DISPOSITION_NOMEM;
888 /* Set transport error counter and association error counter
889 * when sending heartbeat.
891 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_RESET,
892 SCTP_TRANSPORT(transport));
894 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
895 SCTP_TRANSPORT(transport));
897 return SCTP_DISPOSITION_CONSUME;
901 * Process an heartbeat request.
903 * Section: 8.3 Path Heartbeat
904 * The receiver of the HEARTBEAT should immediately respond with a
905 * HEARTBEAT ACK that contains the Heartbeat Information field copied
906 * from the received HEARTBEAT chunk.
908 * Verification Tag: 8.5 Verification Tag [Normal verification]
909 * When receiving an SCTP packet, the endpoint MUST ensure that the
910 * value in the Verification Tag field of the received SCTP packet
911 * matches its own Tag. If the received Verification Tag value does not
912 * match the receiver's own tag value, the receiver shall silently
913 * discard the packet and shall not process it any further except for
914 * those cases listed in Section 8.5.1 below.
917 * (endpoint, asoc, chunk)
920 * (asoc, reply_msg, msg_up, timers, counters)
922 * The return value is the disposition of the chunk.
924 sctp_disposition_t sctp_sf_beat_8_3(const struct sctp_endpoint *ep,
925 const struct sctp_association *asoc,
926 const sctp_subtype_t type,
928 sctp_cmd_seq_t *commands)
930 struct sctp_chunk *chunk = arg;
931 struct sctp_chunk *reply;
934 if (!sctp_vtag_verify(chunk, asoc))
935 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
937 /* Make sure that the HEARTBEAT chunk has a valid length. */
938 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
939 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
942 /* 8.3 The receiver of the HEARTBEAT should immediately
943 * respond with a HEARTBEAT ACK that contains the Heartbeat
944 * Information field copied from the received HEARTBEAT chunk.
946 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
947 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
948 if (!pskb_pull(chunk->skb, paylen))
951 reply = sctp_make_heartbeat_ack(asoc, chunk,
952 chunk->subh.hb_hdr, paylen);
956 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
957 return SCTP_DISPOSITION_CONSUME;
960 return SCTP_DISPOSITION_NOMEM;
964 * Process the returning HEARTBEAT ACK.
966 * Section: 8.3 Path Heartbeat
967 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
968 * should clear the error counter of the destination transport
969 * address to which the HEARTBEAT was sent, and mark the destination
970 * transport address as active if it is not so marked. The endpoint may
971 * optionally report to the upper layer when an inactive destination
972 * address is marked as active due to the reception of the latest
973 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
974 * clear the association overall error count as well (as defined
977 * The receiver of the HEARTBEAT ACK should also perform an RTT
978 * measurement for that destination transport address using the time
979 * value carried in the HEARTBEAT ACK chunk.
981 * Verification Tag: 8.5 Verification Tag [Normal verification]
984 * (endpoint, asoc, chunk)
987 * (asoc, reply_msg, msg_up, timers, counters)
989 * The return value is the disposition of the chunk.
991 sctp_disposition_t sctp_sf_backbeat_8_3(const struct sctp_endpoint *ep,
992 const struct sctp_association *asoc,
993 const sctp_subtype_t type,
995 sctp_cmd_seq_t *commands)
997 struct sctp_chunk *chunk = arg;
998 union sctp_addr from_addr;
999 struct sctp_transport *link;
1000 sctp_sender_hb_info_t *hbinfo;
1001 unsigned long max_interval;
1003 if (!sctp_vtag_verify(chunk, asoc))
1004 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1006 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1007 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1008 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1011 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1012 /* Make sure that the length of the parameter is what we expect */
1013 if (ntohs(hbinfo->param_hdr.length) !=
1014 sizeof(sctp_sender_hb_info_t)) {
1015 return SCTP_DISPOSITION_DISCARD;
1018 from_addr = hbinfo->daddr;
1019 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1021 /* This should never happen, but lets log it if so. */
1022 if (unlikely(!link)) {
1023 if (from_addr.sa.sa_family == AF_INET6) {
1025 "%s association %p could not find address "
1029 NIP6(from_addr.v6.sin6_addr));
1032 "%s association %p could not find address "
1036 NIPQUAD(from_addr.v4.sin_addr.s_addr));
1038 return SCTP_DISPOSITION_DISCARD;
1041 /* Validate the 64-bit random nonce. */
1042 if (hbinfo->hb_nonce != link->hb_nonce)
1043 return SCTP_DISPOSITION_DISCARD;
1045 max_interval = link->hbinterval + link->rto;
1047 /* Check if the timestamp looks valid. */
1048 if (time_after(hbinfo->sent_at, jiffies) ||
1049 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1050 SCTP_DEBUG_PRINTK("%s: HEARTBEAT ACK with invalid timestamp"
1051 "received for transport: %p\n",
1052 __FUNCTION__, link);
1053 return SCTP_DISPOSITION_DISCARD;
1056 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1057 * the HEARTBEAT should clear the error counter of the
1058 * destination transport address to which the HEARTBEAT was
1059 * sent and mark the destination transport address as active if
1060 * it is not so marked.
1062 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1064 return SCTP_DISPOSITION_CONSUME;
1067 /* Helper function to send out an abort for the restart
1070 static int sctp_sf_send_restart_abort(union sctp_addr *ssa,
1071 struct sctp_chunk *init,
1072 sctp_cmd_seq_t *commands)
1075 struct sctp_packet *pkt;
1076 union sctp_addr_param *addrparm;
1077 struct sctp_errhdr *errhdr;
1078 struct sctp_endpoint *ep;
1079 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1080 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1082 /* Build the error on the stack. We are way to malloc crazy
1083 * throughout the code today.
1085 errhdr = (struct sctp_errhdr *)buffer;
1086 addrparm = (union sctp_addr_param *)errhdr->variable;
1088 /* Copy into a parm format. */
1089 len = af->to_addr_param(ssa, addrparm);
1090 len += sizeof(sctp_errhdr_t);
1092 errhdr->cause = SCTP_ERROR_RESTART;
1093 errhdr->length = htons(len);
1095 /* Assign to the control socket. */
1096 ep = sctp_sk((sctp_get_ctl_sock()))->ep;
1098 /* Association is NULL since this may be a restart attack and we
1099 * want to send back the attacker's vtag.
1101 pkt = sctp_abort_pkt_new(ep, NULL, init, errhdr, len);
1105 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1107 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1109 /* Discard the rest of the inbound packet. */
1110 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1113 /* Even if there is no memory, treat as a failure so
1114 * the packet will get dropped.
1119 /* A restart is occurring, check to make sure no new addresses
1120 * are being added as we may be under a takeover attack.
1122 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1123 const struct sctp_association *asoc,
1124 struct sctp_chunk *init,
1125 sctp_cmd_seq_t *commands)
1127 struct sctp_transport *new_addr, *addr;
1128 struct list_head *pos, *pos2;
1131 /* Implementor's Guide - Sectin 5.2.2
1133 * Before responding the endpoint MUST check to see if the
1134 * unexpected INIT adds new addresses to the association. If new
1135 * addresses are added to the association, the endpoint MUST respond
1139 /* Search through all current addresses and make sure
1140 * we aren't adding any new ones.
1145 list_for_each(pos, &new_asoc->peer.transport_addr_list) {
1146 new_addr = list_entry(pos, struct sctp_transport, transports);
1148 list_for_each(pos2, &asoc->peer.transport_addr_list) {
1149 addr = list_entry(pos2, struct sctp_transport,
1151 if (sctp_cmp_addr_exact(&new_addr->ipaddr,
1161 /* If a new address was added, ABORT the sender. */
1162 if (!found && new_addr) {
1163 sctp_sf_send_restart_abort(&new_addr->ipaddr, init, commands);
1166 /* Return success if all addresses were found. */
1170 /* Populate the verification/tie tags based on overlapping INIT
1173 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1175 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1176 const struct sctp_association *asoc)
1178 switch (asoc->state) {
1180 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1182 case SCTP_STATE_COOKIE_WAIT:
1183 new_asoc->c.my_vtag = asoc->c.my_vtag;
1184 new_asoc->c.my_ttag = asoc->c.my_vtag;
1185 new_asoc->c.peer_ttag = 0;
1188 case SCTP_STATE_COOKIE_ECHOED:
1189 new_asoc->c.my_vtag = asoc->c.my_vtag;
1190 new_asoc->c.my_ttag = asoc->c.my_vtag;
1191 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1194 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1195 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1198 new_asoc->c.my_ttag = asoc->c.my_vtag;
1199 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1203 /* Other parameters for the endpoint SHOULD be copied from the
1204 * existing parameters of the association (e.g. number of
1205 * outbound streams) into the INIT ACK and cookie.
1207 new_asoc->rwnd = asoc->rwnd;
1208 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1209 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1210 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1214 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1217 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1219 * Returns value representing action to be taken. These action values
1220 * correspond to Action/Description values in RFC 2960, Table 2.
1222 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1223 const struct sctp_association *asoc)
1225 /* In this case, the peer may have restarted. */
1226 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1227 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1228 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1229 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1232 /* Collision case B. */
1233 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1234 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1235 (0 == asoc->c.peer_vtag))) {
1239 /* Collision case D. */
1240 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1241 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1244 /* Collision case C. */
1245 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1246 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1247 (0 == new_asoc->c.my_ttag) &&
1248 (0 == new_asoc->c.peer_ttag))
1251 /* No match to any of the special cases; discard this packet. */
1255 /* Common helper routine for both duplicate and simulataneous INIT
1258 static sctp_disposition_t sctp_sf_do_unexpected_init(
1259 const struct sctp_endpoint *ep,
1260 const struct sctp_association *asoc,
1261 const sctp_subtype_t type,
1262 void *arg, sctp_cmd_seq_t *commands)
1264 sctp_disposition_t retval;
1265 struct sctp_chunk *chunk = arg;
1266 struct sctp_chunk *repl;
1267 struct sctp_association *new_asoc;
1268 struct sctp_chunk *err_chunk;
1269 struct sctp_packet *packet;
1270 sctp_unrecognized_param_t *unk_param;
1274 * An endpoint MUST NOT bundle INIT, INIT ACK or
1275 * SHUTDOWN COMPLETE with any other chunks.
1278 * Furthermore, we require that the receiver of an INIT chunk MUST
1279 * enforce these rules by silently discarding an arriving packet
1280 * with an INIT chunk that is bundled with other chunks.
1282 if (!chunk->singleton)
1283 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1285 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1288 if (chunk->sctp_hdr->vtag != 0)
1289 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
1291 /* Make sure that the INIT chunk has a valid length.
1292 * In this case, we generate a protocol violation since we have
1293 * an association established.
1295 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1296 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1298 /* Grab the INIT header. */
1299 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1301 /* Tag the variable length parameters. */
1302 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1304 /* Verify the INIT chunk before processing it. */
1306 if (!sctp_verify_init(asoc, chunk->chunk_hdr->type,
1307 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1309 /* This chunk contains fatal error. It is to be discarded.
1310 * Send an ABORT, with causes if there is any.
1313 packet = sctp_abort_pkt_new(ep, asoc, arg,
1314 (__u8 *)(err_chunk->chunk_hdr) +
1315 sizeof(sctp_chunkhdr_t),
1316 ntohs(err_chunk->chunk_hdr->length) -
1317 sizeof(sctp_chunkhdr_t));
1320 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1321 SCTP_PACKET(packet));
1322 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
1323 retval = SCTP_DISPOSITION_CONSUME;
1325 retval = SCTP_DISPOSITION_NOMEM;
1329 return sctp_sf_tabort_8_4_8(ep, asoc, type, arg,
1335 * Other parameters for the endpoint SHOULD be copied from the
1336 * existing parameters of the association (e.g. number of
1337 * outbound streams) into the INIT ACK and cookie.
1338 * FIXME: We are copying parameters from the endpoint not the
1341 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1345 /* In the outbound INIT ACK the endpoint MUST copy its current
1346 * Verification Tag and Peers Verification tag into a reserved
1347 * place (local tie-tag and per tie-tag) within the state cookie.
1349 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1351 (sctp_init_chunk_t *)chunk->chunk_hdr,
1355 /* Make sure no new addresses are being added during the
1356 * restart. Do not do this check for COOKIE-WAIT state,
1357 * since there are no peer addresses to check against.
1358 * Upon return an ABORT will have been sent if needed.
1360 if (!sctp_state(asoc, COOKIE_WAIT)) {
1361 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1363 retval = SCTP_DISPOSITION_CONSUME;
1368 sctp_tietags_populate(new_asoc, asoc);
1370 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1372 /* If there are errors need to be reported for unknown parameters,
1373 * make sure to reserve enough room in the INIT ACK for them.
1377 len = ntohs(err_chunk->chunk_hdr->length) -
1378 sizeof(sctp_chunkhdr_t);
1381 if (sctp_assoc_set_bind_addr_from_ep(new_asoc, GFP_ATOMIC) < 0)
1384 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1388 /* If there are errors need to be reported for unknown parameters,
1389 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1393 /* Get the "Unrecognized parameter" parameter(s) out of the
1394 * ERROR chunk generated by sctp_verify_init(). Since the
1395 * error cause code for "unknown parameter" and the
1396 * "Unrecognized parameter" type is the same, we can
1397 * construct the parameters in INIT ACK by copying the
1398 * ERROR causes over.
1400 unk_param = (sctp_unrecognized_param_t *)
1401 ((__u8 *)(err_chunk->chunk_hdr) +
1402 sizeof(sctp_chunkhdr_t));
1403 /* Replace the cause code with the "Unrecognized parameter"
1406 sctp_addto_chunk(repl, len, unk_param);
1409 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1410 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1413 * Note: After sending out INIT ACK with the State Cookie parameter,
1414 * "Z" MUST NOT allocate any resources for this new association.
1415 * Otherwise, "Z" will be vulnerable to resource attacks.
1417 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1418 retval = SCTP_DISPOSITION_CONSUME;
1423 retval = SCTP_DISPOSITION_NOMEM;
1426 sctp_association_free(new_asoc);
1429 sctp_chunk_free(err_chunk);
1434 * Handle simultanous INIT.
1435 * This means we started an INIT and then we got an INIT request from
1438 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1439 * This usually indicates an initialization collision, i.e., each
1440 * endpoint is attempting, at about the same time, to establish an
1441 * association with the other endpoint.
1443 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1444 * endpoint MUST respond with an INIT ACK using the same parameters it
1445 * sent in its original INIT chunk (including its Verification Tag,
1446 * unchanged). These original parameters are combined with those from the
1447 * newly received INIT chunk. The endpoint shall also generate a State
1448 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1449 * INIT to calculate the State Cookie.
1451 * After that, the endpoint MUST NOT change its state, the T1-init
1452 * timer shall be left running and the corresponding TCB MUST NOT be
1453 * destroyed. The normal procedures for handling State Cookies when
1454 * a TCB exists will resolve the duplicate INITs to a single association.
1456 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1457 * its Tie-Tags with the Tag information of itself and its peer (see
1458 * section 5.2.2 for a description of the Tie-Tags).
1460 * Verification Tag: Not explicit, but an INIT can not have a valid
1461 * verification tag, so we skip the check.
1464 * (endpoint, asoc, chunk)
1467 * (asoc, reply_msg, msg_up, timers, counters)
1469 * The return value is the disposition of the chunk.
1471 sctp_disposition_t sctp_sf_do_5_2_1_siminit(const struct sctp_endpoint *ep,
1472 const struct sctp_association *asoc,
1473 const sctp_subtype_t type,
1475 sctp_cmd_seq_t *commands)
1477 /* Call helper to do the real work for both simulataneous and
1478 * duplicate INIT chunk handling.
1480 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1484 * Handle duplicated INIT messages. These are usually delayed
1487 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1488 * COOKIE-ECHOED and COOKIE-WAIT
1490 * Unless otherwise stated, upon reception of an unexpected INIT for
1491 * this association, the endpoint shall generate an INIT ACK with a
1492 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1493 * current Verification Tag and peer's Verification Tag into a reserved
1494 * place within the state cookie. We shall refer to these locations as
1495 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1496 * containing this INIT ACK MUST carry a Verification Tag value equal to
1497 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1498 * MUST contain a new Initiation Tag (randomly generated see Section
1499 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1500 * existing parameters of the association (e.g. number of outbound
1501 * streams) into the INIT ACK and cookie.
1503 * After sending out the INIT ACK, the endpoint shall take no further
1504 * actions, i.e., the existing association, including its current state,
1505 * and the corresponding TCB MUST NOT be changed.
1507 * Note: Only when a TCB exists and the association is not in a COOKIE-
1508 * WAIT state are the Tie-Tags populated. For a normal association INIT
1509 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1510 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1511 * State Cookie are populated as specified in section 5.2.1.
1513 * Verification Tag: Not specified, but an INIT has no way of knowing
1514 * what the verification tag could be, so we ignore it.
1517 * (endpoint, asoc, chunk)
1520 * (asoc, reply_msg, msg_up, timers, counters)
1522 * The return value is the disposition of the chunk.
1524 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(const struct sctp_endpoint *ep,
1525 const struct sctp_association *asoc,
1526 const sctp_subtype_t type,
1528 sctp_cmd_seq_t *commands)
1530 /* Call helper to do the real work for both simulataneous and
1531 * duplicate INIT chunk handling.
1533 return sctp_sf_do_unexpected_init(ep, asoc, type, arg, commands);
1538 * Unexpected INIT-ACK handler.
1541 * If an INIT ACK received by an endpoint in any state other than the
1542 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1543 * An unexpected INIT ACK usually indicates the processing of an old or
1544 * duplicated INIT chunk.
1546 sctp_disposition_t sctp_sf_do_5_2_3_initack(const struct sctp_endpoint *ep,
1547 const struct sctp_association *asoc,
1548 const sctp_subtype_t type,
1549 void *arg, sctp_cmd_seq_t *commands)
1551 /* Per the above section, we'll discard the chunk if we have an
1552 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1554 if (ep == sctp_sk((sctp_get_ctl_sock()))->ep)
1555 return sctp_sf_ootb(ep, asoc, type, arg, commands);
1557 return sctp_sf_discard_chunk(ep, asoc, type, arg, commands);
1560 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1563 * A) In this case, the peer may have restarted.
1565 static sctp_disposition_t sctp_sf_do_dupcook_a(const struct sctp_endpoint *ep,
1566 const struct sctp_association *asoc,
1567 struct sctp_chunk *chunk,
1568 sctp_cmd_seq_t *commands,
1569 struct sctp_association *new_asoc)
1571 sctp_init_chunk_t *peer_init;
1572 struct sctp_ulpevent *ev;
1573 struct sctp_chunk *repl;
1574 struct sctp_chunk *err;
1575 sctp_disposition_t disposition;
1577 /* new_asoc is a brand-new association, so these are not yet
1578 * side effects--it is safe to run them here.
1580 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1582 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1583 sctp_source(chunk), peer_init,
1587 /* Make sure no new addresses are being added during the
1588 * restart. Though this is a pretty complicated attack
1589 * since you'd have to get inside the cookie.
1591 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1592 return SCTP_DISPOSITION_CONSUME;
1595 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1596 * the peer has restarted (Action A), it MUST NOT setup a new
1597 * association but instead resend the SHUTDOWN ACK and send an ERROR
1598 * chunk with a "Cookie Received while Shutting Down" error cause to
1601 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1602 disposition = sctp_sf_do_9_2_reshutack(ep, asoc,
1603 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1605 if (SCTP_DISPOSITION_NOMEM == disposition)
1608 err = sctp_make_op_error(asoc, chunk,
1609 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1612 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1615 return SCTP_DISPOSITION_CONSUME;
1618 /* For now, fail any unsent/unacked data. Consider the optional
1619 * choice of resending of this data.
1621 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1623 repl = sctp_make_cookie_ack(new_asoc, chunk);
1627 /* Report association restart to upper layer. */
1628 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1629 new_asoc->c.sinit_num_ostreams,
1630 new_asoc->c.sinit_max_instreams,
1635 /* Update the content of current association. */
1636 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1637 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1638 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1639 return SCTP_DISPOSITION_CONSUME;
1642 sctp_chunk_free(repl);
1644 return SCTP_DISPOSITION_NOMEM;
1647 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1650 * B) In this case, both sides may be attempting to start an association
1651 * at about the same time but the peer endpoint started its INIT
1652 * after responding to the local endpoint's INIT
1654 /* This case represents an initialization collision. */
1655 static sctp_disposition_t sctp_sf_do_dupcook_b(const struct sctp_endpoint *ep,
1656 const struct sctp_association *asoc,
1657 struct sctp_chunk *chunk,
1658 sctp_cmd_seq_t *commands,
1659 struct sctp_association *new_asoc)
1661 sctp_init_chunk_t *peer_init;
1662 struct sctp_ulpevent *ev;
1663 struct sctp_chunk *repl;
1665 /* new_asoc is a brand-new association, so these are not yet
1666 * side effects--it is safe to run them here.
1668 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1669 if (!sctp_process_init(new_asoc, chunk->chunk_hdr->type,
1670 sctp_source(chunk), peer_init,
1674 /* Update the content of current association. */
1675 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1676 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1677 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1678 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1679 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1681 repl = sctp_make_cookie_ack(new_asoc, chunk);
1685 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1686 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1688 /* RFC 2960 5.1 Normal Establishment of an Association
1690 * D) IMPLEMENTATION NOTE: An implementation may choose to
1691 * send the Communication Up notification to the SCTP user
1692 * upon reception of a valid COOKIE ECHO chunk.
1694 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP, 0,
1695 new_asoc->c.sinit_num_ostreams,
1696 new_asoc->c.sinit_max_instreams,
1701 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1703 /* Sockets API Draft Section 5.3.1.6
1704 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1705 * delivers this notification to inform the application that of the
1706 * peers requested adaptation layer.
1708 if (asoc->peer.adaptation_ind) {
1709 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
1713 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1717 return SCTP_DISPOSITION_CONSUME;
1720 sctp_chunk_free(repl);
1722 return SCTP_DISPOSITION_NOMEM;
1725 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1728 * C) In this case, the local endpoint's cookie has arrived late.
1729 * Before it arrived, the local endpoint sent an INIT and received an
1730 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1731 * but a new tag of its own.
1733 /* This case represents an initialization collision. */
1734 static sctp_disposition_t sctp_sf_do_dupcook_c(const struct sctp_endpoint *ep,
1735 const struct sctp_association *asoc,
1736 struct sctp_chunk *chunk,
1737 sctp_cmd_seq_t *commands,
1738 struct sctp_association *new_asoc)
1740 /* The cookie should be silently discarded.
1741 * The endpoint SHOULD NOT change states and should leave
1742 * any timers running.
1744 return SCTP_DISPOSITION_DISCARD;
1747 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1751 * D) When both local and remote tags match the endpoint should always
1752 * enter the ESTABLISHED state, if it has not already done so.
1754 /* This case represents an initialization collision. */
1755 static sctp_disposition_t sctp_sf_do_dupcook_d(const struct sctp_endpoint *ep,
1756 const struct sctp_association *asoc,
1757 struct sctp_chunk *chunk,
1758 sctp_cmd_seq_t *commands,
1759 struct sctp_association *new_asoc)
1761 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1762 struct sctp_chunk *repl;
1764 /* Clarification from Implementor's Guide:
1765 * D) When both local and remote tags match the endpoint should
1766 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1767 * It should stop any cookie timer that may be running and send
1771 /* Don't accidentally move back into established state. */
1772 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1773 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1774 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1775 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1776 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1777 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
1778 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1781 /* RFC 2960 5.1 Normal Establishment of an Association
1783 * D) IMPLEMENTATION NOTE: An implementation may choose
1784 * to send the Communication Up notification to the
1785 * SCTP user upon reception of a valid COOKIE
1788 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1790 asoc->c.sinit_num_ostreams,
1791 asoc->c.sinit_max_instreams,
1796 /* Sockets API Draft Section 5.3.1.6
1797 * When a peer sends a Adaptation Layer Indication parameter,
1798 * SCTP delivers this notification to inform the application
1799 * that of the peers requested adaptation layer.
1801 if (asoc->peer.adaptation_ind) {
1802 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1809 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1811 repl = sctp_make_cookie_ack(new_asoc, chunk);
1816 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1819 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1820 SCTP_ULPEVENT(ai_ev));
1822 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1823 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSMIT, SCTP_NULL());
1825 return SCTP_DISPOSITION_CONSUME;
1829 sctp_ulpevent_free(ai_ev);
1831 sctp_ulpevent_free(ev);
1832 return SCTP_DISPOSITION_NOMEM;
1836 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
1837 * chunk was retransmitted and then delayed in the network.
1839 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
1841 * Verification Tag: None. Do cookie validation.
1844 * (endpoint, asoc, chunk)
1847 * (asoc, reply_msg, msg_up, timers, counters)
1849 * The return value is the disposition of the chunk.
1851 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(const struct sctp_endpoint *ep,
1852 const struct sctp_association *asoc,
1853 const sctp_subtype_t type,
1855 sctp_cmd_seq_t *commands)
1857 sctp_disposition_t retval;
1858 struct sctp_chunk *chunk = arg;
1859 struct sctp_association *new_asoc;
1862 struct sctp_chunk *err_chk_p;
1864 /* Make sure that the chunk has a valid length from the protocol
1865 * perspective. In this case check to make sure we have at least
1866 * enough for the chunk header. Cookie length verification is
1869 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
1870 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
1873 /* "Decode" the chunk. We have no optional parameters so we
1874 * are in good shape.
1876 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
1877 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
1878 sizeof(sctp_chunkhdr_t)))
1881 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
1882 * of a duplicate COOKIE ECHO match the Verification Tags of the
1883 * current association, consider the State Cookie valid even if
1884 * the lifespan is exceeded.
1886 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
1890 * If the re-build failed, what is the proper error path
1893 * [We should abort the association. --piggy]
1896 /* FIXME: Several errors are possible. A bad cookie should
1897 * be silently discarded, but think about logging it too.
1900 case -SCTP_IERROR_NOMEM:
1903 case -SCTP_IERROR_STALE_COOKIE:
1904 sctp_send_stale_cookie_err(ep, asoc, chunk, commands,
1906 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1907 case -SCTP_IERROR_BAD_SIG:
1909 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1913 /* Compare the tie_tag in cookie with the verification tag of
1914 * current association.
1916 action = sctp_tietags_compare(new_asoc, asoc);
1919 case 'A': /* Association restart. */
1920 retval = sctp_sf_do_dupcook_a(ep, asoc, chunk, commands,
1924 case 'B': /* Collision case B. */
1925 retval = sctp_sf_do_dupcook_b(ep, asoc, chunk, commands,
1929 case 'C': /* Collision case C. */
1930 retval = sctp_sf_do_dupcook_c(ep, asoc, chunk, commands,
1934 case 'D': /* Collision case D. */
1935 retval = sctp_sf_do_dupcook_d(ep, asoc, chunk, commands,
1939 default: /* Discard packet for all others. */
1940 retval = sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1944 /* Delete the tempory new association. */
1945 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1946 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1951 return SCTP_DISPOSITION_NOMEM;
1955 * Process an ABORT. (SHUTDOWN-PENDING state)
1957 * See sctp_sf_do_9_1_abort().
1959 sctp_disposition_t sctp_sf_shutdown_pending_abort(
1960 const struct sctp_endpoint *ep,
1961 const struct sctp_association *asoc,
1962 const sctp_subtype_t type,
1964 sctp_cmd_seq_t *commands)
1966 struct sctp_chunk *chunk = arg;
1968 if (!sctp_vtag_verify_either(chunk, asoc))
1969 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1971 /* Make sure that the ABORT chunk has a valid length.
1972 * Since this is an ABORT chunk, we have to discard it
1973 * because of the following text:
1974 * RFC 2960, Section 3.3.7
1975 * If an endpoint receives an ABORT with a format error or for an
1976 * association that doesn't exist, it MUST silently discard it.
1977 * Becasue the length is "invalid", we can't really discard just
1978 * as we do not know its true length. So, to be safe, discard the
1981 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
1982 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
1984 /* Stop the T5-shutdown guard timer. */
1985 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1986 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
1988 return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
1992 * Process an ABORT. (SHUTDOWN-SENT state)
1994 * See sctp_sf_do_9_1_abort().
1996 sctp_disposition_t sctp_sf_shutdown_sent_abort(const struct sctp_endpoint *ep,
1997 const struct sctp_association *asoc,
1998 const sctp_subtype_t type,
2000 sctp_cmd_seq_t *commands)
2002 struct sctp_chunk *chunk = arg;
2004 if (!sctp_vtag_verify_either(chunk, asoc))
2005 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2007 /* Make sure that the ABORT chunk has a valid length.
2008 * Since this is an ABORT chunk, we have to discard it
2009 * because of the following text:
2010 * RFC 2960, Section 3.3.7
2011 * If an endpoint receives an ABORT with a format error or for an
2012 * association that doesn't exist, it MUST silently discard it.
2013 * Becasue the length is "invalid", we can't really discard just
2014 * as we do not know its true length. So, to be safe, discard the
2017 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2018 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2020 /* Stop the T2-shutdown timer. */
2021 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2022 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2024 /* Stop the T5-shutdown guard timer. */
2025 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2026 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2028 return sctp_sf_do_9_1_abort(ep, asoc, type, arg, commands);
2032 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2034 * See sctp_sf_do_9_1_abort().
2036 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2037 const struct sctp_endpoint *ep,
2038 const struct sctp_association *asoc,
2039 const sctp_subtype_t type,
2041 sctp_cmd_seq_t *commands)
2043 /* The same T2 timer, so we should be able to use
2044 * common function with the SHUTDOWN-SENT state.
2046 return sctp_sf_shutdown_sent_abort(ep, asoc, type, arg, commands);
2050 * Handle an Error received in COOKIE_ECHOED state.
2052 * Only handle the error type of stale COOKIE Error, the other errors will
2056 * (endpoint, asoc, chunk)
2059 * (asoc, reply_msg, msg_up, timers, counters)
2061 * The return value is the disposition of the chunk.
2063 sctp_disposition_t sctp_sf_cookie_echoed_err(const struct sctp_endpoint *ep,
2064 const struct sctp_association *asoc,
2065 const sctp_subtype_t type,
2067 sctp_cmd_seq_t *commands)
2069 struct sctp_chunk *chunk = arg;
2072 if (!sctp_vtag_verify(chunk, asoc))
2073 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2075 /* Make sure that the ERROR chunk has a valid length.
2076 * The parameter walking depends on this as well.
2078 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2079 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2082 /* Process the error here */
2083 /* FUTURE FIXME: When PR-SCTP related and other optional
2084 * parms are emitted, this will have to change to handle multiple
2087 sctp_walk_errors(err, chunk->chunk_hdr) {
2088 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2089 return sctp_sf_do_5_2_6_stale(ep, asoc, type,
2093 /* It is possible to have malformed error causes, and that
2094 * will cause us to end the walk early. However, since
2095 * we are discarding the packet, there should be no adverse
2098 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2102 * Handle a Stale COOKIE Error
2104 * Section: 5.2.6 Handle Stale COOKIE Error
2105 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2106 * one of the following three alternatives.
2108 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2109 * Preservative parameter requesting an extension to the lifetime of
2110 * the State Cookie. When calculating the time extension, an
2111 * implementation SHOULD use the RTT information measured based on the
2112 * previous COOKIE ECHO / ERROR exchange, and should add no more
2113 * than 1 second beyond the measured RTT, due to long State Cookie
2114 * lifetimes making the endpoint more subject to a replay attack.
2116 * Verification Tag: Not explicit, but safe to ignore.
2119 * (endpoint, asoc, chunk)
2122 * (asoc, reply_msg, msg_up, timers, counters)
2124 * The return value is the disposition of the chunk.
2126 static sctp_disposition_t sctp_sf_do_5_2_6_stale(const struct sctp_endpoint *ep,
2127 const struct sctp_association *asoc,
2128 const sctp_subtype_t type,
2130 sctp_cmd_seq_t *commands)
2132 struct sctp_chunk *chunk = arg;
2134 sctp_cookie_preserve_param_t bht;
2136 struct sctp_chunk *reply;
2137 struct sctp_bind_addr *bp;
2138 int attempts = asoc->init_err_counter + 1;
2140 if (attempts > asoc->max_init_attempts) {
2141 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2142 SCTP_ERROR(ETIMEDOUT));
2143 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2144 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2145 return SCTP_DISPOSITION_DELETE_TCB;
2148 err = (sctp_errhdr_t *)(chunk->skb->data);
2150 /* When calculating the time extension, an implementation
2151 * SHOULD use the RTT information measured based on the
2152 * previous COOKIE ECHO / ERROR exchange, and should add no
2153 * more than 1 second beyond the measured RTT, due to long
2154 * State Cookie lifetimes making the endpoint more subject to
2156 * Measure of Staleness's unit is usec. (1/1000000 sec)
2157 * Suggested Cookie Life-span Increment's unit is msec.
2159 * In general, if you use the suggested cookie life, the value
2160 * found in the field of measure of staleness should be doubled
2161 * to give ample time to retransmit the new cookie and thus
2162 * yield a higher probability of success on the reattempt.
2164 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2165 stale = (stale * 2) / 1000;
2167 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2168 bht.param_hdr.length = htons(sizeof(bht));
2169 bht.lifespan_increment = htonl(stale);
2171 /* Build that new INIT chunk. */
2172 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2173 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2177 sctp_addto_chunk(reply, sizeof(bht), &bht);
2179 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2180 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2182 /* Stop pending T3-rtx and heartbeat timers */
2183 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2184 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2186 /* Delete non-primary peer ip addresses since we are transitioning
2187 * back to the COOKIE-WAIT state
2189 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2191 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2194 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN,
2195 SCTP_TRANSPORT(asoc->peer.primary_path));
2197 /* Cast away the const modifier, as we want to just
2198 * rerun it through as a sideffect.
2200 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2202 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2203 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2204 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2205 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2206 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2207 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2209 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2211 return SCTP_DISPOSITION_CONSUME;
2214 return SCTP_DISPOSITION_NOMEM;
2221 * After checking the Verification Tag, the receiving endpoint shall
2222 * remove the association from its record, and shall report the
2223 * termination to its upper layer.
2225 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2226 * B) Rules for packet carrying ABORT:
2228 * - The endpoint shall always fill in the Verification Tag field of the
2229 * outbound packet with the destination endpoint's tag value if it
2232 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2233 * MUST follow the procedure described in Section 8.4.
2235 * - The receiver MUST accept the packet if the Verification Tag
2236 * matches either its own tag, OR the tag of its peer. Otherwise, the
2237 * receiver MUST silently discard the packet and take no further
2241 * (endpoint, asoc, chunk)
2244 * (asoc, reply_msg, msg_up, timers, counters)
2246 * The return value is the disposition of the chunk.
2248 sctp_disposition_t sctp_sf_do_9_1_abort(const struct sctp_endpoint *ep,
2249 const struct sctp_association *asoc,
2250 const sctp_subtype_t type,
2252 sctp_cmd_seq_t *commands)
2254 struct sctp_chunk *chunk = arg;
2256 __be16 error = SCTP_ERROR_NO_ERROR;
2258 if (!sctp_vtag_verify_either(chunk, asoc))
2259 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2261 /* Make sure that the ABORT chunk has a valid length.
2262 * Since this is an ABORT chunk, we have to discard it
2263 * because of the following text:
2264 * RFC 2960, Section 3.3.7
2265 * If an endpoint receives an ABORT with a format error or for an
2266 * association that doesn't exist, it MUST silently discard it.
2267 * Becasue the length is "invalid", we can't really discard just
2268 * as we do not know its true length. So, to be safe, discard the
2271 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2272 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2274 /* See if we have an error cause code in the chunk. */
2275 len = ntohs(chunk->chunk_hdr->length);
2276 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2277 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2279 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2280 /* ASSOC_FAILED will DELETE_TCB. */
2281 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2282 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2283 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
2285 return SCTP_DISPOSITION_ABORT;
2289 * Process an ABORT. (COOKIE-WAIT state)
2291 * See sctp_sf_do_9_1_abort() above.
2293 sctp_disposition_t sctp_sf_cookie_wait_abort(const struct sctp_endpoint *ep,
2294 const struct sctp_association *asoc,
2295 const sctp_subtype_t type,
2297 sctp_cmd_seq_t *commands)
2299 struct sctp_chunk *chunk = arg;
2301 __be16 error = SCTP_ERROR_NO_ERROR;
2303 if (!sctp_vtag_verify_either(chunk, asoc))
2304 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2306 /* Make sure that the ABORT chunk has a valid length.
2307 * Since this is an ABORT chunk, we have to discard it
2308 * because of the following text:
2309 * RFC 2960, Section 3.3.7
2310 * If an endpoint receives an ABORT with a format error or for an
2311 * association that doesn't exist, it MUST silently discard it.
2312 * Becasue the length is "invalid", we can't really discard just
2313 * as we do not know its true length. So, to be safe, discard the
2316 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2317 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2319 /* See if we have an error cause code in the chunk. */
2320 len = ntohs(chunk->chunk_hdr->length);
2321 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2322 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2324 return sctp_stop_t1_and_abort(commands, error, ECONNREFUSED, asoc,
2329 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2331 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(const struct sctp_endpoint *ep,
2332 const struct sctp_association *asoc,
2333 const sctp_subtype_t type,
2335 sctp_cmd_seq_t *commands)
2337 return sctp_stop_t1_and_abort(commands, SCTP_ERROR_NO_ERROR,
2339 (struct sctp_transport *)arg);
2343 * Process an ABORT. (COOKIE-ECHOED state)
2345 sctp_disposition_t sctp_sf_cookie_echoed_abort(const struct sctp_endpoint *ep,
2346 const struct sctp_association *asoc,
2347 const sctp_subtype_t type,
2349 sctp_cmd_seq_t *commands)
2351 /* There is a single T1 timer, so we should be able to use
2352 * common function with the COOKIE-WAIT state.
2354 return sctp_sf_cookie_wait_abort(ep, asoc, type, arg, commands);
2358 * Stop T1 timer and abort association with "INIT failed".
2360 * This is common code called by several sctp_sf_*_abort() functions above.
2362 static sctp_disposition_t sctp_stop_t1_and_abort(sctp_cmd_seq_t *commands,
2363 __be16 error, int sk_err,
2364 const struct sctp_association *asoc,
2365 struct sctp_transport *transport)
2367 SCTP_DEBUG_PRINTK("ABORT received (INIT).\n");
2368 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2369 SCTP_STATE(SCTP_STATE_CLOSED));
2370 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
2371 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2372 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2373 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2374 /* CMD_INIT_FAILED will DELETE_TCB. */
2375 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2377 return SCTP_DISPOSITION_ABORT;
2381 * sctp_sf_do_9_2_shut
2384 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2385 * - enter the SHUTDOWN-RECEIVED state,
2387 * - stop accepting new data from its SCTP user
2389 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2390 * that all its outstanding DATA chunks have been received by the
2393 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2394 * send a SHUTDOWN in response to a ULP request. And should discard
2395 * subsequent SHUTDOWN chunks.
2397 * If there are still outstanding DATA chunks left, the SHUTDOWN
2398 * receiver shall continue to follow normal data transmission
2399 * procedures defined in Section 6 until all outstanding DATA chunks
2400 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2401 * new data from its SCTP user.
2403 * Verification Tag: 8.5 Verification Tag [Normal verification]
2406 * (endpoint, asoc, chunk)
2409 * (asoc, reply_msg, msg_up, timers, counters)
2411 * The return value is the disposition of the chunk.
2413 sctp_disposition_t sctp_sf_do_9_2_shutdown(const struct sctp_endpoint *ep,
2414 const struct sctp_association *asoc,
2415 const sctp_subtype_t type,
2417 sctp_cmd_seq_t *commands)
2419 struct sctp_chunk *chunk = arg;
2420 sctp_shutdownhdr_t *sdh;
2421 sctp_disposition_t disposition;
2422 struct sctp_ulpevent *ev;
2424 if (!sctp_vtag_verify(chunk, asoc))
2425 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2427 /* Make sure that the SHUTDOWN chunk has a valid length. */
2428 if (!sctp_chunk_length_valid(chunk,
2429 sizeof(struct sctp_shutdown_chunk_t)))
2430 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2433 /* Convert the elaborate header. */
2434 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2435 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2436 chunk->subh.shutdown_hdr = sdh;
2438 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2439 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2440 * inform the application that it should cease sending data.
2442 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2444 disposition = SCTP_DISPOSITION_NOMEM;
2447 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2449 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2450 * - enter the SHUTDOWN-RECEIVED state,
2451 * - stop accepting new data from its SCTP user
2453 * [This is implicit in the new state.]
2455 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2456 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2457 disposition = SCTP_DISPOSITION_CONSUME;
2459 if (sctp_outq_is_empty(&asoc->outqueue)) {
2460 disposition = sctp_sf_do_9_2_shutdown_ack(ep, asoc, type,
2464 if (SCTP_DISPOSITION_NOMEM == disposition)
2467 /* - verify, by checking the Cumulative TSN Ack field of the
2468 * chunk, that all its outstanding DATA chunks have been
2469 * received by the SHUTDOWN sender.
2471 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2472 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2479 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2480 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2481 * transport addresses (either in the IP addresses or in the INIT chunk)
2482 * that belong to this association, it should discard the INIT chunk and
2483 * retransmit the SHUTDOWN ACK chunk.
2485 sctp_disposition_t sctp_sf_do_9_2_reshutack(const struct sctp_endpoint *ep,
2486 const struct sctp_association *asoc,
2487 const sctp_subtype_t type,
2489 sctp_cmd_seq_t *commands)
2491 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2492 struct sctp_chunk *reply;
2494 /* Since we are not going to really process this INIT, there
2495 * is no point in verifying chunk boundries. Just generate
2498 reply = sctp_make_shutdown_ack(asoc, chunk);
2502 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2503 * the T2-SHUTDOWN timer.
2505 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2507 /* and restart the T2-shutdown timer. */
2508 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2509 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2511 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2513 return SCTP_DISPOSITION_CONSUME;
2515 return SCTP_DISPOSITION_NOMEM;
2519 * sctp_sf_do_ecn_cwr
2521 * Section: Appendix A: Explicit Congestion Notification
2525 * RFC 2481 details a specific bit for a sender to send in the header of
2526 * its next outbound TCP segment to indicate to its peer that it has
2527 * reduced its congestion window. This is termed the CWR bit. For
2528 * SCTP the same indication is made by including the CWR chunk.
2529 * This chunk contains one data element, i.e. the TSN number that
2530 * was sent in the ECNE chunk. This element represents the lowest
2531 * TSN number in the datagram that was originally marked with the
2534 * Verification Tag: 8.5 Verification Tag [Normal verification]
2536 * (endpoint, asoc, chunk)
2539 * (asoc, reply_msg, msg_up, timers, counters)
2541 * The return value is the disposition of the chunk.
2543 sctp_disposition_t sctp_sf_do_ecn_cwr(const struct sctp_endpoint *ep,
2544 const struct sctp_association *asoc,
2545 const sctp_subtype_t type,
2547 sctp_cmd_seq_t *commands)
2550 struct sctp_chunk *chunk = arg;
2553 if (!sctp_vtag_verify(chunk, asoc))
2554 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2556 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2557 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2560 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2561 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2563 lowest_tsn = ntohl(cwr->lowest_tsn);
2565 /* Does this CWR ack the last sent congestion notification? */
2566 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2567 /* Stop sending ECNE. */
2568 sctp_add_cmd_sf(commands,
2570 SCTP_U32(lowest_tsn));
2572 return SCTP_DISPOSITION_CONSUME;
2578 * Section: Appendix A: Explicit Congestion Notification
2582 * RFC 2481 details a specific bit for a receiver to send back in its
2583 * TCP acknowledgements to notify the sender of the Congestion
2584 * Experienced (CE) bit having arrived from the network. For SCTP this
2585 * same indication is made by including the ECNE chunk. This chunk
2586 * contains one data element, i.e. the lowest TSN associated with the IP
2587 * datagram marked with the CE bit.....
2589 * Verification Tag: 8.5 Verification Tag [Normal verification]
2591 * (endpoint, asoc, chunk)
2594 * (asoc, reply_msg, msg_up, timers, counters)
2596 * The return value is the disposition of the chunk.
2598 sctp_disposition_t sctp_sf_do_ecne(const struct sctp_endpoint *ep,
2599 const struct sctp_association *asoc,
2600 const sctp_subtype_t type,
2602 sctp_cmd_seq_t *commands)
2604 sctp_ecnehdr_t *ecne;
2605 struct sctp_chunk *chunk = arg;
2607 if (!sctp_vtag_verify(chunk, asoc))
2608 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2610 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2611 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2614 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2615 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2617 /* If this is a newer ECNE than the last CWR packet we sent out */
2618 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2619 SCTP_U32(ntohl(ecne->lowest_tsn)));
2621 return SCTP_DISPOSITION_CONSUME;
2625 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2627 * The SCTP endpoint MUST always acknowledge the reception of each valid
2630 * The guidelines on delayed acknowledgement algorithm specified in
2631 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2632 * acknowledgement SHOULD be generated for at least every second packet
2633 * (not every second DATA chunk) received, and SHOULD be generated within
2634 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2635 * situations it may be beneficial for an SCTP transmitter to be more
2636 * conservative than the algorithms detailed in this document allow.
2637 * However, an SCTP transmitter MUST NOT be more aggressive than the
2638 * following algorithms allow.
2640 * A SCTP receiver MUST NOT generate more than one SACK for every
2641 * incoming packet, other than to update the offered window as the
2642 * receiving application consumes new data.
2644 * Verification Tag: 8.5 Verification Tag [Normal verification]
2647 * (endpoint, asoc, chunk)
2650 * (asoc, reply_msg, msg_up, timers, counters)
2652 * The return value is the disposition of the chunk.
2654 sctp_disposition_t sctp_sf_eat_data_6_2(const struct sctp_endpoint *ep,
2655 const struct sctp_association *asoc,
2656 const sctp_subtype_t type,
2658 sctp_cmd_seq_t *commands)
2660 struct sctp_chunk *chunk = arg;
2663 if (!sctp_vtag_verify(chunk, asoc)) {
2664 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2666 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2669 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2670 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2673 error = sctp_eat_data(asoc, chunk, commands );
2675 case SCTP_IERROR_NO_ERROR:
2677 case SCTP_IERROR_HIGH_TSN:
2678 case SCTP_IERROR_BAD_STREAM:
2679 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2680 goto discard_noforce;
2681 case SCTP_IERROR_DUP_TSN:
2682 case SCTP_IERROR_IGNORE_TSN:
2683 SCTP_INC_STATS(SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2685 case SCTP_IERROR_NO_DATA:
2691 if (asoc->autoclose) {
2692 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2693 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2696 /* If this is the last chunk in a packet, we need to count it
2697 * toward sack generation. Note that we need to SACK every
2698 * OTHER packet containing data chunks, EVEN IF WE DISCARD
2699 * THEM. We elect to NOT generate SACK's if the chunk fails
2700 * the verification tag test.
2702 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2704 * The SCTP endpoint MUST always acknowledge the reception of
2705 * each valid DATA chunk.
2707 * The guidelines on delayed acknowledgement algorithm
2708 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
2709 * Specifically, an acknowledgement SHOULD be generated for at
2710 * least every second packet (not every second DATA chunk)
2711 * received, and SHOULD be generated within 200 ms of the
2712 * arrival of any unacknowledged DATA chunk. In some
2713 * situations it may be beneficial for an SCTP transmitter to
2714 * be more conservative than the algorithms detailed in this
2715 * document allow. However, an SCTP transmitter MUST NOT be
2716 * more aggressive than the following algorithms allow.
2718 if (chunk->end_of_packet)
2719 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2721 return SCTP_DISPOSITION_CONSUME;
2724 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
2726 * When a packet arrives with duplicate DATA chunk(s) and with
2727 * no new DATA chunk(s), the endpoint MUST immediately send a
2728 * SACK with no delay. If a packet arrives with duplicate
2729 * DATA chunk(s) bundled with new DATA chunks, the endpoint
2730 * MAY immediately send a SACK. Normally receipt of duplicate
2731 * DATA chunks will occur when the original SACK chunk was lost
2732 * and the peer's RTO has expired. The duplicate TSN number(s)
2733 * SHOULD be reported in the SACK as duplicate.
2735 /* In our case, we split the MAY SACK advice up whether or not
2736 * the last chunk is a duplicate.'
2738 if (chunk->end_of_packet)
2739 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2740 return SCTP_DISPOSITION_DISCARD;
2743 if (chunk->end_of_packet)
2744 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
2746 return SCTP_DISPOSITION_DISCARD;
2748 return SCTP_DISPOSITION_CONSUME;
2753 * sctp_sf_eat_data_fast_4_4
2756 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
2757 * DATA chunks without delay.
2759 * Verification Tag: 8.5 Verification Tag [Normal verification]
2761 * (endpoint, asoc, chunk)
2764 * (asoc, reply_msg, msg_up, timers, counters)
2766 * The return value is the disposition of the chunk.
2768 sctp_disposition_t sctp_sf_eat_data_fast_4_4(const struct sctp_endpoint *ep,
2769 const struct sctp_association *asoc,
2770 const sctp_subtype_t type,
2772 sctp_cmd_seq_t *commands)
2774 struct sctp_chunk *chunk = arg;
2777 if (!sctp_vtag_verify(chunk, asoc)) {
2778 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2780 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2783 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2784 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2787 error = sctp_eat_data(asoc, chunk, commands );
2789 case SCTP_IERROR_NO_ERROR:
2790 case SCTP_IERROR_HIGH_TSN:
2791 case SCTP_IERROR_DUP_TSN:
2792 case SCTP_IERROR_IGNORE_TSN:
2793 case SCTP_IERROR_BAD_STREAM:
2795 case SCTP_IERROR_NO_DATA:
2801 /* Go a head and force a SACK, since we are shutting down. */
2803 /* Implementor's Guide.
2805 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
2806 * respond to each received packet containing one or more DATA chunk(s)
2807 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
2809 if (chunk->end_of_packet) {
2810 /* We must delay the chunk creation since the cumulative
2811 * TSN has not been updated yet.
2813 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
2814 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
2815 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2816 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2820 return SCTP_DISPOSITION_CONSUME;
2824 * Section: 6.2 Processing a Received SACK
2825 * D) Any time a SACK arrives, the endpoint performs the following:
2827 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
2828 * then drop the SACK. Since Cumulative TSN Ack is monotonically
2829 * increasing, a SACK whose Cumulative TSN Ack is less than the
2830 * Cumulative TSN Ack Point indicates an out-of-order SACK.
2832 * ii) Set rwnd equal to the newly received a_rwnd minus the number
2833 * of bytes still outstanding after processing the Cumulative TSN Ack
2834 * and the Gap Ack Blocks.
2836 * iii) If the SACK is missing a TSN that was previously
2837 * acknowledged via a Gap Ack Block (e.g., the data receiver
2838 * reneged on the data), then mark the corresponding DATA chunk
2839 * as available for retransmit: Mark it as missing for fast
2840 * retransmit as described in Section 7.2.4 and if no retransmit
2841 * timer is running for the destination address to which the DATA
2842 * chunk was originally transmitted, then T3-rtx is started for
2843 * that destination address.
2845 * Verification Tag: 8.5 Verification Tag [Normal verification]
2848 * (endpoint, asoc, chunk)
2851 * (asoc, reply_msg, msg_up, timers, counters)
2853 * The return value is the disposition of the chunk.
2855 sctp_disposition_t sctp_sf_eat_sack_6_2(const struct sctp_endpoint *ep,
2856 const struct sctp_association *asoc,
2857 const sctp_subtype_t type,
2859 sctp_cmd_seq_t *commands)
2861 struct sctp_chunk *chunk = arg;
2862 sctp_sackhdr_t *sackh;
2865 if (!sctp_vtag_verify(chunk, asoc))
2866 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2868 /* Make sure that the SACK chunk has a valid length. */
2869 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
2870 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2873 /* Pull the SACK chunk from the data buffer */
2874 sackh = sctp_sm_pull_sack(chunk);
2875 /* Was this a bogus SACK? */
2877 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2878 chunk->subh.sack_hdr = sackh;
2879 ctsn = ntohl(sackh->cum_tsn_ack);
2881 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
2882 * Ack Point, then drop the SACK. Since Cumulative TSN
2883 * Ack is monotonically increasing, a SACK whose
2884 * Cumulative TSN Ack is less than the Cumulative TSN Ack
2885 * Point indicates an out-of-order SACK.
2887 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2888 SCTP_DEBUG_PRINTK("ctsn %x\n", ctsn);
2889 SCTP_DEBUG_PRINTK("ctsn_ack_point %x\n", asoc->ctsn_ack_point);
2890 return SCTP_DISPOSITION_DISCARD;
2893 /* Return this SACK for further processing. */
2894 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_SACKH(sackh));
2896 /* Note: We do the rest of the work on the PROCESS_SACK
2899 return SCTP_DISPOSITION_CONSUME;
2903 * Generate an ABORT in response to a packet.
2905 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
2907 * 8) The receiver should respond to the sender of the OOTB packet with
2908 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
2909 * MUST fill in the Verification Tag field of the outbound packet
2910 * with the value found in the Verification Tag field of the OOTB
2911 * packet and set the T-bit in the Chunk Flags to indicate that the
2912 * Verification Tag is reflected. After sending this ABORT, the
2913 * receiver of the OOTB packet shall discard the OOTB packet and take
2914 * no further action.
2918 * The return value is the disposition of the chunk.
2920 sctp_disposition_t sctp_sf_tabort_8_4_8(const struct sctp_endpoint *ep,
2921 const struct sctp_association *asoc,
2922 const sctp_subtype_t type,
2924 sctp_cmd_seq_t *commands)
2926 struct sctp_packet *packet = NULL;
2927 struct sctp_chunk *chunk = arg;
2928 struct sctp_chunk *abort;
2930 packet = sctp_ootb_pkt_new(asoc, chunk);
2933 /* Make an ABORT. The T bit will be set if the asoc
2936 abort = sctp_make_abort(asoc, chunk, 0);
2938 sctp_ootb_pkt_free(packet);
2939 return SCTP_DISPOSITION_NOMEM;
2942 /* Reflect vtag if T-Bit is set */
2943 if (sctp_test_T_bit(abort))
2944 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
2946 /* Set the skb to the belonging sock for accounting. */
2947 abort->skb->sk = ep->base.sk;
2949 sctp_packet_append_chunk(packet, abort);
2951 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
2952 SCTP_PACKET(packet));
2954 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
2956 return SCTP_DISPOSITION_CONSUME;
2959 return SCTP_DISPOSITION_NOMEM;
2963 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
2964 * event as ULP notification for each cause included in the chunk.
2966 * API 5.3.1.3 - SCTP_REMOTE_ERROR
2968 * The return value is the disposition of the chunk.
2970 sctp_disposition_t sctp_sf_operr_notify(const struct sctp_endpoint *ep,
2971 const struct sctp_association *asoc,
2972 const sctp_subtype_t type,
2974 sctp_cmd_seq_t *commands)
2976 struct sctp_chunk *chunk = arg;
2977 struct sctp_ulpevent *ev;
2979 if (!sctp_vtag_verify(chunk, asoc))
2980 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
2982 /* Make sure that the ERROR chunk has a valid length. */
2983 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2984 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
2987 while (chunk->chunk_end > chunk->skb->data) {
2988 ev = sctp_ulpevent_make_remote_error(asoc, chunk, 0,
2993 if (!sctp_add_cmd(commands, SCTP_CMD_EVENT_ULP,
2994 SCTP_ULPEVENT(ev))) {
2995 sctp_ulpevent_free(ev);
2999 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3002 return SCTP_DISPOSITION_CONSUME;
3005 return SCTP_DISPOSITION_NOMEM;
3009 * Process an inbound SHUTDOWN ACK.
3012 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3013 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3014 * peer, and remove all record of the association.
3016 * The return value is the disposition.
3018 sctp_disposition_t sctp_sf_do_9_2_final(const struct sctp_endpoint *ep,
3019 const struct sctp_association *asoc,
3020 const sctp_subtype_t type,
3022 sctp_cmd_seq_t *commands)
3024 struct sctp_chunk *chunk = arg;
3025 struct sctp_chunk *reply;
3026 struct sctp_ulpevent *ev;
3028 if (!sctp_vtag_verify(chunk, asoc))
3029 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3031 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3032 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3033 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3035 /* 10.2 H) SHUTDOWN COMPLETE notification
3037 * When SCTP completes the shutdown procedures (section 9.2) this
3038 * notification is passed to the upper layer.
3040 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3041 0, 0, 0, GFP_ATOMIC);
3045 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3046 reply = sctp_make_shutdown_complete(asoc, chunk);
3050 /* Do all the commands now (after allocation), so that we
3051 * have consistent state if memory allocation failes
3053 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3055 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3056 * stop the T2-shutdown timer,
3058 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3059 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3061 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3062 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3064 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3065 SCTP_STATE(SCTP_STATE_CLOSED));
3066 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
3067 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3068 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3070 /* ...and remove all record of the association. */
3071 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3072 return SCTP_DISPOSITION_DELETE_TCB;
3075 sctp_ulpevent_free(ev);
3077 return SCTP_DISPOSITION_NOMEM;
3081 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3083 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3084 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3085 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3086 * packet must fill in the Verification Tag field of the outbound
3087 * packet with the Verification Tag received in the SHUTDOWN ACK and
3088 * set the T-bit in the Chunk Flags to indicate that the Verification
3091 * 8) The receiver should respond to the sender of the OOTB packet with
3092 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3093 * MUST fill in the Verification Tag field of the outbound packet
3094 * with the value found in the Verification Tag field of the OOTB
3095 * packet and set the T-bit in the Chunk Flags to indicate that the
3096 * Verification Tag is reflected. After sending this ABORT, the
3097 * receiver of the OOTB packet shall discard the OOTB packet and take
3098 * no further action.
3100 sctp_disposition_t sctp_sf_ootb(const struct sctp_endpoint *ep,
3101 const struct sctp_association *asoc,
3102 const sctp_subtype_t type,
3104 sctp_cmd_seq_t *commands)
3106 struct sctp_chunk *chunk = arg;
3107 struct sk_buff *skb = chunk->skb;
3108 sctp_chunkhdr_t *ch;
3110 int ootb_shut_ack = 0;
3112 SCTP_INC_STATS(SCTP_MIB_OUTOFBLUES);
3114 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3116 /* Break out if chunk length is less then minimal. */
3117 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3120 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3121 if (ch_end > skb->tail)
3124 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3127 /* RFC 2960, Section 3.3.7
3128 * Moreover, under any circumstances, an endpoint that
3129 * receives an ABORT MUST NOT respond to that ABORT by
3130 * sending an ABORT of its own.
3132 if (SCTP_CID_ABORT == ch->type)
3133 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3135 ch = (sctp_chunkhdr_t *) ch_end;
3136 } while (ch_end < skb->tail);
3139 sctp_sf_shut_8_4_5(ep, asoc, type, arg, commands);
3141 sctp_sf_tabort_8_4_8(ep, asoc, type, arg, commands);
3143 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3147 * Handle an "Out of the blue" SHUTDOWN ACK.
3149 * Section: 8.4 5, sctpimpguide 2.41.
3151 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3152 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3153 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3154 * packet must fill in the Verification Tag field of the outbound
3155 * packet with the Verification Tag received in the SHUTDOWN ACK and
3156 * set the T-bit in the Chunk Flags to indicate that the Verification
3160 * (endpoint, asoc, type, arg, commands)
3163 * (sctp_disposition_t)
3165 * The return value is the disposition of the chunk.
3167 static sctp_disposition_t sctp_sf_shut_8_4_5(const struct sctp_endpoint *ep,
3168 const struct sctp_association *asoc,
3169 const sctp_subtype_t type,
3171 sctp_cmd_seq_t *commands)
3173 struct sctp_packet *packet = NULL;
3174 struct sctp_chunk *chunk = arg;
3175 struct sctp_chunk *shut;
3177 packet = sctp_ootb_pkt_new(asoc, chunk);
3180 /* Make an SHUTDOWN_COMPLETE.
3181 * The T bit will be set if the asoc is NULL.
3183 shut = sctp_make_shutdown_complete(asoc, chunk);
3185 sctp_ootb_pkt_free(packet);
3186 return SCTP_DISPOSITION_NOMEM;
3189 /* Reflect vtag if T-Bit is set */
3190 if (sctp_test_T_bit(shut))
3191 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3193 /* Set the skb to the belonging sock for accounting. */
3194 shut->skb->sk = ep->base.sk;
3196 sctp_packet_append_chunk(packet, shut);
3198 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3199 SCTP_PACKET(packet));
3201 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3203 /* If the chunk length is invalid, we don't want to process
3204 * the reset of the packet.
3206 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3207 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3209 return SCTP_DISPOSITION_CONSUME;
3212 return SCTP_DISPOSITION_NOMEM;
3216 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3218 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3219 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3220 * procedures in section 8.4 SHOULD be followed, in other words it
3221 * should be treated as an Out Of The Blue packet.
3222 * [This means that we do NOT check the Verification Tag on these
3226 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(const struct sctp_endpoint *ep,
3227 const struct sctp_association *asoc,
3228 const sctp_subtype_t type,
3230 sctp_cmd_seq_t *commands)
3232 /* Although we do have an association in this case, it corresponds
3233 * to a restarted association. So the packet is treated as an OOTB
3234 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3235 * called with a NULL association.
3237 return sctp_sf_shut_8_4_5(ep, NULL, type, arg, commands);
3240 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3241 sctp_disposition_t sctp_sf_do_asconf(const struct sctp_endpoint *ep,
3242 const struct sctp_association *asoc,
3243 const sctp_subtype_t type, void *arg,
3244 sctp_cmd_seq_t *commands)
3246 struct sctp_chunk *chunk = arg;
3247 struct sctp_chunk *asconf_ack = NULL;
3248 sctp_addiphdr_t *hdr;
3251 if (!sctp_vtag_verify(chunk, asoc)) {
3252 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3254 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3257 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3258 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3259 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3262 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3263 serial = ntohl(hdr->serial);
3265 /* ADDIP 4.2 C1) Compare the value of the serial number to the value
3266 * the endpoint stored in a new association variable
3267 * 'Peer-Serial-Number'.
3269 if (serial == asoc->peer.addip_serial + 1) {
3270 /* ADDIP 4.2 C2) If the value found in the serial number is
3271 * equal to the ('Peer-Serial-Number' + 1), the endpoint MUST
3274 asconf_ack = sctp_process_asconf((struct sctp_association *)
3277 return SCTP_DISPOSITION_NOMEM;
3278 } else if (serial == asoc->peer.addip_serial) {
3279 /* ADDIP 4.2 C3) If the value found in the serial number is
3280 * equal to the value stored in the 'Peer-Serial-Number'
3281 * IMPLEMENTATION NOTE: As an optimization a receiver may wish
3282 * to save the last ASCONF-ACK for some predetermined period of
3283 * time and instead of re-processing the ASCONF (with the same
3284 * serial number) it may just re-transmit the ASCONF-ACK.
3286 if (asoc->addip_last_asconf_ack)
3287 asconf_ack = asoc->addip_last_asconf_ack;
3289 return SCTP_DISPOSITION_DISCARD;
3291 /* ADDIP 4.2 C4) Otherwise, the ASCONF Chunk is discarded since
3292 * it must be either a stale packet or from an attacker.
3294 return SCTP_DISPOSITION_DISCARD;
3297 /* ADDIP 4.2 C5) In both cases C2 and C3 the ASCONF-ACK MUST be sent
3298 * back to the source address contained in the IP header of the ASCONF
3299 * being responded to.
3301 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3303 return SCTP_DISPOSITION_CONSUME;
3307 * ADDIP Section 4.3 General rules for address manipulation
3308 * When building TLV parameters for the ASCONF Chunk that will add or
3309 * delete IP addresses the D0 to D13 rules should be applied:
3311 sctp_disposition_t sctp_sf_do_asconf_ack(const struct sctp_endpoint *ep,
3312 const struct sctp_association *asoc,
3313 const sctp_subtype_t type, void *arg,
3314 sctp_cmd_seq_t *commands)
3316 struct sctp_chunk *asconf_ack = arg;
3317 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3318 struct sctp_chunk *abort;
3319 sctp_addiphdr_t *addip_hdr;
3320 __u32 sent_serial, rcvd_serial;
3322 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3323 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3325 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3328 /* Make sure that the ADDIP chunk has a valid length. */
3329 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3330 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3333 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3334 rcvd_serial = ntohl(addip_hdr->serial);
3337 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3338 sent_serial = ntohl(addip_hdr->serial);
3340 sent_serial = asoc->addip_serial - 1;
3343 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3344 * equal to the next serial number to be used but no ASCONF chunk is
3345 * outstanding the endpoint MUST ABORT the association. Note that a
3346 * sequence number is greater than if it is no more than 2^^31-1
3347 * larger than the current sequence number (using serial arithmetic).
3349 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3350 !(asoc->addip_last_asconf)) {
3351 abort = sctp_make_abort(asoc, asconf_ack,
3352 sizeof(sctp_errhdr_t));
3354 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, NULL, 0);
3355 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3358 /* We are going to ABORT, so we might as well stop
3359 * processing the rest of the chunks in the packet.
3361 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3362 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3363 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3364 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3365 SCTP_ERROR(ECONNABORTED));
3366 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3367 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3368 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3369 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3370 return SCTP_DISPOSITION_ABORT;
3373 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3374 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3375 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3377 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3379 return SCTP_DISPOSITION_CONSUME;
3381 abort = sctp_make_abort(asoc, asconf_ack,
3382 sizeof(sctp_errhdr_t));
3384 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, NULL, 0);
3385 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3388 /* We are going to ABORT, so we might as well stop
3389 * processing the rest of the chunks in the packet.
3391 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
3392 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3393 SCTP_ERROR(ECONNABORTED));
3394 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3395 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3396 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3397 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3398 return SCTP_DISPOSITION_ABORT;
3401 return SCTP_DISPOSITION_DISCARD;
3405 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3407 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3408 * its cumulative TSN point to the value carried in the FORWARD TSN
3409 * chunk, and then MUST further advance its cumulative TSN point locally
3411 * After the above processing, the data receiver MUST stop reporting any
3412 * missing TSNs earlier than or equal to the new cumulative TSN point.
3414 * Verification Tag: 8.5 Verification Tag [Normal verification]
3416 * The return value is the disposition of the chunk.
3418 sctp_disposition_t sctp_sf_eat_fwd_tsn(const struct sctp_endpoint *ep,
3419 const struct sctp_association *asoc,
3420 const sctp_subtype_t type,
3422 sctp_cmd_seq_t *commands)
3424 struct sctp_chunk *chunk = arg;
3425 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3429 if (!sctp_vtag_verify(chunk, asoc)) {
3430 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3432 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3435 /* Make sure that the FORWARD_TSN chunk has valid length. */
3436 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3437 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3440 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3441 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3442 len = ntohs(chunk->chunk_hdr->length);
3443 len -= sizeof(struct sctp_chunkhdr);
3444 skb_pull(chunk->skb, len);
3446 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3447 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
3449 /* The TSN is too high--silently discard the chunk and count on it
3450 * getting retransmitted later.
3452 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3453 goto discard_noforce;
3455 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3456 if (len > sizeof(struct sctp_fwdtsn_hdr))
3457 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3460 /* Count this as receiving DATA. */
3461 if (asoc->autoclose) {
3462 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3463 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3466 /* FIXME: For now send a SACK, but DATA processing may
3469 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3471 return SCTP_DISPOSITION_CONSUME;
3474 return SCTP_DISPOSITION_DISCARD;
3477 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3478 const struct sctp_endpoint *ep,
3479 const struct sctp_association *asoc,
3480 const sctp_subtype_t type,
3482 sctp_cmd_seq_t *commands)
3484 struct sctp_chunk *chunk = arg;
3485 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3489 if (!sctp_vtag_verify(chunk, asoc)) {
3490 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3492 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3495 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3496 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3497 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3500 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3501 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3502 len = ntohs(chunk->chunk_hdr->length);
3503 len -= sizeof(struct sctp_chunkhdr);
3504 skb_pull(chunk->skb, len);
3506 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3507 SCTP_DEBUG_PRINTK("%s: TSN 0x%x.\n", __FUNCTION__, tsn);
3509 /* The TSN is too high--silently discard the chunk and count on it
3510 * getting retransmitted later.
3512 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3515 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3516 if (len > sizeof(struct sctp_fwdtsn_hdr))
3517 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3520 /* Go a head and force a SACK, since we are shutting down. */
3522 /* Implementor's Guide.
3524 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3525 * respond to each received packet containing one or more DATA chunk(s)
3526 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3528 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3529 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3530 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3531 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3533 return SCTP_DISPOSITION_CONSUME;
3537 * Process an unknown chunk.
3539 * Section: 3.2. Also, 2.1 in the implementor's guide.
3541 * Chunk Types are encoded such that the highest-order two bits specify
3542 * the action that must be taken if the processing endpoint does not
3543 * recognize the Chunk Type.
3545 * 00 - Stop processing this SCTP packet and discard it, do not process
3546 * any further chunks within it.
3548 * 01 - Stop processing this SCTP packet and discard it, do not process
3549 * any further chunks within it, and report the unrecognized
3550 * chunk in an 'Unrecognized Chunk Type'.
3552 * 10 - Skip this chunk and continue processing.
3554 * 11 - Skip this chunk and continue processing, but report in an ERROR
3555 * Chunk using the 'Unrecognized Chunk Type' cause of error.
3557 * The return value is the disposition of the chunk.
3559 sctp_disposition_t sctp_sf_unk_chunk(const struct sctp_endpoint *ep,
3560 const struct sctp_association *asoc,
3561 const sctp_subtype_t type,
3563 sctp_cmd_seq_t *commands)
3565 struct sctp_chunk *unk_chunk = arg;
3566 struct sctp_chunk *err_chunk;
3567 sctp_chunkhdr_t *hdr;
3569 SCTP_DEBUG_PRINTK("Processing the unknown chunk id %d.\n", type.chunk);
3571 if (!sctp_vtag_verify(unk_chunk, asoc))
3572 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3574 /* Make sure that the chunk has a valid length.
3575 * Since we don't know the chunk type, we use a general
3576 * chunkhdr structure to make a comparison.
3578 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
3579 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
3582 switch (type.chunk & SCTP_CID_ACTION_MASK) {
3583 case SCTP_CID_ACTION_DISCARD:
3584 /* Discard the packet. */
3585 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3587 case SCTP_CID_ACTION_DISCARD_ERR:
3588 /* Discard the packet. */
3589 sctp_sf_pdiscard(ep, asoc, type, arg, commands);
3591 /* Generate an ERROR chunk as response. */
3592 hdr = unk_chunk->chunk_hdr;
3593 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3594 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3595 WORD_ROUND(ntohs(hdr->length)));
3597 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3598 SCTP_CHUNK(err_chunk));
3600 return SCTP_DISPOSITION_CONSUME;
3602 case SCTP_CID_ACTION_SKIP:
3603 /* Skip the chunk. */
3604 return SCTP_DISPOSITION_DISCARD;
3606 case SCTP_CID_ACTION_SKIP_ERR:
3607 /* Generate an ERROR chunk as response. */
3608 hdr = unk_chunk->chunk_hdr;
3609 err_chunk = sctp_make_op_error(asoc, unk_chunk,
3610 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
3611 WORD_ROUND(ntohs(hdr->length)));
3613 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3614 SCTP_CHUNK(err_chunk));
3616 /* Skip the chunk. */
3617 return SCTP_DISPOSITION_CONSUME;
3623 return SCTP_DISPOSITION_DISCARD;
3627 * Discard the chunk.
3629 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
3630 * [Too numerous to mention...]
3631 * Verification Tag: No verification needed.
3633 * (endpoint, asoc, chunk)
3636 * (asoc, reply_msg, msg_up, timers, counters)
3638 * The return value is the disposition of the chunk.
3640 sctp_disposition_t sctp_sf_discard_chunk(const struct sctp_endpoint *ep,
3641 const struct sctp_association *asoc,
3642 const sctp_subtype_t type,
3644 sctp_cmd_seq_t *commands)
3646 SCTP_DEBUG_PRINTK("Chunk %d is discarded\n", type.chunk);
3647 return SCTP_DISPOSITION_DISCARD;
3651 * Discard the whole packet.
3655 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
3656 * silently discard the OOTB packet and take no further action.
3658 * Verification Tag: No verification necessary
3661 * (endpoint, asoc, chunk)
3664 * (asoc, reply_msg, msg_up, timers, counters)
3666 * The return value is the disposition of the chunk.
3668 sctp_disposition_t sctp_sf_pdiscard(const struct sctp_endpoint *ep,
3669 const struct sctp_association *asoc,
3670 const sctp_subtype_t type,
3672 sctp_cmd_seq_t *commands)
3674 SCTP_INC_STATS(SCTP_MIB_IN_PKT_DISCARDS);
3675 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3677 return SCTP_DISPOSITION_CONSUME;
3682 * The other end is violating protocol.
3684 * Section: Not specified
3685 * Verification Tag: Not specified
3687 * (endpoint, asoc, chunk)
3690 * (asoc, reply_msg, msg_up, timers, counters)
3692 * We simply tag the chunk as a violation. The state machine will log
3693 * the violation and continue.
3695 sctp_disposition_t sctp_sf_violation(const struct sctp_endpoint *ep,
3696 const struct sctp_association *asoc,
3697 const sctp_subtype_t type,
3699 sctp_cmd_seq_t *commands)
3701 return SCTP_DISPOSITION_VIOLATION;
3706 * Handle a protocol violation when the chunk length is invalid.
3707 * "Invalid" length is identified as smaller then the minimal length a
3708 * given chunk can be. For example, a SACK chunk has invalid length
3709 * if it's length is set to be smaller then the size of sctp_sack_chunk_t.
3711 * We inform the other end by sending an ABORT with a Protocol Violation
3714 * Section: Not specified
3715 * Verification Tag: Nothing to do
3717 * (endpoint, asoc, chunk)
3720 * (reply_msg, msg_up, counters)
3722 * Generate an ABORT chunk and terminate the association.
3724 static sctp_disposition_t sctp_sf_violation_chunklen(
3725 const struct sctp_endpoint *ep,
3726 const struct sctp_association *asoc,
3727 const sctp_subtype_t type,
3729 sctp_cmd_seq_t *commands)
3731 struct sctp_chunk *chunk = arg;
3732 struct sctp_chunk *abort = NULL;
3733 char err_str[]="The following chunk had invalid length:";
3735 /* Make the abort chunk. */
3736 abort = sctp_make_abort_violation(asoc, chunk, err_str,
3741 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
3742 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
3744 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
3745 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3746 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
3747 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3748 SCTP_ERROR(ECONNREFUSED));
3749 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
3750 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
3752 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3753 SCTP_ERROR(ECONNABORTED));
3754 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3755 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
3756 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
3759 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3761 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
3763 return SCTP_DISPOSITION_ABORT;
3766 return SCTP_DISPOSITION_NOMEM;
3769 /***************************************************************************
3770 * These are the state functions for handling primitive (Section 10) events.
3771 ***************************************************************************/
3773 * sctp_sf_do_prm_asoc
3775 * Section: 10.1 ULP-to-SCTP
3778 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
3779 * outbound stream count)
3780 * -> association id [,destination transport addr list] [,outbound stream
3783 * This primitive allows the upper layer to initiate an association to a
3784 * specific peer endpoint.
3786 * The peer endpoint shall be specified by one of the transport addresses
3787 * which defines the endpoint (see Section 1.4). If the local SCTP
3788 * instance has not been initialized, the ASSOCIATE is considered an
3790 * [This is not relevant for the kernel implementation since we do all
3791 * initialization at boot time. It we hadn't initialized we wouldn't
3792 * get anywhere near this code.]
3794 * An association id, which is a local handle to the SCTP association,
3795 * will be returned on successful establishment of the association. If
3796 * SCTP is not able to open an SCTP association with the peer endpoint,
3797 * an error is returned.
3798 * [In the kernel implementation, the struct sctp_association needs to
3799 * be created BEFORE causing this primitive to run.]
3801 * Other association parameters may be returned, including the
3802 * complete destination transport addresses of the peer as well as the
3803 * outbound stream count of the local endpoint. One of the transport
3804 * address from the returned destination addresses will be selected by
3805 * the local endpoint as default primary path for sending SCTP packets
3806 * to this peer. The returned "destination transport addr list" can
3807 * be used by the ULP to change the default primary path or to force
3808 * sending a packet to a specific transport address. [All of this
3809 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
3812 * Mandatory attributes:
3814 * o local SCTP instance name - obtained from the INITIALIZE operation.
3815 * [This is the argument asoc.]
3816 * o destination transport addr - specified as one of the transport
3817 * addresses of the peer endpoint with which the association is to be
3819 * [This is asoc->peer.active_path.]
3820 * o outbound stream count - the number of outbound streams the ULP
3821 * would like to open towards this peer endpoint.
3822 * [BUG: This is not currently implemented.]
3823 * Optional attributes:
3827 * The return value is a disposition.
3829 sctp_disposition_t sctp_sf_do_prm_asoc(const struct sctp_endpoint *ep,
3830 const struct sctp_association *asoc,
3831 const sctp_subtype_t type,
3833 sctp_cmd_seq_t *commands)
3835 struct sctp_chunk *repl;
3837 /* The comment below says that we enter COOKIE-WAIT AFTER
3838 * sending the INIT, but that doesn't actually work in our
3841 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3842 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
3844 /* RFC 2960 5.1 Normal Establishment of an Association
3846 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
3847 * must provide its Verification Tag (Tag_A) in the Initiate
3848 * Tag field. Tag_A SHOULD be a random number in the range of
3849 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
3852 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
3856 /* Cast away the const modifier, as we want to just
3857 * rerun it through as a sideffect.
3859 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC,
3860 SCTP_ASOC((struct sctp_association *) asoc));
3862 /* Choose transport for INIT. */
3863 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
3866 /* After sending the INIT, "A" starts the T1-init timer and
3867 * enters the COOKIE-WAIT state.
3869 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
3870 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
3871 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
3872 return SCTP_DISPOSITION_CONSUME;
3875 return SCTP_DISPOSITION_NOMEM;
3879 * Process the SEND primitive.
3881 * Section: 10.1 ULP-to-SCTP
3884 * Format: SEND(association id, buffer address, byte count [,context]
3885 * [,stream id] [,life time] [,destination transport address]
3886 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
3889 * This is the main method to send user data via SCTP.
3891 * Mandatory attributes:
3893 * o association id - local handle to the SCTP association
3895 * o buffer address - the location where the user message to be
3896 * transmitted is stored;
3898 * o byte count - The size of the user data in number of bytes;
3900 * Optional attributes:
3902 * o context - an optional 32 bit integer that will be carried in the
3903 * sending failure notification to the ULP if the transportation of
3904 * this User Message fails.
3906 * o stream id - to indicate which stream to send the data on. If not
3907 * specified, stream 0 will be used.
3909 * o life time - specifies the life time of the user data. The user data
3910 * will not be sent by SCTP after the life time expires. This
3911 * parameter can be used to avoid efforts to transmit stale
3912 * user messages. SCTP notifies the ULP if the data cannot be
3913 * initiated to transport (i.e. sent to the destination via SCTP's
3914 * send primitive) within the life time variable. However, the
3915 * user data will be transmitted if SCTP has attempted to transmit a
3916 * chunk before the life time expired.
3918 * o destination transport address - specified as one of the destination
3919 * transport addresses of the peer endpoint to which this packet
3920 * should be sent. Whenever possible, SCTP should use this destination
3921 * transport address for sending the packets, instead of the current
3924 * o unorder flag - this flag, if present, indicates that the user
3925 * would like the data delivered in an unordered fashion to the peer
3926 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
3929 * o no-bundle flag - instructs SCTP not to bundle this user data with
3930 * other outbound DATA chunks. SCTP MAY still bundle even when
3931 * this flag is present, when faced with network congestion.
3933 * o payload protocol-id - A 32 bit unsigned integer that is to be
3934 * passed to the peer indicating the type of payload protocol data
3935 * being transmitted. This value is passed as opaque data by SCTP.
3937 * The return value is the disposition.
3939 sctp_disposition_t sctp_sf_do_prm_send(const struct sctp_endpoint *ep,
3940 const struct sctp_association *asoc,
3941 const sctp_subtype_t type,
3943 sctp_cmd_seq_t *commands)
3945 struct sctp_chunk *chunk = arg;
3947 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
3948 return SCTP_DISPOSITION_CONSUME;
3952 * Process the SHUTDOWN primitive.
3957 * Format: SHUTDOWN(association id)
3960 * Gracefully closes an association. Any locally queued user data
3961 * will be delivered to the peer. The association will be terminated only
3962 * after the peer acknowledges all the SCTP packets sent. A success code
3963 * will be returned on successful termination of the association. If
3964 * attempting to terminate the association results in a failure, an error
3965 * code shall be returned.
3967 * Mandatory attributes:
3969 * o association id - local handle to the SCTP association
3971 * Optional attributes:
3975 * The return value is the disposition.
3977 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
3978 const struct sctp_endpoint *ep,
3979 const struct sctp_association *asoc,
3980 const sctp_subtype_t type,
3982 sctp_cmd_seq_t *commands)
3986 /* From 9.2 Shutdown of an Association
3987 * Upon receipt of the SHUTDOWN primitive from its upper
3988 * layer, the endpoint enters SHUTDOWN-PENDING state and
3989 * remains there until all outstanding data has been
3990 * acknowledged by its peer. The endpoint accepts no new data
3991 * from its upper layer, but retransmits data to the far end
3992 * if necessary to fill gaps.
3994 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3995 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
3997 /* sctpimpguide-05 Section 2.12.2
3998 * The sender of the SHUTDOWN MAY also start an overall guard timer
3999 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4001 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4002 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4004 disposition = SCTP_DISPOSITION_CONSUME;
4005 if (sctp_outq_is_empty(&asoc->outqueue)) {
4006 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4013 * Process the ABORT primitive.
4018 * Format: Abort(association id [, cause code])
4021 * Ungracefully closes an association. Any locally queued user data
4022 * will be discarded and an ABORT chunk is sent to the peer. A success code
4023 * will be returned on successful abortion of the association. If
4024 * attempting to abort the association results in a failure, an error
4025 * code shall be returned.
4027 * Mandatory attributes:
4029 * o association id - local handle to the SCTP association
4031 * Optional attributes:
4033 * o cause code - reason of the abort to be passed to the peer
4037 * The return value is the disposition.
4039 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4040 const struct sctp_endpoint *ep,
4041 const struct sctp_association *asoc,
4042 const sctp_subtype_t type,
4044 sctp_cmd_seq_t *commands)
4046 /* From 9.1 Abort of an Association
4047 * Upon receipt of the ABORT primitive from its upper
4048 * layer, the endpoint enters CLOSED state and
4049 * discard all outstanding data has been
4050 * acknowledged by its peer. The endpoint accepts no new data
4051 * from its upper layer, but retransmits data to the far end
4052 * if necessary to fill gaps.
4054 struct sctp_chunk *abort = arg;
4055 sctp_disposition_t retval;
4057 retval = SCTP_DISPOSITION_CONSUME;
4059 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4061 /* Even if we can't send the ABORT due to low memory delete the
4062 * TCB. This is a departure from our typical NOMEM handling.
4065 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4066 SCTP_ERROR(ECONNABORTED));
4067 /* Delete the established association. */
4068 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4069 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4071 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4072 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4077 /* We tried an illegal operation on an association which is closed. */
4078 sctp_disposition_t sctp_sf_error_closed(const struct sctp_endpoint *ep,
4079 const struct sctp_association *asoc,
4080 const sctp_subtype_t type,
4082 sctp_cmd_seq_t *commands)
4084 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4085 return SCTP_DISPOSITION_CONSUME;
4088 /* We tried an illegal operation on an association which is shutting
4091 sctp_disposition_t sctp_sf_error_shutdown(const struct sctp_endpoint *ep,
4092 const struct sctp_association *asoc,
4093 const sctp_subtype_t type,
4095 sctp_cmd_seq_t *commands)
4097 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4098 SCTP_ERROR(-ESHUTDOWN));
4099 return SCTP_DISPOSITION_CONSUME;
4103 * sctp_cookie_wait_prm_shutdown
4105 * Section: 4 Note: 2
4110 * The RFC does not explicitly address this issue, but is the route through the
4111 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4116 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4117 const struct sctp_endpoint *ep,
4118 const struct sctp_association *asoc,
4119 const sctp_subtype_t type,
4121 sctp_cmd_seq_t *commands)
4123 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4124 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4126 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4127 SCTP_STATE(SCTP_STATE_CLOSED));
4129 SCTP_INC_STATS(SCTP_MIB_SHUTDOWNS);
4131 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4133 return SCTP_DISPOSITION_DELETE_TCB;
4137 * sctp_cookie_echoed_prm_shutdown
4139 * Section: 4 Note: 2
4144 * The RFC does not explcitly address this issue, but is the route through the
4145 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4150 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4151 const struct sctp_endpoint *ep,
4152 const struct sctp_association *asoc,
4153 const sctp_subtype_t type,
4154 void *arg, sctp_cmd_seq_t *commands)
4156 /* There is a single T1 timer, so we should be able to use
4157 * common function with the COOKIE-WAIT state.
4159 return sctp_sf_cookie_wait_prm_shutdown(ep, asoc, type, arg, commands);
4163 * sctp_sf_cookie_wait_prm_abort
4165 * Section: 4 Note: 2
4170 * The RFC does not explicitly address this issue, but is the route through the
4171 * state table when someone issues an abort while in COOKIE_WAIT state.
4176 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4177 const struct sctp_endpoint *ep,
4178 const struct sctp_association *asoc,
4179 const sctp_subtype_t type,
4181 sctp_cmd_seq_t *commands)
4183 struct sctp_chunk *abort = arg;
4184 sctp_disposition_t retval;
4186 /* Stop T1-init timer */
4187 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4188 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4189 retval = SCTP_DISPOSITION_CONSUME;
4191 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4193 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4194 SCTP_STATE(SCTP_STATE_CLOSED));
4196 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4198 /* Even if we can't send the ABORT due to low memory delete the
4199 * TCB. This is a departure from our typical NOMEM handling.
4202 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4203 SCTP_ERROR(ECONNREFUSED));
4204 /* Delete the established association. */
4205 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4206 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4212 * sctp_sf_cookie_echoed_prm_abort
4214 * Section: 4 Note: 3
4219 * The RFC does not explcitly address this issue, but is the route through the
4220 * state table when someone issues an abort while in COOKIE_ECHOED state.
4225 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
4226 const struct sctp_endpoint *ep,
4227 const struct sctp_association *asoc,
4228 const sctp_subtype_t type,
4230 sctp_cmd_seq_t *commands)
4232 /* There is a single T1 timer, so we should be able to use
4233 * common function with the COOKIE-WAIT state.
4235 return sctp_sf_cookie_wait_prm_abort(ep, asoc, type, arg, commands);
4239 * sctp_sf_shutdown_pending_prm_abort
4244 * The RFC does not explicitly address this issue, but is the route through the
4245 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
4250 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
4251 const struct sctp_endpoint *ep,
4252 const struct sctp_association *asoc,
4253 const sctp_subtype_t type,
4255 sctp_cmd_seq_t *commands)
4257 /* Stop the T5-shutdown guard timer. */
4258 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4259 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4261 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4265 * sctp_sf_shutdown_sent_prm_abort
4270 * The RFC does not explicitly address this issue, but is the route through the
4271 * state table when someone issues an abort while in SHUTDOWN-SENT state.
4276 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
4277 const struct sctp_endpoint *ep,
4278 const struct sctp_association *asoc,
4279 const sctp_subtype_t type,
4281 sctp_cmd_seq_t *commands)
4283 /* Stop the T2-shutdown timer. */
4284 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4285 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4287 /* Stop the T5-shutdown guard timer. */
4288 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4289 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4291 return sctp_sf_do_9_1_prm_abort(ep, asoc, type, arg, commands);
4295 * sctp_sf_cookie_echoed_prm_abort
4300 * The RFC does not explcitly address this issue, but is the route through the
4301 * state table when someone issues an abort while in COOKIE_ECHOED state.
4306 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
4307 const struct sctp_endpoint *ep,
4308 const struct sctp_association *asoc,
4309 const sctp_subtype_t type,
4311 sctp_cmd_seq_t *commands)
4313 /* The same T2 timer, so we should be able to use
4314 * common function with the SHUTDOWN-SENT state.
4316 return sctp_sf_shutdown_sent_prm_abort(ep, asoc, type, arg, commands);
4320 * Process the REQUESTHEARTBEAT primitive
4323 * J) Request Heartbeat
4325 * Format: REQUESTHEARTBEAT(association id, destination transport address)
4329 * Instructs the local endpoint to perform a HeartBeat on the specified
4330 * destination transport address of the given association. The returned
4331 * result should indicate whether the transmission of the HEARTBEAT
4332 * chunk to the destination address is successful.
4334 * Mandatory attributes:
4336 * o association id - local handle to the SCTP association
4338 * o destination transport address - the transport address of the
4339 * association on which a heartbeat should be issued.
4341 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
4342 const struct sctp_endpoint *ep,
4343 const struct sctp_association *asoc,
4344 const sctp_subtype_t type,
4346 sctp_cmd_seq_t *commands)
4348 return sctp_sf_heartbeat(ep, asoc, type, (struct sctp_transport *)arg,
4353 * ADDIP Section 4.1 ASCONF Chunk Procedures
4354 * When an endpoint has an ASCONF signaled change to be sent to the
4355 * remote endpoint it should do A1 to A9
4357 sctp_disposition_t sctp_sf_do_prm_asconf(const struct sctp_endpoint *ep,
4358 const struct sctp_association *asoc,
4359 const sctp_subtype_t type,
4361 sctp_cmd_seq_t *commands)
4363 struct sctp_chunk *chunk = arg;
4365 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
4366 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4367 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4368 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
4369 return SCTP_DISPOSITION_CONSUME;
4373 * Ignore the primitive event
4375 * The return value is the disposition of the primitive.
4377 sctp_disposition_t sctp_sf_ignore_primitive(
4378 const struct sctp_endpoint *ep,
4379 const struct sctp_association *asoc,
4380 const sctp_subtype_t type,
4382 sctp_cmd_seq_t *commands)
4384 SCTP_DEBUG_PRINTK("Primitive type %d is ignored.\n", type.primitive);
4385 return SCTP_DISPOSITION_DISCARD;
4388 /***************************************************************************
4389 * These are the state functions for the OTHER events.
4390 ***************************************************************************/
4393 * Start the shutdown negotiation.
4396 * Once all its outstanding data has been acknowledged, the endpoint
4397 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
4398 * TSN Ack field the last sequential TSN it has received from the peer.
4399 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
4400 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
4401 * with the updated last sequential TSN received from its peer.
4403 * The return value is the disposition.
4405 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
4406 const struct sctp_endpoint *ep,
4407 const struct sctp_association *asoc,
4408 const sctp_subtype_t type,
4410 sctp_cmd_seq_t *commands)
4412 struct sctp_chunk *reply;
4414 /* Once all its outstanding data has been acknowledged, the
4415 * endpoint shall send a SHUTDOWN chunk to its peer including
4416 * in the Cumulative TSN Ack field the last sequential TSN it
4417 * has received from the peer.
4419 reply = sctp_make_shutdown(asoc, NULL);
4423 /* Set the transport for the SHUTDOWN chunk and the timeout for the
4424 * T2-shutdown timer.
4426 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4428 /* It shall then start the T2-shutdown timer */
4429 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4430 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4432 if (asoc->autoclose)
4433 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4434 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4436 /* and enter the SHUTDOWN-SENT state. */
4437 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4438 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
4440 /* sctp-implguide 2.10 Issues with Heartbeating and failover
4442 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
4445 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
4447 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4449 return SCTP_DISPOSITION_CONSUME;
4452 return SCTP_DISPOSITION_NOMEM;
4456 * Generate a SHUTDOWN ACK now that everything is SACK'd.
4460 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
4461 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
4462 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
4463 * endpoint must re-send the SHUTDOWN ACK.
4465 * The return value is the disposition.
4467 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
4468 const struct sctp_endpoint *ep,
4469 const struct sctp_association *asoc,
4470 const sctp_subtype_t type,
4472 sctp_cmd_seq_t *commands)
4474 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
4475 struct sctp_chunk *reply;
4477 /* There are 2 ways of getting here:
4478 * 1) called in response to a SHUTDOWN chunk
4479 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
4481 * For the case (2), the arg parameter is set to NULL. We need
4482 * to check that we have a chunk before accessing it's fields.
4485 if (!sctp_vtag_verify(chunk, asoc))
4486 return sctp_sf_pdiscard(ep, asoc, type, arg, commands);
4488 /* Make sure that the SHUTDOWN chunk has a valid length. */
4489 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
4490 return sctp_sf_violation_chunklen(ep, asoc, type, arg,
4494 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
4495 * shall send a SHUTDOWN ACK ...
4497 reply = sctp_make_shutdown_ack(asoc, chunk);
4501 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
4502 * the T2-shutdown timer.
4504 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4506 /* and start/restart a T2-shutdown timer of its own, */
4507 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4508 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4510 if (asoc->autoclose)
4511 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4512 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
4514 /* Enter the SHUTDOWN-ACK-SENT state. */
4515 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4516 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
4518 /* sctp-implguide 2.10 Issues with Heartbeating and failover
4520 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
4523 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
4525 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4527 return SCTP_DISPOSITION_CONSUME;
4530 return SCTP_DISPOSITION_NOMEM;
4534 * Ignore the event defined as other
4536 * The return value is the disposition of the event.
4538 sctp_disposition_t sctp_sf_ignore_other(const struct sctp_endpoint *ep,
4539 const struct sctp_association *asoc,
4540 const sctp_subtype_t type,
4542 sctp_cmd_seq_t *commands)
4544 SCTP_DEBUG_PRINTK("The event other type %d is ignored\n", type.other);
4545 return SCTP_DISPOSITION_DISCARD;
4548 /************************************************************
4549 * These are the state functions for handling timeout events.
4550 ************************************************************/
4555 * Section: 6.3.3 Handle T3-rtx Expiration
4557 * Whenever the retransmission timer T3-rtx expires for a destination
4558 * address, do the following:
4561 * The return value is the disposition of the chunk.
4563 sctp_disposition_t sctp_sf_do_6_3_3_rtx(const struct sctp_endpoint *ep,
4564 const struct sctp_association *asoc,
4565 const sctp_subtype_t type,
4567 sctp_cmd_seq_t *commands)
4569 struct sctp_transport *transport = arg;
4571 SCTP_INC_STATS(SCTP_MIB_T3_RTX_EXPIREDS);
4573 if (asoc->overall_error_count >= asoc->max_retrans) {
4574 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4575 SCTP_ERROR(ETIMEDOUT));
4576 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
4577 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4578 SCTP_PERR(SCTP_ERROR_NO_ERROR));
4579 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4580 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4581 return SCTP_DISPOSITION_DELETE_TCB;
4584 /* E1) For the destination address for which the timer
4585 * expires, adjust its ssthresh with rules defined in Section
4586 * 7.2.3 and set the cwnd <- MTU.
4589 /* E2) For the destination address for which the timer
4590 * expires, set RTO <- RTO * 2 ("back off the timer"). The
4591 * maximum value discussed in rule C7 above (RTO.max) may be
4592 * used to provide an upper bound to this doubling operation.
4595 /* E3) Determine how many of the earliest (i.e., lowest TSN)
4596 * outstanding DATA chunks for the address for which the
4597 * T3-rtx has expired will fit into a single packet, subject
4598 * to the MTU constraint for the path corresponding to the
4599 * destination transport address to which the retransmission
4600 * is being sent (this may be different from the address for
4601 * which the timer expires [see Section 6.4]). Call this
4602 * value K. Bundle and retransmit those K DATA chunks in a
4603 * single packet to the destination endpoint.
4605 * Note: Any DATA chunks that were sent to the address for
4606 * which the T3-rtx timer expired but did not fit in one MTU
4607 * (rule E3 above), should be marked for retransmission and
4608 * sent as soon as cwnd allows (normally when a SACK arrives).
4611 /* NB: Rules E4 and F1 are implicit in R1. */
4612 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
4614 /* Do some failure management (Section 8.2). */
4615 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
4617 return SCTP_DISPOSITION_CONSUME;
4621 * Generate delayed SACK on timeout
4623 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
4625 * The guidelines on delayed acknowledgement algorithm specified in
4626 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
4627 * acknowledgement SHOULD be generated for at least every second packet
4628 * (not every second DATA chunk) received, and SHOULD be generated
4629 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
4630 * some situations it may be beneficial for an SCTP transmitter to be
4631 * more conservative than the algorithms detailed in this document
4632 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
4633 * the following algorithms allow.
4635 sctp_disposition_t sctp_sf_do_6_2_sack(const struct sctp_endpoint *ep,
4636 const struct sctp_association *asoc,
4637 const sctp_subtype_t type,
4639 sctp_cmd_seq_t *commands)
4641 SCTP_INC_STATS(SCTP_MIB_DELAY_SACK_EXPIREDS);
4642 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4643 return SCTP_DISPOSITION_CONSUME;
4647 * sctp_sf_t1_init_timer_expire
4649 * Section: 4 Note: 2
4654 * RFC 2960 Section 4 Notes
4655 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
4656 * and re-start the T1-init timer without changing state. This MUST
4657 * be repeated up to 'Max.Init.Retransmits' times. After that, the
4658 * endpoint MUST abort the initialization process and report the
4659 * error to SCTP user.
4665 sctp_disposition_t sctp_sf_t1_init_timer_expire(const struct sctp_endpoint *ep,
4666 const struct sctp_association *asoc,
4667 const sctp_subtype_t type,
4669 sctp_cmd_seq_t *commands)
4671 struct sctp_chunk *repl = NULL;
4672 struct sctp_bind_addr *bp;
4673 int attempts = asoc->init_err_counter + 1;
4675 SCTP_DEBUG_PRINTK("Timer T1 expired (INIT).\n");
4676 SCTP_INC_STATS(SCTP_MIB_T1_INIT_EXPIREDS);
4678 if (attempts <= asoc->max_init_attempts) {
4679 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
4680 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
4682 return SCTP_DISPOSITION_NOMEM;
4684 /* Choose transport for INIT. */
4685 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4688 /* Issue a sideeffect to do the needed accounting. */
4689 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
4690 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4692 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4694 SCTP_DEBUG_PRINTK("Giving up on INIT, attempts: %d"
4695 " max_init_attempts: %d\n",
4696 attempts, asoc->max_init_attempts);
4697 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4698 SCTP_ERROR(ETIMEDOUT));
4699 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4700 SCTP_PERR(SCTP_ERROR_NO_ERROR));
4701 return SCTP_DISPOSITION_DELETE_TCB;
4704 return SCTP_DISPOSITION_CONSUME;
4708 * sctp_sf_t1_cookie_timer_expire
4710 * Section: 4 Note: 2
4715 * RFC 2960 Section 4 Notes
4716 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
4717 * COOKIE ECHO and re-start the T1-cookie timer without changing
4718 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
4719 * After that, the endpoint MUST abort the initialization process and
4720 * report the error to SCTP user.
4726 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(const struct sctp_endpoint *ep,
4727 const struct sctp_association *asoc,
4728 const sctp_subtype_t type,
4730 sctp_cmd_seq_t *commands)
4732 struct sctp_chunk *repl = NULL;
4733 int attempts = asoc->init_err_counter + 1;
4735 SCTP_DEBUG_PRINTK("Timer T1 expired (COOKIE-ECHO).\n");
4736 SCTP_INC_STATS(SCTP_MIB_T1_COOKIE_EXPIREDS);
4738 if (attempts <= asoc->max_init_attempts) {
4739 repl = sctp_make_cookie_echo(asoc, NULL);
4741 return SCTP_DISPOSITION_NOMEM;
4743 /* Issue a sideeffect to do the needed accounting. */
4744 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
4745 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
4747 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4749 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4750 SCTP_ERROR(ETIMEDOUT));
4751 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4752 SCTP_PERR(SCTP_ERROR_NO_ERROR));
4753 return SCTP_DISPOSITION_DELETE_TCB;
4756 return SCTP_DISPOSITION_CONSUME;
4759 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
4760 * with the updated last sequential TSN received from its peer.
4762 * An endpoint should limit the number of retransmissions of the
4763 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
4764 * If this threshold is exceeded the endpoint should destroy the TCB and
4765 * MUST report the peer endpoint unreachable to the upper layer (and
4766 * thus the association enters the CLOSED state). The reception of any
4767 * packet from its peer (i.e. as the peer sends all of its queued DATA
4768 * chunks) should clear the endpoint's retransmission count and restart
4769 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
4770 * all of its queued DATA chunks that have not yet been sent.
4772 sctp_disposition_t sctp_sf_t2_timer_expire(const struct sctp_endpoint *ep,
4773 const struct sctp_association *asoc,
4774 const sctp_subtype_t type,
4776 sctp_cmd_seq_t *commands)
4778 struct sctp_chunk *reply = NULL;
4780 SCTP_DEBUG_PRINTK("Timer T2 expired.\n");
4781 SCTP_INC_STATS(SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
4783 if (asoc->overall_error_count >= asoc->max_retrans) {
4784 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4785 SCTP_ERROR(ETIMEDOUT));
4786 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
4787 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4788 SCTP_PERR(SCTP_ERROR_NO_ERROR));
4789 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4790 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
4791 return SCTP_DISPOSITION_DELETE_TCB;
4794 switch (asoc->state) {
4795 case SCTP_STATE_SHUTDOWN_SENT:
4796 reply = sctp_make_shutdown(asoc, NULL);
4799 case SCTP_STATE_SHUTDOWN_ACK_SENT:
4800 reply = sctp_make_shutdown_ack(asoc, NULL);
4811 /* Do some failure management (Section 8.2). */
4812 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
4813 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
4815 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
4816 * the T2-shutdown timer.
4818 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
4820 /* Restart the T2-shutdown timer. */
4821 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4822 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4823 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4824 return SCTP_DISPOSITION_CONSUME;
4827 return SCTP_DISPOSITION_NOMEM;
4831 * ADDIP Section 4.1 ASCONF CHunk Procedures
4832 * If the T4 RTO timer expires the endpoint should do B1 to B5
4834 sctp_disposition_t sctp_sf_t4_timer_expire(
4835 const struct sctp_endpoint *ep,
4836 const struct sctp_association *asoc,
4837 const sctp_subtype_t type,
4839 sctp_cmd_seq_t *commands)
4841 struct sctp_chunk *chunk = asoc->addip_last_asconf;
4842 struct sctp_transport *transport = chunk->transport;
4844 SCTP_INC_STATS(SCTP_MIB_T4_RTO_EXPIREDS);
4846 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
4847 * detection on the appropriate destination address as defined in
4848 * RFC2960 [5] section 8.1 and 8.2.
4850 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
4852 /* Reconfig T4 timer and transport. */
4853 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
4855 /* ADDIP 4.1 B2) Increment the association error counters and perform
4856 * endpoint failure detection on the association as defined in
4857 * RFC2960 [5] section 8.1 and 8.2.
4858 * association error counter is incremented in SCTP_CMD_STRIKE.
4860 if (asoc->overall_error_count >= asoc->max_retrans) {
4861 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4862 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4863 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4864 SCTP_ERROR(ETIMEDOUT));
4865 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4866 SCTP_PERR(SCTP_ERROR_NO_ERROR));
4867 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
4868 SCTP_INC_STATS(SCTP_MIB_CURRESTAB);
4869 return SCTP_DISPOSITION_ABORT;
4872 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
4873 * the ASCONF chunk was sent by doubling the RTO timer value.
4874 * This is done in SCTP_CMD_STRIKE.
4877 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
4878 * choose an alternate destination address (please refer to RFC2960
4879 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
4880 * chunk, it MUST be the same (including its serial number) as the last
4883 sctp_chunk_hold(asoc->addip_last_asconf);
4884 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4885 SCTP_CHUNK(asoc->addip_last_asconf));
4887 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
4888 * destination is selected, then the RTO used will be that of the new
4889 * destination address.
4891 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4892 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
4894 return SCTP_DISPOSITION_CONSUME;
4897 /* sctpimpguide-05 Section 2.12.2
4898 * The sender of the SHUTDOWN MAY also start an overall guard timer
4899 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4900 * At the expiration of this timer the sender SHOULD abort the association
4901 * by sending an ABORT chunk.
4903 sctp_disposition_t sctp_sf_t5_timer_expire(const struct sctp_endpoint *ep,
4904 const struct sctp_association *asoc,
4905 const sctp_subtype_t type,
4907 sctp_cmd_seq_t *commands)
4909 struct sctp_chunk *reply = NULL;
4911 SCTP_DEBUG_PRINTK("Timer T5 expired.\n");
4912 SCTP_INC_STATS(SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
4914 reply = sctp_make_abort(asoc, NULL, 0);
4918 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
4919 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4920 SCTP_ERROR(ETIMEDOUT));
4921 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4922 SCTP_PERR(SCTP_ERROR_NO_ERROR));
4924 return SCTP_DISPOSITION_DELETE_TCB;
4926 return SCTP_DISPOSITION_NOMEM;
4929 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
4930 * the association is automatically closed by starting the shutdown process.
4931 * The work that needs to be done is same as when SHUTDOWN is initiated by
4932 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
4934 sctp_disposition_t sctp_sf_autoclose_timer_expire(
4935 const struct sctp_endpoint *ep,
4936 const struct sctp_association *asoc,
4937 const sctp_subtype_t type,
4939 sctp_cmd_seq_t *commands)
4943 SCTP_INC_STATS(SCTP_MIB_AUTOCLOSE_EXPIREDS);
4945 /* From 9.2 Shutdown of an Association
4946 * Upon receipt of the SHUTDOWN primitive from its upper
4947 * layer, the endpoint enters SHUTDOWN-PENDING state and
4948 * remains there until all outstanding data has been
4949 * acknowledged by its peer. The endpoint accepts no new data
4950 * from its upper layer, but retransmits data to the far end
4951 * if necessary to fill gaps.
4953 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4954 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4956 /* sctpimpguide-05 Section 2.12.2
4957 * The sender of the SHUTDOWN MAY also start an overall guard timer
4958 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
4960 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4961 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
4962 disposition = SCTP_DISPOSITION_CONSUME;
4963 if (sctp_outq_is_empty(&asoc->outqueue)) {
4964 disposition = sctp_sf_do_9_2_start_shutdown(ep, asoc, type,
4970 /*****************************************************************************
4971 * These are sa state functions which could apply to all types of events.
4972 ****************************************************************************/
4975 * This table entry is not implemented.
4978 * (endpoint, asoc, chunk)
4980 * The return value is the disposition of the chunk.
4982 sctp_disposition_t sctp_sf_not_impl(const struct sctp_endpoint *ep,
4983 const struct sctp_association *asoc,
4984 const sctp_subtype_t type,
4986 sctp_cmd_seq_t *commands)
4988 return SCTP_DISPOSITION_NOT_IMPL;
4992 * This table entry represents a bug.
4995 * (endpoint, asoc, chunk)
4997 * The return value is the disposition of the chunk.
4999 sctp_disposition_t sctp_sf_bug(const struct sctp_endpoint *ep,
5000 const struct sctp_association *asoc,
5001 const sctp_subtype_t type,
5003 sctp_cmd_seq_t *commands)
5005 return SCTP_DISPOSITION_BUG;
5009 * This table entry represents the firing of a timer in the wrong state.
5010 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5011 * when the association is in the wrong state. This event should
5012 * be ignored, so as to prevent any rearming of the timer.
5015 * (endpoint, asoc, chunk)
5017 * The return value is the disposition of the chunk.
5019 sctp_disposition_t sctp_sf_timer_ignore(const struct sctp_endpoint *ep,
5020 const struct sctp_association *asoc,
5021 const sctp_subtype_t type,
5023 sctp_cmd_seq_t *commands)
5025 SCTP_DEBUG_PRINTK("Timer %d ignored.\n", type.chunk);
5026 return SCTP_DISPOSITION_CONSUME;
5029 /********************************************************************
5030 * 2nd Level Abstractions
5031 ********************************************************************/
5033 /* Pull the SACK chunk based on the SACK header. */
5034 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5036 struct sctp_sackhdr *sack;
5041 /* Protect ourselves from reading too far into
5042 * the skb from a bogus sender.
5044 sack = (struct sctp_sackhdr *) chunk->skb->data;
5046 num_blocks = ntohs(sack->num_gap_ack_blocks);
5047 num_dup_tsns = ntohs(sack->num_dup_tsns);
5048 len = sizeof(struct sctp_sackhdr);
5049 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5050 if (len > chunk->skb->len)
5053 skb_pull(chunk->skb, len);
5058 /* Create an ABORT packet to be sent as a response, with the specified
5061 static struct sctp_packet *sctp_abort_pkt_new(const struct sctp_endpoint *ep,
5062 const struct sctp_association *asoc,
5063 struct sctp_chunk *chunk,
5064 const void *payload,
5067 struct sctp_packet *packet;
5068 struct sctp_chunk *abort;
5070 packet = sctp_ootb_pkt_new(asoc, chunk);
5074 * The T bit will be set if the asoc is NULL.
5076 abort = sctp_make_abort(asoc, chunk, paylen);
5078 sctp_ootb_pkt_free(packet);
5082 /* Reflect vtag if T-Bit is set */
5083 if (sctp_test_T_bit(abort))
5084 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5086 /* Add specified error causes, i.e., payload, to the
5089 sctp_addto_chunk(abort, paylen, payload);
5091 /* Set the skb to the belonging sock for accounting. */
5092 abort->skb->sk = ep->base.sk;
5094 sctp_packet_append_chunk(packet, abort);
5101 /* Allocate a packet for responding in the OOTB conditions. */
5102 static struct sctp_packet *sctp_ootb_pkt_new(const struct sctp_association *asoc,
5103 const struct sctp_chunk *chunk)
5105 struct sctp_packet *packet;
5106 struct sctp_transport *transport;
5111 /* Get the source and destination port from the inbound packet. */
5112 sport = ntohs(chunk->sctp_hdr->dest);
5113 dport = ntohs(chunk->sctp_hdr->source);
5115 /* The V-tag is going to be the same as the inbound packet if no
5116 * association exists, otherwise, use the peer's vtag.
5119 vtag = asoc->peer.i.init_tag;
5121 /* Special case the INIT and stale COOKIE_ECHO as there is no
5124 switch(chunk->chunk_hdr->type) {
5127 sctp_init_chunk_t *init;
5129 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
5130 vtag = ntohl(init->init_hdr.init_tag);
5134 vtag = ntohl(chunk->sctp_hdr->vtag);
5139 /* Make a transport for the bucket, Eliza... */
5140 transport = sctp_transport_new(sctp_source(chunk), GFP_ATOMIC);
5144 /* Cache a route for the transport with the chunk's destination as
5145 * the source address.
5147 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
5148 sctp_sk(sctp_get_ctl_sock()));
5150 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
5151 packet = sctp_packet_config(packet, vtag, 0);
5159 /* Free the packet allocated earlier for responding in the OOTB condition. */
5160 void sctp_ootb_pkt_free(struct sctp_packet *packet)
5162 sctp_transport_free(packet->transport);
5165 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
5166 static void sctp_send_stale_cookie_err(const struct sctp_endpoint *ep,
5167 const struct sctp_association *asoc,
5168 const struct sctp_chunk *chunk,
5169 sctp_cmd_seq_t *commands,
5170 struct sctp_chunk *err_chunk)
5172 struct sctp_packet *packet;
5175 packet = sctp_ootb_pkt_new(asoc, chunk);
5177 struct sctp_signed_cookie *cookie;
5179 /* Override the OOTB vtag from the cookie. */
5180 cookie = chunk->subh.cookie_hdr;
5181 packet->vtag = cookie->c.peer_vtag;
5183 /* Set the skb to the belonging sock for accounting. */
5184 err_chunk->skb->sk = ep->base.sk;
5185 sctp_packet_append_chunk(packet, err_chunk);
5186 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
5187 SCTP_PACKET(packet));
5188 SCTP_INC_STATS(SCTP_MIB_OUTCTRLCHUNKS);
5190 sctp_chunk_free (err_chunk);
5195 /* Process a data chunk */
5196 static int sctp_eat_data(const struct sctp_association *asoc,
5197 struct sctp_chunk *chunk,
5198 sctp_cmd_seq_t *commands)
5200 sctp_datahdr_t *data_hdr;
5201 struct sctp_chunk *err;
5203 sctp_verb_t deliver;
5207 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
5208 struct sock *sk = asoc->base.sk;
5209 int rcvbuf_over = 0;
5211 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
5212 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
5214 tsn = ntohl(data_hdr->tsn);
5215 SCTP_DEBUG_PRINTK("eat_data: TSN 0x%x.\n", tsn);
5217 /* ASSERT: Now skb->data is really the user data. */
5220 * If we are established, and we have used up our receive buffer
5221 * memory, think about droping the frame.
5222 * Note that we have an opportunity to improve performance here.
5223 * If we accept one chunk from an skbuff, we have to keep all the
5224 * memory of that skbuff around until the chunk is read into user
5225 * space. Therefore, once we accept 1 chunk we may as well accept all
5226 * remaining chunks in the skbuff. The data_accepted flag helps us do
5229 if ((asoc->state == SCTP_STATE_ESTABLISHED) && (!chunk->data_accepted)) {
5231 * If the receive buffer policy is 1, then each
5232 * association can allocate up to sk_rcvbuf bytes
5233 * otherwise, all the associations in aggregate
5234 * may allocate up to sk_rcvbuf bytes
5236 if (asoc->ep->rcvbuf_policy)
5237 account_value = atomic_read(&asoc->rmem_alloc);
5239 account_value = atomic_read(&sk->sk_rmem_alloc);
5240 if (account_value > sk->sk_rcvbuf) {
5242 * We need to make forward progress, even when we are
5243 * under memory pressure, so we always allow the
5244 * next tsn after the ctsn ack point to be accepted.
5245 * This lets us avoid deadlocks in which we have to
5246 * drop frames that would otherwise let us drain the
5249 if ((sctp_tsnmap_get_ctsn(map) + 1) != tsn)
5250 return SCTP_IERROR_IGNORE_TSN;
5253 * We're going to accept the frame but we should renege
5254 * to make space for it. This will send us down that
5255 * path later in this function.
5261 /* Process ECN based congestion.
5263 * Since the chunk structure is reused for all chunks within
5264 * a packet, we use ecn_ce_done to track if we've already
5265 * done CE processing for this packet.
5267 * We need to do ECN processing even if we plan to discard the
5271 if (!chunk->ecn_ce_done) {
5273 chunk->ecn_ce_done = 1;
5275 af = sctp_get_af_specific(
5276 ipver2af(chunk->skb->nh.iph->version));
5278 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
5279 /* Do real work as sideffect. */
5280 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
5285 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
5287 /* The TSN is too high--silently discard the chunk and
5288 * count on it getting retransmitted later.
5290 return SCTP_IERROR_HIGH_TSN;
5291 } else if (tmp > 0) {
5292 /* This is a duplicate. Record it. */
5293 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
5294 return SCTP_IERROR_DUP_TSN;
5297 /* This is a new TSN. */
5299 /* Discard if there is no room in the receive window.
5300 * Actually, allow a little bit of overflow (up to a MTU).
5302 datalen = ntohs(chunk->chunk_hdr->length);
5303 datalen -= sizeof(sctp_data_chunk_t);
5305 deliver = SCTP_CMD_CHUNK_ULP;
5307 /* Think about partial delivery. */
5308 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
5310 /* Even if we don't accept this chunk there is
5313 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
5316 /* Spill over rwnd a little bit. Note: While allowed, this spill over
5317 * seems a bit troublesome in that frag_point varies based on
5318 * PMTU. In cases, such as loopback, this might be a rather
5320 * NOTE: If we have a full receive buffer here, we only renege if
5321 * our receiver can still make progress without the tsn being
5322 * received. We do this because in the event that the associations
5323 * receive queue is empty we are filling a leading gap, and since
5324 * reneging moves the gap to the end of the tsn stream, we are likely
5325 * to stall again very shortly. Avoiding the renege when we fill a
5326 * leading gap is a good heuristic for avoiding such steady state
5329 if (!asoc->rwnd || asoc->rwnd_over ||
5330 (datalen > asoc->rwnd + asoc->frag_point) ||
5331 (rcvbuf_over && (!skb_queue_len(&sk->sk_receive_queue)))) {
5333 /* If this is the next TSN, consider reneging to make
5334 * room. Note: Playing nice with a confused sender. A
5335 * malicious sender can still eat up all our buffer
5336 * space and in the future we may want to detect and
5337 * do more drastic reneging.
5339 if (sctp_tsnmap_has_gap(map) &&
5340 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
5341 SCTP_DEBUG_PRINTK("Reneging for tsn:%u\n", tsn);
5342 deliver = SCTP_CMD_RENEGE;
5344 SCTP_DEBUG_PRINTK("Discard tsn: %u len: %Zd, "
5345 "rwnd: %d\n", tsn, datalen,
5347 return SCTP_IERROR_IGNORE_TSN;
5352 * Section 3.3.10.9 No User Data (9)
5356 * No User Data: This error cause is returned to the originator of a
5357 * DATA chunk if a received DATA chunk has no user data.
5359 if (unlikely(0 == datalen)) {
5360 err = sctp_make_abort_no_data(asoc, chunk, tsn);
5362 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5365 /* We are going to ABORT, so we might as well stop
5366 * processing the rest of the chunks in the packet.
5368 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET,SCTP_NULL());
5369 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5370 SCTP_ERROR(ECONNABORTED));
5371 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5372 SCTP_PERR(SCTP_ERROR_NO_DATA));
5373 SCTP_INC_STATS(SCTP_MIB_ABORTEDS);
5374 SCTP_DEC_STATS(SCTP_MIB_CURRESTAB);
5375 return SCTP_IERROR_NO_DATA;
5378 /* If definately accepting the DATA chunk, record its TSN, otherwise
5379 * wait for renege processing.
5381 if (SCTP_CMD_CHUNK_ULP == deliver)
5382 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
5384 chunk->data_accepted = 1;
5386 /* Note: Some chunks may get overcounted (if we drop) or overcounted
5387 * if we renege and the chunk arrives again.
5389 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED)
5390 SCTP_INC_STATS(SCTP_MIB_INUNORDERCHUNKS);
5392 SCTP_INC_STATS(SCTP_MIB_INORDERCHUNKS);
5394 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
5396 * If an endpoint receive a DATA chunk with an invalid stream
5397 * identifier, it shall acknowledge the reception of the DATA chunk
5398 * following the normal procedure, immediately send an ERROR chunk
5399 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
5400 * and discard the DATA chunk.
5402 if (ntohs(data_hdr->stream) >= asoc->c.sinit_max_instreams) {
5403 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
5405 sizeof(data_hdr->stream));
5407 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5409 return SCTP_IERROR_BAD_STREAM;
5412 /* Send the data up to the user. Note: Schedule the
5413 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
5414 * chunk needs the updated rwnd.
5416 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
5418 return SCTP_IERROR_NO_ERROR;