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:
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;
3073 return SCTP_DISPOSITION_CONSUME;
3078 * sctp_sf_eat_data_fast_4_4
3081 * (4) In SHUTDOWN-SENT state the endpoint MUST acknowledge any received
3082 * DATA chunks without delay.
3084 * Verification Tag: 8.5 Verification Tag [Normal verification]
3086 * (endpoint, asoc, chunk)
3089 * (asoc, reply_msg, msg_up, timers, counters)
3091 * The return value is the disposition of the chunk.
3093 sctp_disposition_t sctp_sf_eat_data_fast_4_4(struct net *net,
3094 const struct sctp_endpoint *ep,
3095 const struct sctp_association *asoc,
3096 const sctp_subtype_t type,
3098 sctp_cmd_seq_t *commands)
3100 struct sctp_chunk *chunk = arg;
3103 if (!sctp_vtag_verify(chunk, asoc)) {
3104 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3106 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3109 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_data_chunk_t)))
3110 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3113 error = sctp_eat_data(asoc, chunk, commands);
3115 case SCTP_IERROR_NO_ERROR:
3116 case SCTP_IERROR_HIGH_TSN:
3117 case SCTP_IERROR_DUP_TSN:
3118 case SCTP_IERROR_IGNORE_TSN:
3119 case SCTP_IERROR_BAD_STREAM:
3121 case SCTP_IERROR_NO_DATA:
3123 case SCTP_IERROR_PROTO_VIOLATION:
3124 return sctp_sf_abort_violation(net, ep, asoc, chunk, commands,
3125 (u8 *)chunk->subh.data_hdr, sizeof(sctp_datahdr_t));
3130 /* Go a head and force a SACK, since we are shutting down. */
3132 /* Implementor's Guide.
3134 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3135 * respond to each received packet containing one or more DATA chunk(s)
3136 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3138 if (chunk->end_of_packet) {
3139 /* We must delay the chunk creation since the cumulative
3140 * TSN has not been updated yet.
3142 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3143 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3144 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3145 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3149 return SCTP_DISPOSITION_CONSUME;
3153 * Section: 6.2 Processing a Received SACK
3154 * D) Any time a SACK arrives, the endpoint performs the following:
3156 * i) If Cumulative TSN Ack is less than the Cumulative TSN Ack Point,
3157 * then drop the SACK. Since Cumulative TSN Ack is monotonically
3158 * increasing, a SACK whose Cumulative TSN Ack is less than the
3159 * Cumulative TSN Ack Point indicates an out-of-order SACK.
3161 * ii) Set rwnd equal to the newly received a_rwnd minus the number
3162 * of bytes still outstanding after processing the Cumulative TSN Ack
3163 * and the Gap Ack Blocks.
3165 * iii) If the SACK is missing a TSN that was previously
3166 * acknowledged via a Gap Ack Block (e.g., the data receiver
3167 * reneged on the data), then mark the corresponding DATA chunk
3168 * as available for retransmit: Mark it as missing for fast
3169 * retransmit as described in Section 7.2.4 and if no retransmit
3170 * timer is running for the destination address to which the DATA
3171 * chunk was originally transmitted, then T3-rtx is started for
3172 * that destination address.
3174 * Verification Tag: 8.5 Verification Tag [Normal verification]
3177 * (endpoint, asoc, chunk)
3180 * (asoc, reply_msg, msg_up, timers, counters)
3182 * The return value is the disposition of the chunk.
3184 sctp_disposition_t sctp_sf_eat_sack_6_2(struct net *net,
3185 const struct sctp_endpoint *ep,
3186 const struct sctp_association *asoc,
3187 const sctp_subtype_t type,
3189 sctp_cmd_seq_t *commands)
3191 struct sctp_chunk *chunk = arg;
3192 sctp_sackhdr_t *sackh;
3195 if (!sctp_vtag_verify(chunk, asoc))
3196 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3198 /* Make sure that the SACK chunk has a valid length. */
3199 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_sack_chunk_t)))
3200 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3203 /* Pull the SACK chunk from the data buffer */
3204 sackh = sctp_sm_pull_sack(chunk);
3205 /* Was this a bogus SACK? */
3207 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3208 chunk->subh.sack_hdr = sackh;
3209 ctsn = ntohl(sackh->cum_tsn_ack);
3211 /* i) If Cumulative TSN Ack is less than the Cumulative TSN
3212 * Ack Point, then drop the SACK. Since Cumulative TSN
3213 * Ack is monotonically increasing, a SACK whose
3214 * Cumulative TSN Ack is less than the Cumulative TSN Ack
3215 * Point indicates an out-of-order SACK.
3217 if (TSN_lt(ctsn, asoc->ctsn_ack_point)) {
3218 pr_debug("%s: ctsn:%x, ctsn_ack_point:%x\n", __func__, ctsn,
3219 asoc->ctsn_ack_point);
3221 return SCTP_DISPOSITION_DISCARD;
3224 /* If Cumulative TSN Ack beyond the max tsn currently
3225 * send, terminating the association and respond to the
3226 * sender with an ABORT.
3228 if (!TSN_lt(ctsn, asoc->next_tsn))
3229 return sctp_sf_violation_ctsn(net, ep, asoc, type, arg, commands);
3231 /* Return this SACK for further processing. */
3232 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_SACK, SCTP_CHUNK(chunk));
3234 /* Note: We do the rest of the work on the PROCESS_SACK
3237 return SCTP_DISPOSITION_CONSUME;
3241 * Generate an ABORT in response to a packet.
3243 * Section: 8.4 Handle "Out of the blue" Packets, sctpimpguide 2.41
3245 * 8) The receiver should respond to the sender of the OOTB packet with
3246 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3247 * MUST fill in the Verification Tag field of the outbound packet
3248 * with the value found in the Verification Tag field of the OOTB
3249 * packet and set the T-bit in the Chunk Flags to indicate that the
3250 * Verification Tag is reflected. After sending this ABORT, the
3251 * receiver of the OOTB packet shall discard the OOTB packet and take
3252 * no further action.
3256 * The return value is the disposition of the chunk.
3258 static sctp_disposition_t sctp_sf_tabort_8_4_8(struct net *net,
3259 const struct sctp_endpoint *ep,
3260 const struct sctp_association *asoc,
3261 const sctp_subtype_t type,
3263 sctp_cmd_seq_t *commands)
3265 struct sctp_packet *packet = NULL;
3266 struct sctp_chunk *chunk = arg;
3267 struct sctp_chunk *abort;
3269 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3272 /* Make an ABORT. The T bit will be set if the asoc
3275 abort = sctp_make_abort(asoc, chunk, 0);
3277 sctp_ootb_pkt_free(packet);
3278 return SCTP_DISPOSITION_NOMEM;
3281 /* Reflect vtag if T-Bit is set */
3282 if (sctp_test_T_bit(abort))
3283 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3285 /* Set the skb to the belonging sock for accounting. */
3286 abort->skb->sk = ep->base.sk;
3288 sctp_packet_append_chunk(packet, abort);
3290 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3291 SCTP_PACKET(packet));
3293 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3295 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3296 return SCTP_DISPOSITION_CONSUME;
3299 return SCTP_DISPOSITION_NOMEM;
3303 * Received an ERROR chunk from peer. Generate SCTP_REMOTE_ERROR
3304 * event as ULP notification for each cause included in the chunk.
3306 * API 5.3.1.3 - SCTP_REMOTE_ERROR
3308 * The return value is the disposition of the chunk.
3310 sctp_disposition_t sctp_sf_operr_notify(struct net *net,
3311 const struct sctp_endpoint *ep,
3312 const struct sctp_association *asoc,
3313 const sctp_subtype_t type,
3315 sctp_cmd_seq_t *commands)
3317 struct sctp_chunk *chunk = arg;
3320 if (!sctp_vtag_verify(chunk, asoc))
3321 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3323 /* Make sure that the ERROR chunk has a valid length. */
3324 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_operr_chunk_t)))
3325 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3327 sctp_walk_errors(err, chunk->chunk_hdr);
3328 if ((void *)err != (void *)chunk->chunk_end)
3329 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3330 (void *)err, commands);
3332 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_OPERR,
3335 return SCTP_DISPOSITION_CONSUME;
3339 * Process an inbound SHUTDOWN ACK.
3342 * Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3343 * stop the T2-shutdown timer, send a SHUTDOWN COMPLETE chunk to its
3344 * peer, and remove all record of the association.
3346 * The return value is the disposition.
3348 sctp_disposition_t sctp_sf_do_9_2_final(struct net *net,
3349 const struct sctp_endpoint *ep,
3350 const struct sctp_association *asoc,
3351 const sctp_subtype_t type,
3353 sctp_cmd_seq_t *commands)
3355 struct sctp_chunk *chunk = arg;
3356 struct sctp_chunk *reply;
3357 struct sctp_ulpevent *ev;
3359 if (!sctp_vtag_verify(chunk, asoc))
3360 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3362 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3363 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3364 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3366 /* 10.2 H) SHUTDOWN COMPLETE notification
3368 * When SCTP completes the shutdown procedures (section 9.2) this
3369 * notification is passed to the upper layer.
3371 ev = sctp_ulpevent_make_assoc_change(asoc, 0, SCTP_SHUTDOWN_COMP,
3372 0, 0, 0, NULL, GFP_ATOMIC);
3376 /* ...send a SHUTDOWN COMPLETE chunk to its peer, */
3377 reply = sctp_make_shutdown_complete(asoc, chunk);
3381 /* Do all the commands now (after allocation), so that we
3382 * have consistent state if memory allocation failes
3384 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(ev));
3386 /* Upon the receipt of the SHUTDOWN ACK, the SHUTDOWN sender shall
3387 * stop the T2-shutdown timer,
3389 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3390 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3392 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3393 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
3395 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
3396 SCTP_STATE(SCTP_STATE_CLOSED));
3397 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
3398 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3399 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
3401 /* ...and remove all record of the association. */
3402 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
3403 return SCTP_DISPOSITION_DELETE_TCB;
3406 sctp_ulpevent_free(ev);
3408 return SCTP_DISPOSITION_NOMEM;
3412 * RFC 2960, 8.4 - Handle "Out of the blue" Packets, sctpimpguide 2.41.
3414 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3415 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3416 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3417 * packet must fill in the Verification Tag field of the outbound
3418 * packet with the Verification Tag received in the SHUTDOWN ACK and
3419 * set the T-bit in the Chunk Flags to indicate that the Verification
3422 * 8) The receiver should respond to the sender of the OOTB packet with
3423 * an ABORT. When sending the ABORT, the receiver of the OOTB packet
3424 * MUST fill in the Verification Tag field of the outbound packet
3425 * with the value found in the Verification Tag field of the OOTB
3426 * packet and set the T-bit in the Chunk Flags to indicate that the
3427 * Verification Tag is reflected. After sending this ABORT, the
3428 * receiver of the OOTB packet shall discard the OOTB packet and take
3429 * no further action.
3431 sctp_disposition_t sctp_sf_ootb(struct net *net,
3432 const struct sctp_endpoint *ep,
3433 const struct sctp_association *asoc,
3434 const sctp_subtype_t type,
3436 sctp_cmd_seq_t *commands)
3438 struct sctp_chunk *chunk = arg;
3439 struct sk_buff *skb = chunk->skb;
3440 sctp_chunkhdr_t *ch;
3443 int ootb_shut_ack = 0;
3444 int ootb_cookie_ack = 0;
3446 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3448 ch = (sctp_chunkhdr_t *) chunk->chunk_hdr;
3450 /* Report violation if the chunk is less then minimal */
3451 if (ntohs(ch->length) < sizeof(sctp_chunkhdr_t))
3452 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3455 /* Report violation if chunk len overflows */
3456 ch_end = ((__u8 *)ch) + WORD_ROUND(ntohs(ch->length));
3457 if (ch_end > skb_tail_pointer(skb))
3458 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3461 /* Now that we know we at least have a chunk header,
3462 * do things that are type appropriate.
3464 if (SCTP_CID_SHUTDOWN_ACK == ch->type)
3467 /* RFC 2960, Section 3.3.7
3468 * Moreover, under any circumstances, an endpoint that
3469 * receives an ABORT MUST NOT respond to that ABORT by
3470 * sending an ABORT of its own.
3472 if (SCTP_CID_ABORT == ch->type)
3473 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3475 /* RFC 8.4, 7) If the packet contains a "Stale cookie" ERROR
3476 * or a COOKIE ACK the SCTP Packet should be silently
3480 if (SCTP_CID_COOKIE_ACK == ch->type)
3481 ootb_cookie_ack = 1;
3483 if (SCTP_CID_ERROR == ch->type) {
3484 sctp_walk_errors(err, ch) {
3485 if (SCTP_ERROR_STALE_COOKIE == err->cause) {
3486 ootb_cookie_ack = 1;
3492 ch = (sctp_chunkhdr_t *) ch_end;
3493 } while (ch_end < skb_tail_pointer(skb));
3496 return sctp_sf_shut_8_4_5(net, ep, asoc, type, arg, commands);
3497 else if (ootb_cookie_ack)
3498 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3500 return sctp_sf_tabort_8_4_8(net, ep, asoc, type, arg, commands);
3504 * Handle an "Out of the blue" SHUTDOWN ACK.
3506 * Section: 8.4 5, sctpimpguide 2.41.
3508 * 5) If the packet contains a SHUTDOWN ACK chunk, the receiver should
3509 * respond to the sender of the OOTB packet with a SHUTDOWN COMPLETE.
3510 * When sending the SHUTDOWN COMPLETE, the receiver of the OOTB
3511 * packet must fill in the Verification Tag field of the outbound
3512 * packet with the Verification Tag received in the SHUTDOWN ACK and
3513 * set the T-bit in the Chunk Flags to indicate that the Verification
3517 * (endpoint, asoc, type, arg, commands)
3520 * (sctp_disposition_t)
3522 * The return value is the disposition of the chunk.
3524 static sctp_disposition_t sctp_sf_shut_8_4_5(struct net *net,
3525 const struct sctp_endpoint *ep,
3526 const struct sctp_association *asoc,
3527 const sctp_subtype_t type,
3529 sctp_cmd_seq_t *commands)
3531 struct sctp_packet *packet = NULL;
3532 struct sctp_chunk *chunk = arg;
3533 struct sctp_chunk *shut;
3535 packet = sctp_ootb_pkt_new(net, asoc, chunk);
3538 /* Make an SHUTDOWN_COMPLETE.
3539 * The T bit will be set if the asoc is NULL.
3541 shut = sctp_make_shutdown_complete(asoc, chunk);
3543 sctp_ootb_pkt_free(packet);
3544 return SCTP_DISPOSITION_NOMEM;
3547 /* Reflect vtag if T-Bit is set */
3548 if (sctp_test_T_bit(shut))
3549 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
3551 /* Set the skb to the belonging sock for accounting. */
3552 shut->skb->sk = ep->base.sk;
3554 sctp_packet_append_chunk(packet, shut);
3556 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
3557 SCTP_PACKET(packet));
3559 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
3561 /* If the chunk length is invalid, we don't want to process
3562 * the reset of the packet.
3564 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3565 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3567 /* We need to discard the rest of the packet to prevent
3568 * potential bomming attacks from additional bundled chunks.
3569 * This is documented in SCTP Threats ID.
3571 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3574 return SCTP_DISPOSITION_NOMEM;
3578 * Handle SHUTDOWN ACK in COOKIE_ECHOED or COOKIE_WAIT state.
3580 * Verification Tag: 8.5.1 E) Rules for packet carrying a SHUTDOWN ACK
3581 * If the receiver is in COOKIE-ECHOED or COOKIE-WAIT state the
3582 * procedures in section 8.4 SHOULD be followed, in other words it
3583 * should be treated as an Out Of The Blue packet.
3584 * [This means that we do NOT check the Verification Tag on these
3588 sctp_disposition_t sctp_sf_do_8_5_1_E_sa(struct net *net,
3589 const struct sctp_endpoint *ep,
3590 const struct sctp_association *asoc,
3591 const sctp_subtype_t type,
3593 sctp_cmd_seq_t *commands)
3595 struct sctp_chunk *chunk = arg;
3597 /* Make sure that the SHUTDOWN_ACK chunk has a valid length. */
3598 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
3599 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3602 /* Although we do have an association in this case, it corresponds
3603 * to a restarted association. So the packet is treated as an OOTB
3604 * packet and the state function that handles OOTB SHUTDOWN_ACK is
3605 * called with a NULL association.
3607 SCTP_INC_STATS(net, SCTP_MIB_OUTOFBLUES);
3609 return sctp_sf_shut_8_4_5(net, ep, NULL, type, arg, commands);
3612 /* ADDIP Section 4.2 Upon reception of an ASCONF Chunk. */
3613 sctp_disposition_t sctp_sf_do_asconf(struct net *net,
3614 const struct sctp_endpoint *ep,
3615 const struct sctp_association *asoc,
3616 const sctp_subtype_t type, void *arg,
3617 sctp_cmd_seq_t *commands)
3619 struct sctp_chunk *chunk = arg;
3620 struct sctp_chunk *asconf_ack = NULL;
3621 struct sctp_paramhdr *err_param = NULL;
3622 sctp_addiphdr_t *hdr;
3625 if (!sctp_vtag_verify(chunk, asoc)) {
3626 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3628 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3631 /* ADD-IP: Section 4.1.1
3632 * This chunk MUST be sent in an authenticated way by using
3633 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3634 * is received unauthenticated it MUST be silently discarded as
3635 * described in [I-D.ietf-tsvwg-sctp-auth].
3637 if (!net->sctp.addip_noauth && !chunk->auth)
3638 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3640 /* Make sure that the ASCONF ADDIP chunk has a valid length. */
3641 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_addip_chunk_t)))
3642 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3645 hdr = (sctp_addiphdr_t *)chunk->skb->data;
3646 serial = ntohl(hdr->serial);
3648 /* Verify the ASCONF chunk before processing it. */
3649 if (!sctp_verify_asconf(asoc, chunk, true, &err_param))
3650 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3651 (void *)err_param, commands);
3653 /* ADDIP 5.2 E1) Compare the value of the serial number to the value
3654 * the endpoint stored in a new association variable
3655 * 'Peer-Serial-Number'.
3657 if (serial == asoc->peer.addip_serial + 1) {
3658 /* If this is the first instance of ASCONF in the packet,
3659 * we can clean our old ASCONF-ACKs.
3661 if (!chunk->has_asconf)
3662 sctp_assoc_clean_asconf_ack_cache(asoc);
3664 /* ADDIP 5.2 E4) When the Sequence Number matches the next one
3665 * expected, process the ASCONF as described below and after
3666 * processing the ASCONF Chunk, append an ASCONF-ACK Chunk to
3667 * the response packet and cache a copy of it (in the event it
3668 * later needs to be retransmitted).
3670 * Essentially, do V1-V5.
3672 asconf_ack = sctp_process_asconf((struct sctp_association *)
3675 return SCTP_DISPOSITION_NOMEM;
3676 } else if (serial < asoc->peer.addip_serial + 1) {
3678 * If the value found in the Sequence Number is less than the
3679 * ('Peer- Sequence-Number' + 1), simply skip to the next
3680 * ASCONF, and include in the outbound response packet
3681 * any previously cached ASCONF-ACK response that was
3682 * sent and saved that matches the Sequence Number of the
3683 * ASCONF. Note: It is possible that no cached ASCONF-ACK
3684 * Chunk exists. This will occur when an older ASCONF
3685 * arrives out of order. In such a case, the receiver
3686 * should skip the ASCONF Chunk and not include ASCONF-ACK
3687 * Chunk for that chunk.
3689 asconf_ack = sctp_assoc_lookup_asconf_ack(asoc, hdr->serial);
3691 return SCTP_DISPOSITION_DISCARD;
3693 /* Reset the transport so that we select the correct one
3694 * this time around. This is to make sure that we don't
3695 * accidentally use a stale transport that's been removed.
3697 asconf_ack->transport = NULL;
3699 /* ADDIP 5.2 E5) Otherwise, the ASCONF Chunk is discarded since
3700 * it must be either a stale packet or from an attacker.
3702 return SCTP_DISPOSITION_DISCARD;
3705 /* ADDIP 5.2 E6) The destination address of the SCTP packet
3706 * containing the ASCONF-ACK Chunks MUST be the source address of
3707 * the SCTP packet that held the ASCONF Chunks.
3709 * To do this properly, we'll set the destination address of the chunk
3710 * and at the transmit time, will try look up the transport to use.
3711 * Since ASCONFs may be bundled, the correct transport may not be
3712 * created until we process the entire packet, thus this workaround.
3714 asconf_ack->dest = chunk->source;
3715 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(asconf_ack));
3716 if (asoc->new_transport) {
3717 sctp_sf_heartbeat(ep, asoc, type, asoc->new_transport, commands);
3718 ((struct sctp_association *)asoc)->new_transport = NULL;
3721 return SCTP_DISPOSITION_CONSUME;
3725 * ADDIP Section 4.3 General rules for address manipulation
3726 * When building TLV parameters for the ASCONF Chunk that will add or
3727 * delete IP addresses the D0 to D13 rules should be applied:
3729 sctp_disposition_t sctp_sf_do_asconf_ack(struct net *net,
3730 const struct sctp_endpoint *ep,
3731 const struct sctp_association *asoc,
3732 const sctp_subtype_t type, void *arg,
3733 sctp_cmd_seq_t *commands)
3735 struct sctp_chunk *asconf_ack = arg;
3736 struct sctp_chunk *last_asconf = asoc->addip_last_asconf;
3737 struct sctp_chunk *abort;
3738 struct sctp_paramhdr *err_param = NULL;
3739 sctp_addiphdr_t *addip_hdr;
3740 __u32 sent_serial, rcvd_serial;
3742 if (!sctp_vtag_verify(asconf_ack, asoc)) {
3743 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3745 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3748 /* ADD-IP, Section 4.1.2:
3749 * This chunk MUST be sent in an authenticated way by using
3750 * the mechanism defined in [I-D.ietf-tsvwg-sctp-auth]. If this chunk
3751 * is received unauthenticated it MUST be silently discarded as
3752 * described in [I-D.ietf-tsvwg-sctp-auth].
3754 if (!net->sctp.addip_noauth && !asconf_ack->auth)
3755 return sctp_sf_discard_chunk(net, ep, asoc, type, arg, commands);
3757 /* Make sure that the ADDIP chunk has a valid length. */
3758 if (!sctp_chunk_length_valid(asconf_ack, sizeof(sctp_addip_chunk_t)))
3759 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3762 addip_hdr = (sctp_addiphdr_t *)asconf_ack->skb->data;
3763 rcvd_serial = ntohl(addip_hdr->serial);
3765 /* Verify the ASCONF-ACK chunk before processing it. */
3766 if (!sctp_verify_asconf(asoc, asconf_ack, false, &err_param))
3767 return sctp_sf_violation_paramlen(net, ep, asoc, type, arg,
3768 (void *)err_param, commands);
3771 addip_hdr = (sctp_addiphdr_t *)last_asconf->subh.addip_hdr;
3772 sent_serial = ntohl(addip_hdr->serial);
3774 sent_serial = asoc->addip_serial - 1;
3777 /* D0) If an endpoint receives an ASCONF-ACK that is greater than or
3778 * equal to the next serial number to be used but no ASCONF chunk is
3779 * outstanding the endpoint MUST ABORT the association. Note that a
3780 * sequence number is greater than if it is no more than 2^^31-1
3781 * larger than the current sequence number (using serial arithmetic).
3783 if (ADDIP_SERIAL_gte(rcvd_serial, sent_serial + 1) &&
3784 !(asoc->addip_last_asconf)) {
3785 abort = sctp_make_abort(asoc, asconf_ack,
3786 sizeof(sctp_errhdr_t));
3788 sctp_init_cause(abort, SCTP_ERROR_ASCONF_ACK, 0);
3789 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3792 /* We are going to ABORT, so we might as well stop
3793 * processing the rest of the chunks in the packet.
3795 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3796 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3797 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3798 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3799 SCTP_ERROR(ECONNABORTED));
3800 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3801 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3802 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3803 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3804 return SCTP_DISPOSITION_ABORT;
3807 if ((rcvd_serial == sent_serial) && asoc->addip_last_asconf) {
3808 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
3809 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
3811 if (!sctp_process_asconf_ack((struct sctp_association *)asoc,
3813 /* Successfully processed ASCONF_ACK. We can
3814 * release the next asconf if we have one.
3816 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_NEXT_ASCONF,
3818 return SCTP_DISPOSITION_CONSUME;
3821 abort = sctp_make_abort(asoc, asconf_ack,
3822 sizeof(sctp_errhdr_t));
3824 sctp_init_cause(abort, SCTP_ERROR_RSRC_LOW, 0);
3825 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
3828 /* We are going to ABORT, so we might as well stop
3829 * processing the rest of the chunks in the packet.
3831 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
3832 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
3833 SCTP_ERROR(ECONNABORTED));
3834 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
3835 SCTP_PERR(SCTP_ERROR_ASCONF_ACK));
3836 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
3837 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
3838 return SCTP_DISPOSITION_ABORT;
3841 return SCTP_DISPOSITION_DISCARD;
3845 * PR-SCTP Section 3.6 Receiver Side Implementation of PR-SCTP
3847 * When a FORWARD TSN chunk arrives, the data receiver MUST first update
3848 * its cumulative TSN point to the value carried in the FORWARD TSN
3849 * chunk, and then MUST further advance its cumulative TSN point locally
3851 * After the above processing, the data receiver MUST stop reporting any
3852 * missing TSNs earlier than or equal to the new cumulative TSN point.
3854 * Verification Tag: 8.5 Verification Tag [Normal verification]
3856 * The return value is the disposition of the chunk.
3858 sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3859 const struct sctp_endpoint *ep,
3860 const struct sctp_association *asoc,
3861 const sctp_subtype_t type,
3863 sctp_cmd_seq_t *commands)
3865 struct sctp_chunk *chunk = arg;
3866 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3867 struct sctp_fwdtsn_skip *skip;
3871 if (!sctp_vtag_verify(chunk, asoc)) {
3872 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3874 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3877 /* Make sure that the FORWARD_TSN chunk has valid length. */
3878 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3879 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3882 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3883 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3884 len = ntohs(chunk->chunk_hdr->length);
3885 len -= sizeof(struct sctp_chunkhdr);
3886 skb_pull(chunk->skb, len);
3888 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3889 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3891 /* The TSN is too high--silently discard the chunk and count on it
3892 * getting retransmitted later.
3894 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3895 goto discard_noforce;
3897 /* Silently discard the chunk if stream-id is not valid */
3898 sctp_walk_fwdtsn(skip, chunk) {
3899 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3900 goto discard_noforce;
3903 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3904 if (len > sizeof(struct sctp_fwdtsn_hdr))
3905 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3908 /* Count this as receiving DATA. */
3909 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3910 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3911 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3914 /* FIXME: For now send a SACK, but DATA processing may
3917 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_NOFORCE());
3919 return SCTP_DISPOSITION_CONSUME;
3922 return SCTP_DISPOSITION_DISCARD;
3925 sctp_disposition_t sctp_sf_eat_fwd_tsn_fast(
3927 const struct sctp_endpoint *ep,
3928 const struct sctp_association *asoc,
3929 const sctp_subtype_t type,
3931 sctp_cmd_seq_t *commands)
3933 struct sctp_chunk *chunk = arg;
3934 struct sctp_fwdtsn_hdr *fwdtsn_hdr;
3935 struct sctp_fwdtsn_skip *skip;
3939 if (!sctp_vtag_verify(chunk, asoc)) {
3940 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
3942 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
3945 /* Make sure that the FORWARD_TSN chunk has a valid length. */
3946 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_fwdtsn_chunk)))
3947 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
3950 fwdtsn_hdr = (struct sctp_fwdtsn_hdr *)chunk->skb->data;
3951 chunk->subh.fwdtsn_hdr = fwdtsn_hdr;
3952 len = ntohs(chunk->chunk_hdr->length);
3953 len -= sizeof(struct sctp_chunkhdr);
3954 skb_pull(chunk->skb, len);
3956 tsn = ntohl(fwdtsn_hdr->new_cum_tsn);
3957 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
3959 /* The TSN is too high--silently discard the chunk and count on it
3960 * getting retransmitted later.
3962 if (sctp_tsnmap_check(&asoc->peer.tsn_map, tsn) < 0)
3965 /* Silently discard the chunk if stream-id is not valid */
3966 sctp_walk_fwdtsn(skip, chunk) {
3967 if (ntohs(skip->stream) >= asoc->c.sinit_max_instreams)
3971 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_FWDTSN, SCTP_U32(tsn));
3972 if (len > sizeof(struct sctp_fwdtsn_hdr))
3973 sctp_add_cmd_sf(commands, SCTP_CMD_PROCESS_FWDTSN,
3976 /* Go a head and force a SACK, since we are shutting down. */
3978 /* Implementor's Guide.
3980 * While in SHUTDOWN-SENT state, the SHUTDOWN sender MUST immediately
3981 * respond to each received packet containing one or more DATA chunk(s)
3982 * with a SACK, a SHUTDOWN chunk, and restart the T2-shutdown timer
3984 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SHUTDOWN, SCTP_NULL());
3985 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
3986 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3987 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
3989 return SCTP_DISPOSITION_CONSUME;
3993 * SCTP-AUTH Section 6.3 Receiving authenticated chukns
3995 * The receiver MUST use the HMAC algorithm indicated in the HMAC
3996 * Identifier field. If this algorithm was not specified by the
3997 * receiver in the HMAC-ALGO parameter in the INIT or INIT-ACK chunk
3998 * during association setup, the AUTH chunk and all chunks after it MUST
3999 * be discarded and an ERROR chunk SHOULD be sent with the error cause
4000 * defined in Section 4.1.
4002 * If an endpoint with no shared key receives a Shared Key Identifier
4003 * other than 0, it MUST silently discard all authenticated chunks. If
4004 * the endpoint has at least one endpoint pair shared key for the peer,
4005 * it MUST use the key specified by the Shared Key Identifier if a
4006 * key has been configured for that Shared Key Identifier. If no
4007 * endpoint pair shared key has been configured for that Shared Key
4008 * Identifier, all authenticated chunks MUST be silently discarded.
4010 * Verification Tag: 8.5 Verification Tag [Normal verification]
4012 * The return value is the disposition of the chunk.
4014 static sctp_ierror_t sctp_sf_authenticate(
4015 const struct sctp_association *asoc,
4016 struct sctp_chunk *chunk)
4018 struct sctp_authhdr *auth_hdr;
4019 struct sctp_hmac *hmac;
4020 unsigned int sig_len;
4025 /* Pull in the auth header, so we can do some more verification */
4026 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4027 chunk->subh.auth_hdr = auth_hdr;
4028 skb_pull(chunk->skb, sizeof(struct sctp_authhdr));
4030 /* Make sure that we support the HMAC algorithm from the auth
4033 if (!sctp_auth_asoc_verify_hmac_id(asoc, auth_hdr->hmac_id))
4034 return SCTP_IERROR_AUTH_BAD_HMAC;
4036 /* Make sure that the provided shared key identifier has been
4039 key_id = ntohs(auth_hdr->shkey_id);
4040 if (key_id != asoc->active_key_id && !sctp_auth_get_shkey(asoc, key_id))
4041 return SCTP_IERROR_AUTH_BAD_KEYID;
4044 /* Make sure that the length of the signature matches what
4047 sig_len = ntohs(chunk->chunk_hdr->length) - sizeof(sctp_auth_chunk_t);
4048 hmac = sctp_auth_get_hmac(ntohs(auth_hdr->hmac_id));
4049 if (sig_len != hmac->hmac_len)
4050 return SCTP_IERROR_PROTO_VIOLATION;
4052 /* Now that we've done validation checks, we can compute and
4053 * verify the hmac. The steps involved are:
4054 * 1. Save the digest from the chunk.
4055 * 2. Zero out the digest in the chunk.
4056 * 3. Compute the new digest
4057 * 4. Compare saved and new digests.
4059 digest = auth_hdr->hmac;
4060 skb_pull(chunk->skb, sig_len);
4062 save_digest = kmemdup(digest, sig_len, GFP_ATOMIC);
4066 memset(digest, 0, sig_len);
4068 sctp_auth_calculate_hmac(asoc, chunk->skb,
4069 (struct sctp_auth_chunk *)chunk->chunk_hdr,
4072 /* Discard the packet if the digests do not match */
4073 if (memcmp(save_digest, digest, sig_len)) {
4075 return SCTP_IERROR_BAD_SIG;
4081 return SCTP_IERROR_NO_ERROR;
4083 return SCTP_IERROR_NOMEM;
4086 sctp_disposition_t sctp_sf_eat_auth(struct net *net,
4087 const struct sctp_endpoint *ep,
4088 const struct sctp_association *asoc,
4089 const sctp_subtype_t type,
4091 sctp_cmd_seq_t *commands)
4093 struct sctp_authhdr *auth_hdr;
4094 struct sctp_chunk *chunk = arg;
4095 struct sctp_chunk *err_chunk;
4096 sctp_ierror_t error;
4098 /* Make sure that the peer has AUTH capable */
4099 if (!asoc->peer.auth_capable)
4100 return sctp_sf_unk_chunk(net, ep, asoc, type, arg, commands);
4102 if (!sctp_vtag_verify(chunk, asoc)) {
4103 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_BAD_TAG,
4105 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4108 /* Make sure that the AUTH chunk has valid length. */
4109 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_auth_chunk)))
4110 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4113 auth_hdr = (struct sctp_authhdr *)chunk->skb->data;
4114 error = sctp_sf_authenticate(asoc, chunk);
4116 case SCTP_IERROR_AUTH_BAD_HMAC:
4117 /* Generate the ERROR chunk and discard the rest
4120 err_chunk = sctp_make_op_error(asoc, chunk,
4121 SCTP_ERROR_UNSUP_HMAC,
4125 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4126 SCTP_CHUNK(err_chunk));
4129 case SCTP_IERROR_AUTH_BAD_KEYID:
4130 case SCTP_IERROR_BAD_SIG:
4131 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4133 case SCTP_IERROR_PROTO_VIOLATION:
4134 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4137 case SCTP_IERROR_NOMEM:
4138 return SCTP_DISPOSITION_NOMEM;
4140 default: /* Prevent gcc warnings */
4144 if (asoc->active_key_id != ntohs(auth_hdr->shkey_id)) {
4145 struct sctp_ulpevent *ev;
4147 ev = sctp_ulpevent_make_authkey(asoc, ntohs(auth_hdr->shkey_id),
4148 SCTP_AUTH_NEWKEY, GFP_ATOMIC);
4153 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP,
4157 return SCTP_DISPOSITION_CONSUME;
4161 * Process an unknown chunk.
4163 * Section: 3.2. Also, 2.1 in the implementor's guide.
4165 * Chunk Types are encoded such that the highest-order two bits specify
4166 * the action that must be taken if the processing endpoint does not
4167 * recognize the Chunk Type.
4169 * 00 - Stop processing this SCTP packet and discard it, do not process
4170 * any further chunks within it.
4172 * 01 - Stop processing this SCTP packet and discard it, do not process
4173 * any further chunks within it, and report the unrecognized
4174 * chunk in an 'Unrecognized Chunk Type'.
4176 * 10 - Skip this chunk and continue processing.
4178 * 11 - Skip this chunk and continue processing, but report in an ERROR
4179 * Chunk using the 'Unrecognized Chunk Type' cause of error.
4181 * The return value is the disposition of the chunk.
4183 sctp_disposition_t sctp_sf_unk_chunk(struct net *net,
4184 const struct sctp_endpoint *ep,
4185 const struct sctp_association *asoc,
4186 const sctp_subtype_t type,
4188 sctp_cmd_seq_t *commands)
4190 struct sctp_chunk *unk_chunk = arg;
4191 struct sctp_chunk *err_chunk;
4192 sctp_chunkhdr_t *hdr;
4194 pr_debug("%s: processing unknown chunk id:%d\n", __func__, type.chunk);
4196 if (!sctp_vtag_verify(unk_chunk, asoc))
4197 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4199 /* Make sure that the chunk has a valid length.
4200 * Since we don't know the chunk type, we use a general
4201 * chunkhdr structure to make a comparison.
4203 if (!sctp_chunk_length_valid(unk_chunk, sizeof(sctp_chunkhdr_t)))
4204 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4207 switch (type.chunk & SCTP_CID_ACTION_MASK) {
4208 case SCTP_CID_ACTION_DISCARD:
4209 /* Discard the packet. */
4210 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4211 case SCTP_CID_ACTION_DISCARD_ERR:
4212 /* Generate an ERROR chunk as response. */
4213 hdr = unk_chunk->chunk_hdr;
4214 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4215 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4216 WORD_ROUND(ntohs(hdr->length)),
4219 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4220 SCTP_CHUNK(err_chunk));
4223 /* Discard the packet. */
4224 sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4225 return SCTP_DISPOSITION_CONSUME;
4226 case SCTP_CID_ACTION_SKIP:
4227 /* Skip the chunk. */
4228 return SCTP_DISPOSITION_DISCARD;
4229 case SCTP_CID_ACTION_SKIP_ERR:
4230 /* Generate an ERROR chunk as response. */
4231 hdr = unk_chunk->chunk_hdr;
4232 err_chunk = sctp_make_op_error(asoc, unk_chunk,
4233 SCTP_ERROR_UNKNOWN_CHUNK, hdr,
4234 WORD_ROUND(ntohs(hdr->length)),
4237 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
4238 SCTP_CHUNK(err_chunk));
4240 /* Skip the chunk. */
4241 return SCTP_DISPOSITION_CONSUME;
4246 return SCTP_DISPOSITION_DISCARD;
4250 * Discard the chunk.
4252 * Section: 0.2, 5.2.3, 5.2.5, 5.2.6, 6.0, 8.4.6, 8.5.1c, 9.2
4253 * [Too numerous to mention...]
4254 * Verification Tag: No verification needed.
4256 * (endpoint, asoc, chunk)
4259 * (asoc, reply_msg, msg_up, timers, counters)
4261 * The return value is the disposition of the chunk.
4263 sctp_disposition_t sctp_sf_discard_chunk(struct net *net,
4264 const struct sctp_endpoint *ep,
4265 const struct sctp_association *asoc,
4266 const sctp_subtype_t type,
4268 sctp_cmd_seq_t *commands)
4270 struct sctp_chunk *chunk = arg;
4272 /* Make sure that the chunk has a valid length.
4273 * Since we don't know the chunk type, we use a general
4274 * chunkhdr structure to make a comparison.
4276 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4277 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4280 pr_debug("%s: chunk:%d is discarded\n", __func__, type.chunk);
4282 return SCTP_DISPOSITION_DISCARD;
4286 * Discard the whole packet.
4290 * 2) If the OOTB packet contains an ABORT chunk, the receiver MUST
4291 * silently discard the OOTB packet and take no further action.
4293 * Verification Tag: No verification necessary
4296 * (endpoint, asoc, chunk)
4299 * (asoc, reply_msg, msg_up, timers, counters)
4301 * The return value is the disposition of the chunk.
4303 sctp_disposition_t sctp_sf_pdiscard(struct net *net,
4304 const struct sctp_endpoint *ep,
4305 const struct sctp_association *asoc,
4306 const sctp_subtype_t type,
4308 sctp_cmd_seq_t *commands)
4310 SCTP_INC_STATS(net, SCTP_MIB_IN_PKT_DISCARDS);
4311 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
4313 return SCTP_DISPOSITION_CONSUME;
4318 * The other end is violating protocol.
4320 * Section: Not specified
4321 * Verification Tag: Not specified
4323 * (endpoint, asoc, chunk)
4326 * (asoc, reply_msg, msg_up, timers, counters)
4328 * We simply tag the chunk as a violation. The state machine will log
4329 * the violation and continue.
4331 sctp_disposition_t sctp_sf_violation(struct net *net,
4332 const struct sctp_endpoint *ep,
4333 const struct sctp_association *asoc,
4334 const sctp_subtype_t type,
4336 sctp_cmd_seq_t *commands)
4338 struct sctp_chunk *chunk = arg;
4340 if (!sctp_vtag_verify(chunk, asoc))
4341 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
4343 /* Make sure that the chunk has a valid length. */
4344 if (!sctp_chunk_length_valid(chunk, sizeof(sctp_chunkhdr_t)))
4345 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
4348 return SCTP_DISPOSITION_VIOLATION;
4352 * Common function to handle a protocol violation.
4354 static sctp_disposition_t sctp_sf_abort_violation(
4356 const struct sctp_endpoint *ep,
4357 const struct sctp_association *asoc,
4359 sctp_cmd_seq_t *commands,
4360 const __u8 *payload,
4361 const size_t paylen)
4363 struct sctp_packet *packet = NULL;
4364 struct sctp_chunk *chunk = arg;
4365 struct sctp_chunk *abort = NULL;
4367 /* SCTP-AUTH, Section 6.3:
4368 * It should be noted that if the receiver wants to tear
4369 * down an association in an authenticated way only, the
4370 * handling of malformed packets should not result in
4371 * tearing down the association.
4373 * This means that if we only want to abort associations
4374 * in an authenticated way (i.e AUTH+ABORT), then we
4375 * can't destroy this association just because the packet
4378 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4381 /* Make the abort chunk. */
4382 abort = sctp_make_abort_violation(asoc, chunk, payload, paylen);
4387 /* Treat INIT-ACK as a special case during COOKIE-WAIT. */
4388 if (chunk->chunk_hdr->type == SCTP_CID_INIT_ACK &&
4389 !asoc->peer.i.init_tag) {
4390 sctp_initack_chunk_t *initack;
4392 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
4393 if (!sctp_chunk_length_valid(chunk,
4394 sizeof(sctp_initack_chunk_t)))
4395 abort->chunk_hdr->flags |= SCTP_CHUNK_FLAG_T;
4397 unsigned int inittag;
4399 inittag = ntohl(initack->init_hdr.init_tag);
4400 sctp_add_cmd_sf(commands, SCTP_CMD_UPDATE_INITTAG,
4405 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4406 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4408 if (asoc->state <= SCTP_STATE_COOKIE_ECHOED) {
4409 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4410 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4411 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4412 SCTP_ERROR(ECONNREFUSED));
4413 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
4414 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4416 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4417 SCTP_ERROR(ECONNABORTED));
4418 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4419 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4420 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4423 packet = sctp_ootb_pkt_new(net, asoc, chunk);
4428 if (sctp_test_T_bit(abort))
4429 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
4431 abort->skb->sk = ep->base.sk;
4433 sctp_packet_append_chunk(packet, abort);
4435 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
4436 SCTP_PACKET(packet));
4438 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4441 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4444 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4445 return SCTP_DISPOSITION_ABORT;
4448 sctp_chunk_free(abort);
4450 return SCTP_DISPOSITION_NOMEM;
4454 * Handle a protocol violation when the chunk length is invalid.
4455 * "Invalid" length is identified as smaller than the minimal length a
4456 * given chunk can be. For example, a SACK chunk has invalid length
4457 * if its length is set to be smaller than the size of sctp_sack_chunk_t.
4459 * We inform the other end by sending an ABORT with a Protocol Violation
4462 * Section: Not specified
4463 * Verification Tag: Nothing to do
4465 * (endpoint, asoc, chunk)
4468 * (reply_msg, msg_up, counters)
4470 * Generate an ABORT chunk and terminate the association.
4472 static sctp_disposition_t sctp_sf_violation_chunklen(
4474 const struct sctp_endpoint *ep,
4475 const struct sctp_association *asoc,
4476 const sctp_subtype_t type,
4478 sctp_cmd_seq_t *commands)
4480 static const char err_str[] = "The following chunk had invalid length:";
4482 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4487 * Handle a protocol violation when the parameter length is invalid.
4488 * If the length is smaller than the minimum length of a given parameter,
4489 * or accumulated length in multi parameters exceeds the end of the chunk,
4490 * the length is considered as invalid.
4492 static sctp_disposition_t sctp_sf_violation_paramlen(
4494 const struct sctp_endpoint *ep,
4495 const struct sctp_association *asoc,
4496 const sctp_subtype_t type,
4497 void *arg, void *ext,
4498 sctp_cmd_seq_t *commands)
4500 struct sctp_chunk *chunk = arg;
4501 struct sctp_paramhdr *param = ext;
4502 struct sctp_chunk *abort = NULL;
4504 if (sctp_auth_recv_cid(SCTP_CID_ABORT, asoc))
4507 /* Make the abort chunk. */
4508 abort = sctp_make_violation_paramlen(asoc, chunk, param);
4512 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4513 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
4515 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4516 SCTP_ERROR(ECONNABORTED));
4517 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4518 SCTP_PERR(SCTP_ERROR_PROTO_VIOLATION));
4519 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4520 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4523 sctp_sf_pdiscard(net, ep, asoc, SCTP_ST_CHUNK(0), arg, commands);
4524 return SCTP_DISPOSITION_ABORT;
4526 return SCTP_DISPOSITION_NOMEM;
4529 /* Handle a protocol violation when the peer trying to advance the
4530 * cumulative tsn ack to a point beyond the max tsn currently sent.
4532 * We inform the other end by sending an ABORT with a Protocol Violation
4535 static sctp_disposition_t sctp_sf_violation_ctsn(
4537 const struct sctp_endpoint *ep,
4538 const struct sctp_association *asoc,
4539 const sctp_subtype_t type,
4541 sctp_cmd_seq_t *commands)
4543 static const char err_str[] = "The cumulative tsn ack beyond the max tsn currently sent:";
4545 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4549 /* Handle protocol violation of an invalid chunk bundling. For example,
4550 * when we have an association and we receive bundled INIT-ACK, or
4551 * SHUDOWN-COMPLETE, our peer is clearly violationg the "MUST NOT bundle"
4552 * statement from the specs. Additionally, there might be an attacker
4553 * on the path and we may not want to continue this communication.
4555 static sctp_disposition_t sctp_sf_violation_chunk(
4557 const struct sctp_endpoint *ep,
4558 const struct sctp_association *asoc,
4559 const sctp_subtype_t type,
4561 sctp_cmd_seq_t *commands)
4563 static const char err_str[] = "The following chunk violates protocol:";
4566 return sctp_sf_violation(net, ep, asoc, type, arg, commands);
4568 return sctp_sf_abort_violation(net, ep, asoc, arg, commands, err_str,
4571 /***************************************************************************
4572 * These are the state functions for handling primitive (Section 10) events.
4573 ***************************************************************************/
4575 * sctp_sf_do_prm_asoc
4577 * Section: 10.1 ULP-to-SCTP
4580 * Format: ASSOCIATE(local SCTP instance name, destination transport addr,
4581 * outbound stream count)
4582 * -> association id [,destination transport addr list] [,outbound stream
4585 * This primitive allows the upper layer to initiate an association to a
4586 * specific peer endpoint.
4588 * The peer endpoint shall be specified by one of the transport addresses
4589 * which defines the endpoint (see Section 1.4). If the local SCTP
4590 * instance has not been initialized, the ASSOCIATE is considered an
4592 * [This is not relevant for the kernel implementation since we do all
4593 * initialization at boot time. It we hadn't initialized we wouldn't
4594 * get anywhere near this code.]
4596 * An association id, which is a local handle to the SCTP association,
4597 * will be returned on successful establishment of the association. If
4598 * SCTP is not able to open an SCTP association with the peer endpoint,
4599 * an error is returned.
4600 * [In the kernel implementation, the struct sctp_association needs to
4601 * be created BEFORE causing this primitive to run.]
4603 * Other association parameters may be returned, including the
4604 * complete destination transport addresses of the peer as well as the
4605 * outbound stream count of the local endpoint. One of the transport
4606 * address from the returned destination addresses will be selected by
4607 * the local endpoint as default primary path for sending SCTP packets
4608 * to this peer. The returned "destination transport addr list" can
4609 * be used by the ULP to change the default primary path or to force
4610 * sending a packet to a specific transport address. [All of this
4611 * stuff happens when the INIT ACK arrives. This is a NON-BLOCKING
4614 * Mandatory attributes:
4616 * o local SCTP instance name - obtained from the INITIALIZE operation.
4617 * [This is the argument asoc.]
4618 * o destination transport addr - specified as one of the transport
4619 * addresses of the peer endpoint with which the association is to be
4621 * [This is asoc->peer.active_path.]
4622 * o outbound stream count - the number of outbound streams the ULP
4623 * would like to open towards this peer endpoint.
4624 * [BUG: This is not currently implemented.]
4625 * Optional attributes:
4629 * The return value is a disposition.
4631 sctp_disposition_t sctp_sf_do_prm_asoc(struct net *net,
4632 const struct sctp_endpoint *ep,
4633 const struct sctp_association *asoc,
4634 const sctp_subtype_t type,
4636 sctp_cmd_seq_t *commands)
4638 struct sctp_chunk *repl;
4639 struct sctp_association *my_asoc;
4641 /* The comment below says that we enter COOKIE-WAIT AFTER
4642 * sending the INIT, but that doesn't actually work in our
4645 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4646 SCTP_STATE(SCTP_STATE_COOKIE_WAIT));
4648 /* RFC 2960 5.1 Normal Establishment of an Association
4650 * A) "A" first sends an INIT chunk to "Z". In the INIT, "A"
4651 * must provide its Verification Tag (Tag_A) in the Initiate
4652 * Tag field. Tag_A SHOULD be a random number in the range of
4653 * 1 to 4294967295 (see 5.3.1 for Tag value selection). ...
4656 repl = sctp_make_init(asoc, &asoc->base.bind_addr, GFP_ATOMIC, 0);
4660 /* Choose transport for INIT. */
4661 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
4664 /* Cast away the const modifier, as we want to just
4665 * rerun it through as a sideffect.
4667 my_asoc = (struct sctp_association *)asoc;
4668 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_ASOC, SCTP_ASOC(my_asoc));
4670 /* After sending the INIT, "A" starts the T1-init timer and
4671 * enters the COOKIE-WAIT state.
4673 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
4674 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4675 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
4676 return SCTP_DISPOSITION_CONSUME;
4679 return SCTP_DISPOSITION_NOMEM;
4683 * Process the SEND primitive.
4685 * Section: 10.1 ULP-to-SCTP
4688 * Format: SEND(association id, buffer address, byte count [,context]
4689 * [,stream id] [,life time] [,destination transport address]
4690 * [,unorder flag] [,no-bundle flag] [,payload protocol-id] )
4693 * This is the main method to send user data via SCTP.
4695 * Mandatory attributes:
4697 * o association id - local handle to the SCTP association
4699 * o buffer address - the location where the user message to be
4700 * transmitted is stored;
4702 * o byte count - The size of the user data in number of bytes;
4704 * Optional attributes:
4706 * o context - an optional 32 bit integer that will be carried in the
4707 * sending failure notification to the ULP if the transportation of
4708 * this User Message fails.
4710 * o stream id - to indicate which stream to send the data on. If not
4711 * specified, stream 0 will be used.
4713 * o life time - specifies the life time of the user data. The user data
4714 * will not be sent by SCTP after the life time expires. This
4715 * parameter can be used to avoid efforts to transmit stale
4716 * user messages. SCTP notifies the ULP if the data cannot be
4717 * initiated to transport (i.e. sent to the destination via SCTP's
4718 * send primitive) within the life time variable. However, the
4719 * user data will be transmitted if SCTP has attempted to transmit a
4720 * chunk before the life time expired.
4722 * o destination transport address - specified as one of the destination
4723 * transport addresses of the peer endpoint to which this packet
4724 * should be sent. Whenever possible, SCTP should use this destination
4725 * transport address for sending the packets, instead of the current
4728 * o unorder flag - this flag, if present, indicates that the user
4729 * would like the data delivered in an unordered fashion to the peer
4730 * (i.e., the U flag is set to 1 on all DATA chunks carrying this
4733 * o no-bundle flag - instructs SCTP not to bundle this user data with
4734 * other outbound DATA chunks. SCTP MAY still bundle even when
4735 * this flag is present, when faced with network congestion.
4737 * o payload protocol-id - A 32 bit unsigned integer that is to be
4738 * passed to the peer indicating the type of payload protocol data
4739 * being transmitted. This value is passed as opaque data by SCTP.
4741 * The return value is the disposition.
4743 sctp_disposition_t sctp_sf_do_prm_send(struct net *net,
4744 const struct sctp_endpoint *ep,
4745 const struct sctp_association *asoc,
4746 const sctp_subtype_t type,
4748 sctp_cmd_seq_t *commands)
4750 struct sctp_datamsg *msg = arg;
4752 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_MSG, SCTP_DATAMSG(msg));
4753 return SCTP_DISPOSITION_CONSUME;
4757 * Process the SHUTDOWN primitive.
4762 * Format: SHUTDOWN(association id)
4765 * Gracefully closes an association. Any locally queued user data
4766 * will be delivered to the peer. The association will be terminated only
4767 * after the peer acknowledges all the SCTP packets sent. A success code
4768 * will be returned on successful termination of the association. If
4769 * attempting to terminate the association results in a failure, an error
4770 * code shall be returned.
4772 * Mandatory attributes:
4774 * o association id - local handle to the SCTP association
4776 * Optional attributes:
4780 * The return value is the disposition.
4782 sctp_disposition_t sctp_sf_do_9_2_prm_shutdown(
4784 const struct sctp_endpoint *ep,
4785 const struct sctp_association *asoc,
4786 const sctp_subtype_t type,
4788 sctp_cmd_seq_t *commands)
4792 /* From 9.2 Shutdown of an Association
4793 * Upon receipt of the SHUTDOWN primitive from its upper
4794 * layer, the endpoint enters SHUTDOWN-PENDING state and
4795 * remains there until all outstanding data has been
4796 * acknowledged by its peer. The endpoint accepts no new data
4797 * from its upper layer, but retransmits data to the far end
4798 * if necessary to fill gaps.
4800 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4801 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
4803 disposition = SCTP_DISPOSITION_CONSUME;
4804 if (sctp_outq_is_empty(&asoc->outqueue)) {
4805 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
4812 * Process the ABORT primitive.
4817 * Format: Abort(association id [, cause code])
4820 * Ungracefully closes an association. Any locally queued user data
4821 * will be discarded and an ABORT chunk is sent to the peer. A success code
4822 * will be returned on successful abortion of the association. If
4823 * attempting to abort the association results in a failure, an error
4824 * code shall be returned.
4826 * Mandatory attributes:
4828 * o association id - local handle to the SCTP association
4830 * Optional attributes:
4832 * o cause code - reason of the abort to be passed to the peer
4836 * The return value is the disposition.
4838 sctp_disposition_t sctp_sf_do_9_1_prm_abort(
4840 const struct sctp_endpoint *ep,
4841 const struct sctp_association *asoc,
4842 const sctp_subtype_t type,
4844 sctp_cmd_seq_t *commands)
4846 /* From 9.1 Abort of an Association
4847 * Upon receipt of the ABORT primitive from its upper
4848 * layer, the endpoint enters CLOSED state and
4849 * discard all outstanding data has been
4850 * acknowledged by its peer. The endpoint accepts no new data
4851 * from its upper layer, but retransmits data to the far end
4852 * if necessary to fill gaps.
4854 struct sctp_chunk *abort = arg;
4855 sctp_disposition_t retval;
4857 retval = SCTP_DISPOSITION_CONSUME;
4860 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
4862 /* Even if we can't send the ABORT due to low memory delete the
4863 * TCB. This is a departure from our typical NOMEM handling.
4866 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
4867 SCTP_ERROR(ECONNABORTED));
4868 /* Delete the established association. */
4869 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
4870 SCTP_PERR(SCTP_ERROR_USER_ABORT));
4872 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
4873 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
4878 /* We tried an illegal operation on an association which is closed. */
4879 sctp_disposition_t sctp_sf_error_closed(struct net *net,
4880 const struct sctp_endpoint *ep,
4881 const struct sctp_association *asoc,
4882 const sctp_subtype_t type,
4884 sctp_cmd_seq_t *commands)
4886 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR, SCTP_ERROR(-EINVAL));
4887 return SCTP_DISPOSITION_CONSUME;
4890 /* We tried an illegal operation on an association which is shutting
4893 sctp_disposition_t sctp_sf_error_shutdown(struct net *net,
4894 const struct sctp_endpoint *ep,
4895 const struct sctp_association *asoc,
4896 const sctp_subtype_t type,
4898 sctp_cmd_seq_t *commands)
4900 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_ERROR,
4901 SCTP_ERROR(-ESHUTDOWN));
4902 return SCTP_DISPOSITION_CONSUME;
4906 * sctp_cookie_wait_prm_shutdown
4908 * Section: 4 Note: 2
4913 * The RFC does not explicitly address this issue, but is the route through the
4914 * state table when someone issues a shutdown while in COOKIE_WAIT state.
4919 sctp_disposition_t sctp_sf_cookie_wait_prm_shutdown(
4921 const struct sctp_endpoint *ep,
4922 const struct sctp_association *asoc,
4923 const sctp_subtype_t type,
4925 sctp_cmd_seq_t *commands)
4927 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4928 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4930 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
4931 SCTP_STATE(SCTP_STATE_CLOSED));
4933 SCTP_INC_STATS(net, SCTP_MIB_SHUTDOWNS);
4935 sctp_add_cmd_sf(commands, SCTP_CMD_DELETE_TCB, SCTP_NULL());
4937 return SCTP_DISPOSITION_DELETE_TCB;
4941 * sctp_cookie_echoed_prm_shutdown
4943 * Section: 4 Note: 2
4948 * The RFC does not explcitly address this issue, but is the route through the
4949 * state table when someone issues a shutdown while in COOKIE_ECHOED state.
4954 sctp_disposition_t sctp_sf_cookie_echoed_prm_shutdown(
4956 const struct sctp_endpoint *ep,
4957 const struct sctp_association *asoc,
4958 const sctp_subtype_t type,
4959 void *arg, sctp_cmd_seq_t *commands)
4961 /* There is a single T1 timer, so we should be able to use
4962 * common function with the COOKIE-WAIT state.
4964 return sctp_sf_cookie_wait_prm_shutdown(net, ep, asoc, type, arg, commands);
4968 * sctp_sf_cookie_wait_prm_abort
4970 * Section: 4 Note: 2
4975 * The RFC does not explicitly address this issue, but is the route through the
4976 * state table when someone issues an abort while in COOKIE_WAIT state.
4981 sctp_disposition_t sctp_sf_cookie_wait_prm_abort(
4983 const struct sctp_endpoint *ep,
4984 const struct sctp_association *asoc,
4985 const sctp_subtype_t type,
4987 sctp_cmd_seq_t *commands)
4989 struct sctp_chunk *abort = arg;
4990 sctp_disposition_t retval;
4992 /* Stop T1-init timer */
4993 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
4994 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
4995 retval = SCTP_DISPOSITION_CONSUME;
4998 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(abort));
5000 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5001 SCTP_STATE(SCTP_STATE_CLOSED));
5003 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5005 /* Even if we can't send the ABORT due to low memory delete the
5006 * TCB. This is a departure from our typical NOMEM handling.
5009 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5010 SCTP_ERROR(ECONNREFUSED));
5011 /* Delete the established association. */
5012 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5013 SCTP_PERR(SCTP_ERROR_USER_ABORT));
5019 * sctp_sf_cookie_echoed_prm_abort
5021 * Section: 4 Note: 3
5026 * The RFC does not explcitly address this issue, but is the route through the
5027 * state table when someone issues an abort while in COOKIE_ECHOED state.
5032 sctp_disposition_t sctp_sf_cookie_echoed_prm_abort(
5034 const struct sctp_endpoint *ep,
5035 const struct sctp_association *asoc,
5036 const sctp_subtype_t type,
5038 sctp_cmd_seq_t *commands)
5040 /* There is a single T1 timer, so we should be able to use
5041 * common function with the COOKIE-WAIT state.
5043 return sctp_sf_cookie_wait_prm_abort(net, ep, asoc, type, arg, commands);
5047 * sctp_sf_shutdown_pending_prm_abort
5052 * The RFC does not explicitly address this issue, but is the route through the
5053 * state table when someone issues an abort while in SHUTDOWN-PENDING state.
5058 sctp_disposition_t sctp_sf_shutdown_pending_prm_abort(
5060 const struct sctp_endpoint *ep,
5061 const struct sctp_association *asoc,
5062 const sctp_subtype_t type,
5064 sctp_cmd_seq_t *commands)
5066 /* Stop the T5-shutdown guard timer. */
5067 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5068 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5070 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5074 * sctp_sf_shutdown_sent_prm_abort
5079 * The RFC does not explicitly address this issue, but is the route through the
5080 * state table when someone issues an abort while in SHUTDOWN-SENT state.
5085 sctp_disposition_t sctp_sf_shutdown_sent_prm_abort(
5087 const struct sctp_endpoint *ep,
5088 const struct sctp_association *asoc,
5089 const sctp_subtype_t type,
5091 sctp_cmd_seq_t *commands)
5093 /* Stop the T2-shutdown timer. */
5094 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5095 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5097 /* Stop the T5-shutdown guard timer. */
5098 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5099 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5101 return sctp_sf_do_9_1_prm_abort(net, ep, asoc, type, arg, commands);
5105 * sctp_sf_cookie_echoed_prm_abort
5110 * The RFC does not explcitly address this issue, but is the route through the
5111 * state table when someone issues an abort while in COOKIE_ECHOED state.
5116 sctp_disposition_t sctp_sf_shutdown_ack_sent_prm_abort(
5118 const struct sctp_endpoint *ep,
5119 const struct sctp_association *asoc,
5120 const sctp_subtype_t type,
5122 sctp_cmd_seq_t *commands)
5124 /* The same T2 timer, so we should be able to use
5125 * common function with the SHUTDOWN-SENT state.
5127 return sctp_sf_shutdown_sent_prm_abort(net, ep, asoc, type, arg, commands);
5131 * Process the REQUESTHEARTBEAT primitive
5134 * J) Request Heartbeat
5136 * Format: REQUESTHEARTBEAT(association id, destination transport address)
5140 * Instructs the local endpoint to perform a HeartBeat on the specified
5141 * destination transport address of the given association. The returned
5142 * result should indicate whether the transmission of the HEARTBEAT
5143 * chunk to the destination address is successful.
5145 * Mandatory attributes:
5147 * o association id - local handle to the SCTP association
5149 * o destination transport address - the transport address of the
5150 * association on which a heartbeat should be issued.
5152 sctp_disposition_t sctp_sf_do_prm_requestheartbeat(
5154 const struct sctp_endpoint *ep,
5155 const struct sctp_association *asoc,
5156 const sctp_subtype_t type,
5158 sctp_cmd_seq_t *commands)
5160 if (SCTP_DISPOSITION_NOMEM == sctp_sf_heartbeat(ep, asoc, type,
5161 (struct sctp_transport *)arg, commands))
5162 return SCTP_DISPOSITION_NOMEM;
5165 * RFC 2960 (bis), section 8.3
5167 * D) Request an on-demand HEARTBEAT on a specific destination
5168 * transport address of a given association.
5170 * The endpoint should increment the respective error counter of
5171 * the destination transport address each time a HEARTBEAT is sent
5172 * to that address and not acknowledged within one RTO.
5175 sctp_add_cmd_sf(commands, SCTP_CMD_TRANSPORT_HB_SENT,
5176 SCTP_TRANSPORT(arg));
5177 return SCTP_DISPOSITION_CONSUME;
5181 * ADDIP Section 4.1 ASCONF Chunk Procedures
5182 * When an endpoint has an ASCONF signaled change to be sent to the
5183 * remote endpoint it should do A1 to A9
5185 sctp_disposition_t sctp_sf_do_prm_asconf(struct net *net,
5186 const struct sctp_endpoint *ep,
5187 const struct sctp_association *asoc,
5188 const sctp_subtype_t type,
5190 sctp_cmd_seq_t *commands)
5192 struct sctp_chunk *chunk = arg;
5194 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5195 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5196 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5197 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(chunk));
5198 return SCTP_DISPOSITION_CONSUME;
5202 * Ignore the primitive event
5204 * The return value is the disposition of the primitive.
5206 sctp_disposition_t sctp_sf_ignore_primitive(
5208 const struct sctp_endpoint *ep,
5209 const struct sctp_association *asoc,
5210 const sctp_subtype_t type,
5212 sctp_cmd_seq_t *commands)
5214 pr_debug("%s: primitive type:%d is ignored\n", __func__,
5217 return SCTP_DISPOSITION_DISCARD;
5220 /***************************************************************************
5221 * These are the state functions for the OTHER events.
5222 ***************************************************************************/
5225 * When the SCTP stack has no more user data to send or retransmit, this
5226 * notification is given to the user. Also, at the time when a user app
5227 * subscribes to this event, if there is no data to be sent or
5228 * retransmit, the stack will immediately send up this notification.
5230 sctp_disposition_t sctp_sf_do_no_pending_tsn(
5232 const struct sctp_endpoint *ep,
5233 const struct sctp_association *asoc,
5234 const sctp_subtype_t type,
5236 sctp_cmd_seq_t *commands)
5238 struct sctp_ulpevent *event;
5240 event = sctp_ulpevent_make_sender_dry_event(asoc, GFP_ATOMIC);
5242 return SCTP_DISPOSITION_NOMEM;
5244 sctp_add_cmd_sf(commands, SCTP_CMD_EVENT_ULP, SCTP_ULPEVENT(event));
5246 return SCTP_DISPOSITION_CONSUME;
5250 * Start the shutdown negotiation.
5253 * Once all its outstanding data has been acknowledged, the endpoint
5254 * shall send a SHUTDOWN chunk to its peer including in the Cumulative
5255 * TSN Ack field the last sequential TSN it has received from the peer.
5256 * It shall then start the T2-shutdown timer and enter the SHUTDOWN-SENT
5257 * state. If the timer expires, the endpoint must re-send the SHUTDOWN
5258 * with the updated last sequential TSN received from its peer.
5260 * The return value is the disposition.
5262 sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5264 const struct sctp_endpoint *ep,
5265 const struct sctp_association *asoc,
5266 const sctp_subtype_t type,
5268 sctp_cmd_seq_t *commands)
5270 struct sctp_chunk *reply;
5272 /* Once all its outstanding data has been acknowledged, the
5273 * endpoint shall send a SHUTDOWN chunk to its peer including
5274 * in the Cumulative TSN Ack field the last sequential TSN it
5275 * has received from the peer.
5277 reply = sctp_make_shutdown(asoc, NULL);
5281 /* Set the transport for the SHUTDOWN chunk and the timeout for the
5282 * T2-shutdown timer.
5284 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5286 /* It shall then start the T2-shutdown timer */
5287 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
5288 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5290 /* RFC 4960 Section 9.2
5291 * The sender of the SHUTDOWN MAY also start an overall guard timer
5292 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5294 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5295 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5297 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5298 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5299 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5301 /* and enter the SHUTDOWN-SENT state. */
5302 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5303 SCTP_STATE(SCTP_STATE_SHUTDOWN_SENT));
5305 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5307 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5310 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5312 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5314 return SCTP_DISPOSITION_CONSUME;
5317 return SCTP_DISPOSITION_NOMEM;
5321 * Generate a SHUTDOWN ACK now that everything is SACK'd.
5325 * If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5326 * shall send a SHUTDOWN ACK and start a T2-shutdown timer of its own,
5327 * entering the SHUTDOWN-ACK-SENT state. If the timer expires, the
5328 * endpoint must re-send the SHUTDOWN ACK.
5330 * The return value is the disposition.
5332 sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5334 const struct sctp_endpoint *ep,
5335 const struct sctp_association *asoc,
5336 const sctp_subtype_t type,
5338 sctp_cmd_seq_t *commands)
5340 struct sctp_chunk *chunk = (struct sctp_chunk *) arg;
5341 struct sctp_chunk *reply;
5343 /* There are 2 ways of getting here:
5344 * 1) called in response to a SHUTDOWN chunk
5345 * 2) called when SCTP_EVENT_NO_PENDING_TSN event is issued.
5347 * For the case (2), the arg parameter is set to NULL. We need
5348 * to check that we have a chunk before accessing it's fields.
5351 if (!sctp_vtag_verify(chunk, asoc))
5352 return sctp_sf_pdiscard(net, ep, asoc, type, arg, commands);
5354 /* Make sure that the SHUTDOWN chunk has a valid length. */
5355 if (!sctp_chunk_length_valid(chunk, sizeof(struct sctp_shutdown_chunk_t)))
5356 return sctp_sf_violation_chunklen(net, ep, asoc, type, arg,
5360 /* If it has no more outstanding DATA chunks, the SHUTDOWN receiver
5361 * shall send a SHUTDOWN ACK ...
5363 reply = sctp_make_shutdown_ack(asoc, chunk);
5367 /* Set the transport for the SHUTDOWN ACK chunk and the timeout for
5368 * the T2-shutdown timer.
5370 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5372 /* and start/restart a T2-shutdown timer of its own, */
5373 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5374 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5376 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5377 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5378 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5380 /* Enter the SHUTDOWN-ACK-SENT state. */
5381 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5382 SCTP_STATE(SCTP_STATE_SHUTDOWN_ACK_SENT));
5384 /* sctp-implguide 2.10 Issues with Heartbeating and failover
5386 * HEARTBEAT ... is discontinued after sending either SHUTDOWN
5389 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_STOP, SCTP_NULL());
5391 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5393 return SCTP_DISPOSITION_CONSUME;
5396 return SCTP_DISPOSITION_NOMEM;
5400 * Ignore the event defined as other
5402 * The return value is the disposition of the event.
5404 sctp_disposition_t sctp_sf_ignore_other(struct net *net,
5405 const struct sctp_endpoint *ep,
5406 const struct sctp_association *asoc,
5407 const sctp_subtype_t type,
5409 sctp_cmd_seq_t *commands)
5411 pr_debug("%s: the event other type:%d is ignored\n",
5412 __func__, type.other);
5414 return SCTP_DISPOSITION_DISCARD;
5417 /************************************************************
5418 * These are the state functions for handling timeout events.
5419 ************************************************************/
5424 * Section: 6.3.3 Handle T3-rtx Expiration
5426 * Whenever the retransmission timer T3-rtx expires for a destination
5427 * address, do the following:
5430 * The return value is the disposition of the chunk.
5432 sctp_disposition_t sctp_sf_do_6_3_3_rtx(struct net *net,
5433 const struct sctp_endpoint *ep,
5434 const struct sctp_association *asoc,
5435 const sctp_subtype_t type,
5437 sctp_cmd_seq_t *commands)
5439 struct sctp_transport *transport = arg;
5441 SCTP_INC_STATS(net, SCTP_MIB_T3_RTX_EXPIREDS);
5443 if (asoc->overall_error_count >= asoc->max_retrans) {
5444 if (asoc->peer.zero_window_announced &&
5445 asoc->state == SCTP_STATE_SHUTDOWN_PENDING) {
5447 * We are here likely because the receiver had its rwnd
5448 * closed for a while and we have not been able to
5449 * transmit the locally queued data within the maximum
5450 * retransmission attempts limit. Start the T5
5451 * shutdown guard timer to give the receiver one last
5452 * chance and some additional time to recover before
5455 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START_ONCE,
5456 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5458 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5459 SCTP_ERROR(ETIMEDOUT));
5460 /* CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5461 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5462 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5463 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5464 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5465 return SCTP_DISPOSITION_DELETE_TCB;
5469 /* E1) For the destination address for which the timer
5470 * expires, adjust its ssthresh with rules defined in Section
5471 * 7.2.3 and set the cwnd <- MTU.
5474 /* E2) For the destination address for which the timer
5475 * expires, set RTO <- RTO * 2 ("back off the timer"). The
5476 * maximum value discussed in rule C7 above (RTO.max) may be
5477 * used to provide an upper bound to this doubling operation.
5480 /* E3) Determine how many of the earliest (i.e., lowest TSN)
5481 * outstanding DATA chunks for the address for which the
5482 * T3-rtx has expired will fit into a single packet, subject
5483 * to the MTU constraint for the path corresponding to the
5484 * destination transport address to which the retransmission
5485 * is being sent (this may be different from the address for
5486 * which the timer expires [see Section 6.4]). Call this
5487 * value K. Bundle and retransmit those K DATA chunks in a
5488 * single packet to the destination endpoint.
5490 * Note: Any DATA chunks that were sent to the address for
5491 * which the T3-rtx timer expired but did not fit in one MTU
5492 * (rule E3 above), should be marked for retransmission and
5493 * sent as soon as cwnd allows (normally when a SACK arrives).
5496 /* Do some failure management (Section 8.2). */
5497 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE, SCTP_TRANSPORT(transport));
5499 /* NB: Rules E4 and F1 are implicit in R1. */
5500 sctp_add_cmd_sf(commands, SCTP_CMD_RETRAN, SCTP_TRANSPORT(transport));
5502 return SCTP_DISPOSITION_CONSUME;
5506 * Generate delayed SACK on timeout
5508 * Section: 6.2 Acknowledgement on Reception of DATA Chunks
5510 * The guidelines on delayed acknowledgement algorithm specified in
5511 * Section 4.2 of [RFC2581] SHOULD be followed. Specifically, an
5512 * acknowledgement SHOULD be generated for at least every second packet
5513 * (not every second DATA chunk) received, and SHOULD be generated
5514 * within 200 ms of the arrival of any unacknowledged DATA chunk. In
5515 * some situations it may be beneficial for an SCTP transmitter to be
5516 * more conservative than the algorithms detailed in this document
5517 * allow. However, an SCTP transmitter MUST NOT be more aggressive than
5518 * the following algorithms allow.
5520 sctp_disposition_t sctp_sf_do_6_2_sack(struct net *net,
5521 const struct sctp_endpoint *ep,
5522 const struct sctp_association *asoc,
5523 const sctp_subtype_t type,
5525 sctp_cmd_seq_t *commands)
5527 SCTP_INC_STATS(net, SCTP_MIB_DELAY_SACK_EXPIREDS);
5528 sctp_add_cmd_sf(commands, SCTP_CMD_GEN_SACK, SCTP_FORCE());
5529 return SCTP_DISPOSITION_CONSUME;
5533 * sctp_sf_t1_init_timer_expire
5535 * Section: 4 Note: 2
5540 * RFC 2960 Section 4 Notes
5541 * 2) If the T1-init timer expires, the endpoint MUST retransmit INIT
5542 * and re-start the T1-init timer without changing state. This MUST
5543 * be repeated up to 'Max.Init.Retransmits' times. After that, the
5544 * endpoint MUST abort the initialization process and report the
5545 * error to SCTP user.
5551 sctp_disposition_t sctp_sf_t1_init_timer_expire(struct net *net,
5552 const struct sctp_endpoint *ep,
5553 const struct sctp_association *asoc,
5554 const sctp_subtype_t type,
5556 sctp_cmd_seq_t *commands)
5558 struct sctp_chunk *repl = NULL;
5559 struct sctp_bind_addr *bp;
5560 int attempts = asoc->init_err_counter + 1;
5562 pr_debug("%s: timer T1 expired (INIT)\n", __func__);
5564 SCTP_INC_STATS(net, SCTP_MIB_T1_INIT_EXPIREDS);
5566 if (attempts <= asoc->max_init_attempts) {
5567 bp = (struct sctp_bind_addr *) &asoc->base.bind_addr;
5568 repl = sctp_make_init(asoc, bp, GFP_ATOMIC, 0);
5570 return SCTP_DISPOSITION_NOMEM;
5572 /* Choose transport for INIT. */
5573 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5576 /* Issue a sideeffect to do the needed accounting. */
5577 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_RESTART,
5578 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_INIT));
5580 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5582 pr_debug("%s: giving up on INIT, attempts:%d "
5583 "max_init_attempts:%d\n", __func__, attempts,
5584 asoc->max_init_attempts);
5586 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5587 SCTP_ERROR(ETIMEDOUT));
5588 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5589 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5590 return SCTP_DISPOSITION_DELETE_TCB;
5593 return SCTP_DISPOSITION_CONSUME;
5597 * sctp_sf_t1_cookie_timer_expire
5599 * Section: 4 Note: 2
5604 * RFC 2960 Section 4 Notes
5605 * 3) If the T1-cookie timer expires, the endpoint MUST retransmit
5606 * COOKIE ECHO and re-start the T1-cookie timer without changing
5607 * state. This MUST be repeated up to 'Max.Init.Retransmits' times.
5608 * After that, the endpoint MUST abort the initialization process and
5609 * report the error to SCTP user.
5615 sctp_disposition_t sctp_sf_t1_cookie_timer_expire(struct net *net,
5616 const struct sctp_endpoint *ep,
5617 const struct sctp_association *asoc,
5618 const sctp_subtype_t type,
5620 sctp_cmd_seq_t *commands)
5622 struct sctp_chunk *repl = NULL;
5623 int attempts = asoc->init_err_counter + 1;
5625 pr_debug("%s: timer T1 expired (COOKIE-ECHO)\n", __func__);
5627 SCTP_INC_STATS(net, SCTP_MIB_T1_COOKIE_EXPIREDS);
5629 if (attempts <= asoc->max_init_attempts) {
5630 repl = sctp_make_cookie_echo(asoc, NULL);
5632 return SCTP_DISPOSITION_NOMEM;
5634 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_CHOOSE_TRANSPORT,
5636 /* Issue a sideeffect to do the needed accounting. */
5637 sctp_add_cmd_sf(commands, SCTP_CMD_COOKIEECHO_RESTART,
5638 SCTP_TO(SCTP_EVENT_TIMEOUT_T1_COOKIE));
5640 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(repl));
5642 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5643 SCTP_ERROR(ETIMEDOUT));
5644 sctp_add_cmd_sf(commands, SCTP_CMD_INIT_FAILED,
5645 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5646 return SCTP_DISPOSITION_DELETE_TCB;
5649 return SCTP_DISPOSITION_CONSUME;
5652 /* RFC2960 9.2 If the timer expires, the endpoint must re-send the SHUTDOWN
5653 * with the updated last sequential TSN received from its peer.
5655 * An endpoint should limit the number of retransmissions of the
5656 * SHUTDOWN chunk to the protocol parameter 'Association.Max.Retrans'.
5657 * If this threshold is exceeded the endpoint should destroy the TCB and
5658 * MUST report the peer endpoint unreachable to the upper layer (and
5659 * thus the association enters the CLOSED state). The reception of any
5660 * packet from its peer (i.e. as the peer sends all of its queued DATA
5661 * chunks) should clear the endpoint's retransmission count and restart
5662 * the T2-Shutdown timer, giving its peer ample opportunity to transmit
5663 * all of its queued DATA chunks that have not yet been sent.
5665 sctp_disposition_t sctp_sf_t2_timer_expire(struct net *net,
5666 const struct sctp_endpoint *ep,
5667 const struct sctp_association *asoc,
5668 const sctp_subtype_t type,
5670 sctp_cmd_seq_t *commands)
5672 struct sctp_chunk *reply = NULL;
5674 pr_debug("%s: timer T2 expired\n", __func__);
5676 SCTP_INC_STATS(net, SCTP_MIB_T2_SHUTDOWN_EXPIREDS);
5678 ((struct sctp_association *)asoc)->shutdown_retries++;
5680 if (asoc->overall_error_count >= asoc->max_retrans) {
5681 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5682 SCTP_ERROR(ETIMEDOUT));
5683 /* Note: CMD_ASSOC_FAILED calls CMD_DELETE_TCB. */
5684 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5685 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5686 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5687 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5688 return SCTP_DISPOSITION_DELETE_TCB;
5691 switch (asoc->state) {
5692 case SCTP_STATE_SHUTDOWN_SENT:
5693 reply = sctp_make_shutdown(asoc, NULL);
5696 case SCTP_STATE_SHUTDOWN_ACK_SENT:
5697 reply = sctp_make_shutdown_ack(asoc, NULL);
5708 /* Do some failure management (Section 8.2).
5709 * If we remove the transport an SHUTDOWN was last sent to, don't
5710 * do failure management.
5712 if (asoc->shutdown_last_sent_to)
5713 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5714 SCTP_TRANSPORT(asoc->shutdown_last_sent_to));
5716 /* Set the transport for the SHUTDOWN/ACK chunk and the timeout for
5717 * the T2-shutdown timer.
5719 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T2, SCTP_CHUNK(reply));
5721 /* Restart the T2-shutdown timer. */
5722 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5723 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5724 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5725 return SCTP_DISPOSITION_CONSUME;
5728 return SCTP_DISPOSITION_NOMEM;
5732 * ADDIP Section 4.1 ASCONF CHunk Procedures
5733 * If the T4 RTO timer expires the endpoint should do B1 to B5
5735 sctp_disposition_t sctp_sf_t4_timer_expire(
5737 const struct sctp_endpoint *ep,
5738 const struct sctp_association *asoc,
5739 const sctp_subtype_t type,
5741 sctp_cmd_seq_t *commands)
5743 struct sctp_chunk *chunk = asoc->addip_last_asconf;
5744 struct sctp_transport *transport = chunk->transport;
5746 SCTP_INC_STATS(net, SCTP_MIB_T4_RTO_EXPIREDS);
5748 /* ADDIP 4.1 B1) Increment the error counters and perform path failure
5749 * detection on the appropriate destination address as defined in
5750 * RFC2960 [5] section 8.1 and 8.2.
5753 sctp_add_cmd_sf(commands, SCTP_CMD_STRIKE,
5754 SCTP_TRANSPORT(transport));
5756 /* Reconfig T4 timer and transport. */
5757 sctp_add_cmd_sf(commands, SCTP_CMD_SETUP_T4, SCTP_CHUNK(chunk));
5759 /* ADDIP 4.1 B2) Increment the association error counters and perform
5760 * endpoint failure detection on the association as defined in
5761 * RFC2960 [5] section 8.1 and 8.2.
5762 * association error counter is incremented in SCTP_CMD_STRIKE.
5764 if (asoc->overall_error_count >= asoc->max_retrans) {
5765 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5766 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5767 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5768 SCTP_ERROR(ETIMEDOUT));
5769 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5770 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5771 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5772 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5773 return SCTP_DISPOSITION_ABORT;
5776 /* ADDIP 4.1 B3) Back-off the destination address RTO value to which
5777 * the ASCONF chunk was sent by doubling the RTO timer value.
5778 * This is done in SCTP_CMD_STRIKE.
5781 /* ADDIP 4.1 B4) Re-transmit the ASCONF Chunk last sent and if possible
5782 * choose an alternate destination address (please refer to RFC2960
5783 * [5] section 6.4.1). An endpoint MUST NOT add new parameters to this
5784 * chunk, it MUST be the same (including its serial number) as the last
5787 sctp_chunk_hold(asoc->addip_last_asconf);
5788 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
5789 SCTP_CHUNK(asoc->addip_last_asconf));
5791 /* ADDIP 4.1 B5) Restart the T-4 RTO timer. Note that if a different
5792 * destination is selected, then the RTO used will be that of the new
5793 * destination address.
5795 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5796 SCTP_TO(SCTP_EVENT_TIMEOUT_T4_RTO));
5798 return SCTP_DISPOSITION_CONSUME;
5801 /* sctpimpguide-05 Section 2.12.2
5802 * The sender of the SHUTDOWN MAY also start an overall guard timer
5803 * 'T5-shutdown-guard' to bound the overall time for shutdown sequence.
5804 * At the expiration of this timer the sender SHOULD abort the association
5805 * by sending an ABORT chunk.
5807 sctp_disposition_t sctp_sf_t5_timer_expire(struct net *net,
5808 const struct sctp_endpoint *ep,
5809 const struct sctp_association *asoc,
5810 const sctp_subtype_t type,
5812 sctp_cmd_seq_t *commands)
5814 struct sctp_chunk *reply = NULL;
5816 pr_debug("%s: timer T5 expired\n", __func__);
5818 SCTP_INC_STATS(net, SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS);
5820 reply = sctp_make_abort(asoc, NULL, 0);
5824 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY, SCTP_CHUNK(reply));
5825 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
5826 SCTP_ERROR(ETIMEDOUT));
5827 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
5828 SCTP_PERR(SCTP_ERROR_NO_ERROR));
5830 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
5831 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
5833 return SCTP_DISPOSITION_DELETE_TCB;
5835 return SCTP_DISPOSITION_NOMEM;
5838 /* Handle expiration of AUTOCLOSE timer. When the autoclose timer expires,
5839 * the association is automatically closed by starting the shutdown process.
5840 * The work that needs to be done is same as when SHUTDOWN is initiated by
5841 * the user. So this routine looks same as sctp_sf_do_9_2_prm_shutdown().
5843 sctp_disposition_t sctp_sf_autoclose_timer_expire(
5845 const struct sctp_endpoint *ep,
5846 const struct sctp_association *asoc,
5847 const sctp_subtype_t type,
5849 sctp_cmd_seq_t *commands)
5853 SCTP_INC_STATS(net, SCTP_MIB_AUTOCLOSE_EXPIREDS);
5855 /* From 9.2 Shutdown of an Association
5856 * Upon receipt of the SHUTDOWN primitive from its upper
5857 * layer, the endpoint enters SHUTDOWN-PENDING state and
5858 * remains there until all outstanding data has been
5859 * acknowledged by its peer. The endpoint accepts no new data
5860 * from its upper layer, but retransmits data to the far end
5861 * if necessary to fill gaps.
5863 sctp_add_cmd_sf(commands, SCTP_CMD_NEW_STATE,
5864 SCTP_STATE(SCTP_STATE_SHUTDOWN_PENDING));
5866 disposition = SCTP_DISPOSITION_CONSUME;
5867 if (sctp_outq_is_empty(&asoc->outqueue)) {
5868 disposition = sctp_sf_do_9_2_start_shutdown(net, ep, asoc, type,
5874 /*****************************************************************************
5875 * These are sa state functions which could apply to all types of events.
5876 ****************************************************************************/
5879 * This table entry is not implemented.
5882 * (endpoint, asoc, chunk)
5884 * The return value is the disposition of the chunk.
5886 sctp_disposition_t sctp_sf_not_impl(struct net *net,
5887 const struct sctp_endpoint *ep,
5888 const struct sctp_association *asoc,
5889 const sctp_subtype_t type,
5891 sctp_cmd_seq_t *commands)
5893 return SCTP_DISPOSITION_NOT_IMPL;
5897 * This table entry represents a bug.
5900 * (endpoint, asoc, chunk)
5902 * The return value is the disposition of the chunk.
5904 sctp_disposition_t sctp_sf_bug(struct net *net,
5905 const struct sctp_endpoint *ep,
5906 const struct sctp_association *asoc,
5907 const sctp_subtype_t type,
5909 sctp_cmd_seq_t *commands)
5911 return SCTP_DISPOSITION_BUG;
5915 * This table entry represents the firing of a timer in the wrong state.
5916 * Since timer deletion cannot be guaranteed a timer 'may' end up firing
5917 * when the association is in the wrong state. This event should
5918 * be ignored, so as to prevent any rearming of the timer.
5921 * (endpoint, asoc, chunk)
5923 * The return value is the disposition of the chunk.
5925 sctp_disposition_t sctp_sf_timer_ignore(struct net *net,
5926 const struct sctp_endpoint *ep,
5927 const struct sctp_association *asoc,
5928 const sctp_subtype_t type,
5930 sctp_cmd_seq_t *commands)
5932 pr_debug("%s: timer %d ignored\n", __func__, type.chunk);
5934 return SCTP_DISPOSITION_CONSUME;
5937 /********************************************************************
5938 * 2nd Level Abstractions
5939 ********************************************************************/
5941 /* Pull the SACK chunk based on the SACK header. */
5942 static struct sctp_sackhdr *sctp_sm_pull_sack(struct sctp_chunk *chunk)
5944 struct sctp_sackhdr *sack;
5949 /* Protect ourselves from reading too far into
5950 * the skb from a bogus sender.
5952 sack = (struct sctp_sackhdr *) chunk->skb->data;
5954 num_blocks = ntohs(sack->num_gap_ack_blocks);
5955 num_dup_tsns = ntohs(sack->num_dup_tsns);
5956 len = sizeof(struct sctp_sackhdr);
5957 len += (num_blocks + num_dup_tsns) * sizeof(__u32);
5958 if (len > chunk->skb->len)
5961 skb_pull(chunk->skb, len);
5966 /* Create an ABORT packet to be sent as a response, with the specified
5969 static struct sctp_packet *sctp_abort_pkt_new(struct net *net,
5970 const struct sctp_endpoint *ep,
5971 const struct sctp_association *asoc,
5972 struct sctp_chunk *chunk,
5973 const void *payload,
5976 struct sctp_packet *packet;
5977 struct sctp_chunk *abort;
5979 packet = sctp_ootb_pkt_new(net, asoc, chunk);
5983 * The T bit will be set if the asoc is NULL.
5985 abort = sctp_make_abort(asoc, chunk, paylen);
5987 sctp_ootb_pkt_free(packet);
5991 /* Reflect vtag if T-Bit is set */
5992 if (sctp_test_T_bit(abort))
5993 packet->vtag = ntohl(chunk->sctp_hdr->vtag);
5995 /* Add specified error causes, i.e., payload, to the
5998 sctp_addto_chunk(abort, paylen, payload);
6000 /* Set the skb to the belonging sock for accounting. */
6001 abort->skb->sk = ep->base.sk;
6003 sctp_packet_append_chunk(packet, abort);
6010 /* Allocate a packet for responding in the OOTB conditions. */
6011 static struct sctp_packet *sctp_ootb_pkt_new(struct net *net,
6012 const struct sctp_association *asoc,
6013 const struct sctp_chunk *chunk)
6015 struct sctp_packet *packet;
6016 struct sctp_transport *transport;
6021 /* Get the source and destination port from the inbound packet. */
6022 sport = ntohs(chunk->sctp_hdr->dest);
6023 dport = ntohs(chunk->sctp_hdr->source);
6025 /* The V-tag is going to be the same as the inbound packet if no
6026 * association exists, otherwise, use the peer's vtag.
6029 /* Special case the INIT-ACK as there is no peer's vtag
6032 switch (chunk->chunk_hdr->type) {
6034 case SCTP_CID_INIT_ACK:
6036 sctp_initack_chunk_t *initack;
6038 initack = (sctp_initack_chunk_t *)chunk->chunk_hdr;
6039 vtag = ntohl(initack->init_hdr.init_tag);
6043 vtag = asoc->peer.i.init_tag;
6047 /* Special case the INIT and stale COOKIE_ECHO as there is no
6050 switch (chunk->chunk_hdr->type) {
6053 sctp_init_chunk_t *init;
6055 init = (sctp_init_chunk_t *)chunk->chunk_hdr;
6056 vtag = ntohl(init->init_hdr.init_tag);
6060 vtag = ntohl(chunk->sctp_hdr->vtag);
6065 /* Make a transport for the bucket, Eliza... */
6066 transport = sctp_transport_new(net, sctp_source(chunk), GFP_ATOMIC);
6070 /* Cache a route for the transport with the chunk's destination as
6071 * the source address.
6073 sctp_transport_route(transport, (union sctp_addr *)&chunk->dest,
6074 sctp_sk(net->sctp.ctl_sock));
6076 packet = sctp_packet_init(&transport->packet, transport, sport, dport);
6077 packet = sctp_packet_config(packet, vtag, 0);
6085 /* Free the packet allocated earlier for responding in the OOTB condition. */
6086 void sctp_ootb_pkt_free(struct sctp_packet *packet)
6088 sctp_transport_free(packet->transport);
6091 /* Send a stale cookie error when a invalid COOKIE ECHO chunk is found */
6092 static void sctp_send_stale_cookie_err(struct net *net,
6093 const struct sctp_endpoint *ep,
6094 const struct sctp_association *asoc,
6095 const struct sctp_chunk *chunk,
6096 sctp_cmd_seq_t *commands,
6097 struct sctp_chunk *err_chunk)
6099 struct sctp_packet *packet;
6102 packet = sctp_ootb_pkt_new(net, asoc, chunk);
6104 struct sctp_signed_cookie *cookie;
6106 /* Override the OOTB vtag from the cookie. */
6107 cookie = chunk->subh.cookie_hdr;
6108 packet->vtag = cookie->c.peer_vtag;
6110 /* Set the skb to the belonging sock for accounting. */
6111 err_chunk->skb->sk = ep->base.sk;
6112 sctp_packet_append_chunk(packet, err_chunk);
6113 sctp_add_cmd_sf(commands, SCTP_CMD_SEND_PKT,
6114 SCTP_PACKET(packet));
6115 SCTP_INC_STATS(net, SCTP_MIB_OUTCTRLCHUNKS);
6117 sctp_chunk_free (err_chunk);
6122 /* Process a data chunk */
6123 static int sctp_eat_data(const struct sctp_association *asoc,
6124 struct sctp_chunk *chunk,
6125 sctp_cmd_seq_t *commands)
6127 sctp_datahdr_t *data_hdr;
6128 struct sctp_chunk *err;
6130 sctp_verb_t deliver;
6133 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
6134 struct sock *sk = asoc->base.sk;
6135 struct net *net = sock_net(sk);
6140 data_hdr = chunk->subh.data_hdr = (sctp_datahdr_t *)chunk->skb->data;
6141 skb_pull(chunk->skb, sizeof(sctp_datahdr_t));
6143 tsn = ntohl(data_hdr->tsn);
6144 pr_debug("%s: TSN 0x%x\n", __func__, tsn);
6146 /* ASSERT: Now skb->data is really the user data. */
6148 /* Process ECN based congestion.
6150 * Since the chunk structure is reused for all chunks within
6151 * a packet, we use ecn_ce_done to track if we've already
6152 * done CE processing for this packet.
6154 * We need to do ECN processing even if we plan to discard the
6158 if (!chunk->ecn_ce_done) {
6160 chunk->ecn_ce_done = 1;
6162 af = sctp_get_af_specific(
6163 ipver2af(ip_hdr(chunk->skb)->version));
6165 if (af && af->is_ce(chunk->skb) && asoc->peer.ecn_capable) {
6166 /* Do real work as sideffect. */
6167 sctp_add_cmd_sf(commands, SCTP_CMD_ECN_CE,
6172 tmp = sctp_tsnmap_check(&asoc->peer.tsn_map, tsn);
6174 /* The TSN is too high--silently discard the chunk and
6175 * count on it getting retransmitted later.
6178 chunk->asoc->stats.outofseqtsns++;
6179 return SCTP_IERROR_HIGH_TSN;
6180 } else if (tmp > 0) {
6181 /* This is a duplicate. Record it. */
6182 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_DUP, SCTP_U32(tsn));
6183 return SCTP_IERROR_DUP_TSN;
6186 /* This is a new TSN. */
6188 /* Discard if there is no room in the receive window.
6189 * Actually, allow a little bit of overflow (up to a MTU).
6191 datalen = ntohs(chunk->chunk_hdr->length);
6192 datalen -= sizeof(sctp_data_chunk_t);
6194 deliver = SCTP_CMD_CHUNK_ULP;
6196 /* Think about partial delivery. */
6197 if ((datalen >= asoc->rwnd) && (!asoc->ulpq.pd_mode)) {
6199 /* Even if we don't accept this chunk there is
6202 sctp_add_cmd_sf(commands, SCTP_CMD_PART_DELIVER, SCTP_NULL());
6205 /* Spill over rwnd a little bit. Note: While allowed, this spill over
6206 * seems a bit troublesome in that frag_point varies based on
6207 * PMTU. In cases, such as loopback, this might be a rather
6210 if ((!chunk->data_accepted) && (!asoc->rwnd || asoc->rwnd_over ||
6211 (datalen > asoc->rwnd + asoc->frag_point))) {
6213 /* If this is the next TSN, consider reneging to make
6214 * room. Note: Playing nice with a confused sender. A
6215 * malicious sender can still eat up all our buffer
6216 * space and in the future we may want to detect and
6217 * do more drastic reneging.
6219 if (sctp_tsnmap_has_gap(map) &&
6220 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6221 pr_debug("%s: reneging for tsn:%u\n", __func__, tsn);
6222 deliver = SCTP_CMD_RENEGE;
6224 pr_debug("%s: discard tsn:%u len:%zu, rwnd:%d\n",
6225 __func__, tsn, datalen, asoc->rwnd);
6227 return SCTP_IERROR_IGNORE_TSN;
6232 * Also try to renege to limit our memory usage in the event that
6233 * we are under memory pressure
6234 * If we can't renege, don't worry about it, the sk_rmem_schedule
6235 * in sctp_ulpevent_make_rcvmsg will drop the frame if we grow our
6236 * memory usage too much
6238 if (*sk->sk_prot_creator->memory_pressure) {
6239 if (sctp_tsnmap_has_gap(map) &&
6240 (sctp_tsnmap_get_ctsn(map) + 1) == tsn) {
6241 pr_debug("%s: under pressure, reneging for tsn:%u\n",
6243 deliver = SCTP_CMD_RENEGE;
6248 * Section 3.3.10.9 No User Data (9)
6252 * No User Data: This error cause is returned to the originator of a
6253 * DATA chunk if a received DATA chunk has no user data.
6255 if (unlikely(0 == datalen)) {
6256 err = sctp_make_abort_no_data(asoc, chunk, tsn);
6258 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6261 /* We are going to ABORT, so we might as well stop
6262 * processing the rest of the chunks in the packet.
6264 sctp_add_cmd_sf(commands, SCTP_CMD_DISCARD_PACKET, SCTP_NULL());
6265 sctp_add_cmd_sf(commands, SCTP_CMD_SET_SK_ERR,
6266 SCTP_ERROR(ECONNABORTED));
6267 sctp_add_cmd_sf(commands, SCTP_CMD_ASSOC_FAILED,
6268 SCTP_PERR(SCTP_ERROR_NO_DATA));
6269 SCTP_INC_STATS(net, SCTP_MIB_ABORTEDS);
6270 SCTP_DEC_STATS(net, SCTP_MIB_CURRESTAB);
6271 return SCTP_IERROR_NO_DATA;
6274 chunk->data_accepted = 1;
6276 /* Note: Some chunks may get overcounted (if we drop) or overcounted
6277 * if we renege and the chunk arrives again.
6279 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
6280 SCTP_INC_STATS(net, SCTP_MIB_INUNORDERCHUNKS);
6282 chunk->asoc->stats.iuodchunks++;
6284 SCTP_INC_STATS(net, SCTP_MIB_INORDERCHUNKS);
6286 chunk->asoc->stats.iodchunks++;
6290 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
6292 * If an endpoint receive a DATA chunk with an invalid stream
6293 * identifier, it shall acknowledge the reception of the DATA chunk
6294 * following the normal procedure, immediately send an ERROR chunk
6295 * with cause set to "Invalid Stream Identifier" (See Section 3.3.10)
6296 * and discard the DATA chunk.
6298 sid = ntohs(data_hdr->stream);
6299 if (sid >= asoc->c.sinit_max_instreams) {
6300 /* Mark tsn as received even though we drop it */
6301 sctp_add_cmd_sf(commands, SCTP_CMD_REPORT_TSN, SCTP_U32(tsn));
6303 err = sctp_make_op_error(asoc, chunk, SCTP_ERROR_INV_STRM,
6305 sizeof(data_hdr->stream),
6308 sctp_add_cmd_sf(commands, SCTP_CMD_REPLY,
6310 return SCTP_IERROR_BAD_STREAM;
6313 /* Check to see if the SSN is possible for this TSN.
6314 * The biggest gap we can record is 4K wide. Since SSNs wrap
6315 * at an unsigned short, there is no way that an SSN can
6316 * wrap and for a valid TSN. We can simply check if the current
6317 * SSN is smaller then the next expected one. If it is, it wrapped
6320 ssn = ntohs(data_hdr->ssn);
6321 if (ordered && SSN_lt(ssn, sctp_ssn_peek(&asoc->ssnmap->in, sid))) {
6322 return SCTP_IERROR_PROTO_VIOLATION;
6325 /* Send the data up to the user. Note: Schedule the
6326 * SCTP_CMD_CHUNK_ULP cmd before the SCTP_CMD_GEN_SACK, as the SACK
6327 * chunk needs the updated rwnd.
6329 sctp_add_cmd_sf(commands, deliver, SCTP_CHUNK(chunk));
6331 return SCTP_IERROR_NO_ERROR;