1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2001, 2004
3 * Copyright (c) 1999-2000 Cisco, Inc.
4 * Copyright (c) 1999-2001 Motorola, Inc.
5 * Copyright (c) 2001-2002 Intel Corp.
6 * Copyright (c) 2002 Nokia Corp.
8 * This is part of the SCTP Linux Kernel Implementation.
10 * These are the state functions for the state machine.
12 * This SCTP implementation is free software;
13 * you can redistribute it and/or modify it under the terms of
14 * the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This SCTP implementation is distributed in the hope that it
19 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
20 * ************************
21 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 * See the GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with GNU CC; see the file COPYING. If not, see
26 * <http://www.gnu.org/licenses/>.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <linux-sctp@vger.kernel.org>
32 * Written or modified by:
33 * La Monte H.P. Yarroll <piggy@acm.org>
34 * Karl Knutson <karl@athena.chicago.il.us>
35 * Mathew Kotowsky <kotowsky@sctp.org>
36 * Sridhar Samudrala <samudrala@us.ibm.com>
37 * Jon Grimm <jgrimm@us.ibm.com>
38 * Hui Huang <hui.huang@nokia.com>
39 * Dajiang Zhang <dajiang.zhang@nokia.com>
40 * Daisy Chang <daisyc@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
48 #include <linux/types.h>
49 #include <linux/kernel.h>
51 #include <linux/ipv6.h>
52 #include <linux/net.h>
53 #include <linux/inet.h>
54 #include <linux/slab.h>
56 #include <net/inet_ecn.h>
57 #include <linux/skbuff.h>
58 #include <net/sctp/sctp.h>
59 #include <net/sctp/sm.h>
60 #include <net/sctp/structs.h>
62 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
63 const struct sctp_endpoint *ep,
64 const struct sctp_association *asoc,
65 struct sctp_chunk *chunk,
68 static int sctp_eat_data(const struct sctp_association *asoc,
69 struct sctp_chunk *chunk,
70 sctp_cmd_seq_t *commands);
71 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
72 const struct sctp_association *asoc,
73 const struct sctp_chunk *chunk);
74 static void sctp_send_stale_cookie_err(struct net *net,
75 const struct sctp_endpoint *ep,
76 const struct sctp_association *asoc,
77 const struct sctp_chunk *chunk,
78 sctp_cmd_seq_t *commands,
79 struct sctp_chunk *err_chunk);
80 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
81 const struct sctp_endpoint *ep,
82 const struct sctp_association *asoc,
83 const sctp_subtype_t type,
85 sctp_cmd_seq_t *commands);
86 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
87 const struct sctp_endpoint *ep,
88 const struct sctp_association *asoc,
89 const sctp_subtype_t type,
91 sctp_cmd_seq_t *commands);
92 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
93 const struct sctp_endpoint *ep,
94 const struct sctp_association *asoc,
95 const sctp_subtype_t type,
97 sctp_cmd_seq_t *commands);
98 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk);
100 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
101 sctp_cmd_seq_t *commands,
102 __be16 error, int sk_err,
103 const struct sctp_association *asoc,
104 struct sctp_transport *transport);
106 static sctp_disposition_t sctp_sf_abort_violation(
108 const struct sctp_endpoint *ep,
109 const struct sctp_association *asoc,
111 sctp_cmd_seq_t *commands,
113 const size_t paylen);
115 static sctp_disposition_t sctp_sf_violation_chunklen(
117 const struct sctp_endpoint *ep,
118 const struct sctp_association *asoc,
119 const sctp_subtype_t type,
121 sctp_cmd_seq_t *commands);
123 static sctp_disposition_t sctp_sf_violation_paramlen(
125 const struct sctp_endpoint *ep,
126 const struct sctp_association *asoc,
127 const sctp_subtype_t type,
128 void *arg, void *ext,
129 sctp_cmd_seq_t *commands);
131 static sctp_disposition_t sctp_sf_violation_ctsn(
133 const struct sctp_endpoint *ep,
134 const struct sctp_association *asoc,
135 const sctp_subtype_t type,
137 sctp_cmd_seq_t *commands);
139 static sctp_disposition_t sctp_sf_violation_chunk(
141 const struct sctp_endpoint *ep,
142 const struct sctp_association *asoc,
143 const sctp_subtype_t type,
145 sctp_cmd_seq_t *commands);
147 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
148 const struct sctp_endpoint *ep,
149 const struct sctp_association *asoc,
150 const sctp_subtype_t type,
151 struct sctp_chunk *chunk);
153 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
154 const struct sctp_endpoint *ep,
155 const struct sctp_association *asoc,
156 const sctp_subtype_t type,
158 sctp_cmd_seq_t *commands);
160 /* Small helper function that checks if the chunk length
161 * is of the appropriate length. The 'required_length' argument
162 * is set to be the size of a specific chunk we are testing.
163 * Return Values: true = Valid length
164 * false = Invalid length
168 sctp_chunk_length_valid(struct sctp_chunk *chunk, __u16 required_length)
170 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
172 /* Previously already marked? */
173 if (unlikely(chunk->pdiscard))
175 if (unlikely(chunk_length < required_length))
181 /**********************************************************
182 * These are the state functions for handling chunk events.
183 **********************************************************/
186 * Process the final SHUTDOWN COMPLETE.
188 * Section: 4 (C) (diagram), 9.2
189 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
190 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
191 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
192 * should stop the T2-shutdown timer and remove all knowledge of the
193 * association (and thus the association enters the CLOSED state).
195 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
196 * C) Rules for packet carrying SHUTDOWN COMPLETE:
198 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
199 * if the Verification Tag field of the packet matches its own tag and
200 * the T bit is not set
202 * it is set to its peer's tag and the T bit is set in the Chunk
204 * Otherwise, the receiver MUST silently discard the packet
205 * and take no further action. An endpoint MUST ignore the
206 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
209 * (endpoint, asoc, chunk)
212 * (asoc, reply_msg, msg_up, timers, counters)
214 * The return value is the disposition of the chunk.
216 sctp_disposition_t sctp_sf_do_4_C(struct net *net,
217 const struct sctp_endpoint *ep,
218 const struct sctp_association *asoc,
219 const sctp_subtype_t type,
221 sctp_cmd_seq_t *commands)
223 struct sctp_chunk *chunk = arg;
224 struct sctp_ulpevent *ev;
226 if (!sctp_vtag_verify_either(chunk, asoc))
227 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
229 /* RFC 2960 6.10 Bundling
231 * An endpoint MUST NOT bundle INIT, INIT ACK or
232 * SHUTDOWN COMPLETE with any other chunks.
234 if (!chunk->singleton)
235 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
237 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
238 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
239 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
242 /* RFC 2960 10.2 SCTP-to-ULP
244 * H) SHUTDOWN COMPLETE notification
246 * When SCTP completes the shutdown procedures (section 9.2) this
247 * notification is passed to the upper layer.
249 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
250 0, 0, 0, NULL, GFP_ATOMIC);
252 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
255 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
256 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
257 * not the chunk should be discarded. If the endpoint is in
258 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
259 * T2-shutdown timer and remove all knowledge of the
260 * association (and thus the association enters the CLOSED
263 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
264 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
266 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
267 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
269 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
270 SCTP_STATE(SCTP_STATE_CLOSED));
272 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
273 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
275 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
277 return SCTP_DISPOSITION_DELETE_TCB;
281 * Respond to a normal INIT chunk.
282 * We are the side that is being asked for an association.
284 * Section: 5.1 Normal Establishment of an Association, B
285 * B) "Z" shall respond immediately with an INIT ACK chunk. The
286 * destination IP address of the INIT ACK MUST be set to the source
287 * IP address of the INIT to which this INIT ACK is responding. In
288 * the response, besides filling in other parameters, "Z" must set the
289 * Verification Tag field to Tag_A, and also provide its own
290 * Verification Tag (Tag_Z) in the Initiate Tag field.
292 * Verification Tag: Must be 0.
295 * (endpoint, asoc, chunk)
298 * (asoc, reply_msg, msg_up, timers, counters)
300 * The return value is the disposition of the chunk.
302 sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
303 const struct sctp_endpoint *ep,
304 const struct sctp_association *asoc,
305 const sctp_subtype_t type,
307 sctp_cmd_seq_t *commands)
309 struct sctp_chunk *chunk = arg;
310 struct sctp_chunk *repl;
311 struct sctp_association *new_asoc;
312 struct sctp_chunk *err_chunk;
313 struct sctp_packet *packet;
314 sctp_unrecognized_param_t *unk_param;
318 * An endpoint MUST NOT bundle INIT, INIT ACK or
319 * SHUTDOWN COMPLETE with any other chunks.
322 * Furthermore, we require that the receiver of an INIT chunk MUST
323 * enforce these rules by silently discarding an arriving packet
324 * with an INIT chunk that is bundled with other chunks.
326 if (!chunk->singleton)
327 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
329 /* If the packet is an OOTB packet which is temporarily on the
330 * control endpoint, respond with an ABORT.
332 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
333 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
334 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
337 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
340 if (chunk->sctp_hdr->vtag != 0)
341 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
343 /* Make sure that the INIT chunk has a valid length.
344 * Normally, this would cause an ABORT with a Protocol Violation
345 * error, but since we don't have an association, we'll
346 * just discard the packet.
348 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
349 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
351 /* If the INIT is coming toward a closing socket, we'll send back
352 * and ABORT. Essentially, this catches the race of INIT being
353 * backloged to the socket at the same time as the user isses close().
354 * Since the socket and all its associations are going away, we
355 * can treat this OOTB
357 if (sctp_sstate(ep->base.sk, CLOSING))
358 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
360 /* Verify the INIT chunk before processing it. */
362 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
363 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
365 /* This chunk contains fatal error. It is to be discarded.
366 * Send an ABORT, with causes if there is any.
369 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
370 (__u8 *)(err_chunk->chunk_hdr) +
371 sizeof(struct sctp_chunkhdr),
372 ntohs(err_chunk->chunk_hdr->length) -
373 sizeof(struct sctp_chunkhdr));
375 sctp_chunk_free(err_chunk);
378 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
379 SCTP_PACKET(packet));
380 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
381 return SCTP_DISPOSITION_CONSUME;
383 return SCTP_DISPOSITION_NOMEM;
386 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
391 /* Grab the INIT header. */
392 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
394 /* Tag the variable length parameters. */
395 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
397 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
401 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
402 sctp_scope(sctp_source(chunk)),
406 /* The call, sctp_process_init(), can fail on memory allocation. */
407 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
408 (struct sctp_init_chunk *)chunk->chunk_hdr,
412 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
414 /* If there are errors need to be reported for unknown parameters,
415 * make sure to reserve enough room in the INIT ACK for them.
419 len = ntohs(err_chunk->chunk_hdr->length) -
420 sizeof(struct sctp_chunkhdr);
422 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
426 /* If there are errors need to be reported for unknown parameters,
427 * include them in the outgoing INIT ACK as "Unrecognized parameter"
431 /* Get the "Unrecognized parameter" parameter(s) out of the
432 * ERROR chunk generated by sctp_verify_init(). Since the
433 * error cause code for "unknown parameter" and the
434 * "Unrecognized parameter" type is the same, we can
435 * construct the parameters in INIT ACK by copying the
438 unk_param = (sctp_unrecognized_param_t *)
439 ((__u8 *)(err_chunk->chunk_hdr) +
440 sizeof(struct sctp_chunkhdr));
441 /* Replace the cause code with the "Unrecognized parameter"
444 sctp_addto_chunk(repl, len, unk_param);
445 sctp_chunk_free(err_chunk);
448 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
450 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
453 * Note: After sending out INIT ACK with the State Cookie parameter,
454 * "Z" MUST NOT allocate any resources, nor keep any states for the
455 * new association. Otherwise, "Z" will be vulnerable to resource
458 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
460 return SCTP_DISPOSITION_DELETE_TCB;
463 sctp_association_free(new_asoc);
466 sctp_chunk_free(err_chunk);
467 return SCTP_DISPOSITION_NOMEM;
471 * Respond to a normal INIT ACK chunk.
472 * We are the side that is initiating the association.
474 * Section: 5.1 Normal Establishment of an Association, C
475 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
476 * timer and leave COOKIE-WAIT state. "A" shall then send the State
477 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
478 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
480 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
481 * DATA chunks, but it MUST be the first chunk in the packet and
482 * until the COOKIE ACK is returned the sender MUST NOT send any
483 * other packets to the peer.
485 * Verification Tag: 3.3.3
486 * If the value of the Initiate Tag in a received INIT ACK chunk is
487 * found to be 0, the receiver MUST treat it as an error and close the
488 * association by transmitting an ABORT.
491 * (endpoint, asoc, chunk)
494 * (asoc, reply_msg, msg_up, timers, counters)
496 * The return value is the disposition of the chunk.
498 sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
499 const struct sctp_endpoint *ep,
500 const struct sctp_association *asoc,
501 const sctp_subtype_t type,
503 sctp_cmd_seq_t *commands)
505 struct sctp_chunk *chunk = arg;
506 struct sctp_init_chunk *initchunk;
507 struct sctp_chunk *err_chunk;
508 struct sctp_packet *packet;
510 if (!sctp_vtag_verify(chunk, asoc))
511 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
514 * An endpoint MUST NOT bundle INIT, INIT ACK or
515 * SHUTDOWN COMPLETE with any other chunks.
517 if (!chunk->singleton)
518 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
520 /* Make sure that the INIT-ACK chunk has a valid length */
521 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
522 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
524 /* Grab the INIT header. */
525 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
527 /* Verify the INIT chunk before processing it. */
529 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
530 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
533 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
535 /* This chunk contains fatal error. It is to be discarded.
536 * Send an ABORT, with causes. If there are no causes,
537 * then there wasn't enough memory. Just terminate
541 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
542 (__u8 *)(err_chunk->chunk_hdr) +
543 sizeof(struct sctp_chunkhdr),
544 ntohs(err_chunk->chunk_hdr->length) -
545 sizeof(struct sctp_chunkhdr));
547 sctp_chunk_free(err_chunk);
550 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
551 SCTP_PACKET(packet));
552 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
553 error = SCTP_ERROR_INV_PARAM;
557 /* SCTP-AUTH, Section 6.3:
558 * It should be noted that if the receiver wants to tear
559 * down an association in an authenticated way only, the
560 * handling of malformed packets should not result in
561 * tearing down the association.
563 * This means that if we only want to abort associations
564 * in an authenticated way (i.e AUTH+ABORT), then we
565 * can't destroy this association just because the packet
568 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
569 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
571 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
572 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
573 asoc, chunk->transport);
576 /* Tag the variable length parameters. Note that we never
577 * convert the parameters in an INIT chunk.
579 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
581 initchunk = (struct sctp_init_chunk *)chunk->chunk_hdr;
583 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
584 SCTP_PEER_INIT(initchunk));
586 /* Reset init error count upon receipt of INIT-ACK. */
587 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
589 /* 5.1 C) "A" shall stop the T1-init timer and leave
590 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
591 * timer, and enter the COOKIE-ECHOED state.
593 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
594 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
595 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
596 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
597 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
598 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
600 /* SCTP-AUTH: genereate the assocition shared keys so that
601 * we can potentially signe the COOKIE-ECHO.
603 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
605 /* 5.1 C) "A" shall then send the State Cookie received in the
606 * INIT ACK chunk in a COOKIE ECHO chunk, ...
608 /* If there is any errors to report, send the ERROR chunk generated
609 * for unknown parameters as well.
611 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
612 SCTP_CHUNK(err_chunk));
614 return SCTP_DISPOSITION_CONSUME;
618 * Respond to a normal COOKIE ECHO chunk.
619 * We are the side that is being asked for an association.
621 * Section: 5.1 Normal Establishment of an Association, D
622 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
623 * with a COOKIE ACK chunk after building a TCB and moving to
624 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
625 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
626 * chunk MUST be the first chunk in the packet.
628 * IMPLEMENTATION NOTE: An implementation may choose to send the
629 * Communication Up notification to the SCTP user upon reception
630 * of a valid COOKIE ECHO chunk.
632 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
633 * D) Rules for packet carrying a COOKIE ECHO
635 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
636 * Initial Tag received in the INIT ACK.
638 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
641 * (endpoint, asoc, chunk)
644 * (asoc, reply_msg, msg_up, timers, counters)
646 * The return value is the disposition of the chunk.
648 sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
649 const struct sctp_endpoint *ep,
650 const struct sctp_association *asoc,
651 const sctp_subtype_t type, void *arg,
652 sctp_cmd_seq_t *commands)
654 struct sctp_chunk *chunk = arg;
655 struct sctp_association *new_asoc;
656 struct sctp_init_chunk *peer_init;
657 struct sctp_chunk *repl;
658 struct sctp_ulpevent *ev, *ai_ev = NULL;
660 struct sctp_chunk *err_chk_p;
663 /* If the packet is an OOTB packet which is temporarily on the
664 * control endpoint, respond with an ABORT.
666 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
667 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
668 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
671 /* Make sure that the COOKIE_ECHO chunk has a valid length.
672 * In this case, we check that we have enough for at least a
673 * chunk header. More detailed verification is done
674 * in sctp_unpack_cookie().
676 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
677 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
679 /* If the endpoint is not listening or if the number of associations
680 * on the TCP-style socket exceed the max backlog, respond with an
684 if (!sctp_sstate(sk, LISTENING) ||
685 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
686 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
688 /* "Decode" the chunk. We have no optional parameters so we
691 chunk->subh.cookie_hdr =
692 (struct sctp_signed_cookie *)chunk->skb->data;
693 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
694 sizeof(struct sctp_chunkhdr)))
697 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
698 * "Z" will reply with a COOKIE ACK chunk after building a TCB
699 * and moving to the ESTABLISHED state.
701 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
705 * If the re-build failed, what is the proper error path
708 * [We should abort the association. --piggy]
711 /* FIXME: Several errors are possible. A bad cookie should
712 * be silently discarded, but think about logging it too.
715 case -SCTP_IERROR_NOMEM:
718 case -SCTP_IERROR_STALE_COOKIE:
719 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
721 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
723 case -SCTP_IERROR_BAD_SIG:
725 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
730 /* Delay state machine commands until later.
732 * Re-build the bind address for the association is done in
733 * the sctp_unpack_cookie() already.
735 /* This is a brand-new association, so these are not yet side
736 * effects--it is safe to run them here.
738 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
740 if (!sctp_process_init(new_asoc, chunk,
741 &chunk->subh.cookie_hdr->c.peer_addr,
742 peer_init, GFP_ATOMIC))
745 /* SCTP-AUTH: Now that we've populate required fields in
746 * sctp_process_init, set up the assocaition shared keys as
747 * necessary so that we can potentially authenticate the ACK
749 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
753 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
754 * is supposed to be authenticated and we have to do delayed
755 * authentication. We've just recreated the association using
756 * the information in the cookie and now it's much easier to
757 * do the authentication.
759 if (chunk->auth_chunk) {
760 struct sctp_chunk auth;
763 /* Make sure that we and the peer are AUTH capable */
764 if (!net->sctp.auth_enable || !new_asoc->peer.auth_capable) {
765 sctp_association_free(new_asoc);
766 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
769 /* set-up our fake chunk so that we can process it */
770 auth.skb = chunk->auth_chunk;
771 auth.asoc = chunk->asoc;
772 auth.sctp_hdr = chunk->sctp_hdr;
773 auth.chunk_hdr = (struct sctp_chunkhdr *)
774 skb_push(chunk->auth_chunk,
775 sizeof(struct sctp_chunkhdr));
776 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
777 auth.transport = chunk->transport;
779 ret = sctp_sf_authenticate(net, ep, new_asoc, type, &auth);
780 if (ret != SCTP_IERROR_NO_ERROR) {
781 sctp_association_free(new_asoc);
782 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
786 repl = sctp_make_cookie_ack(new_asoc, chunk);
790 /* RFC 2960 5.1 Normal Establishment of an Association
792 * D) IMPLEMENTATION NOTE: An implementation may choose to
793 * send the Communication Up notification to the SCTP user
794 * upon reception of a valid COOKIE ECHO chunk.
796 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
797 new_asoc->c.sinit_num_ostreams,
798 new_asoc->c.sinit_max_instreams,
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 (new_asoc->peer.adaptation_ind) {
809 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
815 /* Add all the state machine commands now since we've created
816 * everything. This way we don't introduce memory corruptions
817 * during side-effect processing and correclty count established
820 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
821 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
822 SCTP_STATE(SCTP_STATE_ESTABLISHED));
823 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
824 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
825 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
827 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
828 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
829 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
831 /* This will send the COOKIE ACK */
832 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
834 /* Queue the ASSOC_CHANGE event */
835 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
837 /* Send up the Adaptation Layer Indication event */
839 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
840 SCTP_ULPEVENT(ai_ev));
842 return SCTP_DISPOSITION_CONSUME;
845 sctp_ulpevent_free(ev);
847 sctp_chunk_free(repl);
849 sctp_association_free(new_asoc);
851 return SCTP_DISPOSITION_NOMEM;
855 * Respond to a normal COOKIE ACK chunk.
856 * We are the side that is asking for an association.
858 * RFC 2960 5.1 Normal Establishment of an Association
860 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
861 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
862 * timer. It may also notify its ULP about the successful
863 * establishment of the association with a Communication Up
864 * notification (see Section 10).
868 * (endpoint, asoc, chunk)
871 * (asoc, reply_msg, msg_up, timers, counters)
873 * The return value is the disposition of the chunk.
875 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
876 const struct sctp_endpoint *ep,
877 const struct sctp_association *asoc,
878 const sctp_subtype_t type, void *arg,
879 sctp_cmd_seq_t *commands)
881 struct sctp_chunk *chunk = arg;
882 struct sctp_ulpevent *ev;
884 if (!sctp_vtag_verify(chunk, asoc))
885 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
887 /* Verify that the chunk length for the COOKIE-ACK is OK.
888 * If we don't do this, any bundled chunks may be junked.
890 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
891 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
894 /* Reset init error count upon receipt of COOKIE-ACK,
895 * to avoid problems with the managemement of this
896 * counter in stale cookie situations when a transition back
897 * from the COOKIE-ECHOED state to the COOKIE-WAIT
898 * state is performed.
900 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
902 /* RFC 2960 5.1 Normal Establishment of an Association
904 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
905 * from the COOKIE-ECHOED state to the ESTABLISHED state,
906 * stopping the T1-cookie timer.
908 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
909 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
910 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
911 SCTP_STATE(SCTP_STATE_ESTABLISHED));
912 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
913 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
914 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
915 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
916 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
917 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
919 /* It may also notify its ULP about the successful
920 * establishment of the association with a Communication Up
921 * notification (see Section 10).
923 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
924 0, asoc->c.sinit_num_ostreams,
925 asoc->c.sinit_max_instreams,
931 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
933 /* Sockets API Draft Section 5.3.1.6
934 * When a peer sends a Adaptation Layer Indication parameter , SCTP
935 * delivers this notification to inform the application that of the
936 * peers requested adaptation layer.
938 if (asoc->peer.adaptation_ind) {
939 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
943 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
947 return SCTP_DISPOSITION_CONSUME;
949 return SCTP_DISPOSITION_NOMEM;
952 /* Generate and sendout a heartbeat packet. */
953 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
954 const struct sctp_association *asoc,
955 const sctp_subtype_t type,
957 sctp_cmd_seq_t *commands)
959 struct sctp_transport *transport = (struct sctp_transport *) arg;
960 struct sctp_chunk *reply;
962 /* Send a heartbeat to our peer. */
963 reply = sctp_make_heartbeat(asoc, transport);
965 return SCTP_DISPOSITION_NOMEM;
967 /* Set rto_pending indicating that an RTT measurement
968 * is started with this heartbeat chunk.
970 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
971 SCTP_TRANSPORT(transport));
973 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
974 return SCTP_DISPOSITION_CONSUME;
977 /* Generate a HEARTBEAT packet on the given transport. */
978 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
979 const struct sctp_endpoint *ep,
980 const struct sctp_association *asoc,
981 const sctp_subtype_t type,
983 sctp_cmd_seq_t *commands)
985 struct sctp_transport *transport = (struct sctp_transport *) arg;
987 if (asoc->overall_error_count >= asoc->max_retrans) {
988 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
989 SCTP_ERROR(ETIMEDOUT));
990 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
991 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
992 SCTP_PERR(SCTP_ERROR_NO_ERROR));
993 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
994 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
995 return SCTP_DISPOSITION_DELETE_TCB;
999 * The Sender-specific Heartbeat Info field should normally include
1000 * information about the sender's current time when this HEARTBEAT
1001 * chunk is sent and the destination transport address to which this
1002 * HEARTBEAT is sent (see Section 8.3).
1005 if (transport->param_flags & SPP_HB_ENABLE) {
1006 if (SCTP_DISPOSITION_NOMEM ==
1007 sctp_sf_heartbeat(ep, asoc, type, arg,
1009 return SCTP_DISPOSITION_NOMEM;
1011 /* Set transport error counter and association error counter
1012 * when sending heartbeat.
1014 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1015 SCTP_TRANSPORT(transport));
1017 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1018 SCTP_TRANSPORT(transport));
1019 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1020 SCTP_TRANSPORT(transport));
1022 return SCTP_DISPOSITION_CONSUME;
1025 /* resend asoc strreset_chunk. */
1026 sctp_disposition_t sctp_sf_send_reconf(struct net *net,
1027 const struct sctp_endpoint *ep,
1028 const struct sctp_association *asoc,
1029 const sctp_subtype_t type, void *arg,
1030 sctp_cmd_seq_t *commands)
1032 struct sctp_transport *transport = arg;
1034 if (asoc->overall_error_count >= asoc->max_retrans) {
1035 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1036 SCTP_ERROR(ETIMEDOUT));
1037 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1038 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1039 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1040 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1041 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1042 return SCTP_DISPOSITION_DELETE_TCB;
1045 sctp_chunk_hold(asoc->strreset_chunk);
1046 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1047 SCTP_CHUNK(asoc->strreset_chunk));
1048 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
1050 return SCTP_DISPOSITION_CONSUME;
1054 * Process an heartbeat request.
1056 * Section: 8.3 Path Heartbeat
1057 * The receiver of the HEARTBEAT should immediately respond with a
1058 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1059 * from the received HEARTBEAT chunk.
1061 * Verification Tag: 8.5 Verification Tag [Normal verification]
1062 * When receiving an SCTP packet, the endpoint MUST ensure that the
1063 * value in the Verification Tag field of the received SCTP packet
1064 * matches its own Tag. If the received Verification Tag value does not
1065 * match the receiver's own tag value, the receiver shall silently
1066 * discard the packet and shall not process it any further except for
1067 * those cases listed in Section 8.5.1 below.
1070 * (endpoint, asoc, chunk)
1073 * (asoc, reply_msg, msg_up, timers, counters)
1075 * The return value is the disposition of the chunk.
1077 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1078 const struct sctp_endpoint *ep,
1079 const struct sctp_association *asoc,
1080 const sctp_subtype_t type,
1082 sctp_cmd_seq_t *commands)
1084 struct sctp_paramhdr *param_hdr;
1085 struct sctp_chunk *chunk = arg;
1086 struct sctp_chunk *reply;
1089 if (!sctp_vtag_verify(chunk, asoc))
1090 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1092 /* Make sure that the HEARTBEAT chunk has a valid length. */
1093 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1094 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1097 /* 8.3 The receiver of the HEARTBEAT should immediately
1098 * respond with a HEARTBEAT ACK that contains the Heartbeat
1099 * Information field copied from the received HEARTBEAT chunk.
1101 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *)chunk->skb->data;
1102 param_hdr = (struct sctp_paramhdr *)chunk->subh.hb_hdr;
1103 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(struct sctp_chunkhdr);
1105 if (ntohs(param_hdr->length) > paylen)
1106 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1107 param_hdr, commands);
1109 if (!pskb_pull(chunk->skb, paylen))
1112 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1116 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1117 return SCTP_DISPOSITION_CONSUME;
1120 return SCTP_DISPOSITION_NOMEM;
1124 * Process the returning HEARTBEAT ACK.
1126 * Section: 8.3 Path Heartbeat
1127 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1128 * should clear the error counter of the destination transport
1129 * address to which the HEARTBEAT was sent, and mark the destination
1130 * transport address as active if it is not so marked. The endpoint may
1131 * optionally report to the upper layer when an inactive destination
1132 * address is marked as active due to the reception of the latest
1133 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1134 * clear the association overall error count as well (as defined
1137 * The receiver of the HEARTBEAT ACK should also perform an RTT
1138 * measurement for that destination transport address using the time
1139 * value carried in the HEARTBEAT ACK chunk.
1141 * Verification Tag: 8.5 Verification Tag [Normal verification]
1144 * (endpoint, asoc, chunk)
1147 * (asoc, reply_msg, msg_up, timers, counters)
1149 * The return value is the disposition of the chunk.
1151 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1152 const struct sctp_endpoint *ep,
1153 const struct sctp_association *asoc,
1154 const sctp_subtype_t type,
1156 sctp_cmd_seq_t *commands)
1158 struct sctp_chunk *chunk = arg;
1159 union sctp_addr from_addr;
1160 struct sctp_transport *link;
1161 sctp_sender_hb_info_t *hbinfo;
1162 unsigned long max_interval;
1164 if (!sctp_vtag_verify(chunk, asoc))
1165 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1167 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1168 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr) +
1169 sizeof(sctp_sender_hb_info_t)))
1170 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1173 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1174 /* Make sure that the length of the parameter is what we expect */
1175 if (ntohs(hbinfo->param_hdr.length) !=
1176 sizeof(sctp_sender_hb_info_t)) {
1177 return SCTP_DISPOSITION_DISCARD;
1180 from_addr = hbinfo->daddr;
1181 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1183 /* This should never happen, but lets log it if so. */
1184 if (unlikely(!link)) {
1185 if (from_addr.sa.sa_family == AF_INET6) {
1186 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1189 &from_addr.v6.sin6_addr);
1191 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1194 &from_addr.v4.sin_addr.s_addr);
1196 return SCTP_DISPOSITION_DISCARD;
1199 /* Validate the 64-bit random nonce. */
1200 if (hbinfo->hb_nonce != link->hb_nonce)
1201 return SCTP_DISPOSITION_DISCARD;
1203 max_interval = link->hbinterval + link->rto;
1205 /* Check if the timestamp looks valid. */
1206 if (time_after(hbinfo->sent_at, jiffies) ||
1207 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1208 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1209 "for transport:%p\n", __func__, link);
1211 return SCTP_DISPOSITION_DISCARD;
1214 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1215 * the HEARTBEAT should clear the error counter of the
1216 * destination transport address to which the HEARTBEAT was
1217 * sent and mark the destination transport address as active if
1218 * it is not so marked.
1220 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1222 return SCTP_DISPOSITION_CONSUME;
1225 /* Helper function to send out an abort for the restart
1228 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1229 struct sctp_chunk *init,
1230 sctp_cmd_seq_t *commands)
1233 struct sctp_packet *pkt;
1234 union sctp_addr_param *addrparm;
1235 struct sctp_errhdr *errhdr;
1236 struct sctp_endpoint *ep;
1237 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1238 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1240 /* Build the error on the stack. We are way to malloc crazy
1241 * throughout the code today.
1243 errhdr = (struct sctp_errhdr *)buffer;
1244 addrparm = (union sctp_addr_param *)errhdr->variable;
1246 /* Copy into a parm format. */
1247 len = af->to_addr_param(ssa, addrparm);
1248 len += sizeof(sctp_errhdr_t);
1250 errhdr->cause = SCTP_ERROR_RESTART;
1251 errhdr->length = htons(len);
1253 /* Assign to the control socket. */
1254 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1256 /* Association is NULL since this may be a restart attack and we
1257 * want to send back the attacker's vtag.
1259 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1263 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1265 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1267 /* Discard the rest of the inbound packet. */
1268 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1271 /* Even if there is no memory, treat as a failure so
1272 * the packet will get dropped.
1277 static bool list_has_sctp_addr(const struct list_head *list,
1278 union sctp_addr *ipaddr)
1280 struct sctp_transport *addr;
1282 list_for_each_entry(addr, list, transports) {
1283 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1289 /* A restart is occurring, check to make sure no new addresses
1290 * are being added as we may be under a takeover attack.
1292 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1293 const struct sctp_association *asoc,
1294 struct sctp_chunk *init,
1295 sctp_cmd_seq_t *commands)
1297 struct net *net = sock_net(new_asoc->base.sk);
1298 struct sctp_transport *new_addr;
1301 /* Implementor's Guide - Section 5.2.2
1303 * Before responding the endpoint MUST check to see if the
1304 * unexpected INIT adds new addresses to the association. If new
1305 * addresses are added to the association, the endpoint MUST respond
1309 /* Search through all current addresses and make sure
1310 * we aren't adding any new ones.
1312 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1314 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1315 &new_addr->ipaddr)) {
1316 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1323 /* Return success if all addresses were found. */
1327 /* Populate the verification/tie tags based on overlapping INIT
1330 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1332 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1333 const struct sctp_association *asoc)
1335 switch (asoc->state) {
1337 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1339 case SCTP_STATE_COOKIE_WAIT:
1340 new_asoc->c.my_vtag = asoc->c.my_vtag;
1341 new_asoc->c.my_ttag = asoc->c.my_vtag;
1342 new_asoc->c.peer_ttag = 0;
1345 case SCTP_STATE_COOKIE_ECHOED:
1346 new_asoc->c.my_vtag = asoc->c.my_vtag;
1347 new_asoc->c.my_ttag = asoc->c.my_vtag;
1348 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1351 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1352 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1355 new_asoc->c.my_ttag = asoc->c.my_vtag;
1356 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1360 /* Other parameters for the endpoint SHOULD be copied from the
1361 * existing parameters of the association (e.g. number of
1362 * outbound streams) into the INIT ACK and cookie.
1364 new_asoc->rwnd = asoc->rwnd;
1365 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1366 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1367 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1371 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1374 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1376 * Returns value representing action to be taken. These action values
1377 * correspond to Action/Description values in RFC 2960, Table 2.
1379 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1380 const struct sctp_association *asoc)
1382 /* In this case, the peer may have restarted. */
1383 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1384 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1385 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1386 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1389 /* Collision case B. */
1390 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1391 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1392 (0 == asoc->c.peer_vtag))) {
1396 /* Collision case D. */
1397 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1398 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1401 /* Collision case C. */
1402 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1403 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1404 (0 == new_asoc->c.my_ttag) &&
1405 (0 == new_asoc->c.peer_ttag))
1408 /* No match to any of the special cases; discard this packet. */
1412 /* Common helper routine for both duplicate and simulataneous INIT
1415 static sctp_disposition_t sctp_sf_do_unexpected_init(
1417 const struct sctp_endpoint *ep,
1418 const struct sctp_association *asoc,
1419 const sctp_subtype_t type,
1420 void *arg, sctp_cmd_seq_t *commands)
1422 sctp_disposition_t retval;
1423 struct sctp_chunk *chunk = arg;
1424 struct sctp_chunk *repl;
1425 struct sctp_association *new_asoc;
1426 struct sctp_chunk *err_chunk;
1427 struct sctp_packet *packet;
1428 sctp_unrecognized_param_t *unk_param;
1432 * An endpoint MUST NOT bundle INIT, INIT ACK or
1433 * SHUTDOWN COMPLETE with any other chunks.
1436 * Furthermore, we require that the receiver of an INIT chunk MUST
1437 * enforce these rules by silently discarding an arriving packet
1438 * with an INIT chunk that is bundled with other chunks.
1440 if (!chunk->singleton)
1441 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1443 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1446 if (chunk->sctp_hdr->vtag != 0)
1447 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1449 /* Make sure that the INIT chunk has a valid length.
1450 * In this case, we generate a protocol violation since we have
1451 * an association established.
1453 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_init_chunk)))
1454 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1456 /* Grab the INIT header. */
1457 chunk->subh.init_hdr = (struct sctp_inithdr *)chunk->skb->data;
1459 /* Tag the variable length parameters. */
1460 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(struct sctp_inithdr));
1462 /* Verify the INIT chunk before processing it. */
1464 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1465 (struct sctp_init_chunk *)chunk->chunk_hdr, chunk,
1467 /* This chunk contains fatal error. It is to be discarded.
1468 * Send an ABORT, with causes if there is any.
1471 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1472 (__u8 *)(err_chunk->chunk_hdr) +
1473 sizeof(struct sctp_chunkhdr),
1474 ntohs(err_chunk->chunk_hdr->length) -
1475 sizeof(struct sctp_chunkhdr));
1478 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1479 SCTP_PACKET(packet));
1480 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1481 retval = SCTP_DISPOSITION_CONSUME;
1483 retval = SCTP_DISPOSITION_NOMEM;
1487 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1493 * Other parameters for the endpoint SHOULD be copied from the
1494 * existing parameters of the association (e.g. number of
1495 * outbound streams) into the INIT ACK and cookie.
1496 * FIXME: We are copying parameters from the endpoint not the
1499 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1503 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1504 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1507 /* In the outbound INIT ACK the endpoint MUST copy its current
1508 * Verification Tag and Peers Verification tag into a reserved
1509 * place (local tie-tag and per tie-tag) within the state cookie.
1511 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1512 (struct sctp_init_chunk *)chunk->chunk_hdr,
1516 /* Make sure no new addresses are being added during the
1517 * restart. Do not do this check for COOKIE-WAIT state,
1518 * since there are no peer addresses to check against.
1519 * Upon return an ABORT will have been sent if needed.
1521 if (!sctp_state(asoc, COOKIE_WAIT)) {
1522 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1524 retval = SCTP_DISPOSITION_CONSUME;
1529 sctp_tietags_populate(new_asoc, asoc);
1531 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1533 /* If there are errors need to be reported for unknown parameters,
1534 * make sure to reserve enough room in the INIT ACK for them.
1538 len = ntohs(err_chunk->chunk_hdr->length) -
1539 sizeof(struct sctp_chunkhdr);
1542 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1546 /* If there are errors need to be reported for unknown parameters,
1547 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1551 /* Get the "Unrecognized parameter" parameter(s) out of the
1552 * ERROR chunk generated by sctp_verify_init(). Since the
1553 * error cause code for "unknown parameter" and the
1554 * "Unrecognized parameter" type is the same, we can
1555 * construct the parameters in INIT ACK by copying the
1556 * ERROR causes over.
1558 unk_param = (sctp_unrecognized_param_t *)
1559 ((__u8 *)(err_chunk->chunk_hdr) +
1560 sizeof(struct sctp_chunkhdr));
1561 /* Replace the cause code with the "Unrecognized parameter"
1564 sctp_addto_chunk(repl, len, unk_param);
1567 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1568 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1571 * Note: After sending out INIT ACK with the State Cookie parameter,
1572 * "Z" MUST NOT allocate any resources for this new association.
1573 * Otherwise, "Z" will be vulnerable to resource attacks.
1575 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1576 retval = SCTP_DISPOSITION_CONSUME;
1581 retval = SCTP_DISPOSITION_NOMEM;
1584 sctp_association_free(new_asoc);
1587 sctp_chunk_free(err_chunk);
1592 * Handle simultaneous INIT.
1593 * This means we started an INIT and then we got an INIT request from
1596 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1597 * This usually indicates an initialization collision, i.e., each
1598 * endpoint is attempting, at about the same time, to establish an
1599 * association with the other endpoint.
1601 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1602 * endpoint MUST respond with an INIT ACK using the same parameters it
1603 * sent in its original INIT chunk (including its Verification Tag,
1604 * unchanged). These original parameters are combined with those from the
1605 * newly received INIT chunk. The endpoint shall also generate a State
1606 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1607 * INIT to calculate the State Cookie.
1609 * After that, the endpoint MUST NOT change its state, the T1-init
1610 * timer shall be left running and the corresponding TCB MUST NOT be
1611 * destroyed. The normal procedures for handling State Cookies when
1612 * a TCB exists will resolve the duplicate INITs to a single association.
1614 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1615 * its Tie-Tags with the Tag information of itself and its peer (see
1616 * section 5.2.2 for a description of the Tie-Tags).
1618 * Verification Tag: Not explicit, but an INIT can not have a valid
1619 * verification tag, so we skip the check.
1622 * (endpoint, asoc, chunk)
1625 * (asoc, reply_msg, msg_up, timers, counters)
1627 * The return value is the disposition of the chunk.
1629 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1630 const struct sctp_endpoint *ep,
1631 const struct sctp_association *asoc,
1632 const sctp_subtype_t type,
1634 sctp_cmd_seq_t *commands)
1636 /* Call helper to do the real work for both simulataneous and
1637 * duplicate INIT chunk handling.
1639 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1643 * Handle duplicated INIT messages. These are usually delayed
1646 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1647 * COOKIE-ECHOED and COOKIE-WAIT
1649 * Unless otherwise stated, upon reception of an unexpected INIT for
1650 * this association, the endpoint shall generate an INIT ACK with a
1651 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1652 * current Verification Tag and peer's Verification Tag into a reserved
1653 * place within the state cookie. We shall refer to these locations as
1654 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1655 * containing this INIT ACK MUST carry a Verification Tag value equal to
1656 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1657 * MUST contain a new Initiation Tag (randomly generated see Section
1658 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1659 * existing parameters of the association (e.g. number of outbound
1660 * streams) into the INIT ACK and cookie.
1662 * After sending out the INIT ACK, the endpoint shall take no further
1663 * actions, i.e., the existing association, including its current state,
1664 * and the corresponding TCB MUST NOT be changed.
1666 * Note: Only when a TCB exists and the association is not in a COOKIE-
1667 * WAIT state are the Tie-Tags populated. For a normal association INIT
1668 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1669 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1670 * State Cookie are populated as specified in section 5.2.1.
1672 * Verification Tag: Not specified, but an INIT has no way of knowing
1673 * what the verification tag could be, so we ignore it.
1676 * (endpoint, asoc, chunk)
1679 * (asoc, reply_msg, msg_up, timers, counters)
1681 * The return value is the disposition of the chunk.
1683 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1684 const struct sctp_endpoint *ep,
1685 const struct sctp_association *asoc,
1686 const sctp_subtype_t type,
1688 sctp_cmd_seq_t *commands)
1690 /* Call helper to do the real work for both simulataneous and
1691 * duplicate INIT chunk handling.
1693 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1698 * Unexpected INIT-ACK handler.
1701 * If an INIT ACK received by an endpoint in any state other than the
1702 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1703 * An unexpected INIT ACK usually indicates the processing of an old or
1704 * duplicated INIT chunk.
1706 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1707 const struct sctp_endpoint *ep,
1708 const struct sctp_association *asoc,
1709 const sctp_subtype_t type,
1710 void *arg, sctp_cmd_seq_t *commands)
1712 /* Per the above section, we'll discard the chunk if we have an
1713 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1715 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1716 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1718 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1721 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1724 * A) In this case, the peer may have restarted.
1726 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1727 const struct sctp_endpoint *ep,
1728 const struct sctp_association *asoc,
1729 struct sctp_chunk *chunk,
1730 sctp_cmd_seq_t *commands,
1731 struct sctp_association *new_asoc)
1733 struct sctp_init_chunk *peer_init;
1734 struct sctp_ulpevent *ev;
1735 struct sctp_chunk *repl;
1736 struct sctp_chunk *err;
1737 sctp_disposition_t disposition;
1739 /* new_asoc is a brand-new association, so these are not yet
1740 * side effects--it is safe to run them here.
1742 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1744 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1748 /* Make sure no new addresses are being added during the
1749 * restart. Though this is a pretty complicated attack
1750 * since you'd have to get inside the cookie.
1752 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands)) {
1753 return SCTP_DISPOSITION_CONSUME;
1756 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1757 * the peer has restarted (Action A), it MUST NOT setup a new
1758 * association but instead resend the SHUTDOWN ACK and send an ERROR
1759 * chunk with a "Cookie Received while Shutting Down" error cause to
1762 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1763 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1764 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1766 if (SCTP_DISPOSITION_NOMEM == disposition)
1769 err = sctp_make_op_error(asoc, chunk,
1770 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1773 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1776 return SCTP_DISPOSITION_CONSUME;
1779 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1780 * data. Consider the optional choice of resending of this data.
1782 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1783 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1784 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1785 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1787 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1788 * and ASCONF-ACK cache.
1790 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1791 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1792 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1794 repl = sctp_make_cookie_ack(new_asoc, chunk);
1798 /* Report association restart to upper layer. */
1799 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1800 new_asoc->c.sinit_num_ostreams,
1801 new_asoc->c.sinit_max_instreams,
1806 /* Update the content of current association. */
1807 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1808 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1809 if (sctp_state(asoc, SHUTDOWN_PENDING) &&
1810 (sctp_sstate(asoc->base.sk, CLOSING) ||
1811 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1812 /* if were currently in SHUTDOWN_PENDING, but the socket
1813 * has been closed by user, don't transition to ESTABLISHED.
1814 * Instead trigger SHUTDOWN bundled with COOKIE_ACK.
1816 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1817 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1818 SCTP_ST_CHUNK(0), NULL,
1821 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1822 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1823 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1825 return SCTP_DISPOSITION_CONSUME;
1828 sctp_chunk_free(repl);
1830 return SCTP_DISPOSITION_NOMEM;
1833 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1836 * B) In this case, both sides may be attempting to start an association
1837 * at about the same time but the peer endpoint started its INIT
1838 * after responding to the local endpoint's INIT
1840 /* This case represents an initialization collision. */
1841 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1842 const struct sctp_endpoint *ep,
1843 const struct sctp_association *asoc,
1844 struct sctp_chunk *chunk,
1845 sctp_cmd_seq_t *commands,
1846 struct sctp_association *new_asoc)
1848 struct sctp_init_chunk *peer_init;
1849 struct sctp_chunk *repl;
1851 /* new_asoc is a brand-new association, so these are not yet
1852 * side effects--it is safe to run them here.
1854 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1855 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1859 /* Update the content of current association. */
1860 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1861 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1862 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1863 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1864 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1866 repl = sctp_make_cookie_ack(new_asoc, chunk);
1870 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1872 /* RFC 2960 5.1 Normal Establishment of an Association
1874 * D) IMPLEMENTATION NOTE: An implementation may choose to
1875 * send the Communication Up notification to the SCTP user
1876 * upon reception of a valid COOKIE ECHO chunk.
1878 * Sadly, this needs to be implemented as a side-effect, because
1879 * we are not guaranteed to have set the association id of the real
1880 * association and so these notifications need to be delayed until
1881 * the association id is allocated.
1884 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1886 /* Sockets API Draft Section 5.3.1.6
1887 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1888 * delivers this notification to inform the application that of the
1889 * peers requested adaptation layer.
1891 * This also needs to be done as a side effect for the same reason as
1894 if (asoc->peer.adaptation_ind)
1895 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1897 return SCTP_DISPOSITION_CONSUME;
1900 return SCTP_DISPOSITION_NOMEM;
1903 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1906 * C) In this case, the local endpoint's cookie has arrived late.
1907 * Before it arrived, the local endpoint sent an INIT and received an
1908 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1909 * but a new tag of its own.
1911 /* This case represents an initialization collision. */
1912 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1913 const struct sctp_endpoint *ep,
1914 const struct sctp_association *asoc,
1915 struct sctp_chunk *chunk,
1916 sctp_cmd_seq_t *commands,
1917 struct sctp_association *new_asoc)
1919 /* The cookie should be silently discarded.
1920 * The endpoint SHOULD NOT change states and should leave
1921 * any timers running.
1923 return SCTP_DISPOSITION_DISCARD;
1926 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1930 * D) When both local and remote tags match the endpoint should always
1931 * enter the ESTABLISHED state, if it has not already done so.
1933 /* This case represents an initialization collision. */
1934 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1935 const struct sctp_endpoint *ep,
1936 const struct sctp_association *asoc,
1937 struct sctp_chunk *chunk,
1938 sctp_cmd_seq_t *commands,
1939 struct sctp_association *new_asoc)
1941 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1942 struct sctp_chunk *repl;
1944 /* Clarification from Implementor's Guide:
1945 * D) When both local and remote tags match the endpoint should
1946 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1947 * It should stop any cookie timer that may be running and send
1951 /* Don't accidentally move back into established state. */
1952 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1953 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1954 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1955 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1956 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1957 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1958 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1961 /* RFC 2960 5.1 Normal Establishment of an Association
1963 * D) IMPLEMENTATION NOTE: An implementation may choose
1964 * to send the Communication Up notification to the
1965 * SCTP user upon reception of a valid COOKIE
1968 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1970 asoc->c.sinit_num_ostreams,
1971 asoc->c.sinit_max_instreams,
1976 /* Sockets API Draft Section 5.3.1.6
1977 * When a peer sends a Adaptation Layer Indication parameter,
1978 * SCTP delivers this notification to inform the application
1979 * that of the peers requested adaptation layer.
1981 if (asoc->peer.adaptation_ind) {
1982 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1990 repl = sctp_make_cookie_ack(new_asoc, chunk);
1994 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1997 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2000 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
2001 SCTP_ULPEVENT(ai_ev));
2003 return SCTP_DISPOSITION_CONSUME;
2007 sctp_ulpevent_free(ai_ev);
2009 sctp_ulpevent_free(ev);
2010 return SCTP_DISPOSITION_NOMEM;
2014 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2015 * chunk was retransmitted and then delayed in the network.
2017 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2019 * Verification Tag: None. Do cookie validation.
2022 * (endpoint, asoc, chunk)
2025 * (asoc, reply_msg, msg_up, timers, counters)
2027 * The return value is the disposition of the chunk.
2029 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2030 const struct sctp_endpoint *ep,
2031 const struct sctp_association *asoc,
2032 const sctp_subtype_t type,
2034 sctp_cmd_seq_t *commands)
2036 sctp_disposition_t retval;
2037 struct sctp_chunk *chunk = arg;
2038 struct sctp_association *new_asoc;
2041 struct sctp_chunk *err_chk_p;
2043 /* Make sure that the chunk has a valid length from the protocol
2044 * perspective. In this case check to make sure we have at least
2045 * enough for the chunk header. Cookie length verification is
2048 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2049 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2052 /* "Decode" the chunk. We have no optional parameters so we
2053 * are in good shape.
2055 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2056 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2057 sizeof(struct sctp_chunkhdr)))
2060 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2061 * of a duplicate COOKIE ECHO match the Verification Tags of the
2062 * current association, consider the State Cookie valid even if
2063 * the lifespan is exceeded.
2065 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2069 * If the re-build failed, what is the proper error path
2072 * [We should abort the association. --piggy]
2075 /* FIXME: Several errors are possible. A bad cookie should
2076 * be silently discarded, but think about logging it too.
2079 case -SCTP_IERROR_NOMEM:
2082 case -SCTP_IERROR_STALE_COOKIE:
2083 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2085 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2086 case -SCTP_IERROR_BAD_SIG:
2088 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2092 /* Set temp so that it won't be added into hashtable */
2095 /* Compare the tie_tag in cookie with the verification tag of
2096 * current association.
2098 action = sctp_tietags_compare(new_asoc, asoc);
2101 case 'A': /* Association restart. */
2102 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2106 case 'B': /* Collision case B. */
2107 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2111 case 'C': /* Collision case C. */
2112 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2116 case 'D': /* Collision case D. */
2117 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2121 default: /* Discard packet for all others. */
2122 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2126 /* Delete the tempory new association. */
2127 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2128 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2130 /* Restore association pointer to provide SCTP command interpeter
2131 * with a valid context in case it needs to manipulate
2133 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2134 SCTP_ASOC((struct sctp_association *)asoc));
2139 return SCTP_DISPOSITION_NOMEM;
2143 * Process an ABORT. (SHUTDOWN-PENDING state)
2145 * See sctp_sf_do_9_1_abort().
2147 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2149 const struct sctp_endpoint *ep,
2150 const struct sctp_association *asoc,
2151 const sctp_subtype_t type,
2153 sctp_cmd_seq_t *commands)
2155 struct sctp_chunk *chunk = arg;
2157 if (!sctp_vtag_verify_either(chunk, asoc))
2158 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2160 /* Make sure that the ABORT chunk has a valid length.
2161 * Since this is an ABORT chunk, we have to discard it
2162 * because of the following text:
2163 * RFC 2960, Section 3.3.7
2164 * If an endpoint receives an ABORT with a format error or for an
2165 * association that doesn't exist, it MUST silently discard it.
2166 * Because the length is "invalid", we can't really discard just
2167 * as we do not know its true length. So, to be safe, discard the
2170 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2171 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2173 /* ADD-IP: Special case for ABORT chunks
2174 * F4) One special consideration is that ABORT Chunks arriving
2175 * destined to the IP address being deleted MUST be
2176 * ignored (see Section 5.3.1 for further details).
2178 if (SCTP_ADDR_DEL ==
2179 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2180 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2182 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2186 * Process an ABORT. (SHUTDOWN-SENT state)
2188 * See sctp_sf_do_9_1_abort().
2190 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2191 const struct sctp_endpoint *ep,
2192 const struct sctp_association *asoc,
2193 const sctp_subtype_t type,
2195 sctp_cmd_seq_t *commands)
2197 struct sctp_chunk *chunk = arg;
2199 if (!sctp_vtag_verify_either(chunk, asoc))
2200 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2202 /* Make sure that the ABORT chunk has a valid length.
2203 * Since this is an ABORT chunk, we have to discard it
2204 * because of the following text:
2205 * RFC 2960, Section 3.3.7
2206 * If an endpoint receives an ABORT with a format error or for an
2207 * association that doesn't exist, it MUST silently discard it.
2208 * Because the length is "invalid", we can't really discard just
2209 * as we do not know its true length. So, to be safe, discard the
2212 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2213 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2215 /* ADD-IP: Special case for ABORT chunks
2216 * F4) One special consideration is that ABORT Chunks arriving
2217 * destined to the IP address being deleted MUST be
2218 * ignored (see Section 5.3.1 for further details).
2220 if (SCTP_ADDR_DEL ==
2221 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2222 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2224 /* Stop the T2-shutdown timer. */
2225 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2226 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2228 /* Stop the T5-shutdown guard timer. */
2229 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2230 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2232 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2236 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2238 * See sctp_sf_do_9_1_abort().
2240 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2242 const struct sctp_endpoint *ep,
2243 const struct sctp_association *asoc,
2244 const sctp_subtype_t type,
2246 sctp_cmd_seq_t *commands)
2248 /* The same T2 timer, so we should be able to use
2249 * common function with the SHUTDOWN-SENT state.
2251 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2255 * Handle an Error received in COOKIE_ECHOED state.
2257 * Only handle the error type of stale COOKIE Error, the other errors will
2261 * (endpoint, asoc, chunk)
2264 * (asoc, reply_msg, msg_up, timers, counters)
2266 * The return value is the disposition of the chunk.
2268 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2269 const struct sctp_endpoint *ep,
2270 const struct sctp_association *asoc,
2271 const sctp_subtype_t type,
2273 sctp_cmd_seq_t *commands)
2275 struct sctp_chunk *chunk = arg;
2278 if (!sctp_vtag_verify(chunk, asoc))
2279 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2281 /* Make sure that the ERROR chunk has a valid length.
2282 * The parameter walking depends on this as well.
2284 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2285 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2288 /* Process the error here */
2289 /* FUTURE FIXME: When PR-SCTP related and other optional
2290 * parms are emitted, this will have to change to handle multiple
2293 sctp_walk_errors(err, chunk->chunk_hdr) {
2294 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2295 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2299 /* It is possible to have malformed error causes, and that
2300 * will cause us to end the walk early. However, since
2301 * we are discarding the packet, there should be no adverse
2304 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2308 * Handle a Stale COOKIE Error
2310 * Section: 5.2.6 Handle Stale COOKIE Error
2311 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2312 * one of the following three alternatives.
2314 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2315 * Preservative parameter requesting an extension to the lifetime of
2316 * the State Cookie. When calculating the time extension, an
2317 * implementation SHOULD use the RTT information measured based on the
2318 * previous COOKIE ECHO / ERROR exchange, and should add no more
2319 * than 1 second beyond the measured RTT, due to long State Cookie
2320 * lifetimes making the endpoint more subject to a replay attack.
2322 * Verification Tag: Not explicit, but safe to ignore.
2325 * (endpoint, asoc, chunk)
2328 * (asoc, reply_msg, msg_up, timers, counters)
2330 * The return value is the disposition of the chunk.
2332 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2333 const struct sctp_endpoint *ep,
2334 const struct sctp_association *asoc,
2335 const sctp_subtype_t type,
2337 sctp_cmd_seq_t *commands)
2339 int attempts = asoc->init_err_counter + 1;
2340 struct sctp_chunk *chunk = arg, *reply;
2341 struct sctp_cookie_preserve_param bht;
2342 struct sctp_bind_addr *bp;
2346 if (attempts > asoc->max_init_attempts) {
2347 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2348 SCTP_ERROR(ETIMEDOUT));
2349 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2350 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2351 return SCTP_DISPOSITION_DELETE_TCB;
2354 err = (sctp_errhdr_t *)(chunk->skb->data);
2356 /* When calculating the time extension, an implementation
2357 * SHOULD use the RTT information measured based on the
2358 * previous COOKIE ECHO / ERROR exchange, and should add no
2359 * more than 1 second beyond the measured RTT, due to long
2360 * State Cookie lifetimes making the endpoint more subject to
2362 * Measure of Staleness's unit is usec. (1/1000000 sec)
2363 * Suggested Cookie Life-span Increment's unit is msec.
2365 * In general, if you use the suggested cookie life, the value
2366 * found in the field of measure of staleness should be doubled
2367 * to give ample time to retransmit the new cookie and thus
2368 * yield a higher probability of success on the reattempt.
2370 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2371 stale = (stale * 2) / 1000;
2373 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2374 bht.param_hdr.length = htons(sizeof(bht));
2375 bht.lifespan_increment = htonl(stale);
2377 /* Build that new INIT chunk. */
2378 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2379 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2383 sctp_addto_chunk(reply, sizeof(bht), &bht);
2385 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2386 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2388 /* Stop pending T3-rtx and heartbeat timers */
2389 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2390 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2392 /* Delete non-primary peer ip addresses since we are transitioning
2393 * back to the COOKIE-WAIT state
2395 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2397 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2400 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2401 SCTP_TRANSPORT(asoc->peer.primary_path));
2403 /* Cast away the const modifier, as we want to just
2404 * rerun it through as a sideffect.
2406 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2408 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2409 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2410 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2411 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2412 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2413 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2415 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2417 return SCTP_DISPOSITION_CONSUME;
2420 return SCTP_DISPOSITION_NOMEM;
2427 * After checking the Verification Tag, the receiving endpoint shall
2428 * remove the association from its record, and shall report the
2429 * termination to its upper layer.
2431 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2432 * B) Rules for packet carrying ABORT:
2434 * - The endpoint shall always fill in the Verification Tag field of the
2435 * outbound packet with the destination endpoint's tag value if it
2438 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2439 * MUST follow the procedure described in Section 8.4.
2441 * - The receiver MUST accept the packet if the Verification Tag
2442 * matches either its own tag, OR the tag of its peer. Otherwise, the
2443 * receiver MUST silently discard the packet and take no further
2447 * (endpoint, asoc, chunk)
2450 * (asoc, reply_msg, msg_up, timers, counters)
2452 * The return value is the disposition of the chunk.
2454 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2455 const struct sctp_endpoint *ep,
2456 const struct sctp_association *asoc,
2457 const sctp_subtype_t type,
2459 sctp_cmd_seq_t *commands)
2461 struct sctp_chunk *chunk = arg;
2463 if (!sctp_vtag_verify_either(chunk, asoc))
2464 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2466 /* Make sure that the ABORT chunk has a valid length.
2467 * Since this is an ABORT chunk, we have to discard it
2468 * because of the following text:
2469 * RFC 2960, Section 3.3.7
2470 * If an endpoint receives an ABORT with a format error or for an
2471 * association that doesn't exist, it MUST silently discard it.
2472 * Because the length is "invalid", we can't really discard just
2473 * as we do not know its true length. So, to be safe, discard the
2476 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2477 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2479 /* ADD-IP: Special case for ABORT chunks
2480 * F4) One special consideration is that ABORT Chunks arriving
2481 * destined to the IP address being deleted MUST be
2482 * ignored (see Section 5.3.1 for further details).
2484 if (SCTP_ADDR_DEL ==
2485 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2486 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2488 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2491 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2492 const struct sctp_endpoint *ep,
2493 const struct sctp_association *asoc,
2494 const sctp_subtype_t type,
2496 sctp_cmd_seq_t *commands)
2498 struct sctp_chunk *chunk = arg;
2500 __be16 error = SCTP_ERROR_NO_ERROR;
2502 /* See if we have an error cause code in the chunk. */
2503 len = ntohs(chunk->chunk_hdr->length);
2504 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr)) {
2507 sctp_walk_errors(err, chunk->chunk_hdr);
2508 if ((void *)err != (void *)chunk->chunk_end)
2509 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2511 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2514 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2515 /* ASSOC_FAILED will DELETE_TCB. */
2516 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2517 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2518 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2520 return SCTP_DISPOSITION_ABORT;
2524 * Process an ABORT. (COOKIE-WAIT state)
2526 * See sctp_sf_do_9_1_abort() above.
2528 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2529 const struct sctp_endpoint *ep,
2530 const struct sctp_association *asoc,
2531 const sctp_subtype_t type,
2533 sctp_cmd_seq_t *commands)
2535 struct sctp_chunk *chunk = arg;
2537 __be16 error = SCTP_ERROR_NO_ERROR;
2539 if (!sctp_vtag_verify_either(chunk, asoc))
2540 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2542 /* Make sure that the ABORT chunk has a valid length.
2543 * Since this is an ABORT chunk, we have to discard it
2544 * because of the following text:
2545 * RFC 2960, Section 3.3.7
2546 * If an endpoint receives an ABORT with a format error or for an
2547 * association that doesn't exist, it MUST silently discard it.
2548 * Because the length is "invalid", we can't really discard just
2549 * as we do not know its true length. So, to be safe, discard the
2552 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2553 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2555 /* See if we have an error cause code in the chunk. */
2556 len = ntohs(chunk->chunk_hdr->length);
2557 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2558 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2560 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2565 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2567 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2568 const struct sctp_endpoint *ep,
2569 const struct sctp_association *asoc,
2570 const sctp_subtype_t type,
2572 sctp_cmd_seq_t *commands)
2574 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2576 (struct sctp_transport *)arg);
2580 * Process an ABORT. (COOKIE-ECHOED state)
2582 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2583 const struct sctp_endpoint *ep,
2584 const struct sctp_association *asoc,
2585 const sctp_subtype_t type,
2587 sctp_cmd_seq_t *commands)
2589 /* There is a single T1 timer, so we should be able to use
2590 * common function with the COOKIE-WAIT state.
2592 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2596 * Stop T1 timer and abort association with "INIT failed".
2598 * This is common code called by several sctp_sf_*_abort() functions above.
2600 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2601 sctp_cmd_seq_t *commands,
2602 __be16 error, int sk_err,
2603 const struct sctp_association *asoc,
2604 struct sctp_transport *transport)
2606 pr_debug("%s: ABORT received (INIT)\n", __func__);
2608 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2609 SCTP_STATE(SCTP_STATE_CLOSED));
2610 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2611 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2612 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2613 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2614 /* CMD_INIT_FAILED will DELETE_TCB. */
2615 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2618 return SCTP_DISPOSITION_ABORT;
2622 * sctp_sf_do_9_2_shut
2625 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2626 * - enter the SHUTDOWN-RECEIVED state,
2628 * - stop accepting new data from its SCTP user
2630 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2631 * that all its outstanding DATA chunks have been received by the
2634 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2635 * send a SHUTDOWN in response to a ULP request. And should discard
2636 * subsequent SHUTDOWN chunks.
2638 * If there are still outstanding DATA chunks left, the SHUTDOWN
2639 * receiver shall continue to follow normal data transmission
2640 * procedures defined in Section 6 until all outstanding DATA chunks
2641 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2642 * new data from its SCTP user.
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_do_9_2_shutdown(struct net *net,
2655 const struct sctp_endpoint *ep,
2656 const struct sctp_association *asoc,
2657 const sctp_subtype_t type,
2659 sctp_cmd_seq_t *commands)
2661 struct sctp_chunk *chunk = arg;
2662 sctp_shutdownhdr_t *sdh;
2663 sctp_disposition_t disposition;
2664 struct sctp_ulpevent *ev;
2667 if (!sctp_vtag_verify(chunk, asoc))
2668 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2670 /* Make sure that the SHUTDOWN chunk has a valid length. */
2671 if (!sctp_chunk_length_valid(chunk,
2672 sizeof(struct sctp_shutdown_chunk_t)))
2673 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2676 /* Convert the elaborate header. */
2677 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2678 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2679 chunk->subh.shutdown_hdr = sdh;
2680 ctsn = ntohl(sdh->cum_tsn_ack);
2682 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2683 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2684 asoc->ctsn_ack_point);
2686 return SCTP_DISPOSITION_DISCARD;
2689 /* If Cumulative TSN Ack beyond the max tsn currently
2690 * send, terminating the association and respond to the
2691 * sender with an ABORT.
2693 if (!TSN_lt(ctsn, asoc->next_tsn))
2694 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2696 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2697 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2698 * inform the application that it should cease sending data.
2700 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2702 disposition = SCTP_DISPOSITION_NOMEM;
2705 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2707 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2708 * - enter the SHUTDOWN-RECEIVED state,
2709 * - stop accepting new data from its SCTP user
2711 * [This is implicit in the new state.]
2713 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2714 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2715 disposition = SCTP_DISPOSITION_CONSUME;
2717 if (sctp_outq_is_empty(&asoc->outqueue)) {
2718 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2722 if (SCTP_DISPOSITION_NOMEM == disposition)
2725 /* - verify, by checking the Cumulative TSN Ack field of the
2726 * chunk, that all its outstanding DATA chunks have been
2727 * received by the SHUTDOWN sender.
2729 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2730 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2737 * sctp_sf_do_9_2_shut_ctsn
2739 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2740 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2741 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2742 * MUST be processed.
2744 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2745 const struct sctp_endpoint *ep,
2746 const struct sctp_association *asoc,
2747 const sctp_subtype_t type,
2749 sctp_cmd_seq_t *commands)
2751 struct sctp_chunk *chunk = arg;
2752 sctp_shutdownhdr_t *sdh;
2755 if (!sctp_vtag_verify(chunk, asoc))
2756 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2758 /* Make sure that the SHUTDOWN chunk has a valid length. */
2759 if (!sctp_chunk_length_valid(chunk,
2760 sizeof(struct sctp_shutdown_chunk_t)))
2761 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2764 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2765 ctsn = ntohl(sdh->cum_tsn_ack);
2767 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2768 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2769 asoc->ctsn_ack_point);
2771 return SCTP_DISPOSITION_DISCARD;
2774 /* If Cumulative TSN Ack beyond the max tsn currently
2775 * send, terminating the association and respond to the
2776 * sender with an ABORT.
2778 if (!TSN_lt(ctsn, asoc->next_tsn))
2779 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2781 /* verify, by checking the Cumulative TSN Ack field of the
2782 * chunk, that all its outstanding DATA chunks have been
2783 * received by the SHUTDOWN sender.
2785 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2786 SCTP_BE32(sdh->cum_tsn_ack));
2788 return SCTP_DISPOSITION_CONSUME;
2792 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2793 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2794 * transport addresses (either in the IP addresses or in the INIT chunk)
2795 * that belong to this association, it should discard the INIT chunk and
2796 * retransmit the SHUTDOWN ACK chunk.
2798 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2799 const struct sctp_endpoint *ep,
2800 const struct sctp_association *asoc,
2801 const sctp_subtype_t type,
2803 sctp_cmd_seq_t *commands)
2805 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2806 struct sctp_chunk *reply;
2808 /* Make sure that the chunk has a valid length */
2809 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
2810 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2813 /* Since we are not going to really process this INIT, there
2814 * is no point in verifying chunk boundries. Just generate
2817 reply = sctp_make_shutdown_ack(asoc, chunk);
2821 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2822 * the T2-SHUTDOWN timer.
2824 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2826 /* and restart the T2-shutdown timer. */
2827 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2828 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2830 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2832 return SCTP_DISPOSITION_CONSUME;
2834 return SCTP_DISPOSITION_NOMEM;
2838 * sctp_sf_do_ecn_cwr
2840 * Section: Appendix A: Explicit Congestion Notification
2844 * RFC 2481 details a specific bit for a sender to send in the header of
2845 * its next outbound TCP segment to indicate to its peer that it has
2846 * reduced its congestion window. This is termed the CWR bit. For
2847 * SCTP the same indication is made by including the CWR chunk.
2848 * This chunk contains one data element, i.e. the TSN number that
2849 * was sent in the ECNE chunk. This element represents the lowest
2850 * TSN number in the datagram that was originally marked with the
2853 * Verification Tag: 8.5 Verification Tag [Normal verification]
2855 * (endpoint, asoc, chunk)
2858 * (asoc, reply_msg, msg_up, timers, counters)
2860 * The return value is the disposition of the chunk.
2862 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2863 const struct sctp_endpoint *ep,
2864 const struct sctp_association *asoc,
2865 const sctp_subtype_t type,
2867 sctp_cmd_seq_t *commands)
2870 struct sctp_chunk *chunk = arg;
2873 if (!sctp_vtag_verify(chunk, asoc))
2874 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2876 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2877 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2880 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2881 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2883 lowest_tsn = ntohl(cwr->lowest_tsn);
2885 /* Does this CWR ack the last sent congestion notification? */
2886 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2887 /* Stop sending ECNE. */
2888 sctp_add_cmd_sf(commands,
2890 SCTP_U32(lowest_tsn));
2892 return SCTP_DISPOSITION_CONSUME;
2898 * Section: Appendix A: Explicit Congestion Notification
2902 * RFC 2481 details a specific bit for a receiver to send back in its
2903 * TCP acknowledgements to notify the sender of the Congestion
2904 * Experienced (CE) bit having arrived from the network. For SCTP this
2905 * same indication is made by including the ECNE chunk. This chunk
2906 * contains one data element, i.e. the lowest TSN associated with the IP
2907 * datagram marked with the CE bit.....
2909 * Verification Tag: 8.5 Verification Tag [Normal verification]
2911 * (endpoint, asoc, chunk)
2914 * (asoc, reply_msg, msg_up, timers, counters)
2916 * The return value is the disposition of the chunk.
2918 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2919 const struct sctp_endpoint *ep,
2920 const struct sctp_association *asoc,
2921 const sctp_subtype_t type,
2923 sctp_cmd_seq_t *commands)
2925 sctp_ecnehdr_t *ecne;
2926 struct sctp_chunk *chunk = arg;
2928 if (!sctp_vtag_verify(chunk, asoc))
2929 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2931 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2932 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2935 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2936 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2938 /* If this is a newer ECNE than the last CWR packet we sent out */
2939 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2940 SCTP_U32(ntohl(ecne->lowest_tsn)));
2942 return SCTP_DISPOSITION_CONSUME;
2946 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2948 * The SCTP endpoint MUST always acknowledge the reception of each valid
2951 * The guidelines on delayed acknowledgement algorithm specified in
2952 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2953 * acknowledgement SHOULD be generated for at least every second packet
2954 * (not every second DATA chunk) received, and SHOULD be generated within
2955 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2956 * situations it may be beneficial for an SCTP transmitter to be more
2957 * conservative than the algorithms detailed in this document allow.
2958 * However, an SCTP transmitter MUST NOT be more aggressive than the
2959 * following algorithms allow.
2961 * A SCTP receiver MUST NOT generate more than one SACK for every
2962 * incoming packet, other than to update the offered window as the
2963 * receiving application consumes new data.
2965 * Verification Tag: 8.5 Verification Tag [Normal verification]
2968 * (endpoint, asoc, chunk)
2971 * (asoc, reply_msg, msg_up, timers, counters)
2973 * The return value is the disposition of the chunk.
2975 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2976 const struct sctp_endpoint *ep,
2977 const struct sctp_association *asoc,
2978 const sctp_subtype_t type,
2980 sctp_cmd_seq_t *commands)
2982 struct sctp_chunk *chunk = arg;
2983 sctp_arg_t force = SCTP_NOFORCE();
2986 if (!sctp_vtag_verify(chunk, asoc)) {
2987 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2989 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2992 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
2993 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2996 error = sctp_eat_data(asoc, chunk, commands);
2998 case SCTP_IERROR_NO_ERROR:
3000 case SCTP_IERROR_HIGH_TSN:
3001 case SCTP_IERROR_BAD_STREAM:
3002 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3003 goto discard_noforce;
3004 case SCTP_IERROR_DUP_TSN:
3005 case SCTP_IERROR_IGNORE_TSN:
3006 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3008 case SCTP_IERROR_NO_DATA:
3009 return SCTP_DISPOSITION_ABORT;
3010 case SCTP_IERROR_PROTO_VIOLATION:
3011 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3012 (u8 *)chunk->subh.data_hdr,
3013 sizeof(struct sctp_datahdr));
3018 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3019 force = SCTP_FORCE();
3021 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3022 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3023 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3026 /* If this is the last chunk in a packet, we need to count it
3027 * toward sack generation. Note that we need to SACK every
3028 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3029 * THEM. We elect to NOT generate SACK's if the chunk fails
3030 * the verification tag test.
3032 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3034 * The SCTP endpoint MUST always acknowledge the reception of
3035 * each valid DATA chunk.
3037 * The guidelines on delayed acknowledgement algorithm
3038 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3039 * Specifically, an acknowledgement SHOULD be generated for at
3040 * least every second packet (not every second DATA chunk)
3041 * received, and SHOULD be generated within 200 ms of the
3042 * arrival of any unacknowledged DATA chunk. In some
3043 * situations it may be beneficial for an SCTP transmitter to
3044 * be more conservative than the algorithms detailed in this
3045 * document allow. However, an SCTP transmitter MUST NOT be
3046 * more aggressive than the following algorithms allow.
3048 if (chunk->end_of_packet)
3049 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3051 return SCTP_DISPOSITION_CONSUME;
3054 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3056 * When a packet arrives with duplicate DATA chunk(s) and with
3057 * no new DATA chunk(s), the endpoint MUST immediately send a
3058 * SACK with no delay. If a packet arrives with duplicate
3059 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3060 * MAY immediately send a SACK. Normally receipt of duplicate
3061 * DATA chunks will occur when the original SACK chunk was lost
3062 * and the peer's RTO has expired. The duplicate TSN number(s)
3063 * SHOULD be reported in the SACK as duplicate.
3065 /* In our case, we split the MAY SACK advice up whether or not
3066 * the last chunk is a duplicate.'
3068 if (chunk->end_of_packet)
3069 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3070 return SCTP_DISPOSITION_DISCARD;
3073 if (chunk->end_of_packet)
3074 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3076 return SCTP_DISPOSITION_DISCARD;
3080 * sctp_sf_eat_data_fast_4_4
3083 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3084 * DATA chunks without delay.
3086 * Verification Tag: 8.5 Verification Tag [Normal verification]
3088 * (endpoint, asoc, chunk)
3091 * (asoc, reply_msg, msg_up, timers, counters)
3093 * The return value is the disposition of the chunk.
3095 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3096 const struct sctp_endpoint *ep,
3097 const struct sctp_association *asoc,
3098 const sctp_subtype_t type,
3100 sctp_cmd_seq_t *commands)
3102 struct sctp_chunk *chunk = arg;
3105 if (!sctp_vtag_verify(chunk, asoc)) {
3106 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3108 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3111 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_data_chunk)))
3112 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3115 error = sctp_eat_data(asoc, chunk, commands);
3117 case SCTP_IERROR_NO_ERROR:
3118 case SCTP_IERROR_HIGH_TSN:
3119 case SCTP_IERROR_DUP_TSN:
3120 case SCTP_IERROR_IGNORE_TSN:
3121 case SCTP_IERROR_BAD_STREAM:
3123 case SCTP_IERROR_NO_DATA:
3124 return SCTP_DISPOSITION_ABORT;
3125 case SCTP_IERROR_PROTO_VIOLATION:
3126 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3127 (u8 *)chunk->subh.data_hdr,
3128 sizeof(struct sctp_datahdr));
3133 /* Go a head and force a SACK, since we are shutting down. */
3135 /* Implementor's Guide.
3137 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3138 * respond to each received packet containing one or more DATA chunk(s)
3139 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3141 if (chunk->end_of_packet) {
3142 /* We must delay the chunk creation since the cumulative
3143 * TSN has not been updated yet.
3145 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3146 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3147 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3148 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3151 return SCTP_DISPOSITION_CONSUME;
3155 * Section: 6.2 Processing a Received SACK
3156 * D) Any time a SACK arrives, the endpoint performs the following:
3158 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3159 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3160 * increasing, a SACK whose Cumulative TSN Ack is less than the
3161 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3163 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3164 * of bytes still outstanding after processing the Cumulative TSN Ack
3165 * and the Gap Ack Blocks.
3167 * iii) If the SACK is missing a TSN that was previously
3168 * acknowledged via a Gap Ack Block (e.g., the data receiver
3169 * reneged on the data), then mark the corresponding DATA chunk
3170 * as available for retransmit: Mark it as missing for fast
3171 * retransmit as described in Section 7.2.4 and if no retransmit
3172 * timer is running for the destination address to which the DATA
3173 * chunk was originally transmitted, then T3-rtx is started for
3174 * that destination address.
3176 * Verification Tag: 8.5 Verification Tag [Normal verification]
3179 * (endpoint, asoc, chunk)
3182 * (asoc, reply_msg, msg_up, timers, counters)
3184 * The return value is the disposition of the chunk.
3186 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3187 const struct sctp_endpoint *ep,
3188 const struct sctp_association *asoc,
3189 const sctp_subtype_t type,
3191 sctp_cmd_seq_t *commands)
3193 struct sctp_chunk *chunk = arg;
3194 sctp_sackhdr_t *sackh;
3197 if (!sctp_vtag_verify(chunk, asoc))
3198 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3200 /* Make sure that the SACK chunk has a valid length. */
3201 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3202 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3205 /* Pull the SACK chunk from the data buffer */
3206 sackh = sctp_sm_pull_sack(chunk);
3207 /* Was this a bogus SACK? */
3209 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3210 chunk->subh.sack_hdr = sackh;
3211 ctsn = ntohl(sackh->cum_tsn_ack);
3213 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3214 * Ack Point, then drop the SACK. Since Cumulative TSN
3215 * Ack is monotonically increasing, a SACK whose
3216 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3217 * Point indicates an out-of-order SACK.
3219 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3220 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3221 asoc->ctsn_ack_point);
3223 return SCTP_DISPOSITION_DISCARD;
3226 /* If Cumulative TSN Ack beyond the max tsn currently
3227 * send, terminating the association and respond to the
3228 * sender with an ABORT.
3230 if (!TSN_lt(ctsn, asoc->next_tsn))
3231 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3233 /* Return this SACK for further processing. */
3234 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3236 /* Note: We do the rest of the work on the PROCESS_SACK
3239 return SCTP_DISPOSITION_CONSUME;
3243 * Generate an ABORT in response to a packet.
3245 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3247 * 8) The receiver should respond to the sender of the OOTB packet with
3248 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3249 * MUST fill in the Verification Tag field of the outbound packet
3250 * with the value found in the Verification Tag field of the OOTB
3251 * packet and set the T-bit in the Chunk Flags to indicate that the
3252 * Verification Tag is reflected. After sending this ABORT, the
3253 * receiver of the OOTB packet shall discard the OOTB packet and take
3254 * no further action.
3258 * The return value is the disposition of the chunk.
3260 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3261 const struct sctp_endpoint *ep,
3262 const struct sctp_association *asoc,
3263 const sctp_subtype_t type,
3265 sctp_cmd_seq_t *commands)
3267 struct sctp_packet *packet = NULL;
3268 struct sctp_chunk *chunk = arg;
3269 struct sctp_chunk *abort;
3271 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3273 return SCTP_DISPOSITION_NOMEM;
3275 /* Make an ABORT. The T bit will be set if the asoc
3278 abort = sctp_make_abort(asoc, chunk, 0);
3280 sctp_ootb_pkt_free(packet);
3281 return SCTP_DISPOSITION_NOMEM;
3284 /* Reflect vtag if T-Bit is set */
3285 if (sctp_test_T_bit(abort))
3286 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3288 /* Set the skb to the belonging sock for accounting. */
3289 abort->skb->sk = ep->base.sk;
3291 sctp_packet_append_chunk(packet, abort);
3293 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3294 SCTP_PACKET(packet));
3296 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3298 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3299 return SCTP_DISPOSITION_CONSUME;
3303 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3304 * event as ULP notification for each cause included in the chunk.
3306 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3308 * The return value is the disposition of the chunk.
3310 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3311 const struct sctp_endpoint *ep,
3312 const struct sctp_association *asoc,
3313 const sctp_subtype_t type,
3315 sctp_cmd_seq_t *commands)
3317 struct sctp_chunk *chunk = arg;
3320 if (!sctp_vtag_verify(chunk, asoc))
3321 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3323 /* Make sure that the ERROR chunk has a valid length. */
3324 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3325 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3327 sctp_walk_errors(err, chunk->chunk_hdr);
3328 if ((void *)err != (void *)chunk->chunk_end)
3329 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3330 (void *)err, commands);
3332 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3335 return SCTP_DISPOSITION_CONSUME;
3339 * Process an inbound SHUTDOWN ACK.
3342 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3343 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3344 * peer, and remove all record of the association.
3346 * The return value is the disposition.
3348 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3349 const struct sctp_endpoint *ep,
3350 const struct sctp_association *asoc,
3351 const sctp_subtype_t type,
3353 sctp_cmd_seq_t *commands)
3355 struct sctp_chunk *chunk = arg;
3356 struct sctp_chunk *reply;
3357 struct sctp_ulpevent *ev;
3359 if (!sctp_vtag_verify(chunk, asoc))
3360 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3362 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3363 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3364 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3366 /* 10.2 H) SHUTDOWN COMPLETE notification
3368 * When SCTP completes the shutdown procedures (section 9.2) this
3369 * notification is passed to the upper layer.
3371 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3372 0, 0, 0, NULL, GFP_ATOMIC);
3376 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3377 reply = sctp_make_shutdown_complete(asoc, chunk);
3381 /* Do all the commands now (after allocation), so that we
3382 * have consistent state if memory allocation failes
3384 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3386 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3387 * stop the T2-shutdown timer,
3389 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3390 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3392 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3393 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3395 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3396 SCTP_STATE(SCTP_STATE_CLOSED));
3397 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3398 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3399 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3401 /* ...and remove all record of the association. */
3402 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3403 return SCTP_DISPOSITION_DELETE_TCB;
3406 sctp_ulpevent_free(ev);
3408 return SCTP_DISPOSITION_NOMEM;
3412 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3414 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3415 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3416 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3417 * packet must fill in the Verification Tag field of the outbound
3418 * packet with the Verification Tag received in the SHUTDOWN ACK and
3419 * set the T-bit in the Chunk Flags to indicate that the Verification
3422 * 8) The receiver should respond to the sender of the OOTB packet with
3423 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3424 * MUST fill in the Verification Tag field of the outbound packet
3425 * with the value found in the Verification Tag field of the OOTB
3426 * packet and set the T-bit in the Chunk Flags to indicate that the
3427 * Verification Tag is reflected. After sending this ABORT, the
3428 * receiver of the OOTB packet shall discard the OOTB packet and take
3429 * no further action.
3431 sctp_disposition_t sctp_sf_ootb(struct net *net,
3432 const struct sctp_endpoint *ep,
3433 const struct sctp_association *asoc,
3434 const sctp_subtype_t type,
3436 sctp_cmd_seq_t *commands)
3438 struct sctp_chunk *chunk = arg;
3439 struct sk_buff *skb = chunk->skb;
3440 struct sctp_chunkhdr *ch;
3443 int ootb_shut_ack = 0;
3444 int ootb_cookie_ack = 0;
3446 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3448 ch = (struct sctp_chunkhdr *)chunk->chunk_hdr;
3450 /* Report violation if the chunk is less then minimal */
3451 if (ntohs(ch->length) < sizeof(*ch))
3452 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3455 /* Report violation if chunk len overflows */
3456 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3457 if (ch_end > skb_tail_pointer(skb))
3458 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3461 /* Now that we know we at least have a chunk header,
3462 * do things that are type appropriate.
3464 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3467 /* RFC 2960, Section 3.3.7
3468 * Moreover, under any circumstances, an endpoint that
3469 * receives an ABORT MUST NOT respond to that ABORT by
3470 * sending an ABORT of its own.
3472 if (SCTP_CID_ABORT == ch->type)
3473 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3475 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3476 * or a COOKIE ACK the SCTP Packet should be silently
3480 if (SCTP_CID_COOKIE_ACK == ch->type)
3481 ootb_cookie_ack = 1;
3483 if (SCTP_CID_ERROR == ch->type) {
3484 sctp_walk_errors(err, ch) {
3485 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3486 ootb_cookie_ack = 1;
3492 ch = (struct sctp_chunkhdr *)ch_end;
3493 } while (ch_end < skb_tail_pointer(skb));
3496 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3497 else if (ootb_cookie_ack)
3498 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3500 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3504 * Handle an "Out of the blue" SHUTDOWN ACK.
3506 * Section: 8.4 5, sctpimpguide 2.41.
3508 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3509 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3510 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3511 * packet must fill in the Verification Tag field of the outbound
3512 * packet with the Verification Tag received in the SHUTDOWN ACK and
3513 * set the T-bit in the Chunk Flags to indicate that the Verification
3517 * (endpoint, asoc, type, arg, commands)
3520 * (sctp_disposition_t)
3522 * The return value is the disposition of the chunk.
3524 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3525 const struct sctp_endpoint *ep,
3526 const struct sctp_association *asoc,
3527 const sctp_subtype_t type,
3529 sctp_cmd_seq_t *commands)
3531 struct sctp_packet *packet = NULL;
3532 struct sctp_chunk *chunk = arg;
3533 struct sctp_chunk *shut;
3535 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3537 return SCTP_DISPOSITION_NOMEM;
3539 /* Make an SHUTDOWN_COMPLETE.
3540 * The T bit will be set if the asoc is NULL.
3542 shut = sctp_make_shutdown_complete(asoc, chunk);
3544 sctp_ootb_pkt_free(packet);
3545 return SCTP_DISPOSITION_NOMEM;
3548 /* Reflect vtag if T-Bit is set */
3549 if (sctp_test_T_bit(shut))
3550 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3552 /* Set the skb to the belonging sock for accounting. */
3553 shut->skb->sk = ep->base.sk;
3555 sctp_packet_append_chunk(packet, shut);
3557 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3558 SCTP_PACKET(packet));
3560 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3562 /* If the chunk length is invalid, we don't want to process
3563 * the reset of the packet.
3565 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3566 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3568 /* We need to discard the rest of the packet to prevent
3569 * potential bomming attacks from additional bundled chunks.
3570 * This is documented in SCTP Threats ID.
3572 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3576 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3578 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3579 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3580 * procedures in section 8.4 SHOULD be followed, in other words it
3581 * should be treated as an Out Of The Blue packet.
3582 * [This means that we do NOT check the Verification Tag on these
3586 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3587 const struct sctp_endpoint *ep,
3588 const struct sctp_association *asoc,
3589 const sctp_subtype_t type,
3591 sctp_cmd_seq_t *commands)
3593 struct sctp_chunk *chunk = arg;
3595 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3596 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
3597 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3600 /* Although we do have an association in this case, it corresponds
3601 * to a restarted association. So the packet is treated as an OOTB
3602 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3603 * called with a NULL association.
3605 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3607 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3610 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3611 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3612 const struct sctp_endpoint *ep,
3613 const struct sctp_association *asoc,
3614 const sctp_subtype_t type, void *arg,
3615 sctp_cmd_seq_t *commands)
3617 struct sctp_chunk *chunk = arg;
3618 struct sctp_chunk *asconf_ack = NULL;
3619 struct sctp_paramhdr *err_param = NULL;
3620 sctp_addiphdr_t *hdr;
3623 if (!sctp_vtag_verify(chunk, asoc)) {
3624 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3626 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3629 /* ADD-IP: Section 4.1.1
3630 * This chunk MUST be sent in an authenticated way by using
3631 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3632 * is received unauthenticated it MUST be silently discarded as
3633 * described in [I-D.ietf-tsvwg-sctp-auth].
3635 if (!net->sctp.addip_noauth && !chunk->auth)
3636 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3638 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3639 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3640 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3643 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3644 serial = ntohl(hdr->serial);
3646 /* Verify the ASCONF chunk before processing it. */
3647 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3648 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3649 (void *)err_param, commands);
3651 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3652 * the endpoint stored in a new association variable
3653 * 'Peer-Serial-Number'.
3655 if (serial == asoc->peer.addip_serial + 1) {
3656 /* If this is the first instance of ASCONF in the packet,
3657 * we can clean our old ASCONF-ACKs.
3659 if (!chunk->has_asconf)
3660 sctp_assoc_clean_asconf_ack_cache(asoc);
3662 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3663 * expected, process the ASCONF as described below and after
3664 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3665 * the response packet and cache a copy of it (in the event it
3666 * later needs to be retransmitted).
3668 * Essentially, do V1-V5.
3670 asconf_ack = sctp_process_asconf((struct sctp_association *)
3673 return SCTP_DISPOSITION_NOMEM;
3674 } else if (serial < asoc->peer.addip_serial + 1) {
3676 * If the value found in the Sequence Number is less than the
3677 * ('Peer- Sequence-Number' + 1), simply skip to the next
3678 * ASCONF, and include in the outbound response packet
3679 * any previously cached ASCONF-ACK response that was
3680 * sent and saved that matches the Sequence Number of the
3681 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3682 * Chunk exists. This will occur when an older ASCONF
3683 * arrives out of order. In such a case, the receiver
3684 * should skip the ASCONF Chunk and not include ASCONF-ACK
3685 * Chunk for that chunk.
3687 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3689 return SCTP_DISPOSITION_DISCARD;
3691 /* Reset the transport so that we select the correct one
3692 * this time around. This is to make sure that we don't
3693 * accidentally use a stale transport that's been removed.
3695 asconf_ack->transport = NULL;
3697 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3698 * it must be either a stale packet or from an attacker.
3700 return SCTP_DISPOSITION_DISCARD;
3703 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3704 * containing the ASCONF-ACK Chunks MUST be the source address of
3705 * the SCTP packet that held the ASCONF Chunks.
3707 * To do this properly, we'll set the destination address of the chunk
3708 * and at the transmit time, will try look up the transport to use.
3709 * Since ASCONFs may be bundled, the correct transport may not be
3710 * created until we process the entire packet, thus this workaround.
3712 asconf_ack->dest = chunk->source;
3713 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3714 if (asoc->new_transport) {
3715 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3716 ((struct sctp_association *)asoc)->new_transport = NULL;
3719 return SCTP_DISPOSITION_CONSUME;
3723 * ADDIP Section 4.3 General rules for address manipulation
3724 * When building TLV parameters for the ASCONF Chunk that will add or
3725 * delete IP addresses the D0 to D13 rules should be applied:
3727 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3728 const struct sctp_endpoint *ep,
3729 const struct sctp_association *asoc,
3730 const sctp_subtype_t type, void *arg,
3731 sctp_cmd_seq_t *commands)
3733 struct sctp_chunk *asconf_ack = arg;
3734 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3735 struct sctp_chunk *abort;
3736 struct sctp_paramhdr *err_param = NULL;
3737 sctp_addiphdr_t *addip_hdr;
3738 __u32 sent_serial, rcvd_serial;
3740 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3741 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3743 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3746 /* ADD-IP, Section 4.1.2:
3747 * This chunk MUST be sent in an authenticated way by using
3748 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3749 * is received unauthenticated it MUST be silently discarded as
3750 * described in [I-D.ietf-tsvwg-sctp-auth].
3752 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3753 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3755 /* Make sure that the ADDIP chunk has a valid length. */
3756 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3757 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3760 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3761 rcvd_serial = ntohl(addip_hdr->serial);
3763 /* Verify the ASCONF-ACK chunk before processing it. */
3764 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3765 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3766 (void *)err_param, commands);
3769 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3770 sent_serial = ntohl(addip_hdr->serial);
3772 sent_serial = asoc->addip_serial - 1;
3775 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3776 * equal to the next serial number to be used but no ASCONF chunk is
3777 * outstanding the endpoint MUST ABORT the association. Note that a
3778 * sequence number is greater than if it is no more than 2^^31-1
3779 * larger than the current sequence number (using serial arithmetic).
3781 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3782 !(asoc->addip_last_asconf)) {
3783 abort = sctp_make_abort(asoc, asconf_ack,
3784 sizeof(sctp_errhdr_t));
3786 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3787 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3790 /* We are going to ABORT, so we might as well stop
3791 * processing the rest of the chunks in the packet.
3793 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3794 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3795 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3796 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3797 SCTP_ERROR(ECONNABORTED));
3798 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3799 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3800 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3801 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3802 return SCTP_DISPOSITION_ABORT;
3805 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3806 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3807 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3809 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3811 /* Successfully processed ASCONF_ACK. We can
3812 * release the next asconf if we have one.
3814 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3816 return SCTP_DISPOSITION_CONSUME;
3819 abort = sctp_make_abort(asoc, asconf_ack,
3820 sizeof(sctp_errhdr_t));
3822 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3823 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3826 /* We are going to ABORT, so we might as well stop
3827 * processing the rest of the chunks in the packet.
3829 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3830 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3831 SCTP_ERROR(ECONNABORTED));
3832 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3833 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3834 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3835 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3836 return SCTP_DISPOSITION_ABORT;
3839 return SCTP_DISPOSITION_DISCARD;
3842 /* RE-CONFIG Section 5.2 Upon reception of an RECONF Chunk. */
3843 sctp_disposition_t sctp_sf_do_reconf(struct net *net,
3844 const struct sctp_endpoint *ep,
3845 const struct sctp_association *asoc,
3846 const sctp_subtype_t type, void *arg,
3847 sctp_cmd_seq_t *commands)
3849 struct sctp_paramhdr *err_param = NULL;
3850 struct sctp_chunk *chunk = arg;
3851 struct sctp_reconf_chunk *hdr;
3852 union sctp_params param;
3854 if (!sctp_vtag_verify(chunk, asoc)) {
3855 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3857 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3860 /* Make sure that the RECONF chunk has a valid length. */
3861 if (!sctp_chunk_length_valid(chunk, sizeof(*hdr)))
3862 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3865 if (!sctp_verify_reconf(asoc, chunk, &err_param))
3866 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3867 (void *)err_param, commands);
3869 hdr = (struct sctp_reconf_chunk *)chunk->chunk_hdr;
3870 sctp_walk_params(param, hdr, params) {
3871 struct sctp_chunk *reply = NULL;
3872 struct sctp_ulpevent *ev = NULL;
3874 if (param.p->type == SCTP_PARAM_RESET_OUT_REQUEST)
3875 reply = sctp_process_strreset_outreq(
3876 (struct sctp_association *)asoc, param, &ev);
3877 else if (param.p->type == SCTP_PARAM_RESET_IN_REQUEST)
3878 reply = sctp_process_strreset_inreq(
3879 (struct sctp_association *)asoc, param, &ev);
3880 else if (param.p->type == SCTP_PARAM_RESET_TSN_REQUEST)
3881 reply = sctp_process_strreset_tsnreq(
3882 (struct sctp_association *)asoc, param, &ev);
3883 else if (param.p->type == SCTP_PARAM_RESET_ADD_OUT_STREAMS)
3884 reply = sctp_process_strreset_addstrm_out(
3885 (struct sctp_association *)asoc, param, &ev);
3886 else if (param.p->type == SCTP_PARAM_RESET_ADD_IN_STREAMS)
3887 reply = sctp_process_strreset_addstrm_in(
3888 (struct sctp_association *)asoc, param, &ev);
3889 else if (param.p->type == SCTP_PARAM_RESET_RESPONSE)
3890 reply = sctp_process_strreset_resp(
3891 (struct sctp_association *)asoc, param, &ev);
3894 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
3898 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3902 return SCTP_DISPOSITION_CONSUME;
3906 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3908 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3909 * its cumulative TSN point to the value carried in the FORWARD TSN
3910 * chunk, and then MUST further advance its cumulative TSN point locally
3912 * After the above processing, the data receiver MUST stop reporting any
3913 * missing TSNs earlier than or equal to the new cumulative TSN point.
3915 * Verification Tag: 8.5 Verification Tag [Normal verification]
3917 * The return value is the disposition of the chunk.
3919 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3920 const struct sctp_endpoint *ep,
3921 const struct sctp_association *asoc,
3922 const sctp_subtype_t type,
3924 sctp_cmd_seq_t *commands)
3926 struct sctp_chunk *chunk = arg;
3927 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3928 struct sctp_fwdtsn_skip *skip;
3932 if (!sctp_vtag_verify(chunk, asoc)) {
3933 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3935 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3938 if (!asoc->peer.prsctp_capable)
3939 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
3941 /* Make sure that the FORWARD_TSN chunk has valid length. */
3942 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3943 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3946 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3947 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3948 len = ntohs(chunk->chunk_hdr->length);
3949 len -= sizeof(struct sctp_chunkhdr);
3950 skb_pull(chunk->skb, len);
3952 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3953 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3955 /* The TSN is too high--silently discard the chunk and count on it
3956 * getting retransmitted later.
3958 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3959 goto discard_noforce;
3961 /* Silently discard the chunk if stream-id is not valid */
3962 sctp_walk_fwdtsn(skip, chunk) {
3963 if (ntohs(skip->stream) >= asoc->stream.incnt)
3964 goto discard_noforce;
3967 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3968 if (len > sizeof(struct sctp_fwdtsn_hdr))
3969 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3972 /* Count this as receiving DATA. */
3973 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3974 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3975 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3978 /* FIXME: For now send a SACK, but DATA processing may
3981 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3983 return SCTP_DISPOSITION_CONSUME;
3986 return SCTP_DISPOSITION_DISCARD;
3989 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3991 const struct sctp_endpoint *ep,
3992 const struct sctp_association *asoc,
3993 const sctp_subtype_t type,
3995 sctp_cmd_seq_t *commands)
3997 struct sctp_chunk *chunk = arg;
3998 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3999 struct sctp_fwdtsn_skip *skip;
4003 if (!sctp_vtag_verify(chunk, asoc)) {
4004 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4006 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4009 if (!asoc->peer.prsctp_capable)
4010 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4012 /* Make sure that the FORWARD_TSN chunk has a valid length. */
4013 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
4014 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4017 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
4018 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
4019 len = ntohs(chunk->chunk_hdr->length);
4020 len -= sizeof(struct sctp_chunkhdr);
4021 skb_pull(chunk->skb, len);
4023 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
4024 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
4026 /* The TSN is too high--silently discard the chunk and count on it
4027 * getting retransmitted later.
4029 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
4032 /* Silently discard the chunk if stream-id is not valid */
4033 sctp_walk_fwdtsn(skip, chunk) {
4034 if (ntohs(skip->stream) >= asoc->stream.incnt)
4038 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
4039 if (len > sizeof(struct sctp_fwdtsn_hdr))
4040 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
4043 /* Go a head and force a SACK, since we are shutting down. */
4045 /* Implementor's Guide.
4047 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
4048 * respond to each received packet containing one or more DATA chunk(s)
4049 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
4051 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
4052 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
4053 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
4054 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
4056 return SCTP_DISPOSITION_CONSUME;
4060 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
4062 * The receiver MUST use the HMAC algorithm indicated in the HMAC
4063 * Identifier field. If this algorithm was not specified by the
4064 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
4065 * during association setup, the AUTH chunk and all chunks after it MUST
4066 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4067 * defined in Section 4.1.
4069 * If an endpoint with no shared key receives a Shared Key Identifier
4070 * other than 0, it MUST silently discard all authenticated chunks. If
4071 * the endpoint has at least one endpoint pair shared key for the peer,
4072 * it MUST use the key specified by the Shared Key Identifier if a
4073 * key has been configured for that Shared Key Identifier. If no
4074 * endpoint pair shared key has been configured for that Shared Key
4075 * Identifier, all authenticated chunks MUST be silently discarded.
4077 * Verification Tag: 8.5 Verification Tag [Normal verification]
4079 * The return value is the disposition of the chunk.
4081 static sctp_ierror_t sctp_sf_authenticate(struct net *net,
4082 const struct sctp_endpoint *ep,
4083 const struct sctp_association *asoc,
4084 const sctp_subtype_t type,
4085 struct sctp_chunk *chunk)
4087 struct sctp_authhdr *auth_hdr;
4088 struct sctp_hmac *hmac;
4089 unsigned int sig_len;
4094 /* Pull in the auth header, so we can do some more verification */
4095 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4096 chunk->subh.auth_hdr = auth_hdr;
4097 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4099 /* Make sure that we support the HMAC algorithm from the auth
4102 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4103 return SCTP_IERROR_AUTH_BAD_HMAC;
4105 /* Make sure that the provided shared key identifier has been
4108 key_id = ntohs(auth_hdr->shkey_id);
4109 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4110 return SCTP_IERROR_AUTH_BAD_KEYID;
4113 /* Make sure that the length of the signature matches what
4116 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4117 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4118 if (sig_len != hmac->hmac_len)
4119 return SCTP_IERROR_PROTO_VIOLATION;
4121 /* Now that we've done validation checks, we can compute and
4122 * verify the hmac. The steps involved are:
4123 * 1. Save the digest from the chunk.
4124 * 2. Zero out the digest in the chunk.
4125 * 3. Compute the new digest
4126 * 4. Compare saved and new digests.
4128 digest = auth_hdr->hmac;
4129 skb_pull(chunk->skb, sig_len);
4131 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4135 memset(digest, 0, sig_len);
4137 sctp_auth_calculate_hmac(asoc, chunk->skb,
4138 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4141 /* Discard the packet if the digests do not match */
4142 if (memcmp(save_digest, digest, sig_len)) {
4144 return SCTP_IERROR_BAD_SIG;
4150 return SCTP_IERROR_NO_ERROR;
4152 return SCTP_IERROR_NOMEM;
4155 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4156 const struct sctp_endpoint *ep,
4157 const struct sctp_association *asoc,
4158 const sctp_subtype_t type,
4160 sctp_cmd_seq_t *commands)
4162 struct sctp_authhdr *auth_hdr;
4163 struct sctp_chunk *chunk = arg;
4164 struct sctp_chunk *err_chunk;
4165 sctp_ierror_t error;
4167 /* Make sure that the peer has AUTH capable */
4168 if (!asoc->peer.auth_capable)
4169 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4171 if (!sctp_vtag_verify(chunk, asoc)) {
4172 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4174 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4177 /* Make sure that the AUTH chunk has valid length. */
4178 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4179 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4182 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4183 error = sctp_sf_authenticate(net, ep, asoc, type, chunk);
4185 case SCTP_IERROR_AUTH_BAD_HMAC:
4186 /* Generate the ERROR chunk and discard the rest
4189 err_chunk = sctp_make_op_error(asoc, chunk,
4190 SCTP_ERROR_UNSUP_HMAC,
4194 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4195 SCTP_CHUNK(err_chunk));
4198 case SCTP_IERROR_AUTH_BAD_KEYID:
4199 case SCTP_IERROR_BAD_SIG:
4200 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4202 case SCTP_IERROR_PROTO_VIOLATION:
4203 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4206 case SCTP_IERROR_NOMEM:
4207 return SCTP_DISPOSITION_NOMEM;
4209 default: /* Prevent gcc warnings */
4213 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4214 struct sctp_ulpevent *ev;
4216 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4217 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4222 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4226 return SCTP_DISPOSITION_CONSUME;
4230 * Process an unknown chunk.
4232 * Section: 3.2. Also, 2.1 in the implementor's guide.
4234 * Chunk Types are encoded such that the highest-order two bits specify
4235 * the action that must be taken if the processing endpoint does not
4236 * recognize the Chunk Type.
4238 * 00 - Stop processing this SCTP packet and discard it, do not process
4239 * any further chunks within it.
4241 * 01 - Stop processing this SCTP packet and discard it, do not process
4242 * any further chunks within it, and report the unrecognized
4243 * chunk in an 'Unrecognized Chunk Type'.
4245 * 10 - Skip this chunk and continue processing.
4247 * 11 - Skip this chunk and continue processing, but report in an ERROR
4248 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4250 * The return value is the disposition of the chunk.
4252 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4253 const struct sctp_endpoint *ep,
4254 const struct sctp_association *asoc,
4255 const sctp_subtype_t type,
4257 sctp_cmd_seq_t *commands)
4259 struct sctp_chunk *unk_chunk = arg;
4260 struct sctp_chunk *err_chunk;
4261 struct sctp_chunkhdr *hdr;
4263 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4265 if (!sctp_vtag_verify(unk_chunk, asoc))
4266 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4268 /* Make sure that the chunk has a valid length.
4269 * Since we don't know the chunk type, we use a general
4270 * chunkhdr structure to make a comparison.
4272 if (!sctp_chunk_length_valid(unk_chunk, sizeof(*hdr)))
4273 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4276 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4277 case SCTP_CID_ACTION_DISCARD:
4278 /* Discard the packet. */
4279 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4280 case SCTP_CID_ACTION_DISCARD_ERR:
4281 /* Generate an ERROR chunk as response. */
4282 hdr = unk_chunk->chunk_hdr;
4283 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4284 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4285 SCTP_PAD4(ntohs(hdr->length)),
4288 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4289 SCTP_CHUNK(err_chunk));
4292 /* Discard the packet. */
4293 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4294 return SCTP_DISPOSITION_CONSUME;
4295 case SCTP_CID_ACTION_SKIP:
4296 /* Skip the chunk. */
4297 return SCTP_DISPOSITION_DISCARD;
4298 case SCTP_CID_ACTION_SKIP_ERR:
4299 /* Generate an ERROR chunk as response. */
4300 hdr = unk_chunk->chunk_hdr;
4301 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4302 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4303 SCTP_PAD4(ntohs(hdr->length)),
4306 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4307 SCTP_CHUNK(err_chunk));
4309 /* Skip the chunk. */
4310 return SCTP_DISPOSITION_CONSUME;
4315 return SCTP_DISPOSITION_DISCARD;
4319 * Discard the chunk.
4321 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4322 * [Too numerous to mention...]
4323 * Verification Tag: No verification needed.
4325 * (endpoint, asoc, chunk)
4328 * (asoc, reply_msg, msg_up, timers, counters)
4330 * The return value is the disposition of the chunk.
4332 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4333 const struct sctp_endpoint *ep,
4334 const struct sctp_association *asoc,
4335 const sctp_subtype_t type,
4337 sctp_cmd_seq_t *commands)
4339 struct sctp_chunk *chunk = arg;
4341 /* Make sure that the chunk has a valid length.
4342 * Since we don't know the chunk type, we use a general
4343 * chunkhdr structure to make a comparison.
4345 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4346 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4349 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4351 return SCTP_DISPOSITION_DISCARD;
4355 * Discard the whole packet.
4359 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4360 * silently discard the OOTB packet and take no further action.
4362 * Verification Tag: No verification necessary
4365 * (endpoint, asoc, chunk)
4368 * (asoc, reply_msg, msg_up, timers, counters)
4370 * The return value is the disposition of the chunk.
4372 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4373 const struct sctp_endpoint *ep,
4374 const struct sctp_association *asoc,
4375 const sctp_subtype_t type,
4377 sctp_cmd_seq_t *commands)
4379 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4380 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4382 return SCTP_DISPOSITION_CONSUME;
4387 * The other end is violating protocol.
4389 * Section: Not specified
4390 * Verification Tag: Not specified
4392 * (endpoint, asoc, chunk)
4395 * (asoc, reply_msg, msg_up, timers, counters)
4397 * We simply tag the chunk as a violation. The state machine will log
4398 * the violation and continue.
4400 sctp_disposition_t sctp_sf_violation(struct net *net,
4401 const struct sctp_endpoint *ep,
4402 const struct sctp_association *asoc,
4403 const sctp_subtype_t type,
4405 sctp_cmd_seq_t *commands)
4407 struct sctp_chunk *chunk = arg;
4409 /* Make sure that the chunk has a valid length. */
4410 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_chunkhdr)))
4411 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4414 return SCTP_DISPOSITION_VIOLATION;
4418 * Common function to handle a protocol violation.
4420 static sctp_disposition_t sctp_sf_abort_violation(
4422 const struct sctp_endpoint *ep,
4423 const struct sctp_association *asoc,
4425 sctp_cmd_seq_t *commands,
4426 const __u8 *payload,
4427 const size_t paylen)
4429 struct sctp_packet *packet = NULL;
4430 struct sctp_chunk *chunk = arg;
4431 struct sctp_chunk *abort = NULL;
4433 /* SCTP-AUTH, Section 6.3:
4434 * It should be noted that if the receiver wants to tear
4435 * down an association in an authenticated way only, the
4436 * handling of malformed packets should not result in
4437 * tearing down the association.
4439 * This means that if we only want to abort associations
4440 * in an authenticated way (i.e AUTH+ABORT), then we
4441 * can't destroy this association just because the packet
4444 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4447 /* Make the abort chunk. */
4448 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4453 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4454 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4455 !asoc->peer.i.init_tag) {
4456 sctp_initack_chunk_t *initack;
4458 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4459 if (!sctp_chunk_length_valid(chunk,
4460 sizeof(sctp_initack_chunk_t)))
4461 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4463 unsigned int inittag;
4465 inittag = ntohl(initack->init_hdr.init_tag);
4466 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4471 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4472 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4474 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4475 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4476 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4477 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4478 SCTP_ERROR(ECONNREFUSED));
4479 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4480 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4482 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4483 SCTP_ERROR(ECONNABORTED));
4484 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4485 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4486 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4489 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4494 if (sctp_test_T_bit(abort))
4495 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4497 abort->skb->sk = ep->base.sk;
4499 sctp_packet_append_chunk(packet, abort);
4501 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4502 SCTP_PACKET(packet));
4504 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4507 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4510 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4511 return SCTP_DISPOSITION_ABORT;
4514 sctp_chunk_free(abort);
4516 return SCTP_DISPOSITION_NOMEM;
4520 * Handle a protocol violation when the chunk length is invalid.
4521 * "Invalid" length is identified as smaller than the minimal length a
4522 * given chunk can be. For example, a SACK chunk has invalid length
4523 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4525 * We inform the other end by sending an ABORT with a Protocol Violation
4528 * Section: Not specified
4529 * Verification Tag: Nothing to do
4531 * (endpoint, asoc, chunk)
4534 * (reply_msg, msg_up, counters)
4536 * Generate an ABORT chunk and terminate the association.
4538 static sctp_disposition_t sctp_sf_violation_chunklen(
4540 const struct sctp_endpoint *ep,
4541 const struct sctp_association *asoc,
4542 const sctp_subtype_t type,
4544 sctp_cmd_seq_t *commands)
4546 static const char err_str[] = "The following chunk had invalid length:";
4548 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4553 * Handle a protocol violation when the parameter length is invalid.
4554 * If the length is smaller than the minimum length of a given parameter,
4555 * or accumulated length in multi parameters exceeds the end of the chunk,
4556 * the length is considered as invalid.
4558 static sctp_disposition_t sctp_sf_violation_paramlen(
4560 const struct sctp_endpoint *ep,
4561 const struct sctp_association *asoc,
4562 const sctp_subtype_t type,
4563 void *arg, void *ext,
4564 sctp_cmd_seq_t *commands)
4566 struct sctp_chunk *chunk = arg;
4567 struct sctp_paramhdr *param = ext;
4568 struct sctp_chunk *abort = NULL;
4570 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4573 /* Make the abort chunk. */
4574 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4578 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4579 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4581 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4582 SCTP_ERROR(ECONNABORTED));
4583 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4584 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4585 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4586 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4589 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4590 return SCTP_DISPOSITION_ABORT;
4592 return SCTP_DISPOSITION_NOMEM;
4595 /* Handle a protocol violation when the peer trying to advance the
4596 * cumulative tsn ack to a point beyond the max tsn currently sent.
4598 * We inform the other end by sending an ABORT with a Protocol Violation
4601 static sctp_disposition_t sctp_sf_violation_ctsn(
4603 const struct sctp_endpoint *ep,
4604 const struct sctp_association *asoc,
4605 const sctp_subtype_t type,
4607 sctp_cmd_seq_t *commands)
4609 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4611 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4615 /* Handle protocol violation of an invalid chunk bundling. For example,
4616 * when we have an association and we receive bundled INIT-ACK, or
4617 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4618 * statement from the specs. Additionally, there might be an attacker
4619 * on the path and we may not want to continue this communication.
4621 static sctp_disposition_t sctp_sf_violation_chunk(
4623 const struct sctp_endpoint *ep,
4624 const struct sctp_association *asoc,
4625 const sctp_subtype_t type,
4627 sctp_cmd_seq_t *commands)
4629 static const char err_str[] = "The following chunk violates protocol:";
4632 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4634 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4637 /***************************************************************************
4638 * These are the state functions for handling primitive (Section 10) events.
4639 ***************************************************************************/
4641 * sctp_sf_do_prm_asoc
4643 * Section: 10.1 ULP-to-SCTP
4646 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4647 * outbound stream count)
4648 * -> association id [,destination transport addr list] [,outbound stream
4651 * This primitive allows the upper layer to initiate an association to a
4652 * specific peer endpoint.
4654 * The peer endpoint shall be specified by one of the transport addresses
4655 * which defines the endpoint (see Section 1.4). If the local SCTP
4656 * instance has not been initialized, the ASSOCIATE is considered an
4658 * [This is not relevant for the kernel implementation since we do all
4659 * initialization at boot time. It we hadn't initialized we wouldn't
4660 * get anywhere near this code.]
4662 * An association id, which is a local handle to the SCTP association,
4663 * will be returned on successful establishment of the association. If
4664 * SCTP is not able to open an SCTP association with the peer endpoint,
4665 * an error is returned.
4666 * [In the kernel implementation, the struct sctp_association needs to
4667 * be created BEFORE causing this primitive to run.]
4669 * Other association parameters may be returned, including the
4670 * complete destination transport addresses of the peer as well as the
4671 * outbound stream count of the local endpoint. One of the transport
4672 * address from the returned destination addresses will be selected by
4673 * the local endpoint as default primary path for sending SCTP packets
4674 * to this peer. The returned "destination transport addr list" can
4675 * be used by the ULP to change the default primary path or to force
4676 * sending a packet to a specific transport address. [All of this
4677 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4680 * Mandatory attributes:
4682 * o local SCTP instance name - obtained from the INITIALIZE operation.
4683 * [This is the argument asoc.]
4684 * o destination transport addr - specified as one of the transport
4685 * addresses of the peer endpoint with which the association is to be
4687 * [This is asoc->peer.active_path.]
4688 * o outbound stream count - the number of outbound streams the ULP
4689 * would like to open towards this peer endpoint.
4690 * [BUG: This is not currently implemented.]
4691 * Optional attributes:
4695 * The return value is a disposition.
4697 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4698 const struct sctp_endpoint *ep,
4699 const struct sctp_association *asoc,
4700 const sctp_subtype_t type,
4702 sctp_cmd_seq_t *commands)
4704 struct sctp_chunk *repl;
4705 struct sctp_association *my_asoc;
4707 /* The comment below says that we enter COOKIE-WAIT AFTER
4708 * sending the INIT, but that doesn't actually work in our
4711 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4712 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4714 /* RFC 2960 5.1 Normal Establishment of an Association
4716 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4717 * must provide its Verification Tag (Tag_A) in the Initiate
4718 * Tag field. Tag_A SHOULD be a random number in the range of
4719 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4722 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4726 /* Choose transport for INIT. */
4727 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4730 /* Cast away the const modifier, as we want to just
4731 * rerun it through as a sideffect.
4733 my_asoc = (struct sctp_association *)asoc;
4734 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4736 /* After sending the INIT, "A" starts the T1-init timer and
4737 * enters the COOKIE-WAIT state.
4739 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4740 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4741 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4742 return SCTP_DISPOSITION_CONSUME;
4745 return SCTP_DISPOSITION_NOMEM;
4749 * Process the SEND primitive.
4751 * Section: 10.1 ULP-to-SCTP
4754 * Format: SEND(association id, buffer address, byte count [,context]
4755 * [,stream id] [,life time] [,destination transport address]
4756 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4759 * This is the main method to send user data via SCTP.
4761 * Mandatory attributes:
4763 * o association id - local handle to the SCTP association
4765 * o buffer address - the location where the user message to be
4766 * transmitted is stored;
4768 * o byte count - The size of the user data in number of bytes;
4770 * Optional attributes:
4772 * o context - an optional 32 bit integer that will be carried in the
4773 * sending failure notification to the ULP if the transportation of
4774 * this User Message fails.
4776 * o stream id - to indicate which stream to send the data on. If not
4777 * specified, stream 0 will be used.
4779 * o life time - specifies the life time of the user data. The user data
4780 * will not be sent by SCTP after the life time expires. This
4781 * parameter can be used to avoid efforts to transmit stale
4782 * user messages. SCTP notifies the ULP if the data cannot be
4783 * initiated to transport (i.e. sent to the destination via SCTP's
4784 * send primitive) within the life time variable. However, the
4785 * user data will be transmitted if SCTP has attempted to transmit a
4786 * chunk before the life time expired.
4788 * o destination transport address - specified as one of the destination
4789 * transport addresses of the peer endpoint to which this packet
4790 * should be sent. Whenever possible, SCTP should use this destination
4791 * transport address for sending the packets, instead of the current
4794 * o unorder flag - this flag, if present, indicates that the user
4795 * would like the data delivered in an unordered fashion to the peer
4796 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4799 * o no-bundle flag - instructs SCTP not to bundle this user data with
4800 * other outbound DATA chunks. SCTP MAY still bundle even when
4801 * this flag is present, when faced with network congestion.
4803 * o payload protocol-id - A 32 bit unsigned integer that is to be
4804 * passed to the peer indicating the type of payload protocol data
4805 * being transmitted. This value is passed as opaque data by SCTP.
4807 * The return value is the disposition.
4809 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4810 const struct sctp_endpoint *ep,
4811 const struct sctp_association *asoc,
4812 const sctp_subtype_t type,
4814 sctp_cmd_seq_t *commands)
4816 struct sctp_datamsg *msg = arg;
4818 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4819 return SCTP_DISPOSITION_CONSUME;
4823 * Process the SHUTDOWN primitive.
4828 * Format: SHUTDOWN(association id)
4831 * Gracefully closes an association. Any locally queued user data
4832 * will be delivered to the peer. The association will be terminated only
4833 * after the peer acknowledges all the SCTP packets sent. A success code
4834 * will be returned on successful termination of the association. If
4835 * attempting to terminate the association results in a failure, an error
4836 * code shall be returned.
4838 * Mandatory attributes:
4840 * o association id - local handle to the SCTP association
4842 * Optional attributes:
4846 * The return value is the disposition.
4848 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4850 const struct sctp_endpoint *ep,
4851 const struct sctp_association *asoc,
4852 const sctp_subtype_t type,
4854 sctp_cmd_seq_t *commands)
4858 /* From 9.2 Shutdown of an Association
4859 * Upon receipt of the SHUTDOWN primitive from its upper
4860 * layer, the endpoint enters SHUTDOWN-PENDING state and
4861 * remains there until all outstanding data has been
4862 * acknowledged by its peer. The endpoint accepts no new data
4863 * from its upper layer, but retransmits data to the far end
4864 * if necessary to fill gaps.
4866 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4867 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4869 disposition = SCTP_DISPOSITION_CONSUME;
4870 if (sctp_outq_is_empty(&asoc->outqueue)) {
4871 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4878 * Process the ABORT primitive.
4883 * Format: Abort(association id [, cause code])
4886 * Ungracefully closes an association. Any locally queued user data
4887 * will be discarded and an ABORT chunk is sent to the peer. A success code
4888 * will be returned on successful abortion of the association. If
4889 * attempting to abort the association results in a failure, an error
4890 * code shall be returned.
4892 * Mandatory attributes:
4894 * o association id - local handle to the SCTP association
4896 * Optional attributes:
4898 * o cause code - reason of the abort to be passed to the peer
4902 * The return value is the disposition.
4904 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4906 const struct sctp_endpoint *ep,
4907 const struct sctp_association *asoc,
4908 const sctp_subtype_t type,
4910 sctp_cmd_seq_t *commands)
4912 /* From 9.1 Abort of an Association
4913 * Upon receipt of the ABORT primitive from its upper
4914 * layer, the endpoint enters CLOSED state and
4915 * discard all outstanding data has been
4916 * acknowledged by its peer. The endpoint accepts no new data
4917 * from its upper layer, but retransmits data to the far end
4918 * if necessary to fill gaps.
4920 struct sctp_chunk *abort = arg;
4923 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4925 /* Even if we can't send the ABORT due to low memory delete the
4926 * TCB. This is a departure from our typical NOMEM handling.
4929 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4930 SCTP_ERROR(ECONNABORTED));
4931 /* Delete the established association. */
4932 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4933 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4935 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4936 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4938 return SCTP_DISPOSITION_ABORT;
4941 /* We tried an illegal operation on an association which is closed. */
4942 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4943 const struct sctp_endpoint *ep,
4944 const struct sctp_association *asoc,
4945 const sctp_subtype_t type,
4947 sctp_cmd_seq_t *commands)
4949 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4950 return SCTP_DISPOSITION_CONSUME;
4953 /* We tried an illegal operation on an association which is shutting
4956 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4957 const struct sctp_endpoint *ep,
4958 const struct sctp_association *asoc,
4959 const sctp_subtype_t type,
4961 sctp_cmd_seq_t *commands)
4963 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4964 SCTP_ERROR(-ESHUTDOWN));
4965 return SCTP_DISPOSITION_CONSUME;
4969 * sctp_cookie_wait_prm_shutdown
4971 * Section: 4 Note: 2
4976 * The RFC does not explicitly address this issue, but is the route through the
4977 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4982 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4984 const struct sctp_endpoint *ep,
4985 const struct sctp_association *asoc,
4986 const sctp_subtype_t type,
4988 sctp_cmd_seq_t *commands)
4990 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4991 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4993 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4994 SCTP_STATE(SCTP_STATE_CLOSED));
4996 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4998 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
5000 return SCTP_DISPOSITION_DELETE_TCB;
5004 * sctp_cookie_echoed_prm_shutdown
5006 * Section: 4 Note: 2
5011 * The RFC does not explcitly address this issue, but is the route through the
5012 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
5017 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
5019 const struct sctp_endpoint *ep,
5020 const struct sctp_association *asoc,
5021 const sctp_subtype_t type,
5022 void *arg, sctp_cmd_seq_t *commands)
5024 /* There is a single T1 timer, so we should be able to use
5025 * common function with the COOKIE-WAIT state.
5027 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
5031 * sctp_sf_cookie_wait_prm_abort
5033 * Section: 4 Note: 2
5038 * The RFC does not explicitly address this issue, but is the route through the
5039 * state table when someone issues an abort while in COOKIE_WAIT state.
5044 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
5046 const struct sctp_endpoint *ep,
5047 const struct sctp_association *asoc,
5048 const sctp_subtype_t type,
5050 sctp_cmd_seq_t *commands)
5052 struct sctp_chunk *abort = arg;
5054 /* Stop T1-init timer */
5055 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5056 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5059 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5061 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5062 SCTP_STATE(SCTP_STATE_CLOSED));
5064 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5066 /* Even if we can't send the ABORT due to low memory delete the
5067 * TCB. This is a departure from our typical NOMEM handling.
5070 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5071 SCTP_ERROR(ECONNREFUSED));
5072 /* Delete the established association. */
5073 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5074 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5076 return SCTP_DISPOSITION_ABORT;
5080 * sctp_sf_cookie_echoed_prm_abort
5082 * Section: 4 Note: 3
5087 * The RFC does not explcitly address this issue, but is the route through the
5088 * state table when someone issues an abort while in COOKIE_ECHOED state.
5093 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5095 const struct sctp_endpoint *ep,
5096 const struct sctp_association *asoc,
5097 const sctp_subtype_t type,
5099 sctp_cmd_seq_t *commands)
5101 /* There is a single T1 timer, so we should be able to use
5102 * common function with the COOKIE-WAIT state.
5104 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5108 * sctp_sf_shutdown_pending_prm_abort
5113 * The RFC does not explicitly address this issue, but is the route through the
5114 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5119 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5121 const struct sctp_endpoint *ep,
5122 const struct sctp_association *asoc,
5123 const sctp_subtype_t type,
5125 sctp_cmd_seq_t *commands)
5127 /* Stop the T5-shutdown guard timer. */
5128 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5129 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5131 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5135 * sctp_sf_shutdown_sent_prm_abort
5140 * The RFC does not explicitly address this issue, but is the route through the
5141 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5146 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5148 const struct sctp_endpoint *ep,
5149 const struct sctp_association *asoc,
5150 const sctp_subtype_t type,
5152 sctp_cmd_seq_t *commands)
5154 /* Stop the T2-shutdown timer. */
5155 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5156 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5158 /* Stop the T5-shutdown guard timer. */
5159 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5160 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5162 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5166 * sctp_sf_cookie_echoed_prm_abort
5171 * The RFC does not explcitly address this issue, but is the route through the
5172 * state table when someone issues an abort while in COOKIE_ECHOED state.
5177 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5179 const struct sctp_endpoint *ep,
5180 const struct sctp_association *asoc,
5181 const sctp_subtype_t type,
5183 sctp_cmd_seq_t *commands)
5185 /* The same T2 timer, so we should be able to use
5186 * common function with the SHUTDOWN-SENT state.
5188 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5192 * Process the REQUESTHEARTBEAT primitive
5195 * J) Request Heartbeat
5197 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5201 * Instructs the local endpoint to perform a HeartBeat on the specified
5202 * destination transport address of the given association. The returned
5203 * result should indicate whether the transmission of the HEARTBEAT
5204 * chunk to the destination address is successful.
5206 * Mandatory attributes:
5208 * o association id - local handle to the SCTP association
5210 * o destination transport address - the transport address of the
5211 * association on which a heartbeat should be issued.
5213 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5215 const struct sctp_endpoint *ep,
5216 const struct sctp_association *asoc,
5217 const sctp_subtype_t type,
5219 sctp_cmd_seq_t *commands)
5221 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5222 (struct sctp_transport *)arg, commands))
5223 return SCTP_DISPOSITION_NOMEM;
5226 * RFC 2960 (bis), section 8.3
5228 * D) Request an on-demand HEARTBEAT on a specific destination
5229 * transport address of a given association.
5231 * The endpoint should increment the respective error counter of
5232 * the destination transport address each time a HEARTBEAT is sent
5233 * to that address and not acknowledged within one RTO.
5236 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5237 SCTP_TRANSPORT(arg));
5238 return SCTP_DISPOSITION_CONSUME;
5242 * ADDIP Section 4.1 ASCONF Chunk Procedures
5243 * When an endpoint has an ASCONF signaled change to be sent to the
5244 * remote endpoint it should do A1 to A9
5246 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5247 const struct sctp_endpoint *ep,
5248 const struct sctp_association *asoc,
5249 const sctp_subtype_t type,
5251 sctp_cmd_seq_t *commands)
5253 struct sctp_chunk *chunk = arg;
5255 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5256 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5257 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5258 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5259 return SCTP_DISPOSITION_CONSUME;
5262 /* RE-CONFIG Section 5.1 RECONF Chunk Procedures */
5263 sctp_disposition_t sctp_sf_do_prm_reconf(struct net *net,
5264 const struct sctp_endpoint *ep,
5265 const struct sctp_association *asoc,
5266 const sctp_subtype_t type,
5267 void *arg, sctp_cmd_seq_t *commands)
5269 struct sctp_chunk *chunk = arg;
5271 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5272 return SCTP_DISPOSITION_CONSUME;
5276 * Ignore the primitive event
5278 * The return value is the disposition of the primitive.
5280 sctp_disposition_t sctp_sf_ignore_primitive(
5282 const struct sctp_endpoint *ep,
5283 const struct sctp_association *asoc,
5284 const sctp_subtype_t type,
5286 sctp_cmd_seq_t *commands)
5288 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5291 return SCTP_DISPOSITION_DISCARD;
5294 /***************************************************************************
5295 * These are the state functions for the OTHER events.
5296 ***************************************************************************/
5299 * When the SCTP stack has no more user data to send or retransmit, this
5300 * notification is given to the user. Also, at the time when a user app
5301 * subscribes to this event, if there is no data to be sent or
5302 * retransmit, the stack will immediately send up this notification.
5304 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5306 const struct sctp_endpoint *ep,
5307 const struct sctp_association *asoc,
5308 const sctp_subtype_t type,
5310 sctp_cmd_seq_t *commands)
5312 struct sctp_ulpevent *event;
5314 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5316 return SCTP_DISPOSITION_NOMEM;
5318 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5320 return SCTP_DISPOSITION_CONSUME;
5324 * Start the shutdown negotiation.
5327 * Once all its outstanding data has been acknowledged, the endpoint
5328 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5329 * TSN Ack field the last sequential TSN it has received from the peer.
5330 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5331 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5332 * with the updated last sequential TSN received from its peer.
5334 * The return value is the disposition.
5336 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5338 const struct sctp_endpoint *ep,
5339 const struct sctp_association *asoc,
5340 const sctp_subtype_t type,
5342 sctp_cmd_seq_t *commands)
5344 struct sctp_chunk *reply;
5346 /* Once all its outstanding data has been acknowledged, the
5347 * endpoint shall send a SHUTDOWN chunk to its peer including
5348 * in the Cumulative TSN Ack field the last sequential TSN it
5349 * has received from the peer.
5351 reply = sctp_make_shutdown(asoc, NULL);
5355 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5356 * T2-shutdown timer.
5358 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5360 /* It shall then start the T2-shutdown timer */
5361 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5362 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5364 /* RFC 4960 Section 9.2
5365 * The sender of the SHUTDOWN MAY also start an overall guard timer
5366 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5368 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5369 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5371 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5372 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5373 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5375 /* and enter the SHUTDOWN-SENT state. */
5376 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5377 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5379 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5381 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5384 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5386 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5388 return SCTP_DISPOSITION_CONSUME;
5391 return SCTP_DISPOSITION_NOMEM;
5395 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5399 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5400 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5401 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5402 * endpoint must re-send the SHUTDOWN ACK.
5404 * The return value is the disposition.
5406 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5408 const struct sctp_endpoint *ep,
5409 const struct sctp_association *asoc,
5410 const sctp_subtype_t type,
5412 sctp_cmd_seq_t *commands)
5414 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5415 struct sctp_chunk *reply;
5417 /* There are 2 ways of getting here:
5418 * 1) called in response to a SHUTDOWN chunk
5419 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5421 * For the case (2), the arg parameter is set to NULL. We need
5422 * to check that we have a chunk before accessing it's fields.
5425 if (!sctp_vtag_verify(chunk, asoc))
5426 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5428 /* Make sure that the SHUTDOWN chunk has a valid length. */
5429 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5430 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5434 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5435 * shall send a SHUTDOWN ACK ...
5437 reply = sctp_make_shutdown_ack(asoc, chunk);
5441 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5442 * the T2-shutdown timer.
5444 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5446 /* and start/restart a T2-shutdown timer of its own, */
5447 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5448 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5450 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5451 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5452 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5454 /* Enter the SHUTDOWN-ACK-SENT state. */
5455 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5456 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5458 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5460 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5463 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5465 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5467 return SCTP_DISPOSITION_CONSUME;
5470 return SCTP_DISPOSITION_NOMEM;
5474 * Ignore the event defined as other
5476 * The return value is the disposition of the event.
5478 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5479 const struct sctp_endpoint *ep,
5480 const struct sctp_association *asoc,
5481 const sctp_subtype_t type,
5483 sctp_cmd_seq_t *commands)
5485 pr_debug("%s: the event other type:%d is ignored\n",
5486 __func__, type.other);
5488 return SCTP_DISPOSITION_DISCARD;
5491 /************************************************************
5492 * These are the state functions for handling timeout events.
5493 ************************************************************/
5498 * Section: 6.3.3 Handle T3-rtx Expiration
5500 * Whenever the retransmission timer T3-rtx expires for a destination
5501 * address, do the following:
5504 * The return value is the disposition of the chunk.
5506 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5507 const struct sctp_endpoint *ep,
5508 const struct sctp_association *asoc,
5509 const sctp_subtype_t type,
5511 sctp_cmd_seq_t *commands)
5513 struct sctp_transport *transport = arg;
5515 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5517 if (asoc->overall_error_count >= asoc->max_retrans) {
5518 if (asoc->peer.zero_window_announced &&
5519 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5521 * We are here likely because the receiver had its rwnd
5522 * closed for a while and we have not been able to
5523 * transmit the locally queued data within the maximum
5524 * retransmission attempts limit. Start the T5
5525 * shutdown guard timer to give the receiver one last
5526 * chance and some additional time to recover before
5529 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5530 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5532 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5533 SCTP_ERROR(ETIMEDOUT));
5534 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5535 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5536 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5537 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5538 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5539 return SCTP_DISPOSITION_DELETE_TCB;
5543 /* E1) For the destination address for which the timer
5544 * expires, adjust its ssthresh with rules defined in Section
5545 * 7.2.3 and set the cwnd <- MTU.
5548 /* E2) For the destination address for which the timer
5549 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5550 * maximum value discussed in rule C7 above (RTO.max) may be
5551 * used to provide an upper bound to this doubling operation.
5554 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5555 * outstanding DATA chunks for the address for which the
5556 * T3-rtx has expired will fit into a single packet, subject
5557 * to the MTU constraint for the path corresponding to the
5558 * destination transport address to which the retransmission
5559 * is being sent (this may be different from the address for
5560 * which the timer expires [see Section 6.4]). Call this
5561 * value K. Bundle and retransmit those K DATA chunks in a
5562 * single packet to the destination endpoint.
5564 * Note: Any DATA chunks that were sent to the address for
5565 * which the T3-rtx timer expired but did not fit in one MTU
5566 * (rule E3 above), should be marked for retransmission and
5567 * sent as soon as cwnd allows (normally when a SACK arrives).
5570 /* Do some failure management (Section 8.2). */
5571 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5573 /* NB: Rules E4 and F1 are implicit in R1. */
5574 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5576 return SCTP_DISPOSITION_CONSUME;
5580 * Generate delayed SACK on timeout
5582 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5584 * The guidelines on delayed acknowledgement algorithm specified in
5585 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5586 * acknowledgement SHOULD be generated for at least every second packet
5587 * (not every second DATA chunk) received, and SHOULD be generated
5588 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5589 * some situations it may be beneficial for an SCTP transmitter to be
5590 * more conservative than the algorithms detailed in this document
5591 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5592 * the following algorithms allow.
5594 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5595 const struct sctp_endpoint *ep,
5596 const struct sctp_association *asoc,
5597 const sctp_subtype_t type,
5599 sctp_cmd_seq_t *commands)
5601 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5602 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5603 return SCTP_DISPOSITION_CONSUME;
5607 * sctp_sf_t1_init_timer_expire
5609 * Section: 4 Note: 2
5614 * RFC 2960 Section 4 Notes
5615 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5616 * and re-start the T1-init timer without changing state. This MUST
5617 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5618 * endpoint MUST abort the initialization process and report the
5619 * error to SCTP user.
5625 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5626 const struct sctp_endpoint *ep,
5627 const struct sctp_association *asoc,
5628 const sctp_subtype_t type,
5630 sctp_cmd_seq_t *commands)
5632 struct sctp_chunk *repl = NULL;
5633 struct sctp_bind_addr *bp;
5634 int attempts = asoc->init_err_counter + 1;
5636 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5638 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5640 if (attempts <= asoc->max_init_attempts) {
5641 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5642 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5644 return SCTP_DISPOSITION_NOMEM;
5646 /* Choose transport for INIT. */
5647 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5650 /* Issue a sideeffect to do the needed accounting. */
5651 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5652 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5654 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5656 pr_debug("%s: giving up on INIT, attempts:%d "
5657 "max_init_attempts:%d\n", __func__, attempts,
5658 asoc->max_init_attempts);
5660 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5661 SCTP_ERROR(ETIMEDOUT));
5662 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5663 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5664 return SCTP_DISPOSITION_DELETE_TCB;
5667 return SCTP_DISPOSITION_CONSUME;
5671 * sctp_sf_t1_cookie_timer_expire
5673 * Section: 4 Note: 2
5678 * RFC 2960 Section 4 Notes
5679 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5680 * COOKIE ECHO and re-start the T1-cookie timer without changing
5681 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5682 * After that, the endpoint MUST abort the initialization process and
5683 * report the error to SCTP user.
5689 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5690 const struct sctp_endpoint *ep,
5691 const struct sctp_association *asoc,
5692 const sctp_subtype_t type,
5694 sctp_cmd_seq_t *commands)
5696 struct sctp_chunk *repl = NULL;
5697 int attempts = asoc->init_err_counter + 1;
5699 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5701 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5703 if (attempts <= asoc->max_init_attempts) {
5704 repl = sctp_make_cookie_echo(asoc, NULL);
5706 return SCTP_DISPOSITION_NOMEM;
5708 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5710 /* Issue a sideeffect to do the needed accounting. */
5711 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5712 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5714 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5716 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5717 SCTP_ERROR(ETIMEDOUT));
5718 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5719 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5720 return SCTP_DISPOSITION_DELETE_TCB;
5723 return SCTP_DISPOSITION_CONSUME;
5726 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5727 * with the updated last sequential TSN received from its peer.
5729 * An endpoint should limit the number of retransmissions of the
5730 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5731 * If this threshold is exceeded the endpoint should destroy the TCB and
5732 * MUST report the peer endpoint unreachable to the upper layer (and
5733 * thus the association enters the CLOSED state). The reception of any
5734 * packet from its peer (i.e. as the peer sends all of its queued DATA
5735 * chunks) should clear the endpoint's retransmission count and restart
5736 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5737 * all of its queued DATA chunks that have not yet been sent.
5739 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5740 const struct sctp_endpoint *ep,
5741 const struct sctp_association *asoc,
5742 const sctp_subtype_t type,
5744 sctp_cmd_seq_t *commands)
5746 struct sctp_chunk *reply = NULL;
5748 pr_debug("%s: timer T2 expired\n", __func__);
5750 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5752 ((struct sctp_association *)asoc)->shutdown_retries++;
5754 if (asoc->overall_error_count >= asoc->max_retrans) {
5755 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5756 SCTP_ERROR(ETIMEDOUT));
5757 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5758 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5759 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5760 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5761 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5762 return SCTP_DISPOSITION_DELETE_TCB;
5765 switch (asoc->state) {
5766 case SCTP_STATE_SHUTDOWN_SENT:
5767 reply = sctp_make_shutdown(asoc, NULL);
5770 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5771 reply = sctp_make_shutdown_ack(asoc, NULL);
5782 /* Do some failure management (Section 8.2).
5783 * If we remove the transport an SHUTDOWN was last sent to, don't
5784 * do failure management.
5786 if (asoc->shutdown_last_sent_to)
5787 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5788 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5790 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5791 * the T2-shutdown timer.
5793 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5795 /* Restart the T2-shutdown timer. */
5796 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5797 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5798 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5799 return SCTP_DISPOSITION_CONSUME;
5802 return SCTP_DISPOSITION_NOMEM;
5806 * ADDIP Section 4.1 ASCONF CHunk Procedures
5807 * If the T4 RTO timer expires the endpoint should do B1 to B5
5809 sctp_disposition_t sctp_sf_t4_timer_expire(
5811 const struct sctp_endpoint *ep,
5812 const struct sctp_association *asoc,
5813 const sctp_subtype_t type,
5815 sctp_cmd_seq_t *commands)
5817 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5818 struct sctp_transport *transport = chunk->transport;
5820 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5822 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5823 * detection on the appropriate destination address as defined in
5824 * RFC2960 [5] section 8.1 and 8.2.
5827 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5828 SCTP_TRANSPORT(transport));
5830 /* Reconfig T4 timer and transport. */
5831 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5833 /* ADDIP 4.1 B2) Increment the association error counters and perform
5834 * endpoint failure detection on the association as defined in
5835 * RFC2960 [5] section 8.1 and 8.2.
5836 * association error counter is incremented in SCTP_CMD_STRIKE.
5838 if (asoc->overall_error_count >= asoc->max_retrans) {
5839 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5840 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5841 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5842 SCTP_ERROR(ETIMEDOUT));
5843 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5844 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5845 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5846 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5847 return SCTP_DISPOSITION_ABORT;
5850 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5851 * the ASCONF chunk was sent by doubling the RTO timer value.
5852 * This is done in SCTP_CMD_STRIKE.
5855 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5856 * choose an alternate destination address (please refer to RFC2960
5857 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5858 * chunk, it MUST be the same (including its serial number) as the last
5861 sctp_chunk_hold(asoc->addip_last_asconf);
5862 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5863 SCTP_CHUNK(asoc->addip_last_asconf));
5865 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5866 * destination is selected, then the RTO used will be that of the new
5867 * destination address.
5869 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5870 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5872 return SCTP_DISPOSITION_CONSUME;
5875 /* sctpimpguide-05 Section 2.12.2
5876 * The sender of the SHUTDOWN MAY also start an overall guard timer
5877 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5878 * At the expiration of this timer the sender SHOULD abort the association
5879 * by sending an ABORT chunk.
5881 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5882 const struct sctp_endpoint *ep,
5883 const struct sctp_association *asoc,
5884 const sctp_subtype_t type,
5886 sctp_cmd_seq_t *commands)
5888 struct sctp_chunk *reply = NULL;
5890 pr_debug("%s: timer T5 expired\n", __func__);
5892 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5894 reply = sctp_make_abort(asoc, NULL, 0);
5898 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5899 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5900 SCTP_ERROR(ETIMEDOUT));
5901 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5902 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5904 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5905 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5907 return SCTP_DISPOSITION_DELETE_TCB;
5909 return SCTP_DISPOSITION_NOMEM;
5912 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5913 * the association is automatically closed by starting the shutdown process.
5914 * The work that needs to be done is same as when SHUTDOWN is initiated by
5915 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5917 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5919 const struct sctp_endpoint *ep,
5920 const struct sctp_association *asoc,
5921 const sctp_subtype_t type,
5923 sctp_cmd_seq_t *commands)
5927 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5929 /* From 9.2 Shutdown of an Association
5930 * Upon receipt of the SHUTDOWN primitive from its upper
5931 * layer, the endpoint enters SHUTDOWN-PENDING state and
5932 * remains there until all outstanding data has been
5933 * acknowledged by its peer. The endpoint accepts no new data
5934 * from its upper layer, but retransmits data to the far end
5935 * if necessary to fill gaps.
5937 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5938 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5940 disposition = SCTP_DISPOSITION_CONSUME;
5941 if (sctp_outq_is_empty(&asoc->outqueue)) {
5942 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5948 /*****************************************************************************
5949 * These are sa state functions which could apply to all types of events.
5950 ****************************************************************************/
5953 * This table entry is not implemented.
5956 * (endpoint, asoc, chunk)
5958 * The return value is the disposition of the chunk.
5960 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5961 const struct sctp_endpoint *ep,
5962 const struct sctp_association *asoc,
5963 const sctp_subtype_t type,
5965 sctp_cmd_seq_t *commands)
5967 return SCTP_DISPOSITION_NOT_IMPL;
5971 * This table entry represents a bug.
5974 * (endpoint, asoc, chunk)
5976 * The return value is the disposition of the chunk.
5978 sctp_disposition_t sctp_sf_bug(struct net *net,
5979 const struct sctp_endpoint *ep,
5980 const struct sctp_association *asoc,
5981 const sctp_subtype_t type,
5983 sctp_cmd_seq_t *commands)
5985 return SCTP_DISPOSITION_BUG;
5989 * This table entry represents the firing of a timer in the wrong state.
5990 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5991 * when the association is in the wrong state. This event should
5992 * be ignored, so as to prevent any rearming of the timer.
5995 * (endpoint, asoc, chunk)
5997 * The return value is the disposition of the chunk.
5999 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
6000 const struct sctp_endpoint *ep,
6001 const struct sctp_association *asoc,
6002 const sctp_subtype_t type,
6004 sctp_cmd_seq_t *commands)
6006 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
6008 return SCTP_DISPOSITION_CONSUME;
6011 /********************************************************************
6012 * 2nd Level Abstractions
6013 ********************************************************************/
6015 /* Pull the SACK chunk based on the SACK header. */
6016 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
6018 struct sctp_sackhdr *sack;
6023 /* Protect ourselves from reading too far into
6024 * the skb from a bogus sender.
6026 sack = (struct sctp_sackhdr *) chunk->skb->data;
6028 num_blocks = ntohs(sack->num_gap_ack_blocks);
6029 num_dup_tsns = ntohs(sack->num_dup_tsns);
6030 len = sizeof(struct sctp_sackhdr);
6031 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
6032 if (len > chunk->skb->len)
6035 skb_pull(chunk->skb, len);
6040 /* Create an ABORT packet to be sent as a response, with the specified
6043 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
6044 const struct sctp_endpoint *ep,
6045 const struct sctp_association *asoc,
6046 struct sctp_chunk *chunk,
6047 const void *payload,
6050 struct sctp_packet *packet;
6051 struct sctp_chunk *abort;
6053 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6057 * The T bit will be set if the asoc is NULL.
6059 abort = sctp_make_abort(asoc, chunk, paylen);
6061 sctp_ootb_pkt_free(packet);
6065 /* Reflect vtag if T-Bit is set */
6066 if (sctp_test_T_bit(abort))
6067 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
6069 /* Add specified error causes, i.e., payload, to the
6072 sctp_addto_chunk(abort, paylen, payload);
6074 /* Set the skb to the belonging sock for accounting. */
6075 abort->skb->sk = ep->base.sk;
6077 sctp_packet_append_chunk(packet, abort);
6084 /* Allocate a packet for responding in the OOTB conditions. */
6085 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6086 const struct sctp_association *asoc,
6087 const struct sctp_chunk *chunk)
6089 struct sctp_packet *packet;
6090 struct sctp_transport *transport;
6095 /* Get the source and destination port from the inbound packet. */
6096 sport = ntohs(chunk->sctp_hdr->dest);
6097 dport = ntohs(chunk->sctp_hdr->source);
6099 /* The V-tag is going to be the same as the inbound packet if no
6100 * association exists, otherwise, use the peer's vtag.
6103 /* Special case the INIT-ACK as there is no peer's vtag
6106 switch (chunk->chunk_hdr->type) {
6107 case SCTP_CID_INIT_ACK:
6109 sctp_initack_chunk_t *initack;
6111 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6112 vtag = ntohl(initack->init_hdr.init_tag);
6116 vtag = asoc->peer.i.init_tag;
6120 /* Special case the INIT and stale COOKIE_ECHO as there is no
6123 switch (chunk->chunk_hdr->type) {
6126 struct sctp_init_chunk *init;
6128 init = (struct sctp_init_chunk *)chunk->chunk_hdr;
6129 vtag = ntohl(init->init_hdr.init_tag);
6133 vtag = ntohl(chunk->sctp_hdr->vtag);
6138 /* Make a transport for the bucket, Eliza... */
6139 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6143 /* Cache a route for the transport with the chunk's destination as
6144 * the source address.
6146 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6147 sctp_sk(net->sctp.ctl_sock));
6149 packet = &transport->packet;
6150 sctp_packet_init(packet, transport, sport, dport);
6151 sctp_packet_config(packet, vtag, 0);
6159 /* Free the packet allocated earlier for responding in the OOTB condition. */
6160 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6162 sctp_transport_free(packet->transport);
6165 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6166 static void sctp_send_stale_cookie_err(struct net *net,
6167 const struct sctp_endpoint *ep,
6168 const struct sctp_association *asoc,
6169 const struct sctp_chunk *chunk,
6170 sctp_cmd_seq_t *commands,
6171 struct sctp_chunk *err_chunk)
6173 struct sctp_packet *packet;
6176 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6178 struct sctp_signed_cookie *cookie;
6180 /* Override the OOTB vtag from the cookie. */
6181 cookie = chunk->subh.cookie_hdr;
6182 packet->vtag = cookie->c.peer_vtag;
6184 /* Set the skb to the belonging sock for accounting. */
6185 err_chunk->skb->sk = ep->base.sk;
6186 sctp_packet_append_chunk(packet, err_chunk);
6187 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6188 SCTP_PACKET(packet));
6189 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6191 sctp_chunk_free (err_chunk);
6196 /* Process a data chunk */
6197 static int sctp_eat_data(const struct sctp_association *asoc,
6198 struct sctp_chunk *chunk,
6199 sctp_cmd_seq_t *commands)
6201 struct sctp_datahdr *data_hdr;
6202 struct sctp_chunk *err;
6204 sctp_verb_t deliver;
6207 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6208 struct sock *sk = asoc->base.sk;
6209 struct net *net = sock_net(sk);
6214 data_hdr = (struct sctp_datahdr *)chunk->skb->data;
6215 chunk->subh.data_hdr = data_hdr;
6216 skb_pull(chunk->skb, sizeof(*data_hdr));
6218 tsn = ntohl(data_hdr->tsn);
6219 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6221 /* ASSERT: Now skb->data is really the user data. */
6223 /* Process ECN based congestion.
6225 * Since the chunk structure is reused for all chunks within
6226 * a packet, we use ecn_ce_done to track if we've already
6227 * done CE processing for this packet.
6229 * We need to do ECN processing even if we plan to discard the
6233 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6234 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6235 chunk->ecn_ce_done = 1;
6237 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6238 /* Do real work as sideffect. */
6239 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6244 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6246 /* The TSN is too high--silently discard the chunk and
6247 * count on it getting retransmitted later.
6250 chunk->asoc->stats.outofseqtsns++;
6251 return SCTP_IERROR_HIGH_TSN;
6252 } else if (tmp > 0) {
6253 /* This is a duplicate. Record it. */
6254 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6255 return SCTP_IERROR_DUP_TSN;
6258 /* This is a new TSN. */
6260 /* Discard if there is no room in the receive window.
6261 * Actually, allow a little bit of overflow (up to a MTU).
6263 datalen = ntohs(chunk->chunk_hdr->length);
6264 datalen -= sizeof(struct sctp_data_chunk);
6266 deliver = SCTP_CMD_CHUNK_ULP;
6268 /* Think about partial delivery. */
6269 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6271 /* Even if we don't accept this chunk there is
6274 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6277 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6278 * seems a bit troublesome in that frag_point varies based on
6279 * PMTU. In cases, such as loopback, this might be a rather
6282 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6283 (datalen > asoc->rwnd + asoc->frag_point))) {
6285 /* If this is the next TSN, consider reneging to make
6286 * room. Note: Playing nice with a confused sender. A
6287 * malicious sender can still eat up all our buffer
6288 * space and in the future we may want to detect and
6289 * do more drastic reneging.
6291 if (sctp_tsnmap_has_gap(map) &&
6292 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6293 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6294 deliver = SCTP_CMD_RENEGE;
6296 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6297 __func__, tsn, datalen, asoc->rwnd);
6299 return SCTP_IERROR_IGNORE_TSN;
6304 * Also try to renege to limit our memory usage in the event that
6305 * we are under memory pressure
6306 * If we can't renege, don't worry about it, the sk_rmem_schedule
6307 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6308 * memory usage too much
6310 if (*sk->sk_prot_creator->memory_pressure) {
6311 if (sctp_tsnmap_has_gap(map) &&
6312 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6313 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6315 deliver = SCTP_CMD_RENEGE;
6320 * Section 3.3.10.9 No User Data (9)
6324 * No User Data: This error cause is returned to the originator of a
6325 * DATA chunk if a received DATA chunk has no user data.
6327 if (unlikely(0 == datalen)) {
6328 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6330 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6333 /* We are going to ABORT, so we might as well stop
6334 * processing the rest of the chunks in the packet.
6336 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6337 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6338 SCTP_ERROR(ECONNABORTED));
6339 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6340 SCTP_PERR(SCTP_ERROR_NO_DATA));
6341 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6342 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6343 return SCTP_IERROR_NO_DATA;
6346 chunk->data_accepted = 1;
6348 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6349 * if we renege and the chunk arrives again.
6351 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6352 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6354 chunk->asoc->stats.iuodchunks++;
6356 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6358 chunk->asoc->stats.iodchunks++;
6362 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6364 * If an endpoint receive a DATA chunk with an invalid stream
6365 * identifier, it shall acknowledge the reception of the DATA chunk
6366 * following the normal procedure, immediately send an ERROR chunk
6367 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6368 * and discard the DATA chunk.
6370 sid = ntohs(data_hdr->stream);
6371 if (sid >= asoc->stream.incnt) {
6372 /* Mark tsn as received even though we drop it */
6373 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6375 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6377 sizeof(data_hdr->stream),
6380 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6382 return SCTP_IERROR_BAD_STREAM;
6385 /* Check to see if the SSN is possible for this TSN.
6386 * The biggest gap we can record is 4K wide. Since SSNs wrap
6387 * at an unsigned short, there is no way that an SSN can
6388 * wrap and for a valid TSN. We can simply check if the current
6389 * SSN is smaller then the next expected one. If it is, it wrapped
6392 ssn = ntohs(data_hdr->ssn);
6393 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->stream, in, sid)))
6394 return SCTP_IERROR_PROTO_VIOLATION;
6396 /* Send the data up to the user. Note: Schedule the
6397 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6398 * chunk needs the updated rwnd.
6400 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6402 return SCTP_IERROR_NO_ERROR;