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(
148 const struct sctp_association *asoc,
149 struct sctp_chunk *chunk);
151 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
152 const struct sctp_endpoint *ep,
153 const struct sctp_association *asoc,
154 const sctp_subtype_t type,
156 sctp_cmd_seq_t *commands);
158 /* Small helper function that checks if the chunk length
159 * is of the appropriate length. The 'required_length' argument
160 * is set to be the size of a specific chunk we are testing.
161 * Return Values: 1 = Valid length
166 sctp_chunk_length_valid(struct sctp_chunk *chunk,
167 __u16 required_length)
169 __u16 chunk_length = ntohs(chunk->chunk_hdr->length);
171 /* Previously already marked? */
172 if (unlikely(chunk->pdiscard))
174 if (unlikely(chunk_length < required_length))
180 /* Check for format error in an ABORT chunk */
181 static inline bool sctp_err_chunk_valid(struct sctp_chunk *chunk)
183 struct sctp_errhdr *err;
185 sctp_walk_errors(err, chunk->chunk_hdr);
187 return (void *)err == (void *)chunk->chunk_end;
190 /**********************************************************
191 * These are the state functions for handling chunk events.
192 **********************************************************/
195 * Process the final SHUTDOWN COMPLETE.
197 * Section: 4 (C) (diagram), 9.2
198 * Upon reception of the SHUTDOWN COMPLETE chunk the endpoint will verify
199 * that it is in SHUTDOWN-ACK-SENT state, if it is not the chunk should be
200 * discarded. If the endpoint is in the SHUTDOWN-ACK-SENT state the endpoint
201 * should stop the T2-shutdown timer and remove all knowledge of the
202 * association (and thus the association enters the CLOSED state).
204 * Verification Tag: 8.5.1(C), sctpimpguide 2.41.
205 * C) Rules for packet carrying SHUTDOWN COMPLETE:
207 * - The receiver of a SHUTDOWN COMPLETE shall accept the packet
208 * if the Verification Tag field of the packet matches its own tag and
209 * the T bit is not set
211 * it is set to its peer's tag and the T bit is set in the Chunk
213 * Otherwise, the receiver MUST silently discard the packet
214 * and take no further action. An endpoint MUST ignore the
215 * SHUTDOWN COMPLETE if it is not in the SHUTDOWN-ACK-SENT state.
218 * (endpoint, asoc, chunk)
221 * (asoc, reply_msg, msg_up, timers, counters)
223 * The return value is the disposition of the chunk.
225 sctp_disposition_t sctp_sf_do_4_C(struct net *net,
226 const struct sctp_endpoint *ep,
227 const struct sctp_association *asoc,
228 const sctp_subtype_t type,
230 sctp_cmd_seq_t *commands)
232 struct sctp_chunk *chunk = arg;
233 struct sctp_ulpevent *ev;
235 if (!sctp_vtag_verify_either(chunk, asoc))
236 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
238 /* RFC 2960 6.10 Bundling
240 * An endpoint MUST NOT bundle INIT, INIT ACK or
241 * SHUTDOWN COMPLETE with any other chunks.
243 if (!chunk->singleton)
244 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
246 /* Make sure that the SHUTDOWN_COMPLETE chunk has a valid length. */
247 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
248 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
251 /* RFC 2960 10.2 SCTP-to-ULP
253 * H) SHUTDOWN COMPLETE notification
255 * When SCTP completes the shutdown procedures (section 9.2) this
256 * notification is passed to the upper layer.
258 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
259 0, 0, 0, NULL, GFP_ATOMIC);
261 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
264 /* Upon reception of the SHUTDOWN COMPLETE chunk the endpoint
265 * will verify that it is in SHUTDOWN-ACK-SENT state, if it is
266 * not the chunk should be discarded. If the endpoint is in
267 * the SHUTDOWN-ACK-SENT state the endpoint should stop the
268 * T2-shutdown timer and remove all knowledge of the
269 * association (and thus the association enters the CLOSED
272 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
273 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
275 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
276 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
278 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
279 SCTP_STATE(SCTP_STATE_CLOSED));
281 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
282 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
284 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
286 return SCTP_DISPOSITION_DELETE_TCB;
290 * Respond to a normal INIT chunk.
291 * We are the side that is being asked for an association.
293 * Section: 5.1 Normal Establishment of an Association, B
294 * B) "Z" shall respond immediately with an INIT ACK chunk. The
295 * destination IP address of the INIT ACK MUST be set to the source
296 * IP address of the INIT to which this INIT ACK is responding. In
297 * the response, besides filling in other parameters, "Z" must set the
298 * Verification Tag field to Tag_A, and also provide its own
299 * Verification Tag (Tag_Z) in the Initiate Tag field.
301 * Verification Tag: Must be 0.
304 * (endpoint, asoc, chunk)
307 * (asoc, reply_msg, msg_up, timers, counters)
309 * The return value is the disposition of the chunk.
311 sctp_disposition_t sctp_sf_do_5_1B_init(struct net *net,
312 const struct sctp_endpoint *ep,
313 const struct sctp_association *asoc,
314 const sctp_subtype_t type,
316 sctp_cmd_seq_t *commands)
318 struct sctp_chunk *chunk = arg;
319 struct sctp_chunk *repl;
320 struct sctp_association *new_asoc;
321 struct sctp_chunk *err_chunk;
322 struct sctp_packet *packet;
323 sctp_unrecognized_param_t *unk_param;
327 * An endpoint MUST NOT bundle INIT, INIT ACK or
328 * SHUTDOWN COMPLETE with any other chunks.
331 * Furthermore, we require that the receiver of an INIT chunk MUST
332 * enforce these rules by silently discarding an arriving packet
333 * with an INIT chunk that is bundled with other chunks.
335 if (!chunk->singleton)
336 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
338 /* If the packet is an OOTB packet which is temporarily on the
339 * control endpoint, respond with an ABORT.
341 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
342 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
343 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
346 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
349 if (chunk->sctp_hdr->vtag != 0)
350 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
352 /* Make sure that the INIT chunk has a valid length.
353 * Normally, this would cause an ABORT with a Protocol Violation
354 * error, but since we don't have an association, we'll
355 * just discard the packet.
357 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
358 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
360 /* If the INIT is coming toward a closing socket, we'll send back
361 * and ABORT. Essentially, this catches the race of INIT being
362 * backloged to the socket at the same time as the user isses close().
363 * Since the socket and all its associations are going away, we
364 * can treat this OOTB
366 if (sctp_sstate(ep->base.sk, CLOSING))
367 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
369 /* Verify the INIT chunk before processing it. */
371 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
372 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
374 /* This chunk contains fatal error. It is to be discarded.
375 * Send an ABORT, with causes if there is any.
378 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
379 (__u8 *)(err_chunk->chunk_hdr) +
380 sizeof(sctp_chunkhdr_t),
381 ntohs(err_chunk->chunk_hdr->length) -
382 sizeof(sctp_chunkhdr_t));
384 sctp_chunk_free(err_chunk);
387 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
388 SCTP_PACKET(packet));
389 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
390 return SCTP_DISPOSITION_CONSUME;
392 return SCTP_DISPOSITION_NOMEM;
395 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
400 /* Grab the INIT header. */
401 chunk->subh.init_hdr = (sctp_inithdr_t *)chunk->skb->data;
403 /* Tag the variable length parameters. */
404 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
406 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
410 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
411 sctp_scope(sctp_source(chunk)),
415 /* The call, sctp_process_init(), can fail on memory allocation. */
416 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
417 (sctp_init_chunk_t *)chunk->chunk_hdr,
421 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
423 /* If there are errors need to be reported for unknown parameters,
424 * make sure to reserve enough room in the INIT ACK for them.
428 len = ntohs(err_chunk->chunk_hdr->length) -
429 sizeof(sctp_chunkhdr_t);
431 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
435 /* If there are errors need to be reported for unknown parameters,
436 * include them in the outgoing INIT ACK as "Unrecognized parameter"
440 /* Get the "Unrecognized parameter" parameter(s) out of the
441 * ERROR chunk generated by sctp_verify_init(). Since the
442 * error cause code for "unknown parameter" and the
443 * "Unrecognized parameter" type is the same, we can
444 * construct the parameters in INIT ACK by copying the
447 unk_param = (sctp_unrecognized_param_t *)
448 ((__u8 *)(err_chunk->chunk_hdr) +
449 sizeof(sctp_chunkhdr_t));
450 /* Replace the cause code with the "Unrecognized parameter"
453 sctp_addto_chunk(repl, len, unk_param);
454 sctp_chunk_free(err_chunk);
457 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
459 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
462 * Note: After sending out INIT ACK with the State Cookie parameter,
463 * "Z" MUST NOT allocate any resources, nor keep any states for the
464 * new association. Otherwise, "Z" will be vulnerable to resource
467 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
469 return SCTP_DISPOSITION_DELETE_TCB;
472 sctp_association_free(new_asoc);
475 sctp_chunk_free(err_chunk);
476 return SCTP_DISPOSITION_NOMEM;
480 * Respond to a normal INIT ACK chunk.
481 * We are the side that is initiating the association.
483 * Section: 5.1 Normal Establishment of an Association, C
484 * C) Upon reception of the INIT ACK from "Z", "A" shall stop the T1-init
485 * timer and leave COOKIE-WAIT state. "A" shall then send the State
486 * Cookie received in the INIT ACK chunk in a COOKIE ECHO chunk, start
487 * the T1-cookie timer, and enter the COOKIE-ECHOED state.
489 * Note: The COOKIE ECHO chunk can be bundled with any pending outbound
490 * DATA chunks, but it MUST be the first chunk in the packet and
491 * until the COOKIE ACK is returned the sender MUST NOT send any
492 * other packets to the peer.
494 * Verification Tag: 3.3.3
495 * If the value of the Initiate Tag in a received INIT ACK chunk is
496 * found to be 0, the receiver MUST treat it as an error and close the
497 * association by transmitting an ABORT.
500 * (endpoint, asoc, chunk)
503 * (asoc, reply_msg, msg_up, timers, counters)
505 * The return value is the disposition of the chunk.
507 sctp_disposition_t sctp_sf_do_5_1C_ack(struct net *net,
508 const struct sctp_endpoint *ep,
509 const struct sctp_association *asoc,
510 const sctp_subtype_t type,
512 sctp_cmd_seq_t *commands)
514 struct sctp_chunk *chunk = arg;
515 sctp_init_chunk_t *initchunk;
516 struct sctp_chunk *err_chunk;
517 struct sctp_packet *packet;
519 if (!sctp_vtag_verify(chunk, asoc))
520 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
523 * An endpoint MUST NOT bundle INIT, INIT ACK or
524 * SHUTDOWN COMPLETE with any other chunks.
526 if (!chunk->singleton)
527 return sctp_sf_violation_chunk(net, ep, asoc, type, arg, commands);
529 /* Make sure that the INIT-ACK chunk has a valid length */
530 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_initack_chunk_t)))
531 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
533 /* Grab the INIT header. */
534 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
536 /* Verify the INIT chunk before processing it. */
538 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
539 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
542 sctp_error_t error = SCTP_ERROR_NO_RESOURCE;
544 /* This chunk contains fatal error. It is to be discarded.
545 * Send an ABORT, with causes. If there are no causes,
546 * then there wasn't enough memory. Just terminate
550 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
551 (__u8 *)(err_chunk->chunk_hdr) +
552 sizeof(sctp_chunkhdr_t),
553 ntohs(err_chunk->chunk_hdr->length) -
554 sizeof(sctp_chunkhdr_t));
556 sctp_chunk_free(err_chunk);
559 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
560 SCTP_PACKET(packet));
561 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
562 error = SCTP_ERROR_INV_PARAM;
566 /* SCTP-AUTH, Section 6.3:
567 * It should be noted that if the receiver wants to tear
568 * down an association in an authenticated way only, the
569 * handling of malformed packets should not result in
570 * tearing down the association.
572 * This means that if we only want to abort associations
573 * in an authenticated way (i.e AUTH+ABORT), then we
574 * can't destroy this association just because the packet
577 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
578 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
580 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
581 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED,
582 asoc, chunk->transport);
585 /* Tag the variable length parameters. Note that we never
586 * convert the parameters in an INIT chunk.
588 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
590 initchunk = (sctp_init_chunk_t *) chunk->chunk_hdr;
592 sctp_add_cmd_sf(commands, SCTP_CMD_PEER_INIT,
593 SCTP_PEER_INIT(initchunk));
595 /* Reset init error count upon receipt of INIT-ACK. */
596 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
598 /* 5.1 C) "A" shall stop the T1-init timer and leave
599 * COOKIE-WAIT state. "A" shall then ... start the T1-cookie
600 * timer, and enter the COOKIE-ECHOED state.
602 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
603 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
604 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
605 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
606 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
607 SCTP_STATE(SCTP_STATE_COOKIE_ECHOED));
609 /* SCTP-AUTH: genereate the assocition shared keys so that
610 * we can potentially signe the COOKIE-ECHO.
612 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_SHKEY, SCTP_NULL());
614 /* 5.1 C) "A" shall then send the State Cookie received in the
615 * INIT ACK chunk in a COOKIE ECHO chunk, ...
617 /* If there is any errors to report, send the ERROR chunk generated
618 * for unknown parameters as well.
620 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_COOKIE_ECHO,
621 SCTP_CHUNK(err_chunk));
623 return SCTP_DISPOSITION_CONSUME;
626 static bool sctp_auth_chunk_verify(struct net *net, struct sctp_chunk *chunk,
627 const struct sctp_association *asoc)
629 struct sctp_chunk auth;
631 if (!chunk->auth_chunk)
634 /* SCTP-AUTH: auth_chunk pointer is only set when the cookie-echo
635 * is supposed to be authenticated and we have to do delayed
636 * authentication. We've just recreated the association using
637 * the information in the cookie and now it's much easier to
638 * do the authentication.
641 /* Make sure that we and the peer are AUTH capable */
642 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
645 /* set-up our fake chunk so that we can process it */
646 auth.skb = chunk->auth_chunk;
647 auth.asoc = chunk->asoc;
648 auth.sctp_hdr = chunk->sctp_hdr;
649 auth.chunk_hdr = (struct sctp_chunkhdr *)
650 skb_push(chunk->auth_chunk,
651 sizeof(struct sctp_chunkhdr));
652 skb_pull(chunk->auth_chunk, sizeof(struct sctp_chunkhdr));
653 auth.transport = chunk->transport;
655 return sctp_sf_authenticate(asoc, &auth) == SCTP_IERROR_NO_ERROR;
659 * Respond to a normal COOKIE ECHO chunk.
660 * We are the side that is being asked for an association.
662 * Section: 5.1 Normal Establishment of an Association, D
663 * D) Upon reception of the COOKIE ECHO chunk, Endpoint "Z" will reply
664 * with a COOKIE ACK chunk after building a TCB and moving to
665 * the ESTABLISHED state. A COOKIE ACK chunk may be bundled with
666 * any pending DATA chunks (and/or SACK chunks), but the COOKIE ACK
667 * chunk MUST be the first chunk in the packet.
669 * IMPLEMENTATION NOTE: An implementation may choose to send the
670 * Communication Up notification to the SCTP user upon reception
671 * of a valid COOKIE ECHO chunk.
673 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
674 * D) Rules for packet carrying a COOKIE ECHO
676 * - When sending a COOKIE ECHO, the endpoint MUST use the value of the
677 * Initial Tag received in the INIT ACK.
679 * - The receiver of a COOKIE ECHO follows the procedures in Section 5.
682 * (endpoint, asoc, chunk)
685 * (asoc, reply_msg, msg_up, timers, counters)
687 * The return value is the disposition of the chunk.
689 sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
690 const struct sctp_endpoint *ep,
691 const struct sctp_association *asoc,
692 const sctp_subtype_t type, void *arg,
693 sctp_cmd_seq_t *commands)
695 struct sctp_chunk *chunk = arg;
696 struct sctp_association *new_asoc;
697 sctp_init_chunk_t *peer_init;
698 struct sctp_chunk *repl;
699 struct sctp_ulpevent *ev, *ai_ev = NULL;
701 struct sctp_chunk *err_chk_p;
704 /* If the packet is an OOTB packet which is temporarily on the
705 * control endpoint, respond with an ABORT.
707 if (ep == sctp_sk(net->sctp.ctl_sock)->ep) {
708 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
709 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
712 /* Make sure that the COOKIE_ECHO chunk has a valid length.
713 * In this case, we check that we have enough for at least a
714 * chunk header. More detailed verification is done
715 * in sctp_unpack_cookie().
717 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
718 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
720 /* If the endpoint is not listening or if the number of associations
721 * on the TCP-style socket exceed the max backlog, respond with an
725 if (!sctp_sstate(sk, LISTENING) ||
726 (sctp_style(sk, TCP) && sk_acceptq_is_full(sk)))
727 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
729 /* "Decode" the chunk. We have no optional parameters so we
732 chunk->subh.cookie_hdr =
733 (struct sctp_signed_cookie *)chunk->skb->data;
734 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
735 sizeof(sctp_chunkhdr_t)))
738 /* 5.1 D) Upon reception of the COOKIE ECHO chunk, Endpoint
739 * "Z" will reply with a COOKIE ACK chunk after building a TCB
740 * and moving to the ESTABLISHED state.
742 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
746 * If the re-build failed, what is the proper error path
749 * [We should abort the association. --piggy]
752 /* FIXME: Several errors are possible. A bad cookie should
753 * be silently discarded, but think about logging it too.
756 case -SCTP_IERROR_NOMEM:
759 case -SCTP_IERROR_STALE_COOKIE:
760 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
762 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
764 case -SCTP_IERROR_BAD_SIG:
766 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
771 /* Delay state machine commands until later.
773 * Re-build the bind address for the association is done in
774 * the sctp_unpack_cookie() already.
776 /* This is a brand-new association, so these are not yet side
777 * effects--it is safe to run them here.
779 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
781 if (!sctp_process_init(new_asoc, chunk,
782 &chunk->subh.cookie_hdr->c.peer_addr,
783 peer_init, GFP_ATOMIC))
786 /* SCTP-AUTH: Now that we've populate required fields in
787 * sctp_process_init, set up the assocaition shared keys as
788 * necessary so that we can potentially authenticate the ACK
790 error = sctp_auth_asoc_init_active_key(new_asoc, GFP_ATOMIC);
794 if (!sctp_auth_chunk_verify(net, chunk, new_asoc)) {
795 sctp_association_free(new_asoc);
796 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
799 repl = sctp_make_cookie_ack(new_asoc, chunk);
803 /* RFC 2960 5.1 Normal Establishment of an Association
805 * D) IMPLEMENTATION NOTE: An implementation may choose to
806 * send the Communication Up notification to the SCTP user
807 * upon reception of a valid COOKIE ECHO chunk.
809 ev = sctp_ulpevent_make_assoc_change(new_asoc, 0, SCTP_COMM_UP, 0,
810 new_asoc->c.sinit_num_ostreams,
811 new_asoc->c.sinit_max_instreams,
816 /* Sockets API Draft Section 5.3.1.6
817 * When a peer sends a Adaptation Layer Indication parameter , SCTP
818 * delivers this notification to inform the application that of the
819 * peers requested adaptation layer.
821 if (new_asoc->peer.adaptation_ind) {
822 ai_ev = sctp_ulpevent_make_adaptation_indication(new_asoc,
828 /* Add all the state machine commands now since we've created
829 * everything. This way we don't introduce memory corruptions
830 * during side-effect processing and correclty count established
833 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
834 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
835 SCTP_STATE(SCTP_STATE_ESTABLISHED));
836 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
837 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
838 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
840 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
841 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
842 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
844 /* This will send the COOKIE ACK */
845 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
847 /* Queue the ASSOC_CHANGE event */
848 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
850 /* Send up the Adaptation Layer Indication event */
852 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
853 SCTP_ULPEVENT(ai_ev));
855 return SCTP_DISPOSITION_CONSUME;
858 sctp_ulpevent_free(ev);
860 sctp_chunk_free(repl);
862 sctp_association_free(new_asoc);
864 return SCTP_DISPOSITION_NOMEM;
868 * Respond to a normal COOKIE ACK chunk.
869 * We are the side that is asking for an association.
871 * RFC 2960 5.1 Normal Establishment of an Association
873 * E) Upon reception of the COOKIE ACK, endpoint "A" will move from the
874 * COOKIE-ECHOED state to the ESTABLISHED state, stopping the T1-cookie
875 * timer. It may also notify its ULP about the successful
876 * establishment of the association with a Communication Up
877 * notification (see Section 10).
881 * (endpoint, asoc, chunk)
884 * (asoc, reply_msg, msg_up, timers, counters)
886 * The return value is the disposition of the chunk.
888 sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
889 const struct sctp_endpoint *ep,
890 const struct sctp_association *asoc,
891 const sctp_subtype_t type, void *arg,
892 sctp_cmd_seq_t *commands)
894 struct sctp_chunk *chunk = arg;
895 struct sctp_ulpevent *ev;
897 if (!sctp_vtag_verify(chunk, asoc))
898 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
900 /* Verify that the chunk length for the COOKIE-ACK is OK.
901 * If we don't do this, any bundled chunks may be junked.
903 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
904 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
907 /* Reset init error count upon receipt of COOKIE-ACK,
908 * to avoid problems with the managemement of this
909 * counter in stale cookie situations when a transition back
910 * from the COOKIE-ECHOED state to the COOKIE-WAIT
911 * state is performed.
913 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_RESET, SCTP_NULL());
915 /* RFC 2960 5.1 Normal Establishment of an Association
917 * E) Upon reception of the COOKIE ACK, endpoint "A" will move
918 * from the COOKIE-ECHOED state to the ESTABLISHED state,
919 * stopping the T1-cookie timer.
921 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
922 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
923 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
924 SCTP_STATE(SCTP_STATE_ESTABLISHED));
925 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
926 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
927 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
928 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
929 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
930 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
932 /* It may also notify its ULP about the successful
933 * establishment of the association with a Communication Up
934 * notification (see Section 10).
936 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_COMM_UP,
937 0, asoc->c.sinit_num_ostreams,
938 asoc->c.sinit_max_instreams,
944 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
946 /* Sockets API Draft Section 5.3.1.6
947 * When a peer sends a Adaptation Layer Indication parameter , SCTP
948 * delivers this notification to inform the application that of the
949 * peers requested adaptation layer.
951 if (asoc->peer.adaptation_ind) {
952 ev = sctp_ulpevent_make_adaptation_indication(asoc, GFP_ATOMIC);
956 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
960 return SCTP_DISPOSITION_CONSUME;
962 return SCTP_DISPOSITION_NOMEM;
965 /* Generate and sendout a heartbeat packet. */
966 static sctp_disposition_t sctp_sf_heartbeat(const struct sctp_endpoint *ep,
967 const struct sctp_association *asoc,
968 const sctp_subtype_t type,
970 sctp_cmd_seq_t *commands)
972 struct sctp_transport *transport = (struct sctp_transport *) arg;
973 struct sctp_chunk *reply;
975 /* Send a heartbeat to our peer. */
976 reply = sctp_make_heartbeat(asoc, transport);
978 return SCTP_DISPOSITION_NOMEM;
980 /* Set rto_pending indicating that an RTT measurement
981 * is started with this heartbeat chunk.
983 sctp_add_cmd_sf(commands, SCTP_CMD_RTO_PENDING,
984 SCTP_TRANSPORT(transport));
986 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
987 return SCTP_DISPOSITION_CONSUME;
990 /* Generate a HEARTBEAT packet on the given transport. */
991 sctp_disposition_t sctp_sf_sendbeat_8_3(struct net *net,
992 const struct sctp_endpoint *ep,
993 const struct sctp_association *asoc,
994 const sctp_subtype_t type,
996 sctp_cmd_seq_t *commands)
998 struct sctp_transport *transport = (struct sctp_transport *) arg;
1000 if (asoc->overall_error_count >= asoc->max_retrans) {
1001 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
1002 SCTP_ERROR(ETIMEDOUT));
1003 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
1004 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
1005 SCTP_PERR(SCTP_ERROR_NO_ERROR));
1006 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
1007 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
1008 return SCTP_DISPOSITION_DELETE_TCB;
1012 * The Sender-specific Heartbeat Info field should normally include
1013 * information about the sender's current time when this HEARTBEAT
1014 * chunk is sent and the destination transport address to which this
1015 * HEARTBEAT is sent (see Section 8.3).
1018 if (transport->param_flags & SPP_HB_ENABLE) {
1019 if (SCTP_DISPOSITION_NOMEM ==
1020 sctp_sf_heartbeat(ep, asoc, type, arg,
1022 return SCTP_DISPOSITION_NOMEM;
1024 /* Set transport error counter and association error counter
1025 * when sending heartbeat.
1027 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
1028 SCTP_TRANSPORT(transport));
1030 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_IDLE,
1031 SCTP_TRANSPORT(transport));
1032 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMER_UPDATE,
1033 SCTP_TRANSPORT(transport));
1035 return SCTP_DISPOSITION_CONSUME;
1039 * Process an heartbeat request.
1041 * Section: 8.3 Path Heartbeat
1042 * The receiver of the HEARTBEAT should immediately respond with a
1043 * HEARTBEAT ACK that contains the Heartbeat Information field copied
1044 * from the received HEARTBEAT chunk.
1046 * Verification Tag: 8.5 Verification Tag [Normal verification]
1047 * When receiving an SCTP packet, the endpoint MUST ensure that the
1048 * value in the Verification Tag field of the received SCTP packet
1049 * matches its own Tag. If the received Verification Tag value does not
1050 * match the receiver's own tag value, the receiver shall silently
1051 * discard the packet and shall not process it any further except for
1052 * those cases listed in Section 8.5.1 below.
1055 * (endpoint, asoc, chunk)
1058 * (asoc, reply_msg, msg_up, timers, counters)
1060 * The return value is the disposition of the chunk.
1062 sctp_disposition_t sctp_sf_beat_8_3(struct net *net,
1063 const struct sctp_endpoint *ep,
1064 const struct sctp_association *asoc,
1065 const sctp_subtype_t type,
1067 sctp_cmd_seq_t *commands)
1069 sctp_paramhdr_t *param_hdr;
1070 struct sctp_chunk *chunk = arg;
1071 struct sctp_chunk *reply;
1074 if (!sctp_vtag_verify(chunk, asoc))
1075 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1077 /* Make sure that the HEARTBEAT chunk has a valid length. */
1078 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_heartbeat_chunk_t)))
1079 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1082 /* 8.3 The receiver of the HEARTBEAT should immediately
1083 * respond with a HEARTBEAT ACK that contains the Heartbeat
1084 * Information field copied from the received HEARTBEAT chunk.
1086 chunk->subh.hb_hdr = (sctp_heartbeathdr_t *) chunk->skb->data;
1087 param_hdr = (sctp_paramhdr_t *) chunk->subh.hb_hdr;
1088 paylen = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_chunkhdr_t);
1090 if (ntohs(param_hdr->length) > paylen)
1091 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
1092 param_hdr, commands);
1094 if (!pskb_pull(chunk->skb, paylen))
1097 reply = sctp_make_heartbeat_ack(asoc, chunk, param_hdr, paylen);
1101 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
1102 return SCTP_DISPOSITION_CONSUME;
1105 return SCTP_DISPOSITION_NOMEM;
1109 * Process the returning HEARTBEAT ACK.
1111 * Section: 8.3 Path Heartbeat
1112 * Upon the receipt of the HEARTBEAT ACK, the sender of the HEARTBEAT
1113 * should clear the error counter of the destination transport
1114 * address to which the HEARTBEAT was sent, and mark the destination
1115 * transport address as active if it is not so marked. The endpoint may
1116 * optionally report to the upper layer when an inactive destination
1117 * address is marked as active due to the reception of the latest
1118 * HEARTBEAT ACK. The receiver of the HEARTBEAT ACK must also
1119 * clear the association overall error count as well (as defined
1122 * The receiver of the HEARTBEAT ACK should also perform an RTT
1123 * measurement for that destination transport address using the time
1124 * value carried in the HEARTBEAT ACK chunk.
1126 * Verification Tag: 8.5 Verification Tag [Normal verification]
1129 * (endpoint, asoc, chunk)
1132 * (asoc, reply_msg, msg_up, timers, counters)
1134 * The return value is the disposition of the chunk.
1136 sctp_disposition_t sctp_sf_backbeat_8_3(struct net *net,
1137 const struct sctp_endpoint *ep,
1138 const struct sctp_association *asoc,
1139 const sctp_subtype_t type,
1141 sctp_cmd_seq_t *commands)
1143 struct sctp_chunk *chunk = arg;
1144 union sctp_addr from_addr;
1145 struct sctp_transport *link;
1146 sctp_sender_hb_info_t *hbinfo;
1147 unsigned long max_interval;
1149 if (!sctp_vtag_verify(chunk, asoc))
1150 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1152 /* Make sure that the HEARTBEAT-ACK chunk has a valid length. */
1153 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t) +
1154 sizeof(sctp_sender_hb_info_t)))
1155 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1158 hbinfo = (sctp_sender_hb_info_t *) chunk->skb->data;
1159 /* Make sure that the length of the parameter is what we expect */
1160 if (ntohs(hbinfo->param_hdr.length) !=
1161 sizeof(sctp_sender_hb_info_t)) {
1162 return SCTP_DISPOSITION_DISCARD;
1165 from_addr = hbinfo->daddr;
1166 link = sctp_assoc_lookup_paddr(asoc, &from_addr);
1168 /* This should never happen, but lets log it if so. */
1169 if (unlikely(!link)) {
1170 if (from_addr.sa.sa_family == AF_INET6) {
1171 net_warn_ratelimited("%s association %p could not find address %pI6\n",
1174 &from_addr.v6.sin6_addr);
1176 net_warn_ratelimited("%s association %p could not find address %pI4\n",
1179 &from_addr.v4.sin_addr.s_addr);
1181 return SCTP_DISPOSITION_DISCARD;
1184 /* Validate the 64-bit random nonce. */
1185 if (hbinfo->hb_nonce != link->hb_nonce)
1186 return SCTP_DISPOSITION_DISCARD;
1188 max_interval = link->hbinterval + link->rto;
1190 /* Check if the timestamp looks valid. */
1191 if (time_after(hbinfo->sent_at, jiffies) ||
1192 time_after(jiffies, hbinfo->sent_at + max_interval)) {
1193 pr_debug("%s: HEARTBEAT ACK with invalid timestamp received "
1194 "for transport:%p\n", __func__, link);
1196 return SCTP_DISPOSITION_DISCARD;
1199 /* 8.3 Upon the receipt of the HEARTBEAT ACK, the sender of
1200 * the HEARTBEAT should clear the error counter of the
1201 * destination transport address to which the HEARTBEAT was
1202 * sent and mark the destination transport address as active if
1203 * it is not so marked.
1205 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_ON, SCTP_TRANSPORT(link));
1207 return SCTP_DISPOSITION_CONSUME;
1210 /* Helper function to send out an abort for the restart
1213 static int sctp_sf_send_restart_abort(struct net *net, union sctp_addr *ssa,
1214 struct sctp_chunk *init,
1215 sctp_cmd_seq_t *commands)
1218 struct sctp_packet *pkt;
1219 union sctp_addr_param *addrparm;
1220 struct sctp_errhdr *errhdr;
1221 struct sctp_endpoint *ep;
1222 char buffer[sizeof(struct sctp_errhdr)+sizeof(union sctp_addr_param)];
1223 struct sctp_af *af = sctp_get_af_specific(ssa->v4.sin_family);
1225 /* Build the error on the stack. We are way to malloc crazy
1226 * throughout the code today.
1228 errhdr = (struct sctp_errhdr *)buffer;
1229 addrparm = (union sctp_addr_param *)errhdr->variable;
1231 /* Copy into a parm format. */
1232 len = af->to_addr_param(ssa, addrparm);
1233 len += sizeof(sctp_errhdr_t);
1235 errhdr->cause = SCTP_ERROR_RESTART;
1236 errhdr->length = htons(len);
1238 /* Assign to the control socket. */
1239 ep = sctp_sk(net->sctp.ctl_sock)->ep;
1241 /* Association is NULL since this may be a restart attack and we
1242 * want to send back the attacker's vtag.
1244 pkt = sctp_abort_pkt_new(net, ep, NULL, init, errhdr, len);
1248 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT, SCTP_PACKET(pkt));
1250 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1252 /* Discard the rest of the inbound packet. */
1253 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
1256 /* Even if there is no memory, treat as a failure so
1257 * the packet will get dropped.
1262 static bool list_has_sctp_addr(const struct list_head *list,
1263 union sctp_addr *ipaddr)
1265 struct sctp_transport *addr;
1267 list_for_each_entry(addr, list, transports) {
1268 if (sctp_cmp_addr_exact(ipaddr, &addr->ipaddr))
1274 /* A restart is occurring, check to make sure no new addresses
1275 * are being added as we may be under a takeover attack.
1277 static int sctp_sf_check_restart_addrs(const struct sctp_association *new_asoc,
1278 const struct sctp_association *asoc,
1279 struct sctp_chunk *init,
1280 sctp_cmd_seq_t *commands)
1282 struct net *net = sock_net(new_asoc->base.sk);
1283 struct sctp_transport *new_addr;
1286 /* Implementor's Guide - Section 5.2.2
1288 * Before responding the endpoint MUST check to see if the
1289 * unexpected INIT adds new addresses to the association. If new
1290 * addresses are added to the association, the endpoint MUST respond
1294 /* Search through all current addresses and make sure
1295 * we aren't adding any new ones.
1297 list_for_each_entry(new_addr, &new_asoc->peer.transport_addr_list,
1299 if (!list_has_sctp_addr(&asoc->peer.transport_addr_list,
1300 &new_addr->ipaddr)) {
1301 sctp_sf_send_restart_abort(net, &new_addr->ipaddr, init,
1308 /* Return success if all addresses were found. */
1312 /* Populate the verification/tie tags based on overlapping INIT
1315 * Note: Do not use in CLOSED or SHUTDOWN-ACK-SENT state.
1317 static void sctp_tietags_populate(struct sctp_association *new_asoc,
1318 const struct sctp_association *asoc)
1320 switch (asoc->state) {
1322 /* 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State */
1324 case SCTP_STATE_COOKIE_WAIT:
1325 new_asoc->c.my_vtag = asoc->c.my_vtag;
1326 new_asoc->c.my_ttag = asoc->c.my_vtag;
1327 new_asoc->c.peer_ttag = 0;
1330 case SCTP_STATE_COOKIE_ECHOED:
1331 new_asoc->c.my_vtag = asoc->c.my_vtag;
1332 new_asoc->c.my_ttag = asoc->c.my_vtag;
1333 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1336 /* 5.2.2 Unexpected INIT in States Other than CLOSED, COOKIE-ECHOED,
1337 * COOKIE-WAIT and SHUTDOWN-ACK-SENT
1340 new_asoc->c.my_ttag = asoc->c.my_vtag;
1341 new_asoc->c.peer_ttag = asoc->c.peer_vtag;
1345 /* Other parameters for the endpoint SHOULD be copied from the
1346 * existing parameters of the association (e.g. number of
1347 * outbound streams) into the INIT ACK and cookie.
1349 new_asoc->rwnd = asoc->rwnd;
1350 new_asoc->c.sinit_num_ostreams = asoc->c.sinit_num_ostreams;
1351 new_asoc->c.sinit_max_instreams = asoc->c.sinit_max_instreams;
1352 new_asoc->c.initial_tsn = asoc->c.initial_tsn;
1356 * Compare vtag/tietag values to determine unexpected COOKIE-ECHO
1359 * RFC 2960 5.2.4 Handle a COOKIE ECHO when a TCB exists.
1361 * Returns value representing action to be taken. These action values
1362 * correspond to Action/Description values in RFC 2960, Table 2.
1364 static char sctp_tietags_compare(struct sctp_association *new_asoc,
1365 const struct sctp_association *asoc)
1367 /* In this case, the peer may have restarted. */
1368 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1369 (asoc->c.peer_vtag != new_asoc->c.peer_vtag) &&
1370 (asoc->c.my_vtag == new_asoc->c.my_ttag) &&
1371 (asoc->c.peer_vtag == new_asoc->c.peer_ttag))
1374 /* Collision case B. */
1375 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1376 ((asoc->c.peer_vtag != new_asoc->c.peer_vtag) ||
1377 (0 == asoc->c.peer_vtag))) {
1381 /* Collision case D. */
1382 if ((asoc->c.my_vtag == new_asoc->c.my_vtag) &&
1383 (asoc->c.peer_vtag == new_asoc->c.peer_vtag))
1386 /* Collision case C. */
1387 if ((asoc->c.my_vtag != new_asoc->c.my_vtag) &&
1388 (asoc->c.peer_vtag == new_asoc->c.peer_vtag) &&
1389 (0 == new_asoc->c.my_ttag) &&
1390 (0 == new_asoc->c.peer_ttag))
1393 /* No match to any of the special cases; discard this packet. */
1397 /* Common helper routine for both duplicate and simulataneous INIT
1400 static sctp_disposition_t sctp_sf_do_unexpected_init(
1402 const struct sctp_endpoint *ep,
1403 const struct sctp_association *asoc,
1404 const sctp_subtype_t type,
1405 void *arg, sctp_cmd_seq_t *commands)
1407 sctp_disposition_t retval;
1408 struct sctp_chunk *chunk = arg;
1409 struct sctp_chunk *repl;
1410 struct sctp_association *new_asoc;
1411 struct sctp_chunk *err_chunk;
1412 struct sctp_packet *packet;
1413 sctp_unrecognized_param_t *unk_param;
1417 * An endpoint MUST NOT bundle INIT, INIT ACK or
1418 * SHUTDOWN COMPLETE with any other chunks.
1421 * Furthermore, we require that the receiver of an INIT chunk MUST
1422 * enforce these rules by silently discarding an arriving packet
1423 * with an INIT chunk that is bundled with other chunks.
1425 if (!chunk->singleton)
1426 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
1428 /* 3.1 A packet containing an INIT chunk MUST have a zero Verification
1431 if (chunk->sctp_hdr->vtag != 0)
1432 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
1434 /* Make sure that the INIT chunk has a valid length.
1435 * In this case, we generate a protocol violation since we have
1436 * an association established.
1438 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_init_chunk_t)))
1439 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
1441 /* Grab the INIT header. */
1442 chunk->subh.init_hdr = (sctp_inithdr_t *) chunk->skb->data;
1444 /* Tag the variable length parameters. */
1445 chunk->param_hdr.v = skb_pull(chunk->skb, sizeof(sctp_inithdr_t));
1447 /* Verify the INIT chunk before processing it. */
1449 if (!sctp_verify_init(net, ep, asoc, chunk->chunk_hdr->type,
1450 (sctp_init_chunk_t *)chunk->chunk_hdr, chunk,
1452 /* This chunk contains fatal error. It is to be discarded.
1453 * Send an ABORT, with causes if there is any.
1456 packet = sctp_abort_pkt_new(net, ep, asoc, arg,
1457 (__u8 *)(err_chunk->chunk_hdr) +
1458 sizeof(sctp_chunkhdr_t),
1459 ntohs(err_chunk->chunk_hdr->length) -
1460 sizeof(sctp_chunkhdr_t));
1463 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
1464 SCTP_PACKET(packet));
1465 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
1466 retval = SCTP_DISPOSITION_CONSUME;
1468 retval = SCTP_DISPOSITION_NOMEM;
1472 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg,
1478 * Other parameters for the endpoint SHOULD be copied from the
1479 * existing parameters of the association (e.g. number of
1480 * outbound streams) into the INIT ACK and cookie.
1481 * FIXME: We are copying parameters from the endpoint not the
1484 new_asoc = sctp_make_temp_asoc(ep, chunk, GFP_ATOMIC);
1488 if (sctp_assoc_set_bind_addr_from_ep(new_asoc,
1489 sctp_scope(sctp_source(chunk)), GFP_ATOMIC) < 0)
1492 /* In the outbound INIT ACK the endpoint MUST copy its current
1493 * Verification Tag and Peers Verification tag into a reserved
1494 * place (local tie-tag and per tie-tag) within the state cookie.
1496 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk),
1497 (sctp_init_chunk_t *)chunk->chunk_hdr,
1501 /* Make sure no new addresses are being added during the
1502 * restart. Do not do this check for COOKIE-WAIT state,
1503 * since there are no peer addresses to check against.
1504 * Upon return an ABORT will have been sent if needed.
1506 if (!sctp_state(asoc, COOKIE_WAIT)) {
1507 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk,
1509 retval = SCTP_DISPOSITION_CONSUME;
1514 sctp_tietags_populate(new_asoc, asoc);
1516 /* B) "Z" shall respond immediately with an INIT ACK chunk. */
1518 /* If there are errors need to be reported for unknown parameters,
1519 * make sure to reserve enough room in the INIT ACK for them.
1523 len = ntohs(err_chunk->chunk_hdr->length) -
1524 sizeof(sctp_chunkhdr_t);
1527 repl = sctp_make_init_ack(new_asoc, chunk, GFP_ATOMIC, len);
1531 /* If there are errors need to be reported for unknown parameters,
1532 * include them in the outgoing INIT ACK as "Unrecognized parameter"
1536 /* Get the "Unrecognized parameter" parameter(s) out of the
1537 * ERROR chunk generated by sctp_verify_init(). Since the
1538 * error cause code for "unknown parameter" and the
1539 * "Unrecognized parameter" type is the same, we can
1540 * construct the parameters in INIT ACK by copying the
1541 * ERROR causes over.
1543 unk_param = (sctp_unrecognized_param_t *)
1544 ((__u8 *)(err_chunk->chunk_hdr) +
1545 sizeof(sctp_chunkhdr_t));
1546 /* Replace the cause code with the "Unrecognized parameter"
1549 sctp_addto_chunk(repl, len, unk_param);
1552 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(new_asoc));
1553 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1556 * Note: After sending out INIT ACK with the State Cookie parameter,
1557 * "Z" MUST NOT allocate any resources for this new association.
1558 * Otherwise, "Z" will be vulnerable to resource attacks.
1560 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
1561 retval = SCTP_DISPOSITION_CONSUME;
1566 retval = SCTP_DISPOSITION_NOMEM;
1569 sctp_association_free(new_asoc);
1572 sctp_chunk_free(err_chunk);
1577 * Handle simultaneous INIT.
1578 * This means we started an INIT and then we got an INIT request from
1581 * Section: 5.2.1 INIT received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
1582 * This usually indicates an initialization collision, i.e., each
1583 * endpoint is attempting, at about the same time, to establish an
1584 * association with the other endpoint.
1586 * Upon receipt of an INIT in the COOKIE-WAIT or COOKIE-ECHOED state, an
1587 * endpoint MUST respond with an INIT ACK using the same parameters it
1588 * sent in its original INIT chunk (including its Verification Tag,
1589 * unchanged). These original parameters are combined with those from the
1590 * newly received INIT chunk. The endpoint shall also generate a State
1591 * Cookie with the INIT ACK. The endpoint uses the parameters sent in its
1592 * INIT to calculate the State Cookie.
1594 * After that, the endpoint MUST NOT change its state, the T1-init
1595 * timer shall be left running and the corresponding TCB MUST NOT be
1596 * destroyed. The normal procedures for handling State Cookies when
1597 * a TCB exists will resolve the duplicate INITs to a single association.
1599 * For an endpoint that is in the COOKIE-ECHOED state it MUST populate
1600 * its Tie-Tags with the Tag information of itself and its peer (see
1601 * section 5.2.2 for a description of the Tie-Tags).
1603 * Verification Tag: Not explicit, but an INIT can not have a valid
1604 * verification tag, so we skip the check.
1607 * (endpoint, asoc, chunk)
1610 * (asoc, reply_msg, msg_up, timers, counters)
1612 * The return value is the disposition of the chunk.
1614 sctp_disposition_t sctp_sf_do_5_2_1_siminit(struct net *net,
1615 const struct sctp_endpoint *ep,
1616 const struct sctp_association *asoc,
1617 const sctp_subtype_t type,
1619 sctp_cmd_seq_t *commands)
1621 /* Call helper to do the real work for both simulataneous and
1622 * duplicate INIT chunk handling.
1624 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1628 * Handle duplicated INIT messages. These are usually delayed
1631 * Section: 5.2.2 Unexpected INIT in States Other than CLOSED,
1632 * COOKIE-ECHOED and COOKIE-WAIT
1634 * Unless otherwise stated, upon reception of an unexpected INIT for
1635 * this association, the endpoint shall generate an INIT ACK with a
1636 * State Cookie. In the outbound INIT ACK the endpoint MUST copy its
1637 * current Verification Tag and peer's Verification Tag into a reserved
1638 * place within the state cookie. We shall refer to these locations as
1639 * the Peer's-Tie-Tag and the Local-Tie-Tag. The outbound SCTP packet
1640 * containing this INIT ACK MUST carry a Verification Tag value equal to
1641 * the Initiation Tag found in the unexpected INIT. And the INIT ACK
1642 * MUST contain a new Initiation Tag (randomly generated see Section
1643 * 5.3.1). Other parameters for the endpoint SHOULD be copied from the
1644 * existing parameters of the association (e.g. number of outbound
1645 * streams) into the INIT ACK and cookie.
1647 * After sending out the INIT ACK, the endpoint shall take no further
1648 * actions, i.e., the existing association, including its current state,
1649 * and the corresponding TCB MUST NOT be changed.
1651 * Note: Only when a TCB exists and the association is not in a COOKIE-
1652 * WAIT state are the Tie-Tags populated. For a normal association INIT
1653 * (i.e. the endpoint is in a COOKIE-WAIT state), the Tie-Tags MUST be
1654 * set to 0 (indicating that no previous TCB existed). The INIT ACK and
1655 * State Cookie are populated as specified in section 5.2.1.
1657 * Verification Tag: Not specified, but an INIT has no way of knowing
1658 * what the verification tag could be, so we ignore it.
1661 * (endpoint, asoc, chunk)
1664 * (asoc, reply_msg, msg_up, timers, counters)
1666 * The return value is the disposition of the chunk.
1668 sctp_disposition_t sctp_sf_do_5_2_2_dupinit(struct net *net,
1669 const struct sctp_endpoint *ep,
1670 const struct sctp_association *asoc,
1671 const sctp_subtype_t type,
1673 sctp_cmd_seq_t *commands)
1675 /* Call helper to do the real work for both simulataneous and
1676 * duplicate INIT chunk handling.
1678 return sctp_sf_do_unexpected_init(net, ep, asoc, type, arg, commands);
1683 * Unexpected INIT-ACK handler.
1686 * If an INIT ACK received by an endpoint in any state other than the
1687 * COOKIE-WAIT state, the endpoint should discard the INIT ACK chunk.
1688 * An unexpected INIT ACK usually indicates the processing of an old or
1689 * duplicated INIT chunk.
1691 sctp_disposition_t sctp_sf_do_5_2_3_initack(struct net *net,
1692 const struct sctp_endpoint *ep,
1693 const struct sctp_association *asoc,
1694 const sctp_subtype_t type,
1695 void *arg, sctp_cmd_seq_t *commands)
1697 /* Per the above section, we'll discard the chunk if we have an
1698 * endpoint. If this is an OOTB INIT-ACK, treat it as such.
1700 if (ep == sctp_sk(net->sctp.ctl_sock)->ep)
1701 return sctp_sf_ootb(net, ep, asoc, type, arg, commands);
1703 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
1706 /* Unexpected COOKIE-ECHO handler for peer restart (Table 2, action 'A')
1709 * A) In this case, the peer may have restarted.
1711 static sctp_disposition_t sctp_sf_do_dupcook_a(struct net *net,
1712 const struct sctp_endpoint *ep,
1713 const struct sctp_association *asoc,
1714 struct sctp_chunk *chunk,
1715 sctp_cmd_seq_t *commands,
1716 struct sctp_association *new_asoc)
1718 sctp_init_chunk_t *peer_init;
1719 struct sctp_ulpevent *ev;
1720 struct sctp_chunk *repl;
1721 struct sctp_chunk *err;
1722 sctp_disposition_t disposition;
1724 /* new_asoc is a brand-new association, so these are not yet
1725 * side effects--it is safe to run them here.
1727 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1729 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1733 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1734 return SCTP_DISPOSITION_DISCARD;
1736 /* Make sure no new addresses are being added during the
1737 * restart. Though this is a pretty complicated attack
1738 * since you'd have to get inside the cookie.
1740 if (!sctp_sf_check_restart_addrs(new_asoc, asoc, chunk, commands))
1741 return SCTP_DISPOSITION_CONSUME;
1743 /* If the endpoint is in the SHUTDOWN-ACK-SENT state and recognizes
1744 * the peer has restarted (Action A), it MUST NOT setup a new
1745 * association but instead resend the SHUTDOWN ACK and send an ERROR
1746 * chunk with a "Cookie Received while Shutting Down" error cause to
1749 if (sctp_state(asoc, SHUTDOWN_ACK_SENT)) {
1750 disposition = sctp_sf_do_9_2_reshutack(net, ep, asoc,
1751 SCTP_ST_CHUNK(chunk->chunk_hdr->type),
1753 if (SCTP_DISPOSITION_NOMEM == disposition)
1756 err = sctp_make_op_error(asoc, chunk,
1757 SCTP_ERROR_COOKIE_IN_SHUTDOWN,
1760 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
1763 return SCTP_DISPOSITION_CONSUME;
1766 /* For now, stop pending T3-rtx and SACK timers, fail any unsent/unacked
1767 * data. Consider the optional choice of resending of this data.
1769 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
1770 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1771 SCTP_TO(SCTP_EVENT_TIMEOUT_SACK));
1772 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
1774 /* Stop pending T4-rto timer, teardown ASCONF queue, ASCONF-ACK queue
1775 * and ASCONF-ACK cache.
1777 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1778 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
1779 sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_ASCONF_QUEUE, SCTP_NULL());
1781 repl = sctp_make_cookie_ack(new_asoc, chunk);
1785 /* Report association restart to upper layer. */
1786 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_RESTART, 0,
1787 new_asoc->c.sinit_num_ostreams,
1788 new_asoc->c.sinit_max_instreams,
1793 /* Update the content of current association. */
1794 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1795 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
1796 if ((sctp_state(asoc, SHUTDOWN_PENDING) ||
1797 sctp_state(asoc, SHUTDOWN_SENT)) &&
1798 (sctp_sstate(asoc->base.sk, CLOSING) ||
1799 sock_flag(asoc->base.sk, SOCK_DEAD))) {
1800 /* If the socket has been closed by user, don't
1801 * transition to ESTABLISHED. Instead trigger SHUTDOWN
1802 * bundled with COOKIE_ACK.
1804 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1805 return sctp_sf_do_9_2_start_shutdown(net, ep, asoc,
1806 SCTP_ST_CHUNK(0), NULL,
1809 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1810 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1811 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1813 return SCTP_DISPOSITION_CONSUME;
1816 sctp_chunk_free(repl);
1818 return SCTP_DISPOSITION_NOMEM;
1821 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'B')
1824 * B) In this case, both sides may be attempting to start an association
1825 * at about the same time but the peer endpoint started its INIT
1826 * after responding to the local endpoint's INIT
1828 /* This case represents an initialization collision. */
1829 static sctp_disposition_t sctp_sf_do_dupcook_b(struct net *net,
1830 const struct sctp_endpoint *ep,
1831 const struct sctp_association *asoc,
1832 struct sctp_chunk *chunk,
1833 sctp_cmd_seq_t *commands,
1834 struct sctp_association *new_asoc)
1836 sctp_init_chunk_t *peer_init;
1837 struct sctp_chunk *repl;
1839 /* new_asoc is a brand-new association, so these are not yet
1840 * side effects--it is safe to run them here.
1842 peer_init = &chunk->subh.cookie_hdr->c.peer_init[0];
1843 if (!sctp_process_init(new_asoc, chunk, sctp_source(chunk), peer_init,
1847 if (!sctp_auth_chunk_verify(net, chunk, new_asoc))
1848 return SCTP_DISPOSITION_DISCARD;
1850 /* Update the content of current association. */
1851 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_ASSOC, SCTP_ASOC(new_asoc));
1852 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1853 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1854 if (asoc->state < SCTP_STATE_ESTABLISHED)
1855 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1856 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
1858 repl = sctp_make_cookie_ack(new_asoc, chunk);
1862 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1864 /* RFC 2960 5.1 Normal Establishment of an Association
1866 * D) IMPLEMENTATION NOTE: An implementation may choose to
1867 * send the Communication Up notification to the SCTP user
1868 * upon reception of a valid COOKIE ECHO chunk.
1870 * Sadly, this needs to be implemented as a side-effect, because
1871 * we are not guaranteed to have set the association id of the real
1872 * association and so these notifications need to be delayed until
1873 * the association id is allocated.
1876 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_CHANGE, SCTP_U8(SCTP_COMM_UP));
1878 /* Sockets API Draft Section 5.3.1.6
1879 * When a peer sends a Adaptation Layer Indication parameter , SCTP
1880 * delivers this notification to inform the application that of the
1881 * peers requested adaptation layer.
1883 * This also needs to be done as a side effect for the same reason as
1886 if (asoc->peer.adaptation_ind)
1887 sctp_add_cmd_sf(commands, SCTP_CMD_ADAPTATION_IND, SCTP_NULL());
1889 return SCTP_DISPOSITION_CONSUME;
1892 return SCTP_DISPOSITION_NOMEM;
1895 /* Unexpected COOKIE-ECHO handler for setup collision (Table 2, action 'C')
1898 * C) In this case, the local endpoint's cookie has arrived late.
1899 * Before it arrived, the local endpoint sent an INIT and received an
1900 * INIT-ACK and finally sent a COOKIE ECHO with the peer's same tag
1901 * but a new tag of its own.
1903 /* This case represents an initialization collision. */
1904 static sctp_disposition_t sctp_sf_do_dupcook_c(struct net *net,
1905 const struct sctp_endpoint *ep,
1906 const struct sctp_association *asoc,
1907 struct sctp_chunk *chunk,
1908 sctp_cmd_seq_t *commands,
1909 struct sctp_association *new_asoc)
1911 /* The cookie should be silently discarded.
1912 * The endpoint SHOULD NOT change states and should leave
1913 * any timers running.
1915 return SCTP_DISPOSITION_DISCARD;
1918 /* Unexpected COOKIE-ECHO handler lost chunk (Table 2, action 'D')
1922 * D) When both local and remote tags match the endpoint should always
1923 * enter the ESTABLISHED state, if it has not already done so.
1925 /* This case represents an initialization collision. */
1926 static sctp_disposition_t sctp_sf_do_dupcook_d(struct net *net,
1927 const struct sctp_endpoint *ep,
1928 const struct sctp_association *asoc,
1929 struct sctp_chunk *chunk,
1930 sctp_cmd_seq_t *commands,
1931 struct sctp_association *new_asoc)
1933 struct sctp_ulpevent *ev = NULL, *ai_ev = NULL;
1934 struct sctp_chunk *repl;
1936 /* Clarification from Implementor's Guide:
1937 * D) When both local and remote tags match the endpoint should
1938 * enter the ESTABLISHED state, if it is in the COOKIE-ECHOED state.
1939 * It should stop any cookie timer that may be running and send
1943 if (!sctp_auth_chunk_verify(net, chunk, asoc))
1944 return SCTP_DISPOSITION_DISCARD;
1946 /* Don't accidentally move back into established state. */
1947 if (asoc->state < SCTP_STATE_ESTABLISHED) {
1948 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
1949 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
1950 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
1951 SCTP_STATE(SCTP_STATE_ESTABLISHED));
1952 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
1953 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START,
1956 /* RFC 2960 5.1 Normal Establishment of an Association
1958 * D) IMPLEMENTATION NOTE: An implementation may choose
1959 * to send the Communication Up notification to the
1960 * SCTP user upon reception of a valid COOKIE
1963 ev = sctp_ulpevent_make_assoc_change(asoc, 0,
1965 asoc->c.sinit_num_ostreams,
1966 asoc->c.sinit_max_instreams,
1971 /* Sockets API Draft Section 5.3.1.6
1972 * When a peer sends a Adaptation Layer Indication parameter,
1973 * SCTP delivers this notification to inform the application
1974 * that of the peers requested adaptation layer.
1976 if (asoc->peer.adaptation_ind) {
1977 ai_ev = sctp_ulpevent_make_adaptation_indication(asoc,
1985 repl = sctp_make_cookie_ack(asoc, chunk);
1989 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
1992 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1995 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
1996 SCTP_ULPEVENT(ai_ev));
1998 return SCTP_DISPOSITION_CONSUME;
2002 sctp_ulpevent_free(ai_ev);
2004 sctp_ulpevent_free(ev);
2005 return SCTP_DISPOSITION_NOMEM;
2009 * Handle a duplicate COOKIE-ECHO. This usually means a cookie-carrying
2010 * chunk was retransmitted and then delayed in the network.
2012 * Section: 5.2.4 Handle a COOKIE ECHO when a TCB exists
2014 * Verification Tag: None. Do cookie validation.
2017 * (endpoint, asoc, chunk)
2020 * (asoc, reply_msg, msg_up, timers, counters)
2022 * The return value is the disposition of the chunk.
2024 sctp_disposition_t sctp_sf_do_5_2_4_dupcook(struct net *net,
2025 const struct sctp_endpoint *ep,
2026 const struct sctp_association *asoc,
2027 const sctp_subtype_t type,
2029 sctp_cmd_seq_t *commands)
2031 sctp_disposition_t retval;
2032 struct sctp_chunk *chunk = arg;
2033 struct sctp_association *new_asoc;
2036 struct sctp_chunk *err_chk_p;
2038 /* Make sure that the chunk has a valid length from the protocol
2039 * perspective. In this case check to make sure we have at least
2040 * enough for the chunk header. Cookie length verification is
2043 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2044 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2047 /* "Decode" the chunk. We have no optional parameters so we
2048 * are in good shape.
2050 chunk->subh.cookie_hdr = (struct sctp_signed_cookie *)chunk->skb->data;
2051 if (!pskb_pull(chunk->skb, ntohs(chunk->chunk_hdr->length) -
2052 sizeof(sctp_chunkhdr_t)))
2055 /* In RFC 2960 5.2.4 3, if both Verification Tags in the State Cookie
2056 * of a duplicate COOKIE ECHO match the Verification Tags of the
2057 * current association, consider the State Cookie valid even if
2058 * the lifespan is exceeded.
2060 new_asoc = sctp_unpack_cookie(ep, asoc, chunk, GFP_ATOMIC, &error,
2064 * If the re-build failed, what is the proper error path
2067 * [We should abort the association. --piggy]
2070 /* FIXME: Several errors are possible. A bad cookie should
2071 * be silently discarded, but think about logging it too.
2074 case -SCTP_IERROR_NOMEM:
2077 case -SCTP_IERROR_STALE_COOKIE:
2078 sctp_send_stale_cookie_err(net, ep, asoc, chunk, commands,
2080 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2081 case -SCTP_IERROR_BAD_SIG:
2083 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2087 /* Compare the tie_tag in cookie with the verification tag of
2088 * current association.
2090 action = sctp_tietags_compare(new_asoc, asoc);
2093 case 'A': /* Association restart. */
2094 retval = sctp_sf_do_dupcook_a(net, ep, asoc, chunk, commands,
2098 case 'B': /* Collision case B. */
2099 retval = sctp_sf_do_dupcook_b(net, ep, asoc, chunk, commands,
2103 case 'C': /* Collision case C. */
2104 retval = sctp_sf_do_dupcook_c(net, ep, asoc, chunk, commands,
2108 case 'D': /* Collision case D. */
2109 retval = sctp_sf_do_dupcook_d(net, ep, asoc, chunk, commands,
2113 default: /* Discard packet for all others. */
2114 retval = sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2118 /* Delete the tempory new association. */
2119 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC, SCTP_ASOC(new_asoc));
2120 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
2122 /* Restore association pointer to provide SCTP command interpeter
2123 * with a valid context in case it needs to manipulate
2125 sctp_add_cmd_sf(commands, SCTP_CMD_SET_ASOC,
2126 SCTP_ASOC((struct sctp_association *)asoc));
2131 return SCTP_DISPOSITION_NOMEM;
2135 * Process an ABORT. (SHUTDOWN-PENDING state)
2137 * See sctp_sf_do_9_1_abort().
2139 sctp_disposition_t sctp_sf_shutdown_pending_abort(
2141 const struct sctp_endpoint *ep,
2142 const struct sctp_association *asoc,
2143 const sctp_subtype_t type,
2145 sctp_cmd_seq_t *commands)
2147 struct sctp_chunk *chunk = arg;
2149 if (!sctp_vtag_verify_either(chunk, asoc))
2150 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2152 /* Make sure that the ABORT chunk has a valid length.
2153 * Since this is an ABORT chunk, we have to discard it
2154 * because of the following text:
2155 * RFC 2960, Section 3.3.7
2156 * If an endpoint receives an ABORT with a format error or for an
2157 * association that doesn't exist, it MUST silently discard it.
2158 * Because the length is "invalid", we can't really discard just
2159 * as we do not know its true length. So, to be safe, discard the
2162 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2163 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2165 /* ADD-IP: Special case for ABORT chunks
2166 * F4) One special consideration is that ABORT Chunks arriving
2167 * destined to the IP address being deleted MUST be
2168 * ignored (see Section 5.3.1 for further details).
2170 if (SCTP_ADDR_DEL ==
2171 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2172 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2174 if (!sctp_err_chunk_valid(chunk))
2175 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2177 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2181 * Process an ABORT. (SHUTDOWN-SENT state)
2183 * See sctp_sf_do_9_1_abort().
2185 sctp_disposition_t sctp_sf_shutdown_sent_abort(struct net *net,
2186 const struct sctp_endpoint *ep,
2187 const struct sctp_association *asoc,
2188 const sctp_subtype_t type,
2190 sctp_cmd_seq_t *commands)
2192 struct sctp_chunk *chunk = arg;
2194 if (!sctp_vtag_verify_either(chunk, asoc))
2195 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2197 /* Make sure that the ABORT chunk has a valid length.
2198 * Since this is an ABORT chunk, we have to discard it
2199 * because of the following text:
2200 * RFC 2960, Section 3.3.7
2201 * If an endpoint receives an ABORT with a format error or for an
2202 * association that doesn't exist, it MUST silently discard it.
2203 * Because the length is "invalid", we can't really discard just
2204 * as we do not know its true length. So, to be safe, discard the
2207 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2208 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2210 /* ADD-IP: Special case for ABORT chunks
2211 * F4) One special consideration is that ABORT Chunks arriving
2212 * destined to the IP address being deleted MUST be
2213 * ignored (see Section 5.3.1 for further details).
2215 if (SCTP_ADDR_DEL ==
2216 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2217 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2219 if (!sctp_err_chunk_valid(chunk))
2220 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2222 /* Stop the T2-shutdown timer. */
2223 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2224 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2226 /* Stop the T5-shutdown guard timer. */
2227 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2228 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
2230 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2234 * Process an ABORT. (SHUTDOWN-ACK-SENT state)
2236 * See sctp_sf_do_9_1_abort().
2238 sctp_disposition_t sctp_sf_shutdown_ack_sent_abort(
2240 const struct sctp_endpoint *ep,
2241 const struct sctp_association *asoc,
2242 const sctp_subtype_t type,
2244 sctp_cmd_seq_t *commands)
2246 /* The same T2 timer, so we should be able to use
2247 * common function with the SHUTDOWN-SENT state.
2249 return sctp_sf_shutdown_sent_abort(net, ep, asoc, type, arg, commands);
2253 * Handle an Error received in COOKIE_ECHOED state.
2255 * Only handle the error type of stale COOKIE Error, the other errors will
2259 * (endpoint, asoc, chunk)
2262 * (asoc, reply_msg, msg_up, timers, counters)
2264 * The return value is the disposition of the chunk.
2266 sctp_disposition_t sctp_sf_cookie_echoed_err(struct net *net,
2267 const struct sctp_endpoint *ep,
2268 const struct sctp_association *asoc,
2269 const sctp_subtype_t type,
2271 sctp_cmd_seq_t *commands)
2273 struct sctp_chunk *chunk = arg;
2276 if (!sctp_vtag_verify(chunk, asoc))
2277 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2279 /* Make sure that the ERROR chunk has a valid length.
2280 * The parameter walking depends on this as well.
2282 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
2283 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2286 /* Process the error here */
2287 /* FUTURE FIXME: When PR-SCTP related and other optional
2288 * parms are emitted, this will have to change to handle multiple
2291 sctp_walk_errors(err, chunk->chunk_hdr) {
2292 if (SCTP_ERROR_STALE_COOKIE == err->cause)
2293 return sctp_sf_do_5_2_6_stale(net, ep, asoc, type,
2297 /* It is possible to have malformed error causes, and that
2298 * will cause us to end the walk early. However, since
2299 * we are discarding the packet, there should be no adverse
2302 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2306 * Handle a Stale COOKIE Error
2308 * Section: 5.2.6 Handle Stale COOKIE Error
2309 * If the association is in the COOKIE-ECHOED state, the endpoint may elect
2310 * one of the following three alternatives.
2312 * 3) Send a new INIT chunk to the endpoint, adding a Cookie
2313 * Preservative parameter requesting an extension to the lifetime of
2314 * the State Cookie. When calculating the time extension, an
2315 * implementation SHOULD use the RTT information measured based on the
2316 * previous COOKIE ECHO / ERROR exchange, and should add no more
2317 * than 1 second beyond the measured RTT, due to long State Cookie
2318 * lifetimes making the endpoint more subject to a replay attack.
2320 * Verification Tag: Not explicit, but safe to ignore.
2323 * (endpoint, asoc, chunk)
2326 * (asoc, reply_msg, msg_up, timers, counters)
2328 * The return value is the disposition of the chunk.
2330 static sctp_disposition_t sctp_sf_do_5_2_6_stale(struct net *net,
2331 const struct sctp_endpoint *ep,
2332 const struct sctp_association *asoc,
2333 const sctp_subtype_t type,
2335 sctp_cmd_seq_t *commands)
2337 struct sctp_chunk *chunk = arg;
2339 sctp_cookie_preserve_param_t bht;
2341 struct sctp_chunk *reply;
2342 struct sctp_bind_addr *bp;
2343 int attempts = asoc->init_err_counter + 1;
2345 if (attempts > asoc->max_init_attempts) {
2346 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
2347 SCTP_ERROR(ETIMEDOUT));
2348 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2349 SCTP_PERR(SCTP_ERROR_STALE_COOKIE));
2350 return SCTP_DISPOSITION_DELETE_TCB;
2353 err = (sctp_errhdr_t *)(chunk->skb->data);
2355 /* When calculating the time extension, an implementation
2356 * SHOULD use the RTT information measured based on the
2357 * previous COOKIE ECHO / ERROR exchange, and should add no
2358 * more than 1 second beyond the measured RTT, due to long
2359 * State Cookie lifetimes making the endpoint more subject to
2361 * Measure of Staleness's unit is usec. (1/1000000 sec)
2362 * Suggested Cookie Life-span Increment's unit is msec.
2364 * In general, if you use the suggested cookie life, the value
2365 * found in the field of measure of staleness should be doubled
2366 * to give ample time to retransmit the new cookie and thus
2367 * yield a higher probability of success on the reattempt.
2369 stale = ntohl(*(__be32 *)((u8 *)err + sizeof(sctp_errhdr_t)));
2370 stale = (stale * 2) / 1000;
2372 bht.param_hdr.type = SCTP_PARAM_COOKIE_PRESERVATIVE;
2373 bht.param_hdr.length = htons(sizeof(bht));
2374 bht.lifespan_increment = htonl(stale);
2376 /* Build that new INIT chunk. */
2377 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
2378 reply = sctp_make_init(asoc, bp, GFP_ATOMIC, sizeof(bht));
2382 sctp_addto_chunk(reply, sizeof(bht), &bht);
2384 /* Clear peer's init_tag cached in assoc as we are sending a new INIT */
2385 sctp_add_cmd_sf(commands, SCTP_CMD_CLEAR_INIT_TAG, SCTP_NULL());
2387 /* Stop pending T3-rtx and heartbeat timers */
2388 sctp_add_cmd_sf(commands, SCTP_CMD_T3_RTX_TIMERS_STOP, SCTP_NULL());
2389 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
2391 /* Delete non-primary peer ip addresses since we are transitioning
2392 * back to the COOKIE-WAIT state
2394 sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
2396 /* If we've sent any data bundled with COOKIE-ECHO we will need to
2399 sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
2400 SCTP_TRANSPORT(asoc->peer.primary_path));
2402 /* Cast away the const modifier, as we want to just
2403 * rerun it through as a sideffect.
2405 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_COUNTER_INC, SCTP_NULL());
2407 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2408 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
2409 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2410 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
2411 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
2412 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2414 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2416 return SCTP_DISPOSITION_CONSUME;
2419 return SCTP_DISPOSITION_NOMEM;
2426 * After checking the Verification Tag, the receiving endpoint shall
2427 * remove the association from its record, and shall report the
2428 * termination to its upper layer.
2430 * Verification Tag: 8.5.1 Exceptions in Verification Tag Rules
2431 * B) Rules for packet carrying ABORT:
2433 * - The endpoint shall always fill in the Verification Tag field of the
2434 * outbound packet with the destination endpoint's tag value if it
2437 * - If the ABORT is sent in response to an OOTB packet, the endpoint
2438 * MUST follow the procedure described in Section 8.4.
2440 * - The receiver MUST accept the packet if the Verification Tag
2441 * matches either its own tag, OR the tag of its peer. Otherwise, the
2442 * receiver MUST silently discard the packet and take no further
2446 * (endpoint, asoc, chunk)
2449 * (asoc, reply_msg, msg_up, timers, counters)
2451 * The return value is the disposition of the chunk.
2453 sctp_disposition_t sctp_sf_do_9_1_abort(struct net *net,
2454 const struct sctp_endpoint *ep,
2455 const struct sctp_association *asoc,
2456 const sctp_subtype_t type,
2458 sctp_cmd_seq_t *commands)
2460 struct sctp_chunk *chunk = arg;
2462 if (!sctp_vtag_verify_either(chunk, asoc))
2463 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2465 /* Make sure that the ABORT chunk has a valid length.
2466 * Since this is an ABORT chunk, we have to discard it
2467 * because of the following text:
2468 * RFC 2960, Section 3.3.7
2469 * If an endpoint receives an ABORT with a format error or for an
2470 * association that doesn't exist, it MUST silently discard it.
2471 * Because the length is "invalid", we can't really discard just
2472 * as we do not know its true length. So, to be safe, discard the
2475 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2476 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2478 /* ADD-IP: Special case for ABORT chunks
2479 * F4) One special consideration is that ABORT Chunks arriving
2480 * destined to the IP address being deleted MUST be
2481 * ignored (see Section 5.3.1 for further details).
2483 if (SCTP_ADDR_DEL ==
2484 sctp_bind_addr_state(&asoc->base.bind_addr, &chunk->dest))
2485 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
2487 if (!sctp_err_chunk_valid(chunk))
2488 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2490 return __sctp_sf_do_9_1_abort(net, ep, asoc, type, arg, commands);
2493 static sctp_disposition_t __sctp_sf_do_9_1_abort(struct net *net,
2494 const struct sctp_endpoint *ep,
2495 const struct sctp_association *asoc,
2496 const sctp_subtype_t type,
2498 sctp_cmd_seq_t *commands)
2500 struct sctp_chunk *chunk = arg;
2502 __be16 error = SCTP_ERROR_NO_ERROR;
2504 /* See if we have an error cause code in the chunk. */
2505 len = ntohs(chunk->chunk_hdr->length);
2507 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2508 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2510 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(ECONNRESET));
2511 /* ASSOC_FAILED will DELETE_TCB. */
2512 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED, SCTP_PERR(error));
2513 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2514 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
2516 return SCTP_DISPOSITION_ABORT;
2520 * Process an ABORT. (COOKIE-WAIT state)
2522 * See sctp_sf_do_9_1_abort() above.
2524 sctp_disposition_t sctp_sf_cookie_wait_abort(struct net *net,
2525 const struct sctp_endpoint *ep,
2526 const struct sctp_association *asoc,
2527 const sctp_subtype_t type,
2529 sctp_cmd_seq_t *commands)
2531 struct sctp_chunk *chunk = arg;
2533 __be16 error = SCTP_ERROR_NO_ERROR;
2535 if (!sctp_vtag_verify_either(chunk, asoc))
2536 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2538 /* Make sure that the ABORT chunk has a valid length.
2539 * Since this is an ABORT chunk, we have to discard it
2540 * because of the following text:
2541 * RFC 2960, Section 3.3.7
2542 * If an endpoint receives an ABORT with a format error or for an
2543 * association that doesn't exist, it MUST silently discard it.
2544 * Because the length is "invalid", we can't really discard just
2545 * as we do not know its true length. So, to be safe, discard the
2548 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_abort_chunk_t)))
2549 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2551 /* See if we have an error cause code in the chunk. */
2552 len = ntohs(chunk->chunk_hdr->length);
2553 if (len >= sizeof(struct sctp_chunkhdr) + sizeof(struct sctp_errhdr))
2554 error = ((sctp_errhdr_t *)chunk->skb->data)->cause;
2556 return sctp_stop_t1_and_abort(net, commands, error, ECONNREFUSED, asoc,
2561 * Process an incoming ICMP as an ABORT. (COOKIE-WAIT state)
2563 sctp_disposition_t sctp_sf_cookie_wait_icmp_abort(struct net *net,
2564 const struct sctp_endpoint *ep,
2565 const struct sctp_association *asoc,
2566 const sctp_subtype_t type,
2568 sctp_cmd_seq_t *commands)
2570 return sctp_stop_t1_and_abort(net, commands, SCTP_ERROR_NO_ERROR,
2572 (struct sctp_transport *)arg);
2576 * Process an ABORT. (COOKIE-ECHOED state)
2578 sctp_disposition_t sctp_sf_cookie_echoed_abort(struct net *net,
2579 const struct sctp_endpoint *ep,
2580 const struct sctp_association *asoc,
2581 const sctp_subtype_t type,
2583 sctp_cmd_seq_t *commands)
2585 /* There is a single T1 timer, so we should be able to use
2586 * common function with the COOKIE-WAIT state.
2588 return sctp_sf_cookie_wait_abort(net, ep, asoc, type, arg, commands);
2592 * Stop T1 timer and abort association with "INIT failed".
2594 * This is common code called by several sctp_sf_*_abort() functions above.
2596 static sctp_disposition_t sctp_stop_t1_and_abort(struct net *net,
2597 sctp_cmd_seq_t *commands,
2598 __be16 error, int sk_err,
2599 const struct sctp_association *asoc,
2600 struct sctp_transport *transport)
2602 pr_debug("%s: ABORT received (INIT)\n", __func__);
2604 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2605 SCTP_STATE(SCTP_STATE_CLOSED));
2606 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
2607 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
2608 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
2609 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR, SCTP_ERROR(sk_err));
2610 /* CMD_INIT_FAILED will DELETE_TCB. */
2611 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
2614 return SCTP_DISPOSITION_ABORT;
2618 * sctp_sf_do_9_2_shut
2621 * Upon the reception of the SHUTDOWN, the peer endpoint shall
2622 * - enter the SHUTDOWN-RECEIVED state,
2624 * - stop accepting new data from its SCTP user
2626 * - verify, by checking the Cumulative TSN Ack field of the chunk,
2627 * that all its outstanding DATA chunks have been received by the
2630 * Once an endpoint as reached the SHUTDOWN-RECEIVED state it MUST NOT
2631 * send a SHUTDOWN in response to a ULP request. And should discard
2632 * subsequent SHUTDOWN chunks.
2634 * If there are still outstanding DATA chunks left, the SHUTDOWN
2635 * receiver shall continue to follow normal data transmission
2636 * procedures defined in Section 6 until all outstanding DATA chunks
2637 * are acknowledged; however, the SHUTDOWN receiver MUST NOT accept
2638 * new data from its SCTP user.
2640 * Verification Tag: 8.5 Verification Tag [Normal verification]
2643 * (endpoint, asoc, chunk)
2646 * (asoc, reply_msg, msg_up, timers, counters)
2648 * The return value is the disposition of the chunk.
2650 sctp_disposition_t sctp_sf_do_9_2_shutdown(struct net *net,
2651 const struct sctp_endpoint *ep,
2652 const struct sctp_association *asoc,
2653 const sctp_subtype_t type,
2655 sctp_cmd_seq_t *commands)
2657 struct sctp_chunk *chunk = arg;
2658 sctp_shutdownhdr_t *sdh;
2659 sctp_disposition_t disposition;
2660 struct sctp_ulpevent *ev;
2663 if (!sctp_vtag_verify(chunk, asoc))
2664 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2666 /* Make sure that the SHUTDOWN chunk has a valid length. */
2667 if (!sctp_chunk_length_valid(chunk,
2668 sizeof(struct sctp_shutdown_chunk_t)))
2669 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2672 /* Convert the elaborate header. */
2673 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2674 skb_pull(chunk->skb, sizeof(sctp_shutdownhdr_t));
2675 chunk->subh.shutdown_hdr = sdh;
2676 ctsn = ntohl(sdh->cum_tsn_ack);
2678 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2679 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2680 asoc->ctsn_ack_point);
2682 return SCTP_DISPOSITION_DISCARD;
2685 /* If Cumulative TSN Ack beyond the max tsn currently
2686 * send, terminating the association and respond to the
2687 * sender with an ABORT.
2689 if (!TSN_lt(ctsn, asoc->next_tsn))
2690 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2692 /* API 5.3.1.5 SCTP_SHUTDOWN_EVENT
2693 * When a peer sends a SHUTDOWN, SCTP delivers this notification to
2694 * inform the application that it should cease sending data.
2696 ev = sctp_ulpevent_make_shutdown_event(asoc, 0, GFP_ATOMIC);
2698 disposition = SCTP_DISPOSITION_NOMEM;
2701 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
2703 /* Upon the reception of the SHUTDOWN, the peer endpoint shall
2704 * - enter the SHUTDOWN-RECEIVED state,
2705 * - stop accepting new data from its SCTP user
2707 * [This is implicit in the new state.]
2709 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
2710 SCTP_STATE(SCTP_STATE_SHUTDOWN_RECEIVED));
2711 disposition = SCTP_DISPOSITION_CONSUME;
2713 if (sctp_outq_is_empty(&asoc->outqueue)) {
2714 disposition = sctp_sf_do_9_2_shutdown_ack(net, ep, asoc, type,
2718 if (SCTP_DISPOSITION_NOMEM == disposition)
2721 /* - verify, by checking the Cumulative TSN Ack field of the
2722 * chunk, that all its outstanding DATA chunks have been
2723 * received by the SHUTDOWN sender.
2725 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2726 SCTP_BE32(chunk->subh.shutdown_hdr->cum_tsn_ack));
2733 * sctp_sf_do_9_2_shut_ctsn
2735 * Once an endpoint has reached the SHUTDOWN-RECEIVED state,
2736 * it MUST NOT send a SHUTDOWN in response to a ULP request.
2737 * The Cumulative TSN Ack of the received SHUTDOWN chunk
2738 * MUST be processed.
2740 sctp_disposition_t sctp_sf_do_9_2_shut_ctsn(struct net *net,
2741 const struct sctp_endpoint *ep,
2742 const struct sctp_association *asoc,
2743 const sctp_subtype_t type,
2745 sctp_cmd_seq_t *commands)
2747 struct sctp_chunk *chunk = arg;
2748 sctp_shutdownhdr_t *sdh;
2751 if (!sctp_vtag_verify(chunk, asoc))
2752 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2754 /* Make sure that the SHUTDOWN chunk has a valid length. */
2755 if (!sctp_chunk_length_valid(chunk,
2756 sizeof(struct sctp_shutdown_chunk_t)))
2757 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2760 sdh = (sctp_shutdownhdr_t *)chunk->skb->data;
2761 ctsn = ntohl(sdh->cum_tsn_ack);
2763 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
2764 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
2765 asoc->ctsn_ack_point);
2767 return SCTP_DISPOSITION_DISCARD;
2770 /* If Cumulative TSN Ack beyond the max tsn currently
2771 * send, terminating the association and respond to the
2772 * sender with an ABORT.
2774 if (!TSN_lt(ctsn, asoc->next_tsn))
2775 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
2777 /* verify, by checking the Cumulative TSN Ack field of the
2778 * chunk, that all its outstanding DATA chunks have been
2779 * received by the SHUTDOWN sender.
2781 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_CTSN,
2782 SCTP_BE32(sdh->cum_tsn_ack));
2784 return SCTP_DISPOSITION_CONSUME;
2788 * If an endpoint is in SHUTDOWN-ACK-SENT state and receives an INIT chunk
2789 * (e.g., if the SHUTDOWN COMPLETE was lost) with source and destination
2790 * transport addresses (either in the IP addresses or in the INIT chunk)
2791 * that belong to this association, it should discard the INIT chunk and
2792 * retransmit the SHUTDOWN ACK chunk.
2794 sctp_disposition_t sctp_sf_do_9_2_reshutack(struct net *net,
2795 const struct sctp_endpoint *ep,
2796 const struct sctp_association *asoc,
2797 const sctp_subtype_t type,
2799 sctp_cmd_seq_t *commands)
2801 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
2802 struct sctp_chunk *reply;
2804 /* Make sure that the chunk has a valid length */
2805 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
2806 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2809 /* Since we are not going to really process this INIT, there
2810 * is no point in verifying chunk boundries. Just generate
2813 reply = sctp_make_shutdown_ack(asoc, chunk);
2817 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
2818 * the T2-SHUTDOWN timer.
2820 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
2822 /* and restart the T2-shutdown timer. */
2823 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2824 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
2826 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
2828 return SCTP_DISPOSITION_CONSUME;
2830 return SCTP_DISPOSITION_NOMEM;
2834 * sctp_sf_do_ecn_cwr
2836 * Section: Appendix A: Explicit Congestion Notification
2840 * RFC 2481 details a specific bit for a sender to send in the header of
2841 * its next outbound TCP segment to indicate to its peer that it has
2842 * reduced its congestion window. This is termed the CWR bit. For
2843 * SCTP the same indication is made by including the CWR chunk.
2844 * This chunk contains one data element, i.e. the TSN number that
2845 * was sent in the ECNE chunk. This element represents the lowest
2846 * TSN number in the datagram that was originally marked with the
2849 * Verification Tag: 8.5 Verification Tag [Normal verification]
2851 * (endpoint, asoc, chunk)
2854 * (asoc, reply_msg, msg_up, timers, counters)
2856 * The return value is the disposition of the chunk.
2858 sctp_disposition_t sctp_sf_do_ecn_cwr(struct net *net,
2859 const struct sctp_endpoint *ep,
2860 const struct sctp_association *asoc,
2861 const sctp_subtype_t type,
2863 sctp_cmd_seq_t *commands)
2866 struct sctp_chunk *chunk = arg;
2869 if (!sctp_vtag_verify(chunk, asoc))
2870 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2872 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2873 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2876 cwr = (sctp_cwrhdr_t *) chunk->skb->data;
2877 skb_pull(chunk->skb, sizeof(sctp_cwrhdr_t));
2879 lowest_tsn = ntohl(cwr->lowest_tsn);
2881 /* Does this CWR ack the last sent congestion notification? */
2882 if (TSN_lte(asoc->last_ecne_tsn, lowest_tsn)) {
2883 /* Stop sending ECNE. */
2884 sctp_add_cmd_sf(commands,
2886 SCTP_U32(lowest_tsn));
2888 return SCTP_DISPOSITION_CONSUME;
2894 * Section: Appendix A: Explicit Congestion Notification
2898 * RFC 2481 details a specific bit for a receiver to send back in its
2899 * TCP acknowledgements to notify the sender of the Congestion
2900 * Experienced (CE) bit having arrived from the network. For SCTP this
2901 * same indication is made by including the ECNE chunk. This chunk
2902 * contains one data element, i.e. the lowest TSN associated with the IP
2903 * datagram marked with the CE bit.....
2905 * Verification Tag: 8.5 Verification Tag [Normal verification]
2907 * (endpoint, asoc, chunk)
2910 * (asoc, reply_msg, msg_up, timers, counters)
2912 * The return value is the disposition of the chunk.
2914 sctp_disposition_t sctp_sf_do_ecne(struct net *net,
2915 const struct sctp_endpoint *ep,
2916 const struct sctp_association *asoc,
2917 const sctp_subtype_t type,
2919 sctp_cmd_seq_t *commands)
2921 sctp_ecnehdr_t *ecne;
2922 struct sctp_chunk *chunk = arg;
2924 if (!sctp_vtag_verify(chunk, asoc))
2925 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2927 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_ecne_chunk_t)))
2928 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2931 ecne = (sctp_ecnehdr_t *) chunk->skb->data;
2932 skb_pull(chunk->skb, sizeof(sctp_ecnehdr_t));
2934 /* If this is a newer ECNE than the last CWR packet we sent out */
2935 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_ECNE,
2936 SCTP_U32(ntohl(ecne->lowest_tsn)));
2938 return SCTP_DISPOSITION_CONSUME;
2942 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
2944 * The SCTP endpoint MUST always acknowledge the reception of each valid
2947 * The guidelines on delayed acknowledgement algorithm specified in
2948 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
2949 * acknowledgement SHOULD be generated for at least every second packet
2950 * (not every second DATA chunk) received, and SHOULD be generated within
2951 * 200 ms of the arrival of any unacknowledged DATA chunk. In some
2952 * situations it may be beneficial for an SCTP transmitter to be more
2953 * conservative than the algorithms detailed in this document allow.
2954 * However, an SCTP transmitter MUST NOT be more aggressive than the
2955 * following algorithms allow.
2957 * A SCTP receiver MUST NOT generate more than one SACK for every
2958 * incoming packet, other than to update the offered window as the
2959 * receiving application consumes new data.
2961 * Verification Tag: 8.5 Verification Tag [Normal verification]
2964 * (endpoint, asoc, chunk)
2967 * (asoc, reply_msg, msg_up, timers, counters)
2969 * The return value is the disposition of the chunk.
2971 sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2972 const struct sctp_endpoint *ep,
2973 const struct sctp_association *asoc,
2974 const sctp_subtype_t type,
2976 sctp_cmd_seq_t *commands)
2978 struct sctp_chunk *chunk = arg;
2979 sctp_arg_t force = SCTP_NOFORCE();
2982 if (!sctp_vtag_verify(chunk, asoc)) {
2983 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
2985 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
2988 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
2989 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
2992 error = sctp_eat_data(asoc, chunk, commands);
2994 case SCTP_IERROR_NO_ERROR:
2996 case SCTP_IERROR_HIGH_TSN:
2997 case SCTP_IERROR_BAD_STREAM:
2998 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
2999 goto discard_noforce;
3000 case SCTP_IERROR_DUP_TSN:
3001 case SCTP_IERROR_IGNORE_TSN:
3002 SCTP_INC_STATS(net, SCTP_MIB_IN_DATA_CHUNK_DISCARDS);
3004 case SCTP_IERROR_NO_DATA:
3005 return SCTP_DISPOSITION_ABORT;
3006 case SCTP_IERROR_PROTO_VIOLATION:
3007 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3008 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3013 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
3014 force = SCTP_FORCE();
3016 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3017 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3018 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3021 /* If this is the last chunk in a packet, we need to count it
3022 * toward sack generation. Note that we need to SACK every
3023 * OTHER packet containing data chunks, EVEN IF WE DISCARD
3024 * THEM. We elect to NOT generate SACK's if the chunk fails
3025 * the verification tag test.
3027 * RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3029 * The SCTP endpoint MUST always acknowledge the reception of
3030 * each valid DATA chunk.
3032 * The guidelines on delayed acknowledgement algorithm
3033 * specified in Section 4.2 of [RFC2581] SHOULD be followed.
3034 * Specifically, an acknowledgement SHOULD be generated for at
3035 * least every second packet (not every second DATA chunk)
3036 * received, and SHOULD be generated within 200 ms of the
3037 * arrival of any unacknowledged DATA chunk. In some
3038 * situations it may be beneficial for an SCTP transmitter to
3039 * be more conservative than the algorithms detailed in this
3040 * document allow. However, an SCTP transmitter MUST NOT be
3041 * more aggressive than the following algorithms allow.
3043 if (chunk->end_of_packet)
3044 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3046 return SCTP_DISPOSITION_CONSUME;
3049 /* RFC 2960 6.2 Acknowledgement on Reception of DATA Chunks
3051 * When a packet arrives with duplicate DATA chunk(s) and with
3052 * no new DATA chunk(s), the endpoint MUST immediately send a
3053 * SACK with no delay. If a packet arrives with duplicate
3054 * DATA chunk(s) bundled with new DATA chunks, the endpoint
3055 * MAY immediately send a SACK. Normally receipt of duplicate
3056 * DATA chunks will occur when the original SACK chunk was lost
3057 * and the peer's RTO has expired. The duplicate TSN number(s)
3058 * SHOULD be reported in the SACK as duplicate.
3060 /* In our case, we split the MAY SACK advice up whether or not
3061 * the last chunk is a duplicate.'
3063 if (chunk->end_of_packet)
3064 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3065 return SCTP_DISPOSITION_DISCARD;
3068 if (chunk->end_of_packet)
3069 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, force);
3071 return SCTP_DISPOSITION_DISCARD;
3075 * sctp_sf_eat_data_fast_4_4
3078 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3079 * DATA chunks without delay.
3081 * Verification Tag: 8.5 Verification Tag [Normal verification]
3083 * (endpoint, asoc, chunk)
3086 * (asoc, reply_msg, msg_up, timers, counters)
3088 * The return value is the disposition of the chunk.
3090 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3091 const struct sctp_endpoint *ep,
3092 const struct sctp_association *asoc,
3093 const sctp_subtype_t type,
3095 sctp_cmd_seq_t *commands)
3097 struct sctp_chunk *chunk = arg;
3100 if (!sctp_vtag_verify(chunk, asoc)) {
3101 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3103 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3106 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3107 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3110 error = sctp_eat_data(asoc, chunk, commands);
3112 case SCTP_IERROR_NO_ERROR:
3113 case SCTP_IERROR_HIGH_TSN:
3114 case SCTP_IERROR_DUP_TSN:
3115 case SCTP_IERROR_IGNORE_TSN:
3116 case SCTP_IERROR_BAD_STREAM:
3118 case SCTP_IERROR_NO_DATA:
3119 return SCTP_DISPOSITION_ABORT;
3120 case SCTP_IERROR_PROTO_VIOLATION:
3121 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3122 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3127 /* Go a head and force a SACK, since we are shutting down. */
3129 /* Implementor's Guide.
3131 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3132 * respond to each received packet containing one or more DATA chunk(s)
3133 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3135 if (chunk->end_of_packet) {
3136 /* We must delay the chunk creation since the cumulative
3137 * TSN has not been updated yet.
3139 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3140 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3141 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3142 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3145 return SCTP_DISPOSITION_CONSUME;
3149 * Section: 6.2 Processing a Received SACK
3150 * D) Any time a SACK arrives, the endpoint performs the following:
3152 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3153 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3154 * increasing, a SACK whose Cumulative TSN Ack is less than the
3155 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3157 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3158 * of bytes still outstanding after processing the Cumulative TSN Ack
3159 * and the Gap Ack Blocks.
3161 * iii) If the SACK is missing a TSN that was previously
3162 * acknowledged via a Gap Ack Block (e.g., the data receiver
3163 * reneged on the data), then mark the corresponding DATA chunk
3164 * as available for retransmit: Mark it as missing for fast
3165 * retransmit as described in Section 7.2.4 and if no retransmit
3166 * timer is running for the destination address to which the DATA
3167 * chunk was originally transmitted, then T3-rtx is started for
3168 * that destination address.
3170 * Verification Tag: 8.5 Verification Tag [Normal verification]
3173 * (endpoint, asoc, chunk)
3176 * (asoc, reply_msg, msg_up, timers, counters)
3178 * The return value is the disposition of the chunk.
3180 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3181 const struct sctp_endpoint *ep,
3182 const struct sctp_association *asoc,
3183 const sctp_subtype_t type,
3185 sctp_cmd_seq_t *commands)
3187 struct sctp_chunk *chunk = arg;
3188 sctp_sackhdr_t *sackh;
3191 if (!sctp_vtag_verify(chunk, asoc))
3192 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3194 /* Make sure that the SACK chunk has a valid length. */
3195 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3196 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3199 /* Pull the SACK chunk from the data buffer */
3200 sackh = sctp_sm_pull_sack(chunk);
3201 /* Was this a bogus SACK? */
3203 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3204 chunk->subh.sack_hdr = sackh;
3205 ctsn = ntohl(sackh->cum_tsn_ack);
3207 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3208 * Ack Point, then drop the SACK. Since Cumulative TSN
3209 * Ack is monotonically increasing, a SACK whose
3210 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3211 * Point indicates an out-of-order SACK.
3213 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3214 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3215 asoc->ctsn_ack_point);
3217 return SCTP_DISPOSITION_DISCARD;
3220 /* If Cumulative TSN Ack beyond the max tsn currently
3221 * send, terminating the association and respond to the
3222 * sender with an ABORT.
3224 if (!TSN_lt(ctsn, asoc->next_tsn))
3225 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3227 /* Return this SACK for further processing. */
3228 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3230 /* Note: We do the rest of the work on the PROCESS_SACK
3233 return SCTP_DISPOSITION_CONSUME;
3237 * Generate an ABORT in response to a packet.
3239 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3241 * 8) The receiver should respond to the sender of the OOTB packet with
3242 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3243 * MUST fill in the Verification Tag field of the outbound packet
3244 * with the value found in the Verification Tag field of the OOTB
3245 * packet and set the T-bit in the Chunk Flags to indicate that the
3246 * Verification Tag is reflected. After sending this ABORT, the
3247 * receiver of the OOTB packet shall discard the OOTB packet and take
3248 * no further action.
3252 * The return value is the disposition of the chunk.
3254 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3255 const struct sctp_endpoint *ep,
3256 const struct sctp_association *asoc,
3257 const sctp_subtype_t type,
3259 sctp_cmd_seq_t *commands)
3261 struct sctp_packet *packet = NULL;
3262 struct sctp_chunk *chunk = arg;
3263 struct sctp_chunk *abort;
3265 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3268 /* Make an ABORT. The T bit will be set if the asoc
3271 abort = sctp_make_abort(asoc, chunk, 0);
3273 sctp_ootb_pkt_free(packet);
3274 return SCTP_DISPOSITION_NOMEM;
3277 /* Reflect vtag if T-Bit is set */
3278 if (sctp_test_T_bit(abort))
3279 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3281 /* Set the skb to the belonging sock for accounting. */
3282 abort->skb->sk = ep->base.sk;
3284 sctp_packet_append_chunk(packet, abort);
3286 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3287 SCTP_PACKET(packet));
3289 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3291 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3292 return SCTP_DISPOSITION_CONSUME;
3295 return SCTP_DISPOSITION_NOMEM;
3299 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3300 * event as ULP notification for each cause included in the chunk.
3302 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3304 * The return value is the disposition of the chunk.
3306 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3307 const struct sctp_endpoint *ep,
3308 const struct sctp_association *asoc,
3309 const sctp_subtype_t type,
3311 sctp_cmd_seq_t *commands)
3313 struct sctp_chunk *chunk = arg;
3316 if (!sctp_vtag_verify(chunk, asoc))
3317 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3319 /* Make sure that the ERROR chunk has a valid length. */
3320 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3321 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3323 sctp_walk_errors(err, chunk->chunk_hdr);
3324 if ((void *)err != (void *)chunk->chunk_end)
3325 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3326 (void *)err, commands);
3328 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3331 return SCTP_DISPOSITION_CONSUME;
3335 * Process an inbound SHUTDOWN ACK.
3338 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3339 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3340 * peer, and remove all record of the association.
3342 * The return value is the disposition.
3344 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3345 const struct sctp_endpoint *ep,
3346 const struct sctp_association *asoc,
3347 const sctp_subtype_t type,
3349 sctp_cmd_seq_t *commands)
3351 struct sctp_chunk *chunk = arg;
3352 struct sctp_chunk *reply;
3353 struct sctp_ulpevent *ev;
3355 if (!sctp_vtag_verify(chunk, asoc))
3356 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3358 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3359 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3360 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3362 /* 10.2 H) SHUTDOWN COMPLETE notification
3364 * When SCTP completes the shutdown procedures (section 9.2) this
3365 * notification is passed to the upper layer.
3367 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3368 0, 0, 0, NULL, GFP_ATOMIC);
3372 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3373 reply = sctp_make_shutdown_complete(asoc, chunk);
3377 /* Do all the commands now (after allocation), so that we
3378 * have consistent state if memory allocation failes
3380 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3382 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3383 * stop the T2-shutdown timer,
3385 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3386 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3388 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3389 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3391 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3392 SCTP_STATE(SCTP_STATE_CLOSED));
3393 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3394 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3395 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3397 /* ...and remove all record of the association. */
3398 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3399 return SCTP_DISPOSITION_DELETE_TCB;
3402 sctp_ulpevent_free(ev);
3404 return SCTP_DISPOSITION_NOMEM;
3408 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3410 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3411 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3412 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3413 * packet must fill in the Verification Tag field of the outbound
3414 * packet with the Verification Tag received in the SHUTDOWN ACK and
3415 * set the T-bit in the Chunk Flags to indicate that the Verification
3418 * 8) The receiver should respond to the sender of the OOTB packet with
3419 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3420 * MUST fill in the Verification Tag field of the outbound packet
3421 * with the value found in the Verification Tag field of the OOTB
3422 * packet and set the T-bit in the Chunk Flags to indicate that the
3423 * Verification Tag is reflected. After sending this ABORT, the
3424 * receiver of the OOTB packet shall discard the OOTB packet and take
3425 * no further action.
3427 sctp_disposition_t sctp_sf_ootb(struct net *net,
3428 const struct sctp_endpoint *ep,
3429 const struct sctp_association *asoc,
3430 const sctp_subtype_t type,
3432 sctp_cmd_seq_t *commands)
3434 struct sctp_chunk *chunk = arg;
3435 struct sk_buff *skb = chunk->skb;
3436 sctp_chunkhdr_t *ch;
3439 int ootb_shut_ack = 0;
3440 int ootb_cookie_ack = 0;
3442 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3444 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3446 /* Report violation if the chunk is less then minimal */
3447 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3448 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3451 /* Report violation if chunk len overflows */
3452 ch_end = ((__u8 *)ch) + SCTP_PAD4(ntohs(ch->length));
3453 if (ch_end > skb_tail_pointer(skb))
3454 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3457 /* Now that we know we at least have a chunk header,
3458 * do things that are type appropriate.
3460 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3463 /* RFC 2960, Section 3.3.7
3464 * Moreover, under any circumstances, an endpoint that
3465 * receives an ABORT MUST NOT respond to that ABORT by
3466 * sending an ABORT of its own.
3468 if (SCTP_CID_ABORT == ch->type)
3469 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3471 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3472 * or a COOKIE ACK the SCTP Packet should be silently
3476 if (SCTP_CID_COOKIE_ACK == ch->type)
3477 ootb_cookie_ack = 1;
3479 if (SCTP_CID_ERROR == ch->type) {
3480 sctp_walk_errors(err, ch) {
3481 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3482 ootb_cookie_ack = 1;
3488 ch = (sctp_chunkhdr_t *) ch_end;
3489 } while (ch_end < skb_tail_pointer(skb));
3492 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3493 else if (ootb_cookie_ack)
3494 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3496 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3500 * Handle an "Out of the blue" SHUTDOWN ACK.
3502 * Section: 8.4 5, sctpimpguide 2.41.
3504 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3505 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3506 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3507 * packet must fill in the Verification Tag field of the outbound
3508 * packet with the Verification Tag received in the SHUTDOWN ACK and
3509 * set the T-bit in the Chunk Flags to indicate that the Verification
3513 * (endpoint, asoc, type, arg, commands)
3516 * (sctp_disposition_t)
3518 * The return value is the disposition of the chunk.
3520 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3521 const struct sctp_endpoint *ep,
3522 const struct sctp_association *asoc,
3523 const sctp_subtype_t type,
3525 sctp_cmd_seq_t *commands)
3527 struct sctp_packet *packet = NULL;
3528 struct sctp_chunk *chunk = arg;
3529 struct sctp_chunk *shut;
3531 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3534 /* Make an SHUTDOWN_COMPLETE.
3535 * The T bit will be set if the asoc is NULL.
3537 shut = sctp_make_shutdown_complete(asoc, chunk);
3539 sctp_ootb_pkt_free(packet);
3540 return SCTP_DISPOSITION_NOMEM;
3543 /* Reflect vtag if T-Bit is set */
3544 if (sctp_test_T_bit(shut))
3545 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3547 /* Set the skb to the belonging sock for accounting. */
3548 shut->skb->sk = ep->base.sk;
3550 sctp_packet_append_chunk(packet, shut);
3552 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3553 SCTP_PACKET(packet));
3555 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3557 /* If the chunk length is invalid, we don't want to process
3558 * the reset of the packet.
3560 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3561 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3563 /* We need to discard the rest of the packet to prevent
3564 * potential bomming attacks from additional bundled chunks.
3565 * This is documented in SCTP Threats ID.
3567 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3570 return SCTP_DISPOSITION_NOMEM;
3574 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3576 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3577 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3578 * procedures in section 8.4 SHOULD be followed, in other words it
3579 * should be treated as an Out Of The Blue packet.
3580 * [This means that we do NOT check the Verification Tag on these
3584 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3585 const struct sctp_endpoint *ep,
3586 const struct sctp_association *asoc,
3587 const sctp_subtype_t type,
3589 sctp_cmd_seq_t *commands)
3591 struct sctp_chunk *chunk = arg;
3593 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3594 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3595 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3598 /* Although we do have an association in this case, it corresponds
3599 * to a restarted association. So the packet is treated as an OOTB
3600 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3601 * called with a NULL association.
3603 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3605 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3608 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3609 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3610 const struct sctp_endpoint *ep,
3611 const struct sctp_association *asoc,
3612 const sctp_subtype_t type, void *arg,
3613 sctp_cmd_seq_t *commands)
3615 struct sctp_chunk *chunk = arg;
3616 struct sctp_chunk *asconf_ack = NULL;
3617 struct sctp_paramhdr *err_param = NULL;
3618 sctp_addiphdr_t *hdr;
3621 if (!sctp_vtag_verify(chunk, asoc)) {
3622 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3624 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3627 /* ADD-IP: Section 4.1.1
3628 * This chunk MUST be sent in an authenticated way by using
3629 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3630 * is received unauthenticated it MUST be silently discarded as
3631 * described in [I-D.ietf-tsvwg-sctp-auth].
3633 if (!net->sctp.addip_noauth && !chunk->auth)
3634 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3636 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3637 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3638 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3641 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3642 serial = ntohl(hdr->serial);
3644 /* Verify the ASCONF chunk before processing it. */
3645 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3646 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3647 (void *)err_param, commands);
3649 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3650 * the endpoint stored in a new association variable
3651 * 'Peer-Serial-Number'.
3653 if (serial == asoc->peer.addip_serial + 1) {
3654 /* If this is the first instance of ASCONF in the packet,
3655 * we can clean our old ASCONF-ACKs.
3657 if (!chunk->has_asconf)
3658 sctp_assoc_clean_asconf_ack_cache(asoc);
3660 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3661 * expected, process the ASCONF as described below and after
3662 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3663 * the response packet and cache a copy of it (in the event it
3664 * later needs to be retransmitted).
3666 * Essentially, do V1-V5.
3668 asconf_ack = sctp_process_asconf((struct sctp_association *)
3671 return SCTP_DISPOSITION_NOMEM;
3672 } else if (serial < asoc->peer.addip_serial + 1) {
3674 * If the value found in the Sequence Number is less than the
3675 * ('Peer- Sequence-Number' + 1), simply skip to the next
3676 * ASCONF, and include in the outbound response packet
3677 * any previously cached ASCONF-ACK response that was
3678 * sent and saved that matches the Sequence Number of the
3679 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3680 * Chunk exists. This will occur when an older ASCONF
3681 * arrives out of order. In such a case, the receiver
3682 * should skip the ASCONF Chunk and not include ASCONF-ACK
3683 * Chunk for that chunk.
3685 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3687 return SCTP_DISPOSITION_DISCARD;
3689 /* Reset the transport so that we select the correct one
3690 * this time around. This is to make sure that we don't
3691 * accidentally use a stale transport that's been removed.
3693 asconf_ack->transport = NULL;
3695 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3696 * it must be either a stale packet or from an attacker.
3698 return SCTP_DISPOSITION_DISCARD;
3701 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3702 * containing the ASCONF-ACK Chunks MUST be the source address of
3703 * the SCTP packet that held the ASCONF Chunks.
3705 * To do this properly, we'll set the destination address of the chunk
3706 * and at the transmit time, will try look up the transport to use.
3707 * Since ASCONFs may be bundled, the correct transport may not be
3708 * created until we process the entire packet, thus this workaround.
3710 asconf_ack->dest = chunk->source;
3711 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3712 if (asoc->new_transport) {
3713 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3714 ((struct sctp_association *)asoc)->new_transport = NULL;
3717 return SCTP_DISPOSITION_CONSUME;
3721 * ADDIP Section 4.3 General rules for address manipulation
3722 * When building TLV parameters for the ASCONF Chunk that will add or
3723 * delete IP addresses the D0 to D13 rules should be applied:
3725 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3726 const struct sctp_endpoint *ep,
3727 const struct sctp_association *asoc,
3728 const sctp_subtype_t type, void *arg,
3729 sctp_cmd_seq_t *commands)
3731 struct sctp_chunk *asconf_ack = arg;
3732 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3733 struct sctp_chunk *abort;
3734 struct sctp_paramhdr *err_param = NULL;
3735 sctp_addiphdr_t *addip_hdr;
3736 __u32 sent_serial, rcvd_serial;
3738 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3739 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3741 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3744 /* ADD-IP, Section 4.1.2:
3745 * This chunk MUST be sent in an authenticated way by using
3746 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3747 * is received unauthenticated it MUST be silently discarded as
3748 * described in [I-D.ietf-tsvwg-sctp-auth].
3750 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3751 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3753 /* Make sure that the ADDIP chunk has a valid length. */
3754 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3755 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3758 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3759 rcvd_serial = ntohl(addip_hdr->serial);
3761 /* Verify the ASCONF-ACK chunk before processing it. */
3762 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3763 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3764 (void *)err_param, commands);
3767 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3768 sent_serial = ntohl(addip_hdr->serial);
3770 sent_serial = asoc->addip_serial - 1;
3773 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3774 * equal to the next serial number to be used but no ASCONF chunk is
3775 * outstanding the endpoint MUST ABORT the association. Note that a
3776 * sequence number is greater than if it is no more than 2^^31-1
3777 * larger than the current sequence number (using serial arithmetic).
3779 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3780 !(asoc->addip_last_asconf)) {
3781 abort = sctp_make_abort(asoc, asconf_ack,
3782 sizeof(sctp_errhdr_t));
3784 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3785 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3788 /* We are going to ABORT, so we might as well stop
3789 * processing the rest of the chunks in the packet.
3791 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3792 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3793 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3794 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3795 SCTP_ERROR(ECONNABORTED));
3796 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3797 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3798 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3799 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3800 return SCTP_DISPOSITION_ABORT;
3803 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3804 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3805 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3807 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3809 /* Successfully processed ASCONF_ACK. We can
3810 * release the next asconf if we have one.
3812 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3814 return SCTP_DISPOSITION_CONSUME;
3817 abort = sctp_make_abort(asoc, asconf_ack,
3818 sizeof(sctp_errhdr_t));
3820 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3821 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3824 /* We are going to ABORT, so we might as well stop
3825 * processing the rest of the chunks in the packet.
3827 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3828 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3829 SCTP_ERROR(ECONNABORTED));
3830 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3831 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3832 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3833 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3834 return SCTP_DISPOSITION_ABORT;
3837 return SCTP_DISPOSITION_DISCARD;
3841 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3843 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3844 * its cumulative TSN point to the value carried in the FORWARD TSN
3845 * chunk, and then MUST further advance its cumulative TSN point locally
3847 * After the above processing, the data receiver MUST stop reporting any
3848 * missing TSNs earlier than or equal to the new cumulative TSN point.
3850 * Verification Tag: 8.5 Verification Tag [Normal verification]
3852 * The return value is the disposition of the chunk.
3854 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3855 const struct sctp_endpoint *ep,
3856 const struct sctp_association *asoc,
3857 const sctp_subtype_t type,
3859 sctp_cmd_seq_t *commands)
3861 struct sctp_chunk *chunk = arg;
3862 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3863 struct sctp_fwdtsn_skip *skip;
3867 if (!sctp_vtag_verify(chunk, asoc)) {
3868 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3870 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3873 /* Make sure that the FORWARD_TSN chunk has valid length. */
3874 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3875 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3878 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3879 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3880 len = ntohs(chunk->chunk_hdr->length);
3881 len -= sizeof(struct sctp_chunkhdr);
3882 skb_pull(chunk->skb, len);
3884 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3885 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3887 /* The TSN is too high--silently discard the chunk and count on it
3888 * getting retransmitted later.
3890 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3891 goto discard_noforce;
3893 /* Silently discard the chunk if stream-id is not valid */
3894 sctp_walk_fwdtsn(skip, chunk) {
3895 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3896 goto discard_noforce;
3899 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3900 if (len > sizeof(struct sctp_fwdtsn_hdr))
3901 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3904 /* Count this as receiving DATA. */
3905 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3906 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3907 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3910 /* FIXME: For now send a SACK, but DATA processing may
3913 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3915 return SCTP_DISPOSITION_CONSUME;
3918 return SCTP_DISPOSITION_DISCARD;
3921 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3923 const struct sctp_endpoint *ep,
3924 const struct sctp_association *asoc,
3925 const sctp_subtype_t type,
3927 sctp_cmd_seq_t *commands)
3929 struct sctp_chunk *chunk = arg;
3930 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3931 struct sctp_fwdtsn_skip *skip;
3935 if (!sctp_vtag_verify(chunk, asoc)) {
3936 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3938 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3941 /* Make sure that the FORWARD_TSN chunk has a 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)
3961 /* Silently discard the chunk if stream-id is not valid */
3962 sctp_walk_fwdtsn(skip, chunk) {
3963 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
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 /* Go a head and force a SACK, since we are shutting down. */
3974 /* Implementor's Guide.
3976 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3977 * respond to each received packet containing one or more DATA chunk(s)
3978 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3980 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3981 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3982 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3983 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3985 return SCTP_DISPOSITION_CONSUME;
3989 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3991 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3992 * Identifier field. If this algorithm was not specified by the
3993 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3994 * during association setup, the AUTH chunk and all chunks after it MUST
3995 * be discarded and an ERROR chunk SHOULD be sent with the error cause
3996 * defined in Section 4.1.
3998 * If an endpoint with no shared key receives a Shared Key Identifier
3999 * other than 0, it MUST silently discard all authenticated chunks. If
4000 * the endpoint has at least one endpoint pair shared key for the peer,
4001 * it MUST use the key specified by the Shared Key Identifier if a
4002 * key has been configured for that Shared Key Identifier. If no
4003 * endpoint pair shared key has been configured for that Shared Key
4004 * Identifier, all authenticated chunks MUST be silently discarded.
4006 * Verification Tag: 8.5 Verification Tag [Normal verification]
4008 * The return value is the disposition of the chunk.
4010 static sctp_ierror_t sctp_sf_authenticate(
4011 const struct sctp_association *asoc,
4012 struct sctp_chunk *chunk)
4014 struct sctp_authhdr *auth_hdr;
4015 struct sctp_hmac *hmac;
4016 unsigned int sig_len;
4021 /* Pull in the auth header, so we can do some more verification */
4022 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4023 chunk->subh.auth_hdr = auth_hdr;
4024 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4026 /* Make sure that we support the HMAC algorithm from the auth
4029 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4030 return SCTP_IERROR_AUTH_BAD_HMAC;
4032 /* Make sure that the provided shared key identifier has been
4035 key_id = ntohs(auth_hdr->shkey_id);
4036 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4037 return SCTP_IERROR_AUTH_BAD_KEYID;
4040 /* Make sure that the length of the signature matches what
4043 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4044 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4045 if (sig_len != hmac->hmac_len)
4046 return SCTP_IERROR_PROTO_VIOLATION;
4048 /* Now that we've done validation checks, we can compute and
4049 * verify the hmac. The steps involved are:
4050 * 1. Save the digest from the chunk.
4051 * 2. Zero out the digest in the chunk.
4052 * 3. Compute the new digest
4053 * 4. Compare saved and new digests.
4055 digest = auth_hdr->hmac;
4056 skb_pull(chunk->skb, sig_len);
4058 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4062 memset(digest, 0, sig_len);
4064 sctp_auth_calculate_hmac(asoc, chunk->skb,
4065 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4068 /* Discard the packet if the digests do not match */
4069 if (memcmp(save_digest, digest, sig_len)) {
4071 return SCTP_IERROR_BAD_SIG;
4077 return SCTP_IERROR_NO_ERROR;
4079 return SCTP_IERROR_NOMEM;
4082 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4083 const struct sctp_endpoint *ep,
4084 const struct sctp_association *asoc,
4085 const sctp_subtype_t type,
4087 sctp_cmd_seq_t *commands)
4089 struct sctp_authhdr *auth_hdr;
4090 struct sctp_chunk *chunk = arg;
4091 struct sctp_chunk *err_chunk;
4092 sctp_ierror_t error;
4094 /* Make sure that the peer has AUTH capable */
4095 if (!asoc->peer.auth_capable)
4096 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4098 if (!sctp_vtag_verify(chunk, asoc)) {
4099 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4101 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4104 /* Make sure that the AUTH chunk has valid length. */
4105 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4106 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4109 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4110 error = sctp_sf_authenticate(asoc, chunk);
4112 case SCTP_IERROR_AUTH_BAD_HMAC:
4113 /* Generate the ERROR chunk and discard the rest
4116 err_chunk = sctp_make_op_error(asoc, chunk,
4117 SCTP_ERROR_UNSUP_HMAC,
4121 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4122 SCTP_CHUNK(err_chunk));
4125 case SCTP_IERROR_AUTH_BAD_KEYID:
4126 case SCTP_IERROR_BAD_SIG:
4127 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4129 case SCTP_IERROR_PROTO_VIOLATION:
4130 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4133 case SCTP_IERROR_NOMEM:
4134 return SCTP_DISPOSITION_NOMEM;
4136 default: /* Prevent gcc warnings */
4140 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4141 struct sctp_ulpevent *ev;
4143 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4144 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4149 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4153 return SCTP_DISPOSITION_CONSUME;
4157 * Process an unknown chunk.
4159 * Section: 3.2. Also, 2.1 in the implementor's guide.
4161 * Chunk Types are encoded such that the highest-order two bits specify
4162 * the action that must be taken if the processing endpoint does not
4163 * recognize the Chunk Type.
4165 * 00 - Stop processing this SCTP packet and discard it, do not process
4166 * any further chunks within it.
4168 * 01 - Stop processing this SCTP packet and discard it, do not process
4169 * any further chunks within it, and report the unrecognized
4170 * chunk in an 'Unrecognized Chunk Type'.
4172 * 10 - Skip this chunk and continue processing.
4174 * 11 - Skip this chunk and continue processing, but report in an ERROR
4175 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4177 * The return value is the disposition of the chunk.
4179 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4180 const struct sctp_endpoint *ep,
4181 const struct sctp_association *asoc,
4182 const sctp_subtype_t type,
4184 sctp_cmd_seq_t *commands)
4186 struct sctp_chunk *unk_chunk = arg;
4187 struct sctp_chunk *err_chunk;
4188 sctp_chunkhdr_t *hdr;
4190 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4192 if (!sctp_vtag_verify(unk_chunk, asoc))
4193 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4195 /* Make sure that the chunk has a valid length.
4196 * Since we don't know the chunk type, we use a general
4197 * chunkhdr structure to make a comparison.
4199 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4200 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4203 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4204 case SCTP_CID_ACTION_DISCARD:
4205 /* Discard the packet. */
4206 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4207 case SCTP_CID_ACTION_DISCARD_ERR:
4208 /* Generate an ERROR chunk as response. */
4209 hdr = unk_chunk->chunk_hdr;
4210 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4211 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4212 SCTP_PAD4(ntohs(hdr->length)),
4215 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4216 SCTP_CHUNK(err_chunk));
4219 /* Discard the packet. */
4220 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4221 return SCTP_DISPOSITION_CONSUME;
4222 case SCTP_CID_ACTION_SKIP:
4223 /* Skip the chunk. */
4224 return SCTP_DISPOSITION_DISCARD;
4225 case SCTP_CID_ACTION_SKIP_ERR:
4226 /* Generate an ERROR chunk as response. */
4227 hdr = unk_chunk->chunk_hdr;
4228 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4229 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4230 SCTP_PAD4(ntohs(hdr->length)),
4233 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4234 SCTP_CHUNK(err_chunk));
4236 /* Skip the chunk. */
4237 return SCTP_DISPOSITION_CONSUME;
4242 return SCTP_DISPOSITION_DISCARD;
4246 * Discard the chunk.
4248 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4249 * [Too numerous to mention...]
4250 * Verification Tag: No verification needed.
4252 * (endpoint, asoc, chunk)
4255 * (asoc, reply_msg, msg_up, timers, counters)
4257 * The return value is the disposition of the chunk.
4259 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4260 const struct sctp_endpoint *ep,
4261 const struct sctp_association *asoc,
4262 const sctp_subtype_t type,
4264 sctp_cmd_seq_t *commands)
4266 struct sctp_chunk *chunk = arg;
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(chunk, sizeof(sctp_chunkhdr_t)))
4273 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4276 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4278 return SCTP_DISPOSITION_DISCARD;
4282 * Discard the whole packet.
4286 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4287 * silently discard the OOTB packet and take no further action.
4289 * Verification Tag: No verification necessary
4292 * (endpoint, asoc, chunk)
4295 * (asoc, reply_msg, msg_up, timers, counters)
4297 * The return value is the disposition of the chunk.
4299 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4300 const struct sctp_endpoint *ep,
4301 const struct sctp_association *asoc,
4302 const sctp_subtype_t type,
4304 sctp_cmd_seq_t *commands)
4306 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4307 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4309 return SCTP_DISPOSITION_CONSUME;
4314 * The other end is violating protocol.
4316 * Section: Not specified
4317 * Verification Tag: Not specified
4319 * (endpoint, asoc, chunk)
4322 * (asoc, reply_msg, msg_up, timers, counters)
4324 * We simply tag the chunk as a violation. The state machine will log
4325 * the violation and continue.
4327 sctp_disposition_t sctp_sf_violation(struct net *net,
4328 const struct sctp_endpoint *ep,
4329 const struct sctp_association *asoc,
4330 const sctp_subtype_t type,
4332 sctp_cmd_seq_t *commands)
4334 struct sctp_chunk *chunk = arg;
4336 if (!sctp_vtag_verify(chunk, asoc))
4337 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4339 /* Make sure that the chunk has a valid length. */
4340 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4341 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4344 return SCTP_DISPOSITION_VIOLATION;
4348 * Common function to handle a protocol violation.
4350 static sctp_disposition_t sctp_sf_abort_violation(
4352 const struct sctp_endpoint *ep,
4353 const struct sctp_association *asoc,
4355 sctp_cmd_seq_t *commands,
4356 const __u8 *payload,
4357 const size_t paylen)
4359 struct sctp_packet *packet = NULL;
4360 struct sctp_chunk *chunk = arg;
4361 struct sctp_chunk *abort = NULL;
4363 /* SCTP-AUTH, Section 6.3:
4364 * It should be noted that if the receiver wants to tear
4365 * down an association in an authenticated way only, the
4366 * handling of malformed packets should not result in
4367 * tearing down the association.
4369 * This means that if we only want to abort associations
4370 * in an authenticated way (i.e AUTH+ABORT), then we
4371 * can't destroy this association just because the packet
4374 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4377 /* Make the abort chunk. */
4378 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4383 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4384 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4385 !asoc->peer.i.init_tag) {
4386 sctp_initack_chunk_t *initack;
4388 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4389 if (!sctp_chunk_length_valid(chunk,
4390 sizeof(sctp_initack_chunk_t)))
4391 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4393 unsigned int inittag;
4395 inittag = ntohl(initack->init_hdr.init_tag);
4396 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4401 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4402 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4404 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4405 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4406 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4407 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4408 SCTP_ERROR(ECONNREFUSED));
4409 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4410 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4412 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4413 SCTP_ERROR(ECONNABORTED));
4414 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4415 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4416 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4419 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4424 if (sctp_test_T_bit(abort))
4425 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4427 abort->skb->sk = ep->base.sk;
4429 sctp_packet_append_chunk(packet, abort);
4431 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4432 SCTP_PACKET(packet));
4434 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4437 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4440 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4441 return SCTP_DISPOSITION_ABORT;
4444 sctp_chunk_free(abort);
4446 return SCTP_DISPOSITION_NOMEM;
4450 * Handle a protocol violation when the chunk length is invalid.
4451 * "Invalid" length is identified as smaller than the minimal length a
4452 * given chunk can be. For example, a SACK chunk has invalid length
4453 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4455 * We inform the other end by sending an ABORT with a Protocol Violation
4458 * Section: Not specified
4459 * Verification Tag: Nothing to do
4461 * (endpoint, asoc, chunk)
4464 * (reply_msg, msg_up, counters)
4466 * Generate an ABORT chunk and terminate the association.
4468 static sctp_disposition_t sctp_sf_violation_chunklen(
4470 const struct sctp_endpoint *ep,
4471 const struct sctp_association *asoc,
4472 const sctp_subtype_t type,
4474 sctp_cmd_seq_t *commands)
4476 static const char err_str[] = "The following chunk had invalid length:";
4478 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4483 * Handle a protocol violation when the parameter length is invalid.
4484 * If the length is smaller than the minimum length of a given parameter,
4485 * or accumulated length in multi parameters exceeds the end of the chunk,
4486 * the length is considered as invalid.
4488 static sctp_disposition_t sctp_sf_violation_paramlen(
4490 const struct sctp_endpoint *ep,
4491 const struct sctp_association *asoc,
4492 const sctp_subtype_t type,
4493 void *arg, void *ext,
4494 sctp_cmd_seq_t *commands)
4496 struct sctp_chunk *chunk = arg;
4497 struct sctp_paramhdr *param = ext;
4498 struct sctp_chunk *abort = NULL;
4500 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4503 /* Make the abort chunk. */
4504 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4508 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4509 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4511 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4512 SCTP_ERROR(ECONNABORTED));
4513 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4514 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4515 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4516 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4519 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4520 return SCTP_DISPOSITION_ABORT;
4522 return SCTP_DISPOSITION_NOMEM;
4525 /* Handle a protocol violation when the peer trying to advance the
4526 * cumulative tsn ack to a point beyond the max tsn currently sent.
4528 * We inform the other end by sending an ABORT with a Protocol Violation
4531 static sctp_disposition_t sctp_sf_violation_ctsn(
4533 const struct sctp_endpoint *ep,
4534 const struct sctp_association *asoc,
4535 const sctp_subtype_t type,
4537 sctp_cmd_seq_t *commands)
4539 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4541 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4545 /* Handle protocol violation of an invalid chunk bundling. For example,
4546 * when we have an association and we receive bundled INIT-ACK, or
4547 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4548 * statement from the specs. Additionally, there might be an attacker
4549 * on the path and we may not want to continue this communication.
4551 static sctp_disposition_t sctp_sf_violation_chunk(
4553 const struct sctp_endpoint *ep,
4554 const struct sctp_association *asoc,
4555 const sctp_subtype_t type,
4557 sctp_cmd_seq_t *commands)
4559 static const char err_str[] = "The following chunk violates protocol:";
4562 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4564 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4567 /***************************************************************************
4568 * These are the state functions for handling primitive (Section 10) events.
4569 ***************************************************************************/
4571 * sctp_sf_do_prm_asoc
4573 * Section: 10.1 ULP-to-SCTP
4576 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4577 * outbound stream count)
4578 * -> association id [,destination transport addr list] [,outbound stream
4581 * This primitive allows the upper layer to initiate an association to a
4582 * specific peer endpoint.
4584 * The peer endpoint shall be specified by one of the transport addresses
4585 * which defines the endpoint (see Section 1.4). If the local SCTP
4586 * instance has not been initialized, the ASSOCIATE is considered an
4588 * [This is not relevant for the kernel implementation since we do all
4589 * initialization at boot time. It we hadn't initialized we wouldn't
4590 * get anywhere near this code.]
4592 * An association id, which is a local handle to the SCTP association,
4593 * will be returned on successful establishment of the association. If
4594 * SCTP is not able to open an SCTP association with the peer endpoint,
4595 * an error is returned.
4596 * [In the kernel implementation, the struct sctp_association needs to
4597 * be created BEFORE causing this primitive to run.]
4599 * Other association parameters may be returned, including the
4600 * complete destination transport addresses of the peer as well as the
4601 * outbound stream count of the local endpoint. One of the transport
4602 * address from the returned destination addresses will be selected by
4603 * the local endpoint as default primary path for sending SCTP packets
4604 * to this peer. The returned "destination transport addr list" can
4605 * be used by the ULP to change the default primary path or to force
4606 * sending a packet to a specific transport address. [All of this
4607 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4610 * Mandatory attributes:
4612 * o local SCTP instance name - obtained from the INITIALIZE operation.
4613 * [This is the argument asoc.]
4614 * o destination transport addr - specified as one of the transport
4615 * addresses of the peer endpoint with which the association is to be
4617 * [This is asoc->peer.active_path.]
4618 * o outbound stream count - the number of outbound streams the ULP
4619 * would like to open towards this peer endpoint.
4620 * [BUG: This is not currently implemented.]
4621 * Optional attributes:
4625 * The return value is a disposition.
4627 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4628 const struct sctp_endpoint *ep,
4629 const struct sctp_association *asoc,
4630 const sctp_subtype_t type,
4632 sctp_cmd_seq_t *commands)
4634 struct sctp_chunk *repl;
4635 struct sctp_association *my_asoc;
4637 /* The comment below says that we enter COOKIE-WAIT AFTER
4638 * sending the INIT, but that doesn't actually work in our
4641 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4642 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4644 /* RFC 2960 5.1 Normal Establishment of an Association
4646 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4647 * must provide its Verification Tag (Tag_A) in the Initiate
4648 * Tag field. Tag_A SHOULD be a random number in the range of
4649 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4652 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4656 /* Choose transport for INIT. */
4657 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4660 /* Cast away the const modifier, as we want to just
4661 * rerun it through as a sideffect.
4663 my_asoc = (struct sctp_association *)asoc;
4664 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4666 /* After sending the INIT, "A" starts the T1-init timer and
4667 * enters the COOKIE-WAIT state.
4669 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4670 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4671 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4672 return SCTP_DISPOSITION_CONSUME;
4675 return SCTP_DISPOSITION_NOMEM;
4679 * Process the SEND primitive.
4681 * Section: 10.1 ULP-to-SCTP
4684 * Format: SEND(association id, buffer address, byte count [,context]
4685 * [,stream id] [,life time] [,destination transport address]
4686 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4689 * This is the main method to send user data via SCTP.
4691 * Mandatory attributes:
4693 * o association id - local handle to the SCTP association
4695 * o buffer address - the location where the user message to be
4696 * transmitted is stored;
4698 * o byte count - The size of the user data in number of bytes;
4700 * Optional attributes:
4702 * o context - an optional 32 bit integer that will be carried in the
4703 * sending failure notification to the ULP if the transportation of
4704 * this User Message fails.
4706 * o stream id - to indicate which stream to send the data on. If not
4707 * specified, stream 0 will be used.
4709 * o life time - specifies the life time of the user data. The user data
4710 * will not be sent by SCTP after the life time expires. This
4711 * parameter can be used to avoid efforts to transmit stale
4712 * user messages. SCTP notifies the ULP if the data cannot be
4713 * initiated to transport (i.e. sent to the destination via SCTP's
4714 * send primitive) within the life time variable. However, the
4715 * user data will be transmitted if SCTP has attempted to transmit a
4716 * chunk before the life time expired.
4718 * o destination transport address - specified as one of the destination
4719 * transport addresses of the peer endpoint to which this packet
4720 * should be sent. Whenever possible, SCTP should use this destination
4721 * transport address for sending the packets, instead of the current
4724 * o unorder flag - this flag, if present, indicates that the user
4725 * would like the data delivered in an unordered fashion to the peer
4726 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4729 * o no-bundle flag - instructs SCTP not to bundle this user data with
4730 * other outbound DATA chunks. SCTP MAY still bundle even when
4731 * this flag is present, when faced with network congestion.
4733 * o payload protocol-id - A 32 bit unsigned integer that is to be
4734 * passed to the peer indicating the type of payload protocol data
4735 * being transmitted. This value is passed as opaque data by SCTP.
4737 * The return value is the disposition.
4739 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4740 const struct sctp_endpoint *ep,
4741 const struct sctp_association *asoc,
4742 const sctp_subtype_t type,
4744 sctp_cmd_seq_t *commands)
4746 struct sctp_datamsg *msg = arg;
4748 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4749 return SCTP_DISPOSITION_CONSUME;
4753 * Process the SHUTDOWN primitive.
4758 * Format: SHUTDOWN(association id)
4761 * Gracefully closes an association. Any locally queued user data
4762 * will be delivered to the peer. The association will be terminated only
4763 * after the peer acknowledges all the SCTP packets sent. A success code
4764 * will be returned on successful termination of the association. If
4765 * attempting to terminate the association results in a failure, an error
4766 * code shall be returned.
4768 * Mandatory attributes:
4770 * o association id - local handle to the SCTP association
4772 * Optional attributes:
4776 * The return value is the disposition.
4778 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4780 const struct sctp_endpoint *ep,
4781 const struct sctp_association *asoc,
4782 const sctp_subtype_t type,
4784 sctp_cmd_seq_t *commands)
4788 /* From 9.2 Shutdown of an Association
4789 * Upon receipt of the SHUTDOWN primitive from its upper
4790 * layer, the endpoint enters SHUTDOWN-PENDING state and
4791 * remains there until all outstanding data has been
4792 * acknowledged by its peer. The endpoint accepts no new data
4793 * from its upper layer, but retransmits data to the far end
4794 * if necessary to fill gaps.
4796 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4797 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4799 disposition = SCTP_DISPOSITION_CONSUME;
4800 if (sctp_outq_is_empty(&asoc->outqueue)) {
4801 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4808 * Process the ABORT primitive.
4813 * Format: Abort(association id [, cause code])
4816 * Ungracefully closes an association. Any locally queued user data
4817 * will be discarded and an ABORT chunk is sent to the peer. A success code
4818 * will be returned on successful abortion of the association. If
4819 * attempting to abort the association results in a failure, an error
4820 * code shall be returned.
4822 * Mandatory attributes:
4824 * o association id - local handle to the SCTP association
4826 * Optional attributes:
4828 * o cause code - reason of the abort to be passed to the peer
4832 * The return value is the disposition.
4834 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4836 const struct sctp_endpoint *ep,
4837 const struct sctp_association *asoc,
4838 const sctp_subtype_t type,
4840 sctp_cmd_seq_t *commands)
4842 /* From 9.1 Abort of an Association
4843 * Upon receipt of the ABORT primitive from its upper
4844 * layer, the endpoint enters CLOSED state and
4845 * discard all outstanding data has been
4846 * acknowledged by its peer. The endpoint accepts no new data
4847 * from its upper layer, but retransmits data to the far end
4848 * if necessary to fill gaps.
4850 struct sctp_chunk *abort = arg;
4853 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4855 /* Even if we can't send the ABORT due to low memory delete the
4856 * TCB. This is a departure from our typical NOMEM handling.
4859 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4860 SCTP_ERROR(ECONNABORTED));
4861 /* Delete the established association. */
4862 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4863 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4865 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4866 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4868 return SCTP_DISPOSITION_ABORT;
4871 /* We tried an illegal operation on an association which is closed. */
4872 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4873 const struct sctp_endpoint *ep,
4874 const struct sctp_association *asoc,
4875 const sctp_subtype_t type,
4877 sctp_cmd_seq_t *commands)
4879 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4880 return SCTP_DISPOSITION_CONSUME;
4883 /* We tried an illegal operation on an association which is shutting
4886 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4887 const struct sctp_endpoint *ep,
4888 const struct sctp_association *asoc,
4889 const sctp_subtype_t type,
4891 sctp_cmd_seq_t *commands)
4893 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4894 SCTP_ERROR(-ESHUTDOWN));
4895 return SCTP_DISPOSITION_CONSUME;
4899 * sctp_cookie_wait_prm_shutdown
4901 * Section: 4 Note: 2
4906 * The RFC does not explicitly address this issue, but is the route through the
4907 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4912 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4914 const struct sctp_endpoint *ep,
4915 const struct sctp_association *asoc,
4916 const sctp_subtype_t type,
4918 sctp_cmd_seq_t *commands)
4920 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4921 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4923 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4924 SCTP_STATE(SCTP_STATE_CLOSED));
4926 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4928 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4930 return SCTP_DISPOSITION_DELETE_TCB;
4934 * sctp_cookie_echoed_prm_shutdown
4936 * Section: 4 Note: 2
4941 * The RFC does not explcitly address this issue, but is the route through the
4942 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4947 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4949 const struct sctp_endpoint *ep,
4950 const struct sctp_association *asoc,
4951 const sctp_subtype_t type,
4952 void *arg, sctp_cmd_seq_t *commands)
4954 /* There is a single T1 timer, so we should be able to use
4955 * common function with the COOKIE-WAIT state.
4957 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4961 * sctp_sf_cookie_wait_prm_abort
4963 * Section: 4 Note: 2
4968 * The RFC does not explicitly address this issue, but is the route through the
4969 * state table when someone issues an abort while in COOKIE_WAIT state.
4974 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4976 const struct sctp_endpoint *ep,
4977 const struct sctp_association *asoc,
4978 const sctp_subtype_t type,
4980 sctp_cmd_seq_t *commands)
4982 struct sctp_chunk *abort = arg;
4984 /* Stop T1-init timer */
4985 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4986 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4989 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4991 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4992 SCTP_STATE(SCTP_STATE_CLOSED));
4994 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4996 /* Even if we can't send the ABORT due to low memory delete the
4997 * TCB. This is a departure from our typical NOMEM handling.
5000 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5001 SCTP_ERROR(ECONNREFUSED));
5002 /* Delete the established association. */
5003 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5004 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5006 return SCTP_DISPOSITION_ABORT;
5010 * sctp_sf_cookie_echoed_prm_abort
5012 * Section: 4 Note: 3
5017 * The RFC does not explcitly address this issue, but is the route through the
5018 * state table when someone issues an abort while in COOKIE_ECHOED state.
5023 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5025 const struct sctp_endpoint *ep,
5026 const struct sctp_association *asoc,
5027 const sctp_subtype_t type,
5029 sctp_cmd_seq_t *commands)
5031 /* There is a single T1 timer, so we should be able to use
5032 * common function with the COOKIE-WAIT state.
5034 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5038 * sctp_sf_shutdown_pending_prm_abort
5043 * The RFC does not explicitly address this issue, but is the route through the
5044 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5049 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5051 const struct sctp_endpoint *ep,
5052 const struct sctp_association *asoc,
5053 const sctp_subtype_t type,
5055 sctp_cmd_seq_t *commands)
5057 /* Stop the T5-shutdown guard timer. */
5058 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5059 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5061 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5065 * sctp_sf_shutdown_sent_prm_abort
5070 * The RFC does not explicitly address this issue, but is the route through the
5071 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5076 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5078 const struct sctp_endpoint *ep,
5079 const struct sctp_association *asoc,
5080 const sctp_subtype_t type,
5082 sctp_cmd_seq_t *commands)
5084 /* Stop the T2-shutdown timer. */
5085 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5086 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5088 /* Stop the T5-shutdown guard timer. */
5089 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5090 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5092 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5096 * sctp_sf_cookie_echoed_prm_abort
5101 * The RFC does not explcitly address this issue, but is the route through the
5102 * state table when someone issues an abort while in COOKIE_ECHOED state.
5107 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5109 const struct sctp_endpoint *ep,
5110 const struct sctp_association *asoc,
5111 const sctp_subtype_t type,
5113 sctp_cmd_seq_t *commands)
5115 /* The same T2 timer, so we should be able to use
5116 * common function with the SHUTDOWN-SENT state.
5118 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5122 * Process the REQUESTHEARTBEAT primitive
5125 * J) Request Heartbeat
5127 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5131 * Instructs the local endpoint to perform a HeartBeat on the specified
5132 * destination transport address of the given association. The returned
5133 * result should indicate whether the transmission of the HEARTBEAT
5134 * chunk to the destination address is successful.
5136 * Mandatory attributes:
5138 * o association id - local handle to the SCTP association
5140 * o destination transport address - the transport address of the
5141 * association on which a heartbeat should be issued.
5143 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5145 const struct sctp_endpoint *ep,
5146 const struct sctp_association *asoc,
5147 const sctp_subtype_t type,
5149 sctp_cmd_seq_t *commands)
5151 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5152 (struct sctp_transport *)arg, commands))
5153 return SCTP_DISPOSITION_NOMEM;
5156 * RFC 2960 (bis), section 8.3
5158 * D) Request an on-demand HEARTBEAT on a specific destination
5159 * transport address of a given association.
5161 * The endpoint should increment the respective error counter of
5162 * the destination transport address each time a HEARTBEAT is sent
5163 * to that address and not acknowledged within one RTO.
5166 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5167 SCTP_TRANSPORT(arg));
5168 return SCTP_DISPOSITION_CONSUME;
5172 * ADDIP Section 4.1 ASCONF Chunk Procedures
5173 * When an endpoint has an ASCONF signaled change to be sent to the
5174 * remote endpoint it should do A1 to A9
5176 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5177 const struct sctp_endpoint *ep,
5178 const struct sctp_association *asoc,
5179 const sctp_subtype_t type,
5181 sctp_cmd_seq_t *commands)
5183 struct sctp_chunk *chunk = arg;
5185 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5186 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5187 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5188 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5189 return SCTP_DISPOSITION_CONSUME;
5193 * Ignore the primitive event
5195 * The return value is the disposition of the primitive.
5197 sctp_disposition_t sctp_sf_ignore_primitive(
5199 const struct sctp_endpoint *ep,
5200 const struct sctp_association *asoc,
5201 const sctp_subtype_t type,
5203 sctp_cmd_seq_t *commands)
5205 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5208 return SCTP_DISPOSITION_DISCARD;
5211 /***************************************************************************
5212 * These are the state functions for the OTHER events.
5213 ***************************************************************************/
5216 * When the SCTP stack has no more user data to send or retransmit, this
5217 * notification is given to the user. Also, at the time when a user app
5218 * subscribes to this event, if there is no data to be sent or
5219 * retransmit, the stack will immediately send up this notification.
5221 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5223 const struct sctp_endpoint *ep,
5224 const struct sctp_association *asoc,
5225 const sctp_subtype_t type,
5227 sctp_cmd_seq_t *commands)
5229 struct sctp_ulpevent *event;
5231 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5233 return SCTP_DISPOSITION_NOMEM;
5235 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5237 return SCTP_DISPOSITION_CONSUME;
5241 * Start the shutdown negotiation.
5244 * Once all its outstanding data has been acknowledged, the endpoint
5245 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5246 * TSN Ack field the last sequential TSN it has received from the peer.
5247 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5248 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5249 * with the updated last sequential TSN received from its peer.
5251 * The return value is the disposition.
5253 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5255 const struct sctp_endpoint *ep,
5256 const struct sctp_association *asoc,
5257 const sctp_subtype_t type,
5259 sctp_cmd_seq_t *commands)
5261 struct sctp_chunk *reply;
5263 /* Once all its outstanding data has been acknowledged, the
5264 * endpoint shall send a SHUTDOWN chunk to its peer including
5265 * in the Cumulative TSN Ack field the last sequential TSN it
5266 * has received from the peer.
5268 reply = sctp_make_shutdown(asoc, NULL);
5272 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5273 * T2-shutdown timer.
5275 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5277 /* It shall then start the T2-shutdown timer */
5278 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5279 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5281 /* RFC 4960 Section 9.2
5282 * The sender of the SHUTDOWN MAY also start an overall guard timer
5283 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5285 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5286 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5288 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5289 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5290 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5292 /* and enter the SHUTDOWN-SENT state. */
5293 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5294 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5296 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5298 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5301 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5303 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5305 return SCTP_DISPOSITION_CONSUME;
5308 return SCTP_DISPOSITION_NOMEM;
5312 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5316 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5317 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5318 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5319 * endpoint must re-send the SHUTDOWN ACK.
5321 * The return value is the disposition.
5323 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5325 const struct sctp_endpoint *ep,
5326 const struct sctp_association *asoc,
5327 const sctp_subtype_t type,
5329 sctp_cmd_seq_t *commands)
5331 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5332 struct sctp_chunk *reply;
5334 /* There are 2 ways of getting here:
5335 * 1) called in response to a SHUTDOWN chunk
5336 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5338 * For the case (2), the arg parameter is set to NULL. We need
5339 * to check that we have a chunk before accessing it's fields.
5342 if (!sctp_vtag_verify(chunk, asoc))
5343 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5345 /* Make sure that the SHUTDOWN chunk has a valid length. */
5346 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5347 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5351 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5352 * shall send a SHUTDOWN ACK ...
5354 reply = sctp_make_shutdown_ack(asoc, chunk);
5358 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5359 * the T2-shutdown timer.
5361 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5363 /* and start/restart a T2-shutdown timer of its own, */
5364 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5365 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5367 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5368 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5369 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5371 /* Enter the SHUTDOWN-ACK-SENT state. */
5372 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5373 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5375 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5377 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5380 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5382 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5384 return SCTP_DISPOSITION_CONSUME;
5387 return SCTP_DISPOSITION_NOMEM;
5391 * Ignore the event defined as other
5393 * The return value is the disposition of the event.
5395 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5396 const struct sctp_endpoint *ep,
5397 const struct sctp_association *asoc,
5398 const sctp_subtype_t type,
5400 sctp_cmd_seq_t *commands)
5402 pr_debug("%s: the event other type:%d is ignored\n",
5403 __func__, type.other);
5405 return SCTP_DISPOSITION_DISCARD;
5408 /************************************************************
5409 * These are the state functions for handling timeout events.
5410 ************************************************************/
5415 * Section: 6.3.3 Handle T3-rtx Expiration
5417 * Whenever the retransmission timer T3-rtx expires for a destination
5418 * address, do the following:
5421 * The return value is the disposition of the chunk.
5423 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5424 const struct sctp_endpoint *ep,
5425 const struct sctp_association *asoc,
5426 const sctp_subtype_t type,
5428 sctp_cmd_seq_t *commands)
5430 struct sctp_transport *transport = arg;
5432 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5434 if (asoc->overall_error_count >= asoc->max_retrans) {
5435 if (asoc->peer.zero_window_announced &&
5436 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5438 * We are here likely because the receiver had its rwnd
5439 * closed for a while and we have not been able to
5440 * transmit the locally queued data within the maximum
5441 * retransmission attempts limit. Start the T5
5442 * shutdown guard timer to give the receiver one last
5443 * chance and some additional time to recover before
5446 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5447 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5449 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5450 SCTP_ERROR(ETIMEDOUT));
5451 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5452 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5453 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5454 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5455 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5456 return SCTP_DISPOSITION_DELETE_TCB;
5460 /* E1) For the destination address for which the timer
5461 * expires, adjust its ssthresh with rules defined in Section
5462 * 7.2.3 and set the cwnd <- MTU.
5465 /* E2) For the destination address for which the timer
5466 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5467 * maximum value discussed in rule C7 above (RTO.max) may be
5468 * used to provide an upper bound to this doubling operation.
5471 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5472 * outstanding DATA chunks for the address for which the
5473 * T3-rtx has expired will fit into a single packet, subject
5474 * to the MTU constraint for the path corresponding to the
5475 * destination transport address to which the retransmission
5476 * is being sent (this may be different from the address for
5477 * which the timer expires [see Section 6.4]). Call this
5478 * value K. Bundle and retransmit those K DATA chunks in a
5479 * single packet to the destination endpoint.
5481 * Note: Any DATA chunks that were sent to the address for
5482 * which the T3-rtx timer expired but did not fit in one MTU
5483 * (rule E3 above), should be marked for retransmission and
5484 * sent as soon as cwnd allows (normally when a SACK arrives).
5487 /* Do some failure management (Section 8.2). */
5488 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5490 /* NB: Rules E4 and F1 are implicit in R1. */
5491 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5493 return SCTP_DISPOSITION_CONSUME;
5497 * Generate delayed SACK on timeout
5499 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5501 * The guidelines on delayed acknowledgement algorithm specified in
5502 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5503 * acknowledgement SHOULD be generated for at least every second packet
5504 * (not every second DATA chunk) received, and SHOULD be generated
5505 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5506 * some situations it may be beneficial for an SCTP transmitter to be
5507 * more conservative than the algorithms detailed in this document
5508 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5509 * the following algorithms allow.
5511 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5512 const struct sctp_endpoint *ep,
5513 const struct sctp_association *asoc,
5514 const sctp_subtype_t type,
5516 sctp_cmd_seq_t *commands)
5518 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5519 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5520 return SCTP_DISPOSITION_CONSUME;
5524 * sctp_sf_t1_init_timer_expire
5526 * Section: 4 Note: 2
5531 * RFC 2960 Section 4 Notes
5532 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5533 * and re-start the T1-init timer without changing state. This MUST
5534 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5535 * endpoint MUST abort the initialization process and report the
5536 * error to SCTP user.
5542 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5543 const struct sctp_endpoint *ep,
5544 const struct sctp_association *asoc,
5545 const sctp_subtype_t type,
5547 sctp_cmd_seq_t *commands)
5549 struct sctp_chunk *repl = NULL;
5550 struct sctp_bind_addr *bp;
5551 int attempts = asoc->init_err_counter + 1;
5553 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5555 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5557 if (attempts <= asoc->max_init_attempts) {
5558 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5559 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5561 return SCTP_DISPOSITION_NOMEM;
5563 /* Choose transport for INIT. */
5564 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5567 /* Issue a sideeffect to do the needed accounting. */
5568 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5569 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5571 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5573 pr_debug("%s: giving up on INIT, attempts:%d "
5574 "max_init_attempts:%d\n", __func__, attempts,
5575 asoc->max_init_attempts);
5577 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5578 SCTP_ERROR(ETIMEDOUT));
5579 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5580 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5581 return SCTP_DISPOSITION_DELETE_TCB;
5584 return SCTP_DISPOSITION_CONSUME;
5588 * sctp_sf_t1_cookie_timer_expire
5590 * Section: 4 Note: 2
5595 * RFC 2960 Section 4 Notes
5596 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5597 * COOKIE ECHO and re-start the T1-cookie timer without changing
5598 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5599 * After that, the endpoint MUST abort the initialization process and
5600 * report the error to SCTP user.
5606 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5607 const struct sctp_endpoint *ep,
5608 const struct sctp_association *asoc,
5609 const sctp_subtype_t type,
5611 sctp_cmd_seq_t *commands)
5613 struct sctp_chunk *repl = NULL;
5614 int attempts = asoc->init_err_counter + 1;
5616 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5618 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5620 if (attempts <= asoc->max_init_attempts) {
5621 repl = sctp_make_cookie_echo(asoc, NULL);
5623 return SCTP_DISPOSITION_NOMEM;
5625 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5627 /* Issue a sideeffect to do the needed accounting. */
5628 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5629 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5631 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5633 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5634 SCTP_ERROR(ETIMEDOUT));
5635 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5636 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5637 return SCTP_DISPOSITION_DELETE_TCB;
5640 return SCTP_DISPOSITION_CONSUME;
5643 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5644 * with the updated last sequential TSN received from its peer.
5646 * An endpoint should limit the number of retransmissions of the
5647 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5648 * If this threshold is exceeded the endpoint should destroy the TCB and
5649 * MUST report the peer endpoint unreachable to the upper layer (and
5650 * thus the association enters the CLOSED state). The reception of any
5651 * packet from its peer (i.e. as the peer sends all of its queued DATA
5652 * chunks) should clear the endpoint's retransmission count and restart
5653 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5654 * all of its queued DATA chunks that have not yet been sent.
5656 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5657 const struct sctp_endpoint *ep,
5658 const struct sctp_association *asoc,
5659 const sctp_subtype_t type,
5661 sctp_cmd_seq_t *commands)
5663 struct sctp_chunk *reply = NULL;
5665 pr_debug("%s: timer T2 expired\n", __func__);
5667 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5669 ((struct sctp_association *)asoc)->shutdown_retries++;
5671 if (asoc->overall_error_count >= asoc->max_retrans) {
5672 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5673 SCTP_ERROR(ETIMEDOUT));
5674 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5675 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5676 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5677 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5678 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5679 return SCTP_DISPOSITION_DELETE_TCB;
5682 switch (asoc->state) {
5683 case SCTP_STATE_SHUTDOWN_SENT:
5684 reply = sctp_make_shutdown(asoc, NULL);
5687 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5688 reply = sctp_make_shutdown_ack(asoc, NULL);
5699 /* Do some failure management (Section 8.2).
5700 * If we remove the transport an SHUTDOWN was last sent to, don't
5701 * do failure management.
5703 if (asoc->shutdown_last_sent_to)
5704 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5705 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5707 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5708 * the T2-shutdown timer.
5710 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5712 /* Restart the T2-shutdown timer. */
5713 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5714 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5715 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5716 return SCTP_DISPOSITION_CONSUME;
5719 return SCTP_DISPOSITION_NOMEM;
5723 * ADDIP Section 4.1 ASCONF CHunk Procedures
5724 * If the T4 RTO timer expires the endpoint should do B1 to B5
5726 sctp_disposition_t sctp_sf_t4_timer_expire(
5728 const struct sctp_endpoint *ep,
5729 const struct sctp_association *asoc,
5730 const sctp_subtype_t type,
5732 sctp_cmd_seq_t *commands)
5734 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5735 struct sctp_transport *transport = chunk->transport;
5737 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5739 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5740 * detection on the appropriate destination address as defined in
5741 * RFC2960 [5] section 8.1 and 8.2.
5744 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5745 SCTP_TRANSPORT(transport));
5747 /* Reconfig T4 timer and transport. */
5748 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5750 /* ADDIP 4.1 B2) Increment the association error counters and perform
5751 * endpoint failure detection on the association as defined in
5752 * RFC2960 [5] section 8.1 and 8.2.
5753 * association error counter is incremented in SCTP_CMD_STRIKE.
5755 if (asoc->overall_error_count >= asoc->max_retrans) {
5756 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5757 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5758 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5759 SCTP_ERROR(ETIMEDOUT));
5760 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5761 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5762 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5763 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5764 return SCTP_DISPOSITION_ABORT;
5767 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5768 * the ASCONF chunk was sent by doubling the RTO timer value.
5769 * This is done in SCTP_CMD_STRIKE.
5772 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5773 * choose an alternate destination address (please refer to RFC2960
5774 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5775 * chunk, it MUST be the same (including its serial number) as the last
5778 sctp_chunk_hold(asoc->addip_last_asconf);
5779 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5780 SCTP_CHUNK(asoc->addip_last_asconf));
5782 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5783 * destination is selected, then the RTO used will be that of the new
5784 * destination address.
5786 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5787 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5789 return SCTP_DISPOSITION_CONSUME;
5792 /* sctpimpguide-05 Section 2.12.2
5793 * The sender of the SHUTDOWN MAY also start an overall guard timer
5794 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5795 * At the expiration of this timer the sender SHOULD abort the association
5796 * by sending an ABORT chunk.
5798 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5799 const struct sctp_endpoint *ep,
5800 const struct sctp_association *asoc,
5801 const sctp_subtype_t type,
5803 sctp_cmd_seq_t *commands)
5805 struct sctp_chunk *reply = NULL;
5807 pr_debug("%s: timer T5 expired\n", __func__);
5809 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5811 reply = sctp_make_abort(asoc, NULL, 0);
5815 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5816 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5817 SCTP_ERROR(ETIMEDOUT));
5818 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5819 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5821 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5822 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5824 return SCTP_DISPOSITION_DELETE_TCB;
5826 return SCTP_DISPOSITION_NOMEM;
5829 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5830 * the association is automatically closed by starting the shutdown process.
5831 * The work that needs to be done is same as when SHUTDOWN is initiated by
5832 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5834 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5836 const struct sctp_endpoint *ep,
5837 const struct sctp_association *asoc,
5838 const sctp_subtype_t type,
5840 sctp_cmd_seq_t *commands)
5844 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5846 /* From 9.2 Shutdown of an Association
5847 * Upon receipt of the SHUTDOWN primitive from its upper
5848 * layer, the endpoint enters SHUTDOWN-PENDING state and
5849 * remains there until all outstanding data has been
5850 * acknowledged by its peer. The endpoint accepts no new data
5851 * from its upper layer, but retransmits data to the far end
5852 * if necessary to fill gaps.
5854 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5855 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5857 disposition = SCTP_DISPOSITION_CONSUME;
5858 if (sctp_outq_is_empty(&asoc->outqueue)) {
5859 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5865 /*****************************************************************************
5866 * These are sa state functions which could apply to all types of events.
5867 ****************************************************************************/
5870 * This table entry is not implemented.
5873 * (endpoint, asoc, chunk)
5875 * The return value is the disposition of the chunk.
5877 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5878 const struct sctp_endpoint *ep,
5879 const struct sctp_association *asoc,
5880 const sctp_subtype_t type,
5882 sctp_cmd_seq_t *commands)
5884 return SCTP_DISPOSITION_NOT_IMPL;
5888 * This table entry represents a bug.
5891 * (endpoint, asoc, chunk)
5893 * The return value is the disposition of the chunk.
5895 sctp_disposition_t sctp_sf_bug(struct net *net,
5896 const struct sctp_endpoint *ep,
5897 const struct sctp_association *asoc,
5898 const sctp_subtype_t type,
5900 sctp_cmd_seq_t *commands)
5902 return SCTP_DISPOSITION_BUG;
5906 * This table entry represents the firing of a timer in the wrong state.
5907 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5908 * when the association is in the wrong state. This event should
5909 * be ignored, so as to prevent any rearming of the timer.
5912 * (endpoint, asoc, chunk)
5914 * The return value is the disposition of the chunk.
5916 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5917 const struct sctp_endpoint *ep,
5918 const struct sctp_association *asoc,
5919 const sctp_subtype_t type,
5921 sctp_cmd_seq_t *commands)
5923 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5925 return SCTP_DISPOSITION_CONSUME;
5928 /********************************************************************
5929 * 2nd Level Abstractions
5930 ********************************************************************/
5932 /* Pull the SACK chunk based on the SACK header. */
5933 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5935 struct sctp_sackhdr *sack;
5940 /* Protect ourselves from reading too far into
5941 * the skb from a bogus sender.
5943 sack = (struct sctp_sackhdr *) chunk->skb->data;
5945 num_blocks = ntohs(sack->num_gap_ack_blocks);
5946 num_dup_tsns = ntohs(sack->num_dup_tsns);
5947 len = sizeof(struct sctp_sackhdr);
5948 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5949 if (len > chunk->skb->len)
5952 skb_pull(chunk->skb, len);
5957 /* Create an ABORT packet to be sent as a response, with the specified
5960 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5961 const struct sctp_endpoint *ep,
5962 const struct sctp_association *asoc,
5963 struct sctp_chunk *chunk,
5964 const void *payload,
5967 struct sctp_packet *packet;
5968 struct sctp_chunk *abort;
5970 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5974 * The T bit will be set if the asoc is NULL.
5976 abort = sctp_make_abort(asoc, chunk, paylen);
5978 sctp_ootb_pkt_free(packet);
5982 /* Reflect vtag if T-Bit is set */
5983 if (sctp_test_T_bit(abort))
5984 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5986 /* Add specified error causes, i.e., payload, to the
5989 sctp_addto_chunk(abort, paylen, payload);
5991 /* Set the skb to the belonging sock for accounting. */
5992 abort->skb->sk = ep->base.sk;
5994 sctp_packet_append_chunk(packet, abort);
6001 /* Allocate a packet for responding in the OOTB conditions. */
6002 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6003 const struct sctp_association *asoc,
6004 const struct sctp_chunk *chunk)
6006 struct sctp_packet *packet;
6007 struct sctp_transport *transport;
6012 /* Get the source and destination port from the inbound packet. */
6013 sport = ntohs(chunk->sctp_hdr->dest);
6014 dport = ntohs(chunk->sctp_hdr->source);
6016 /* The V-tag is going to be the same as the inbound packet if no
6017 * association exists, otherwise, use the peer's vtag.
6020 /* Special case the INIT-ACK as there is no peer's vtag
6023 switch (chunk->chunk_hdr->type) {
6025 case SCTP_CID_INIT_ACK:
6027 sctp_initack_chunk_t *initack;
6029 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6030 vtag = ntohl(initack->init_hdr.init_tag);
6034 vtag = asoc->peer.i.init_tag;
6038 /* Special case the INIT and stale COOKIE_ECHO as there is no
6041 switch (chunk->chunk_hdr->type) {
6044 sctp_init_chunk_t *init;
6046 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6047 vtag = ntohl(init->init_hdr.init_tag);
6051 vtag = ntohl(chunk->sctp_hdr->vtag);
6056 /* Make a transport for the bucket, Eliza... */
6057 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6061 /* Cache a route for the transport with the chunk's destination as
6062 * the source address.
6064 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6065 sctp_sk(net->sctp.ctl_sock));
6067 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6068 packet = sctp_packet_config(packet, vtag, 0);
6076 /* Free the packet allocated earlier for responding in the OOTB condition. */
6077 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6079 sctp_transport_free(packet->transport);
6082 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6083 static void sctp_send_stale_cookie_err(struct net *net,
6084 const struct sctp_endpoint *ep,
6085 const struct sctp_association *asoc,
6086 const struct sctp_chunk *chunk,
6087 sctp_cmd_seq_t *commands,
6088 struct sctp_chunk *err_chunk)
6090 struct sctp_packet *packet;
6093 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6095 struct sctp_signed_cookie *cookie;
6097 /* Override the OOTB vtag from the cookie. */
6098 cookie = chunk->subh.cookie_hdr;
6099 packet->vtag = cookie->c.peer_vtag;
6101 /* Set the skb to the belonging sock for accounting. */
6102 err_chunk->skb->sk = ep->base.sk;
6103 sctp_packet_append_chunk(packet, err_chunk);
6104 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6105 SCTP_PACKET(packet));
6106 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6108 sctp_chunk_free (err_chunk);
6113 /* Process a data chunk */
6114 static int sctp_eat_data(const struct sctp_association *asoc,
6115 struct sctp_chunk *chunk,
6116 sctp_cmd_seq_t *commands)
6118 sctp_datahdr_t *data_hdr;
6119 struct sctp_chunk *err;
6121 sctp_verb_t deliver;
6124 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6125 struct sock *sk = asoc->base.sk;
6126 struct net *net = sock_net(sk);
6131 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6132 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6134 tsn = ntohl(data_hdr->tsn);
6135 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6137 /* ASSERT: Now skb->data is really the user data. */
6139 /* Process ECN based congestion.
6141 * Since the chunk structure is reused for all chunks within
6142 * a packet, we use ecn_ce_done to track if we've already
6143 * done CE processing for this packet.
6145 * We need to do ECN processing even if we plan to discard the
6149 if (asoc->peer.ecn_capable && !chunk->ecn_ce_done) {
6150 struct sctp_af *af = SCTP_INPUT_CB(chunk->skb)->af;
6151 chunk->ecn_ce_done = 1;
6153 if (af->is_ce(sctp_gso_headskb(chunk->skb))) {
6154 /* Do real work as sideffect. */
6155 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6160 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6162 /* The TSN is too high--silently discard the chunk and
6163 * count on it getting retransmitted later.
6166 chunk->asoc->stats.outofseqtsns++;
6167 return SCTP_IERROR_HIGH_TSN;
6168 } else if (tmp > 0) {
6169 /* This is a duplicate. Record it. */
6170 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6171 return SCTP_IERROR_DUP_TSN;
6174 /* This is a new TSN. */
6176 /* Discard if there is no room in the receive window.
6177 * Actually, allow a little bit of overflow (up to a MTU).
6179 datalen = ntohs(chunk->chunk_hdr->length);
6180 datalen -= sizeof(sctp_data_chunk_t);
6182 deliver = SCTP_CMD_CHUNK_ULP;
6184 /* Think about partial delivery. */
6185 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6187 /* Even if we don't accept this chunk there is
6190 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6193 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6194 * seems a bit troublesome in that frag_point varies based on
6195 * PMTU. In cases, such as loopback, this might be a rather
6198 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6199 (datalen > asoc->rwnd + asoc->frag_point))) {
6201 /* If this is the next TSN, consider reneging to make
6202 * room. Note: Playing nice with a confused sender. A
6203 * malicious sender can still eat up all our buffer
6204 * space and in the future we may want to detect and
6205 * do more drastic reneging.
6207 if (sctp_tsnmap_has_gap(map) &&
6208 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6209 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6210 deliver = SCTP_CMD_RENEGE;
6212 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6213 __func__, tsn, datalen, asoc->rwnd);
6215 return SCTP_IERROR_IGNORE_TSN;
6220 * Also try to renege to limit our memory usage in the event that
6221 * we are under memory pressure
6222 * If we can't renege, don't worry about it, the sk_rmem_schedule
6223 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6224 * memory usage too much
6226 if (*sk->sk_prot_creator->memory_pressure) {
6227 if (sctp_tsnmap_has_gap(map) &&
6228 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6229 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6231 deliver = SCTP_CMD_RENEGE;
6236 * Section 3.3.10.9 No User Data (9)
6240 * No User Data: This error cause is returned to the originator of a
6241 * DATA chunk if a received DATA chunk has no user data.
6243 if (unlikely(0 == datalen)) {
6244 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6246 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6249 /* We are going to ABORT, so we might as well stop
6250 * processing the rest of the chunks in the packet.
6252 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6253 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6254 SCTP_ERROR(ECONNABORTED));
6255 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6256 SCTP_PERR(SCTP_ERROR_NO_DATA));
6257 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6258 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6259 return SCTP_IERROR_NO_DATA;
6262 chunk->data_accepted = 1;
6264 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6265 * if we renege and the chunk arrives again.
6267 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6268 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6270 chunk->asoc->stats.iuodchunks++;
6272 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6274 chunk->asoc->stats.iodchunks++;
6278 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6280 * If an endpoint receive a DATA chunk with an invalid stream
6281 * identifier, it shall acknowledge the reception of the DATA chunk
6282 * following the normal procedure, immediately send an ERROR chunk
6283 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6284 * and discard the DATA chunk.
6286 sid = ntohs(data_hdr->stream);
6287 if (sid >= asoc->c.sinit_max_instreams) {
6288 /* Mark tsn as received even though we drop it */
6289 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6291 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6293 sizeof(data_hdr->stream),
6296 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6298 return SCTP_IERROR_BAD_STREAM;
6301 /* Check to see if the SSN is possible for this TSN.
6302 * The biggest gap we can record is 4K wide. Since SSNs wrap
6303 * at an unsigned short, there is no way that an SSN can
6304 * wrap and for a valid TSN. We can simply check if the current
6305 * SSN is smaller then the next expected one. If it is, it wrapped
6308 ssn = ntohs(data_hdr->ssn);
6309 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6310 return SCTP_IERROR_PROTO_VIOLATION;
6313 /* Send the data up to the user. Note: Schedule the
6314 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6315 * chunk needs the updated rwnd.
6317 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6319 return SCTP_IERROR_NO_ERROR;